💡 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
Update console application to automatically wrap text based on terminal width
davidmezzetti9c2d57547c96
diff --git a/examples/console.py b/examples/console.pyindex 7272e728..f3644bb4 100644--- a/examples/console.py+++ b/examples/console.py@@ -1,11 +1,13 @@"""txtai console module.-Requires streamlit to be installed.- pip install streamlit+Requires tabulate to be installed.+ pip install tabulate"""+import shutilimport sys+import textwrapfrom cmd import Cmd@@ -37,11 +39,34 @@ class Console(Cmd):def default(self, line):# pylint: disable=W0703try:- results = self.embeddings.search(line)+ results = []+ maxwidth = None+ for result in self.embeddings.search(line):+ # Calculate max width using current terminal width and number of columns+ if not maxwidth:+ maxwidth = int(shutil.get_terminal_size()[0] / len(result))++ # Wrap each value at maxwidth, if necessary+ results.append({key: self.wrap(value, maxwidth) for key, value in result.items()})+print(tabulate(results, headers="keys", tablefmt="psql"))except Exception as ex:print(ex)+ def wrap(self, value, maxwidth):+ """+ Wraps value at maxwidth if value is a string.++ Args:+ value: input value+ maxwidth: maximum number of characters before splitting text++ Returns:+ newline wrapped text at maxwidth+ """++ return "\n".join(textwrap.wrap(value, maxwidth)) if isinstance(value, str) else value+def main(path=None):"""