Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Support pulling attribute data in graph.scan, closes #849

davidmezzetti4e5331e6411e
diff --git a/src/python/txtai/graph/networkx.py b/src/python/txtai/graph/networkx.py
index b838e244..821d9f0a 100644
--- a/src/python/txtai/graph/networkx.py
+++ b/src/python/txtai/graph/networkx.py
@@ -43,13 +43,16 @@ class NetworkX(Graph):
def count(self):
return self.backend.number_of_nodes()
- def scan(self, attribute=None):
- # Nodes containing an attribute
+ def scan(self, attribute=None, data=False):
+ # Full graph
+ graph = self.backend
+
+ # Filter graph to nodes having a specified attribute
if attribute:
- return nx.subgraph_view(self.backend, filter_node=lambda x: attribute in self.node(x))
+ graph = nx.subgraph_view(self.backend, filter_node=lambda x: attribute in self.node(x))
- # Return all nodes
- return self.backend
+ # Return either list of matching ids or tuple of (id, attribute dictionary)
+ return graph.nodes(data=True) if data else graph
def node(self, node):
return self.backend.nodes.get(node)
diff --git a/src/python/txtai/graph/rdbms.py b/src/python/txtai/graph/rdbms.py
index 564575aa..5397dc4e 100644
--- a/src/python/txtai/graph/rdbms.py
+++ b/src/python/txtai/graph/rdbms.py
@@ -50,13 +50,14 @@ class RDBMS(NetworkX):
# Return NetworkX compatible backend
return self.graph.nx
- def scan(self, attribute=None):
+ def scan(self, attribute=None, data=False):
if attribute:
for node in self.backend:
- if attribute in self.node(node):
- yield node
+ attributes = self.node(node)
+ if attribute in attributes:
+ yield (node, attributes) if data else node
else:
- yield from super().scan(attribute)
+ yield from super().scan(attribute, data)
def load(self, path):
# Create graph instance