Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update API to support embeddings + database search, closes #176

davidmezzetti6aae0015a321
diff --git a/src/python/txtai/api/base.py b/src/python/txtai/api/base.py
index a6ad19e3..04340829 100644
--- a/src/python/txtai/api/base.py
+++ b/src/python/txtai/api/base.py
@@ -56,7 +56,7 @@ class API:
# Initialize member variables
self.config, self.documents, self.embeddings, self.cluster = API.read(config), None, None, None
- # Write lock
+ # Write lock - allows only a single thread to update embeddings
self.lock = Lock()
# Local embeddings index
@@ -214,7 +214,8 @@ class API:
if self.cluster:
return self.cluster.search(query, limit)
if self.embeddings:
- return [{"id": uid, "score": float(score)} for uid, score in self.embeddings.search(query, limit)]
+ # Unpack (id, score) tuple, if necessary. Otherwise, results are dictionaries.
+ return [{"id": r[0], "score": float(r[1])} if isinstance(r, tuple) else r for r in self.embeddings.search(query, limit)]
return None
@@ -235,8 +236,13 @@ class API:
if self.cluster:
return self.cluster.batchsearch(queries, self.limit(limit))
if self.embeddings:
- results = self.embeddings.batchsearch(queries, self.limit(limit))
- return [[{"id": uid, "score": float(score)} for uid, score in result] for result in results]
+ search = self.embeddings.batchsearch(queries, self.limit(limit))
+
+ results = []
+ for result in search:
+ # Unpack (id, score) tuple, if necessary. Otherwise, results are dictionaries.
+ results.append([{"id": r[0], "score": float(r[1])} if isinstance(r, tuple) else r for r in result])
+ return results
return None
@@ -266,10 +272,10 @@ class API:
index = self.count() + len(self.documents)
for document in documents:
if isinstance(document, dict):
- # Copy value from dict to a new tuple
- document = (document["id"], document["text"], None)
+ # Pass dictionary, the embeddings instance handles parsing out the "text" field
+ document = (document["id"], document, None)
elif isinstance(document, str):
- # Add uid via autosequence
+ # Add id via autosequence
document = (index, document, None)
index += 1
elif isinstance(document, tuple) and len(document) < 3:
diff --git a/src/python/txtai/api/cluster.py b/src/python/txtai/api/cluster.py
index 74d9332b..7084eef2 100644
--- a/src/python/txtai/api/cluster.py
+++ b/src/python/txtai/api/cluster.py
@@ -3,6 +3,7 @@ Cluster module
"""
import asyncio
+import urllib.parse
import zlib
import aiohttp
@@ -45,7 +46,7 @@ class Cluster:
"""
# Build URL
- action = f"search?query={query}"
+ action = f"search?query={urllib.parse.quote_plus(query)}"
if limit:
action += f"&limit={limit}"
@@ -54,8 +55,12 @@ class Cluster:
for result in self.execute("get", action):
results.extend(result)
- # Sort and limit results
- return sorted(results, key=lambda x: x["score"], reverse=True)[: (limit if limit else 10)]
+ # Sort results - there will be no score when there is no associated similarity query
+ if results and "score" in results[0]:
+ results = sorted(results, key=lambda x: x["score"], reverse=True)
+
+ # Limit results
+ return results[: (limit if limit else 10)]
def batchsearch(self, queries, limit=None):
"""