💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Return no answer for unmapped QA span tokens (#1430)
Hua.T5fc929732edd
diff --git a/src/python/txtai/pipeline/text/questions.py b/src/python/txtai/pipeline/text/questions.pyindex f3438142..f149aea8 100644--- a/src/python/txtai/pipeline/text/questions.py+++ b/src/python/txtai/pipeline/text/questions.py@@ -89,6 +89,11 @@ class Questions(HFModel):startword = tokens.token_to_word(start)endword = tokens.token_to_word(end)++ # Special tokens, such as the CLS no-answer prediction, have no word mapping.+ if startword is None or endword is None:+ return None+startindex = tokens.word_to_chars(startword, sequence_index=1)[0]endindex = tokens.word_to_chars(endword, sequence_index=1)[1]diff --git a/test/python/testpipeline/testtext/testquestions.py b/test/python/testpipeline/testtext/testquestions.pynew file mode 100644index 00000000..ee5648ea--- /dev/null+++ b/test/python/testpipeline/testtext/testquestions.py@@ -0,0 +1,28 @@+"""+Questions module tests+"""++import unittest++from txtai.pipeline import Questions+++class TestQuestions(unittest.TestCase):+ """+ Questions tests.+ """++ def testNoAnswer(self):+ """+ Test unmapped span tokens alongside an ordinary context span+ """++ questions = Questions("distilbert-base-cased-distilled-squad", gpu=False)+ context = "Paris is the capital of France."+ tokens = questions.tokenizer("What is the capital of France?", context, return_tensors="pt")+ cls = tokens["input_ids"][0].tolist().index(questions.tokenizer.cls_token_id)+ start = tokens.char_to_token(0, sequence_index=1)+ end = tokens.char_to_token(4, sequence_index=1)++ self.assertIsNone(questions.answer(context, tokens, cls, cls))+ self.assertEqual(questions.answer(context, tokens, start, end), "Paris")