Skip to content

armgit / txtai

Public Imported from GitHub
0

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

fix(graph): resolve integer ids when content is disabled (#1476)

Raphael Malikian80062b11b832
diff --git a/src/python/txtai/embeddings/search/ids.py b/src/python/txtai/embeddings/search/ids.py
index 677dec80..073355e3 100644
--- a/src/python/txtai/embeddings/search/ids.py
+++ b/src/python/txtai/embeddings/search/ids.py
@@ -34,8 +34,12 @@ class Ids:
results = self.database.ids(ids) if self.database else self.scan(ids)
# Create dict of id: [iids] given there is a one to many relationship
+ # Keys are stringified: graph relationship resolution looks up ids
+ # with str(id), and ids stored without content keep their original
+ # type (e.g. int), which would otherwise never match.
ids = {}
for iid, uid in results:
+ uid = str(uid)
if uid not in ids:
ids[uid] = []
ids[uid].append(iid)
diff --git a/test/python/testgraph.py b/test/python/testgraph.py
index afacfe74..c4d3e965 100644
--- a/test/python/testgraph.py
+++ b/test/python/testgraph.py
@@ -442,6 +442,32 @@ class TestGraph(unittest.TestCase):
self.assertEqual(len(embeddings.graph.edges(0)), len(self.data) - 1)
embeddings.close()
+ def testRelationshipsIntIds(self):
+ """
+ Test manually-provided relationships with integer ids and content disabled.
+
+ Regression test: graph relationship resolution looks up ids with
+ str(id), but ids stored without content kept their original int type,
+ so the lookup missed and the edge was silently dropped.
+ """
+
+ cases = [
+ (True, (0, 1)),
+ (False, ("0", "1")),
+ (False, (0, 1)),
+ ]
+
+ for content, ids in cases:
+ embeddings = Embeddings(keyword=True, content=content, graph={"minscore": 1.0})
+ embeddings.index(
+ [
+ {"id": ids[0], "text": "apple", "relationships": [{"id": ids[1], "weight": 0.7}]},
+ {"id": ids[1], "text": "banana"},
+ ]
+ )
+ self.assertEqual(embeddings.graph.edgecount(), 1)
+ embeddings.close()
+
def testRelationshipsInvalid(self):
"""
Test manually-provided relationships with no matching id