-
Notifications
You must be signed in to change notification settings - Fork 3.5k
streaming finalize_graph #2240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
streaming finalize_graph #2240
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "patch", | ||
| "description": "finalize_graph streaming" | ||
| } |
76 changes: 49 additions & 27 deletions
76
packages/graphrag/graphrag/index/operations/finalize_entities.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,56 @@ | ||
| # Copyright (c) 2024 Microsoft Corporation. | ||
| # Copyright (C) 2026 Microsoft | ||
| # Licensed under the MIT License | ||
|
|
||
| """All the steps to transform final entities.""" | ||
| """Stream-finalize entity rows into an output Table.""" | ||
|
|
||
| from typing import Any | ||
| from uuid import uuid4 | ||
|
|
||
| import pandas as pd | ||
| from graphrag_storage.tables.table import Table | ||
|
|
||
| from graphrag.data_model.schemas import ENTITIES_FINAL_COLUMNS | ||
| from graphrag.graphs.compute_degree import compute_degree | ||
|
|
||
|
|
||
| def finalize_entities( | ||
| entities: pd.DataFrame, | ||
| relationships: pd.DataFrame, | ||
| ) -> pd.DataFrame: | ||
| """All the steps to transform final entities.""" | ||
| degrees = compute_degree(relationships) | ||
| final_entities = entities.merge(degrees, on="title", how="left").drop_duplicates( | ||
| subset="title" | ||
| ) | ||
| final_entities = final_entities.loc[entities["title"].notna()].reset_index() | ||
| # disconnected nodes and those with no community even at level 0 can be missing degree | ||
| final_entities["degree"] = final_entities["degree"].fillna(0).astype(int) | ||
| final_entities.reset_index(inplace=True) | ||
| final_entities["human_readable_id"] = final_entities.index | ||
| final_entities["id"] = final_entities["human_readable_id"].apply( | ||
| lambda _x: str(uuid4()) | ||
| ) | ||
| return final_entities.loc[ | ||
| :, | ||
| ENTITIES_FINAL_COLUMNS, | ||
| ] | ||
|
|
||
|
|
||
| async def finalize_entities( | ||
| entities_table: Table, | ||
| degree_map: dict[str, int], | ||
| ) -> list[dict[str, Any]]: | ||
| """Read entity rows, enrich with degree, and write back. | ||
|
|
||
| Streams through the entities table, deduplicates by title, | ||
| assigns degree from the pre-computed degree map, and writes | ||
| each finalized row back to the same table (safe when using | ||
| truncate=True, which reads from the original and writes to | ||
| a temp file). | ||
|
|
||
| Args | ||
| ---- | ||
| entities_table: Table | ||
| Opened table for both reading input and writing output. | ||
| degree_map: dict[str, int] | ||
| Pre-computed mapping of entity title to node degree. | ||
|
|
||
| Returns | ||
| ------- | ||
| list[dict[str, Any]] | ||
| Sample of up to 5 entity rows for logging. | ||
| """ | ||
| sample_rows: list[dict[str, Any]] = [] | ||
| seen_titles: set[str] = set() | ||
| human_readable_id = 0 | ||
|
|
||
| async for row in entities_table: | ||
| title = row.get("title") | ||
| if not title or title in seen_titles: | ||
| continue | ||
| seen_titles.add(title) | ||
| row["degree"] = degree_map.get(title, 0) | ||
| row["human_readable_id"] = human_readable_id | ||
| row["id"] = str(uuid4()) | ||
| human_readable_id += 1 | ||
| out = {col: row.get(col) for col in ENTITIES_FINAL_COLUMNS} | ||
| await entities_table.write(out) | ||
| if len(sample_rows) < 5: | ||
| sample_rows.append(out) | ||
|
|
||
| return sample_rows |
83 changes: 48 additions & 35 deletions
83
packages/graphrag/graphrag/index/operations/finalize_relationships.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,55 @@ | ||
| # Copyright (c) 2024 Microsoft Corporation. | ||
| # Copyright (C) 2026 Microsoft | ||
| # Licensed under the MIT License | ||
|
|
||
| """All the steps to transform final relationships.""" | ||
| """Stream-finalize relationship rows into an output Table.""" | ||
|
|
||
| from typing import Any | ||
| from uuid import uuid4 | ||
|
|
||
| import pandas as pd | ||
| from graphrag_storage.tables.table import Table | ||
|
|
||
| from graphrag.data_model.schemas import RELATIONSHIPS_FINAL_COLUMNS | ||
| from graphrag.graphs.compute_degree import compute_degree | ||
| from graphrag.index.operations.compute_edge_combined_degree import ( | ||
| compute_edge_combined_degree, | ||
| ) | ||
|
|
||
|
|
||
| def finalize_relationships( | ||
| relationships: pd.DataFrame, | ||
| ) -> pd.DataFrame: | ||
| """All the steps to transform final relationships.""" | ||
| degrees = compute_degree(relationships) | ||
|
|
||
| final_relationships = relationships.drop_duplicates(subset=["source", "target"]) | ||
| final_relationships["combined_degree"] = compute_edge_combined_degree( | ||
| final_relationships, | ||
| degrees, | ||
| node_name_column="title", | ||
| node_degree_column="degree", | ||
| edge_source_column="source", | ||
| edge_target_column="target", | ||
| ) | ||
|
|
||
| final_relationships.reset_index(inplace=True) | ||
| final_relationships["human_readable_id"] = final_relationships.index | ||
| final_relationships["id"] = final_relationships["human_readable_id"].apply( | ||
| lambda _x: str(uuid4()) | ||
| ) | ||
|
|
||
| return final_relationships.loc[ | ||
| :, | ||
| RELATIONSHIPS_FINAL_COLUMNS, | ||
| ] | ||
|
|
||
|
|
||
| async def finalize_relationships( | ||
| relationships_table: Table, | ||
| degree_map: dict[str, int], | ||
| ) -> list[dict[str, Any]]: | ||
| """Deduplicate relationships, enrich with combined degree, and write. | ||
|
|
||
| Streams through the relationships table, deduplicates by | ||
| (source, target) pair, computes combined_degree as the sum of | ||
| source and target node degrees, and writes each finalized row | ||
| back to the table. | ||
|
|
||
| Args | ||
| ---- | ||
| relationships_table: Table | ||
| Opened table for reading and writing relationship rows. | ||
| degree_map: dict[str, int] | ||
| Pre-computed mapping of entity title to node degree. | ||
|
|
||
| Returns | ||
| ------- | ||
| list[dict[str, Any]] | ||
| Sample of up to 5 relationship rows for logging. | ||
| """ | ||
| sample_rows: list[dict[str, Any]] = [] | ||
| seen: set[tuple[str, str]] = set() | ||
| human_readable_id = 0 | ||
|
|
||
| async for row in relationships_table: | ||
| key = (row.get("source", ""), row.get("target", "")) | ||
| if key in seen: | ||
| continue | ||
| seen.add(key) | ||
| row["combined_degree"] = degree_map.get(key[0], 0) + degree_map.get(key[1], 0) | ||
| row["human_readable_id"] = human_readable_id | ||
| row["id"] = str(uuid4()) | ||
| human_readable_id += 1 | ||
| final = {col: row.get(col) for col in RELATIONSHIPS_FINAL_COLUMNS} | ||
| await relationships_table.write(final) | ||
| if len(sample_rows) < 5: | ||
| sample_rows.append(final) | ||
|
|
||
| return sample_rows |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.