docstr: Test that StopWordsCriteria will check stop word tokens in a continuous and sequential order
docstr_tok: ["Test","that","StopWordsCriteria","will","check","stop","word","tokens","in","a","continuous","and","sequential","order"]
code: def test_stop_words_criteria(self):
      """
      Test that StopWordsCriteria will check stop word tokens in a continuous and sequential order
      """
      # input ids for "unambiguously"
      stop_words_id = torch.tensor([[73, 24621, 11937]])
  
      # input ids for "This is ambiguously, but is unrelated."
      input_ids1 = torch.tensor(
          [[100, 19, 24621, 11937, 6, 68, 19, 73, 3897, 5]]
      )
      # input ids for "This is unambiguously"
      input_ids2 = torch.tensor([[100, 19, 73, 24621, 11937]])
  
      # We used to implement stop words algorithm using the torch.isin function like this:
      # `all(torch.isin(stop_words_id, input_ids1)[0])`
      # However, this algorithm is not correct as it will return True for presence of "unambiguously" in input_ids1
      # and True for presence of "unambiguously" in input_ids2. This is because the algorithm will check
      # if the stop word tokens are present in the input_ids, but it does not check if the stop word tokens are
      # present in a continuous/sequential order.
  
      # In "This is ambiguously, but is unrelated." sentence the "un" token comes from "unrelated" and the
      # "ambiguously" token comes from "ambiguously". The algorithm will return True for presence of
      # "unambiguously" in input_ids1 which is not correct.
  
      stop_words_criteria = StopWordsCriteria(
          tokenizer=Mock(spec=PreTrainedTokenizerFast),
          stop_words=["mock data"],
      )
      # because we are mocking the tokenizer, we need to set the stop words manually
      stop_words_criteria.stop_ids = stop_words_id
  
      # this is the correct algorithm to check if the stop word tokens are present in a continuous and sequential order
      # For the input_ids1, the stop word tokens are present BUT not in a continuous order
      present_and_continuous = stop_words_criteria(
          input_ids1, scores=None
      )
      assert not present_and_continuous
  
      # For the input_ids2, the stop word tokens are both present and in a continuous order
      present_and_continuous = stop_words_criteria(
          input_ids2, scores=None
      )
      assert present_and_continuous
  
imports: from unittest.mock import patch, Mock
  import pytest
  import torch
  from transformers import PreTrainedTokenizerFast
  from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator, StopWordsCriteria
label_desc: Use image node_id 22 for calling the TestHuggingFaceLocalGenerator obj's underlying member method code with example usage: obj.test_stop_words_criteria() without return types