graphrag icon indicating copy to clipboard operation
graphrag copied to clipboard

Community report context building re-renders and re-tokenizes the whole context after every edge

Open deadstrobe5 opened this issue 1 month ago • 1 comments

sort_context in the graph context builder for community reports (summarize_communities/graph_context/sort_context.py) adds edges one at a time and, after each edge, rebuilds the entity, claim and relationship DataFrames, renders them with to_csv and tokenizes the whole context again. That's quadratic in the number of edges per community, and it also runs when no token limit is passed (how build_mixed_context calls it), where the per-edge work is thrown away.

On the tests/verbs/data fixture build_local_context takes 0.91 s, and on a synthetic 10k-entity graph 12.5 s. Recording the prefix sizes once and searching for the first edge that exceeds the limit gives 0.14 s and 2.85 s with byte-identical output. I have a PR ready if that's welcome.

deadstrobe5 avatar Oct 16 '26 14:10 deadstrobe5

I tried to open the PR but the repo doesn't accept PRs from outside accounts right now. The change is on this branch if you want to pull it: https://github.com/deadstrobe5/graphrag/tree/community-context-prefix-search (one file plus a semversioner patch entry; outputs are identical on all 244 fixture community contexts and 7k randomized cases).

deadstrobe5 avatar Oct 16 '26 14:10 deadstrobe5