💡 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
Escape search result text in the console so it isn't parsed as markup
Mohammad Hijjawidceb5d573ffd
diff --git a/src/python/txtai/console/base.py b/src/python/txtai/console/base.pyindex 4c9138f1..b440c278 100644--- a/src/python/txtai/console/base.py+++ b/src/python/txtai/console/base.py@@ -11,6 +11,7 @@ from cmd import Cmdtry:from rich import boxfrom rich.console import Console as RichConsole+ from rich.markup import escapefrom rich.table import TableRICH = True@@ -259,6 +260,8 @@ class Console(Cmd):output = ""for token, _, color in spans:+ # Escape token text so that it isn't parsed as console markup+ token = escape(token)if color:output += f"[{color}]{token}[/{color}] "else:@@ -266,4 +269,5 @@ class Console(Cmd):return output- return str(value)+ # Escape value text so that it isn't parsed as console markup+ return escape(str(value))diff --git a/test/python/testconsole.py b/test/python/testconsole.pyindex 52392d71..2adcfefc 100644--- a/test/python/testconsole.py+++ b/test/python/testconsole.py@@ -8,6 +8,8 @@ import osimport tempfileimport unittest+from rich.markup import render+from txtai.console import Consolefrom txtai.embeddings import Embeddings@@ -122,6 +124,31 @@ class TestConsole(unittest.TestCase):self.assertIn("wins", self.command("feel good story"))self.assertIn("Taiwan", self.command("asia"))+ def testMarkup(self):+ """+ Test results with text that looks like console markup+ """++ console = Console()++ # Create a keyword index with text containing square brackets+ embeddings = Embeddings(keyword=True, content=True)+ embeddings.index(["Format: [INST] query [/INST] answer", "Use the [bold] flag"])+ console.app = embeddings++ output = self.command("select id, text from txtai where id = 0", console)+ self.assertNotIn("Traceback", output)+ self.assertIn("[INST] query [/INST] answer", output)++ output = self.command("select id, text from txtai where id = 1", console)+ self.assertIn("Use the [bold] flag", output)++ # Explain highlighting+ result = {"text": "[INST] query [/INST]", "score": 0.5, "tokens": [("[INST]", 0.01), ("query", 0.5), ("[/INST]", 0.01)]}+ self.command(".highlight", console)+ output = render(console.render(result, "text", result["text"]))+ self.assertEqual(output.plain.strip(), "[INST] query [/INST]")+def testNoResults(self):"""Test a query that matches nothing renders a message instead of a traceback