Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

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.py
index 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.py
new file mode 100644
index 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")