참고소스 수정본
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from typing import Any, Optional
|
||||
|
||||
from neo4j_graphrag.experimental.components.entity_relation_extractor import (
|
||||
EntityRelationExtractor,
|
||||
OnError,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import (
|
||||
DocumentInfo,
|
||||
LexicalGraphConfig,
|
||||
Neo4jGraph,
|
||||
TextChunks,
|
||||
)
|
||||
|
||||
|
||||
class MyExtractor(EntityRelationExtractor):
|
||||
def __init__(
|
||||
self,
|
||||
*args: Any,
|
||||
on_error: OnError = OnError.IGNORE,
|
||||
create_lexical_graph: bool = True,
|
||||
**kwargs: Any,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
*args,
|
||||
on_error=on_error,
|
||||
create_lexical_graph=create_lexical_graph,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
async def run(
|
||||
self,
|
||||
chunks: TextChunks,
|
||||
document_info: Optional[DocumentInfo] = None,
|
||||
lexical_graph_config: Optional[LexicalGraphConfig] = None,
|
||||
**kwargs: Any,
|
||||
) -> Neo4jGraph:
|
||||
# Implement your logic here
|
||||
# you can loop over all text chunks with:
|
||||
for chunk in chunks.chunks:
|
||||
pass
|
||||
return Neo4jGraph(nodes=[], relationships=[])
|
||||
@@ -0,0 +1,33 @@
|
||||
from neo4j_graphrag.experimental.components.entity_relation_extractor import (
|
||||
LLMEntityRelationExtractor,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import (
|
||||
Neo4jGraph,
|
||||
TextChunk,
|
||||
TextChunks,
|
||||
)
|
||||
from neo4j_graphrag.llm import LLMInterface
|
||||
|
||||
|
||||
async def main(llm: LLMInterface) -> Neo4jGraph:
|
||||
"""
|
||||
|
||||
Args:
|
||||
llm (LLMInterface): Any LLM implemented in neo4j_graphrag.llm or from LangChain chat models.
|
||||
"""
|
||||
extractor = LLMEntityRelationExtractor(
|
||||
llm=llm,
|
||||
# optional: customize the prompt used for entity and relation extraction
|
||||
# prompt_template="",
|
||||
# optional: disable the creation of the lexical graph (Document and Chunk nodes)
|
||||
# create_lexical_graph=False,
|
||||
# optional: if an LLM error happens, ignore the chunk and continue process with the next ones
|
||||
# default value is OnError.RAISE which will end the process
|
||||
# on_error=OnError.IGNORE,
|
||||
# optional: tune the max_concurrency parameter to optimize speed
|
||||
# max_concurrency=5,
|
||||
)
|
||||
graph = await extractor.run(
|
||||
chunks=TextChunks(chunks=[TextChunk(text="....", index=0)])
|
||||
)
|
||||
return graph
|
||||
@@ -0,0 +1,33 @@
|
||||
from neo4j_graphrag.experimental.components.entity_relation_extractor import (
|
||||
LLMEntityRelationExtractor,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import (
|
||||
Neo4jGraph,
|
||||
TextChunk,
|
||||
TextChunks,
|
||||
)
|
||||
from neo4j_graphrag.llm import LLMInterface
|
||||
|
||||
|
||||
async def main(llm: LLMInterface) -> Neo4jGraph:
|
||||
"""
|
||||
|
||||
Args:
|
||||
llm (LLMInterface): Any LLM implemented in neo4j_graphrag.llm or from LangChain chat models.
|
||||
"""
|
||||
extractor = LLMEntityRelationExtractor(
|
||||
llm=llm,
|
||||
# optional: customize the prompt used for entity and relation extraction
|
||||
# prompt_template="",
|
||||
# optional: disable the creation of the lexical graph (Document and Chunk nodes)
|
||||
# create_lexical_graph=False,
|
||||
# optional: if an LLM error happens, ignore the chunk and continue process with the next ones
|
||||
# default value is OnError.RAISE which will end the process
|
||||
# on_error=OnError.IGNORE,
|
||||
# optional: tune the max_concurrency parameter to optimize speed
|
||||
# max_concurrency=5,
|
||||
)
|
||||
graph = await extractor.run(
|
||||
chunks=TextChunks(chunks=[TextChunk(text="....", index=0)])
|
||||
)
|
||||
return graph
|
||||
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
Simple example demonstrating structured output with LLMEntityRelationExtractor.
|
||||
|
||||
This example shows how to use structured output for more reliable entity and
|
||||
relationship extraction with automatic schema validation.
|
||||
|
||||
The Neo4jGraph schema is now compatible with both OpenAI and VertexAI structured
|
||||
output APIs, with strict schema validation (additionalProperties: false) and
|
||||
proper required field definitions.
|
||||
|
||||
Prerequisites:
|
||||
- Google Cloud credentials configured for VertexAI
|
||||
- Or OpenAI API key set in OPENAI_API_KEY environment variable
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from neo4j_graphrag.experimental.components.entity_relation_extractor import (
|
||||
LLMEntityRelationExtractor,
|
||||
)
|
||||
from neo4j_graphrag.experimental.components.types import (
|
||||
Neo4jGraph,
|
||||
TextChunk,
|
||||
TextChunks,
|
||||
)
|
||||
from neo4j_graphrag.llm import VertexAILLM
|
||||
|
||||
|
||||
async def main() -> Neo4jGraph:
|
||||
"""
|
||||
Demonstrates entity and relation extraction with structured output.
|
||||
|
||||
With use_structured_output=True:
|
||||
- Uses LLMInterfaceV2 (list of messages)
|
||||
- Passes Neo4jGraph Pydantic model as response_format to invoke()
|
||||
- Ensures response conforms to expected graph structure
|
||||
- Provides automatic type validation
|
||||
- Reduces need for JSON repair and error handling
|
||||
"""
|
||||
load_dotenv()
|
||||
# Initialize LLM - no response_format in constructor!
|
||||
llm = VertexAILLM(model_name="gemini-2.5-flash")
|
||||
|
||||
# llm = OpenAILLM(
|
||||
# model_name="gpt-5-mini",
|
||||
# model_params={"temperature": 0}
|
||||
# )
|
||||
|
||||
# Enable structured output for reliable extraction
|
||||
extractor = LLMEntityRelationExtractor(
|
||||
llm=llm,
|
||||
use_structured_output=True, # This is the key parameter!
|
||||
)
|
||||
|
||||
# Sample text about a person and organization
|
||||
sample_text = """
|
||||
Albert Einstein was a theoretical physicist who developed the theory of relativity.
|
||||
He worked at the Institute for Advanced Study in Princeton from 1933 until his death in 1955.
|
||||
"""
|
||||
|
||||
# Extract entities and relationships
|
||||
graph = await extractor.run(
|
||||
chunks=TextChunks(chunks=[TextChunk(text=sample_text, index=0)])
|
||||
)
|
||||
|
||||
return graph
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run extraction
|
||||
graph = asyncio.run(main())
|
||||
|
||||
print(graph)
|
||||
Reference in New Issue
Block a user