Skip to content

Commit 9816e90

Browse files
Update change log and docs
1 parent 4ebe9bd commit 9816e90

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Next
44

5+
### Added
6+
7+
- Added optional schema enforcement as a validation layer after entity and relation extraction.
8+
59
## 1.5.0
610

711
### Added

docs/source/user_guide_kg_builder.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ This schema information can be provided to the `SimpleKGBuilder` as demonstrated
125125
# ...
126126
)
127127
128-
Prompt Template, Lexical Graph Config and Error Behavior
128+
Prompt Template, Lexical Graph Config, Schema Enforcement, and Error Behavior
129129
--------------------------------------------------------
130130

131131
These parameters are part of the `EntityAndRelationExtractor` component.
@@ -138,6 +138,7 @@ They are also accessible via the `SimpleKGPipeline` interface.
138138
# ...
139139
prompt_template="",
140140
lexical_graph_config=my_config,
141+
enforce_schema=SchemaEnforcementMode.Strict
141142
on_error="RAISE",
142143
# ...
143144
)
@@ -829,6 +830,38 @@ It can be used in this way:
829830

830831
The LLM to use can be customized, the only constraint is that it obeys the :ref:`LLMInterface <llminterface>`.
831832

833+
Schema Enforcement Behaviour
834+
---------------
835+
By default, even if a schema is provided to guide the LLM in the entity and relation extraction, the LLM response is not validated against that schema.
836+
This behaviour can be changed by using the `enforce_schema` flag in the `LLMEntityRelationExtractor` constructor:
837+
838+
.. code:: python
839+
840+
from neo4j_graphrag.experimental.components.entity_relation_extractor import LLMEntityRelationExtractor
841+
from neo4j_graphrag.experimental.components.types import SchemaEnforcementMode
842+
843+
extractor = LLMEntityRelationExtractor(
844+
# ...
845+
enforce_schema=SchemaEnforcementMode.STRICT,
846+
)
847+
schema = SchemaConfig(
848+
entities={"Label": {"name": str}},
849+
relations={"REL_TYPE": {}},
850+
potential_schema=[("Label", "REL_TYPE", "Label")]
851+
)
852+
853+
#....
854+
result = await extractor.run(
855+
#...
856+
schema=schema
857+
)
858+
859+
In this scenario, any extracted node/relation/property that is not part of the provided schema will be pruned.
860+
Any relation whose start node or end node does not conform to the provided tuple in `potential_schema` will be pruned.
861+
If a node is left with no properties, it will be also pruned.
862+
863+
Note that if the schema enforcement mode is on but the schema is not provided, no schema enforcement will be applied.
864+
832865
Error Behaviour
833866
---------------
834867

0 commit comments

Comments
 (0)