Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add terms method to embeddings, closes #445

davidmezzetti18325be25626
diff --git a/src/python/txtai/embeddings/__init__.py b/src/python/txtai/embeddings/__init__.py
index 55d3c39c..2df4abbb 100644
--- a/src/python/txtai/embeddings/__init__.py
+++ b/src/python/txtai/embeddings/__init__.py
@@ -9,4 +9,5 @@ from .functions import *
from .query import Query
from .reducer import Reducer
from .search import Search
+from .terms import Terms
from .transform import Transform
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 9629ebce..c5706fdf 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -25,6 +25,7 @@ from .functions import Functions
from .reducer import Reducer
from .query import Query
from .search import Search
+from .terms import Terms
from .transform import Action, Transform
@@ -397,7 +398,7 @@ class Embeddings:
Explains the importance of each input token in text for a list of queries.
Args:
- query: input queries
+ queries: input queries
texts: optional list of (text|list of tokens), otherwise runs search queries
limit: optional limit if texts is None
@@ -407,6 +408,32 @@ class Embeddings:
return Explain(self)(queries, texts, limit)
+ def terms(self, query):
+ """
+ Extracts keyword terms from a query.
+
+ Args:
+ query: input query
+
+ Returns:
+ query reduced down to keyword terms
+ """
+
+ return self.batchterms([query])[0]
+
+ def batchterms(self, queries):
+ """
+ Extracts keyword terms from a list of queries.
+
+ Args:
+ queries: list of queries
+
+ Returns:
+ list of queries reduced down to keyword term strings
+ """
+
+ return Terms(self)(queries)
+
def exists(self, path=None, cloud=None, **kwargs):
"""
Checks if an index exists at path.
diff --git a/src/python/txtai/embeddings/terms.py b/src/python/txtai/embeddings/terms.py
new file mode 100644
index 00000000..eb3a771f
--- /dev/null
+++ b/src/python/txtai/embeddings/terms.py
@@ -0,0 +1,46 @@
+"""
+Terms module
+"""
+
+
+class Terms:
+ """
+ Reduces a query statement down to keyword terms. This method extracts the query text from similar clauses if it's a SQL statement.
+ Otherwise, the original query is returned.
+ """
+
+ def __init__(self, embeddings):
+ """
+ Create a new terms action.
+
+ Args:
+ embeddings: embeddings instance
+ """
+
+ self.database = embeddings.database
+
+ def __call__(self, queries):
+ """
+ Extracts keyword terms from a list of queries.
+
+ Args:
+ queries: list of queries
+
+ Returns:
+ list of queries reduced down to keyword term strings
+ """
+
+ # Parse queries and extract keyword terms for each query
+ if self.database:
+ terms = []
+ for query in queries:
+ # Parse query
+ parse = self.database.parse(query)
+
+ # Join terms from similar clauses
+ terms.append(" ".join(" ".join(s) for s in parse["similar"]))
+
+ return terms
+
+ # Return original query when database is None
+ return queries