Skip to content

Commit 325c3dd

Browse files
committed
Ruff
1 parent f427d66 commit 325c3dd

File tree

3 files changed

+165
-120
lines changed

3 files changed

+165
-120
lines changed

src/neo4j_graphrag/experimental/components/entity_relation_extractor.py

+27-31
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,11 @@ async def run(
346346
return graph
347347

348348
def validate_chunk(
349-
self,
350-
chunk_graph: Neo4jGraph,
351-
schema: SchemaConfig
349+
self, chunk_graph: Neo4jGraph, schema: SchemaConfig
352350
) -> Neo4jGraph:
353351
"""
354-
Perform validation after entity and relation extraction:
355-
- Enforce schema if schema enforcement mode is on and schema is provided
352+
Perform validation after entity and relation extraction:
353+
- Enforce schema if schema enforcement mode is on and schema is provided
356354
"""
357355
if self.enforce_schema != SchemaEnforcementMode.NONE:
358356
if not schema or not schema.entities: # schema is not provided
@@ -365,9 +363,9 @@ def validate_chunk(
365363
return chunk_graph
366364

367365
def _clean_graph(
368-
self,
369-
graph: Neo4jGraph,
370-
schema: SchemaConfig,
366+
self,
367+
graph: Neo4jGraph,
368+
schema: SchemaConfig,
371369
) -> Neo4jGraph:
372370
"""
373371
Verify that the graph conforms to the provided schema.
@@ -389,17 +387,15 @@ def _clean_graph(
389387
return Neo4jGraph(nodes=filtered_nodes, relationships=filtered_rels)
390388

391389
def _enforce_nodes(
392-
self,
393-
extracted_nodes: List[Neo4jNode],
394-
schema: SchemaConfig
390+
self, extracted_nodes: List[Neo4jNode], schema: SchemaConfig
395391
) -> List[Neo4jNode]:
396392
"""
397-
Filter extracted nodes to be conformant to the schema.
393+
Filter extracted nodes to be conformant to the schema.
398394
399-
Keep only those whose label is in schema.
400-
For each valid node, filter out properties not present in the schema.
401-
Remove a node if it ends up with no valid properties.
402-
"""
395+
Keep only those whose label is in schema.
396+
For each valid node, filter out properties not present in the schema.
397+
Remove a node if it ends up with no valid properties.
398+
"""
403399
if self.enforce_schema != SchemaEnforcementMode.STRICT:
404400
return extracted_nodes
405401

@@ -424,10 +420,10 @@ def _enforce_nodes(
424420
return valid_nodes
425421

426422
def _enforce_relationships(
427-
self,
428-
extracted_relationships: List[Neo4jRelationship],
429-
filtered_nodes: List[Neo4jNode],
430-
schema: SchemaConfig
423+
self,
424+
extracted_relationships: List[Neo4jRelationship],
425+
filtered_nodes: List[Neo4jNode],
426+
schema: SchemaConfig,
431427
) -> List[Neo4jRelationship]:
432428
"""
433429
Filter extracted nodes to be conformant to the schema.
@@ -451,8 +447,10 @@ def _enforce_relationships(
451447
if not schema_relation:
452448
continue
453449

454-
if (rel.start_node_id not in valid_nodes or
455-
rel.end_node_id not in valid_nodes):
450+
if (
451+
rel.start_node_id not in valid_nodes
452+
or rel.end_node_id not in valid_nodes
453+
):
456454
continue
457455

458456
start_label = valid_nodes[rel.start_node_id]
@@ -461,8 +459,11 @@ def _enforce_relationships(
461459
tuple_valid = True
462460
if potential_schema:
463461
tuple_valid = (start_label, rel.type, end_label) in potential_schema
464-
reverse_tuple_valid = ((end_label, rel.type, start_label) in
465-
potential_schema)
462+
reverse_tuple_valid = (
463+
end_label,
464+
rel.type,
465+
start_label,
466+
) in potential_schema
466467

467468
if not tuple_valid and not reverse_tuple_valid:
468469
continue
@@ -483,18 +484,13 @@ def _enforce_relationships(
483484
return valid_rels
484485

485486
def _enforce_properties(
486-
self,
487-
properties: Dict[str, Any],
488-
valid_properties: List[Dict[str, Any]]
487+
self, properties: Dict[str, Any], valid_properties: List[Dict[str, Any]]
489488
) -> Dict[str, Any]:
490489
"""
491490
Filter properties.
492491
Keep only those that exist in schema (i.e., valid properties).
493492
"""
494493
valid_prop_names = {prop["name"] for prop in valid_properties}
495494
return {
496-
key: value
497-
for key, value in properties.items()
498-
if key in valid_prop_names
495+
key: value for key, value in properties.items() if key in valid_prop_names
499496
}
500-

src/neo4j_graphrag/experimental/pipeline/config/template_pipeline/simple_kg_builder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040
from neo4j_graphrag.experimental.components.types import (
4141
LexicalGraphConfig,
42-
SchemaEnforcementMode
42+
SchemaEnforcementMode,
4343
)
4444
from neo4j_graphrag.experimental.pipeline.config.object_config import ComponentType
4545
from neo4j_graphrag.experimental.pipeline.config.template_pipeline.base import (

0 commit comments

Comments
 (0)