Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Replace python-louvain library with networkx equivalent, closes #867

davidmezzetti61ca27a12829
diff --git a/setup.py b/setup.py
index f3625356..0b23e34a 100644
--- a/setup.py
+++ b/setup.py
@@ -49,7 +49,7 @@ extras["console"] = ["rich>=12.0.1"]
extras["database"] = ["duckdb>=0.7.1", "pillow>=7.1.2", "sqlalchemy>=2.0.20"]
-extras["graph"] = ["grand-cypher>=0.6.0", "grand-graph>=0.6.0", "networkx>=2.6.3", "python-louvain>=0.16", "sqlalchemy>=2.0.20"]
+extras["graph"] = ["grand-cypher>=0.6.0", "grand-graph>=0.6.0", "networkx>=2.7.1", "sqlalchemy>=2.0.20"]
extras["model"] = ["onnx>=1.11.0", "onnxruntime>=1.11.0"]
diff --git a/src/python/txtai/graph/networkx.py b/src/python/txtai/graph/networkx.py
index 38226fb3..1aa7cde4 100644
--- a/src/python/txtai/graph/networkx.py
+++ b/src/python/txtai/graph/networkx.py
@@ -10,9 +10,8 @@ from tempfile import TemporaryDirectory
try:
import networkx as nx
- from community import community_louvain
from grandcypher import GrandCypher
- from networkx.algorithms.community import asyn_lpa_communities, greedy_modularity_communities
+ from networkx.algorithms.community import asyn_lpa_communities, greedy_modularity_communities, louvain_partitions
from networkx.readwrite import json_graph
NETWORKX = True
@@ -210,24 +209,17 @@ class NetworkX(Graph):
config: topic configuration
Returns:
- dictionary of {topic name:[ids]}
+ list of [ids] per community
"""
# Partition level to use
level = config.get("level", "best")
# Run community detection
- results = community_louvain.generate_dendrogram(self.backend, weight="weight", resolution=config.get("resolution", 100), random_state=0)
+ results = list(louvain_partitions(self.backend, weight="weight", resolution=config.get("resolution", 100), seed=0))
# Get partition level (first or best)
- results = results[0] if level == "first" else community_louvain.partition_at_level(results, len(results) - 1)
-
- # Build mapping of community to list of ids
- communities = {}
- for k, v in results.items():
- communities[v] = [k] if v not in communities else communities[v] + [k]
-
- return communities.values()
+ return results[0] if level == "first" else results[-1]
# pylint: disable=W0613
def distance(self, source, target, attrs):