-
Notifications
You must be signed in to change notification settings - Fork 102
New schema and pruning #347
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
stellasia
merged 25 commits into
neo4j:main
from
stellasia:feature/schema-driven-pruning
Jun 11, 2025
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
11423d9
Update schema definition
stellasia ea91b68
Add graph pruning component and tests (WIP)
stellasia a95b3c8
Cleaning
stellasia 869129f
Add pruner to SimpleKGPipeline
stellasia fd3cdce
Add test for relationship enforcement
stellasia aeb3c81
Change return model to have some stats about pruned objects
stellasia 53b0cdf
We need to filter out relationships if start/end node is not valid in…
stellasia fc1d65a
Do not filter based on patterns if relationship type not in schema an…
stellasia df22c7f
Raise proper error type
stellasia c9beb95
Ruff/mypy
stellasia 81023d2
Add e2e test for graph pruning component
stellasia 80bd037
Mypy
stellasia 6c666c0
Update changelog and doc
stellasia bddcd71
Mypy
stellasia 71893bd
ChatGPT was wrong
stellasia 7d70aef
Change edge case behaviour
stellasia da4bc45
Fix doc
stellasia f7b024c
Update doc
stellasia a0d3e0a
Fix condition
stellasia 91ed088
Remove incomplete comments
stellasia 27c7ba6
More pruning stats
stellasia dd52c44
Typo
stellasia 50da0e4
Remove default value for consistency
stellasia ea1e29e
Add a section to the doc
stellasia bc501d9
Mypy checks
stellasia 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
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
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
136 changes: 136 additions & 0 deletions
136
examples/customize/build_graph/components/pruners/graph_pruner.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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
"""This example demonstrates how to use the GraphPruner component.""" | ||
|
||
import asyncio | ||
|
||
from neo4j_graphrag.experimental.components.graph_pruning import GraphPruning | ||
from neo4j_graphrag.experimental.components.schema import ( | ||
GraphSchema, | ||
NodeType, | ||
PropertyType, | ||
RelationshipType, | ||
) | ||
from neo4j_graphrag.experimental.components.types import ( | ||
Neo4jGraph, | ||
Neo4jNode, | ||
Neo4jRelationship, | ||
) | ||
|
||
graph = Neo4jGraph( | ||
nodes=[ | ||
Neo4jNode( | ||
id="Person/John", | ||
label="Person", | ||
properties={ | ||
"firstName": "John", | ||
"lastName": "Doe", | ||
"occupation": "employee", | ||
}, | ||
), | ||
Neo4jNode( | ||
id="Person/Jane", | ||
label="Person", | ||
properties={ | ||
"firstName": "Jane", | ||
}, | ||
), | ||
Neo4jNode( | ||
id="Person/Jack", | ||
label="Person", | ||
properties={"firstName": "Jack", "lastName": "Dae"}, | ||
), | ||
Neo4jNode( | ||
id="Organization/Corp1", | ||
label="Organization", | ||
properties={"name": "CorpA"}, | ||
), | ||
], | ||
relationships=[ | ||
Neo4jRelationship( | ||
start_node_id="Person/John", | ||
end_node_id="Person/Jack", | ||
type="KNOWS", | ||
), | ||
Neo4jRelationship( | ||
start_node_id="Organization/CorpA", | ||
end_node_id="Person/Jack", | ||
type="WORKS_FOR", | ||
), | ||
Neo4jRelationship( | ||
start_node_id="Person/John", | ||
end_node_id="Person/Jack", | ||
type="PARENT_OF", | ||
), | ||
], | ||
) | ||
|
||
schema = GraphSchema( | ||
node_types=( | ||
NodeType( | ||
label="Person", | ||
properties=[ | ||
PropertyType(name="firstName", type="STRING", required=True), | ||
PropertyType(name="lastName", type="STRING", required=True), | ||
PropertyType(name="age", type="INTEGER"), | ||
], | ||
additional_properties=False, | ||
), | ||
NodeType( | ||
label="Organization", | ||
properties=[ | ||
PropertyType(name="name", type="STRING", required=True), | ||
PropertyType(name="address", type="STRING"), | ||
], | ||
), | ||
), | ||
relationship_types=( | ||
RelationshipType( | ||
label="WORKS_FOR", | ||
properties=[PropertyType(name="since", type="LOCAL_DATETIME")], | ||
), | ||
RelationshipType( | ||
label="KNOWS", | ||
), | ||
), | ||
patterns=( | ||
("Person", "KNOWS", "Person"), | ||
("Person", "WORKS_FOR", "Organization"), | ||
), | ||
additional_node_types=False, | ||
additional_relationship_types=False, | ||
additional_patterns=False, | ||
) | ||
|
||
|
||
async def main() -> None: | ||
pruner = GraphPruning() | ||
res = await pruner.run(graph, schema) | ||
print("=" * 20, "FINAL CLEANED GRAPH:", "=" * 20) | ||
print(res.graph) | ||
print("=" * 20, "PRUNED ITEM:", "=" * 20) | ||
print(res.pruning_stats) | ||
print("-" * 10, "PRUNED NODES:") | ||
for node in res.pruning_stats.pruned_nodes: | ||
print( | ||
node.item.label, | ||
"with properties", | ||
node.item.properties, | ||
"pruned because", | ||
node.pruned_reason, | ||
node.metadata, | ||
) | ||
print("-" * 10, "PRUNED RELATIONSHIPS:") | ||
for rel in res.pruning_stats.pruned_relationships: | ||
print(rel.item.type, "pruned because", rel.pruned_reason) | ||
print("-" * 10, "PRUNED PROPERTIES:") | ||
for prop in res.pruning_stats.pruned_properties: | ||
print( | ||
prop.item, | ||
"from node label", | ||
prop.label, | ||
"pruned because", | ||
prop.pruned_reason, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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.