참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -0,0 +1,87 @@
"""This example demonstrates how to use SimpleKGPipeline with automatic schema extraction
from a PDF file. When no schema is provided to SimpleKGPipeline, automatic schema extraction
is performed using the LLM.
Note: This example requires an OpenAI API key to be set in the .env file.
"""
import asyncio
import logging
import os
from pathlib import Path
from dotenv import load_dotenv
import neo4j
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.embeddings import OpenAIEmbeddings
# Load environment variables from .env file
load_dotenv()
# Configure logging
logging.basicConfig()
logging.getLogger("neo4j_graphrag").setLevel(logging.INFO)
# PDF file path
root_dir = Path(__file__).parents[2]
PDF_FILE = str(
root_dir / "data" / "Harry Potter and the Chamber of Secrets Summary.pdf"
)
async def run_kg_pipeline_with_auto_schema() -> None:
"""Run the SimpleKGPipeline with automatic schema extraction from a PDF file."""
# Define Neo4j connection
uri = os.getenv("NEO4J_URI", "neo4j://localhost:7687")
user = os.getenv("NEO4J_USER", "neo4j")
password = os.getenv("NEO4J_PASSWORD", "password")
# Initialize the Neo4j driver
driver = neo4j.GraphDatabase.driver(uri, auth=(user, password))
# Create the LLM instance
llm = OpenAILLM(model_name="gpt-5")
# Create the embedder instance
embedder = OpenAIEmbeddings()
try:
# Create a SimpleKGPipeline instance without providing a schema
# This will trigger automatic schema extraction
kg_builder = SimpleKGPipeline(
llm=llm,
driver=driver,
embedder=embedder,
from_file=True,
)
print(f"Processing PDF file: {PDF_FILE}")
# Run the pipeline on the PDF file
await kg_builder.run_async(file_path=PDF_FILE)
finally:
# Close connections
await llm.aclose()
driver.close()
async def main() -> None:
"""Run the example."""
# Create data directory if it doesn't exist
data_dir = root_dir / "data"
data_dir.mkdir(exist_ok=True)
# Check if the PDF file exists
if not Path(PDF_FILE).exists():
print(f"Warning: PDF file not found at {PDF_FILE}")
print("Please replace with a valid PDF file path.")
return
# Run the pipeline
await run_kg_pipeline_with_auto_schema()
if __name__ == "__main__":
asyncio.run(main())

View File

@@ -0,0 +1,94 @@
"""This example demonstrates how to use SimpleKGPipeline with automatic schema extraction
from a text input. When no schema is provided to SimpleKGPipeline, automatic schema extraction
is performed using the LLM.
Note: This example requires an OpenAI API key to be set in the .env file.
"""
import asyncio
import logging
import os
from dotenv import load_dotenv
import neo4j
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.llm import OpenAILLM
from neo4j_graphrag.embeddings import OpenAIEmbeddings
# Load environment variables from .env file
load_dotenv()
# Configure logging
logging.basicConfig()
logging.getLogger("neo4j_graphrag").setLevel(logging.DEBUG)
# Sample text to build a knowledge graph from
TEXT = """
Acme Corporation was founded in 1985 by John Smith in New York City.
The company specializes in manufacturing high-quality widgets and gadgets
for the consumer electronics industry.
Sarah Johnson joined Acme in 2010 as a Senior Engineer and was promoted to
Engineering Director in 2015. She oversees a team of 12 engineers working on
next-generation products. Sarah holds a PhD in Electrical Engineering from MIT
and has filed 5 patents during her time at Acme.
The company expanded to international markets in 2012, opening offices in London,
Tokyo, and Berlin. Each office is managed by a regional director who reports
directly to the CEO, Michael Brown, who took over leadership in 2008.
Acme's most successful product, the SuperWidget X1, was launched in 2018 and
has sold over 2 million units worldwide. The product was developed by a team led
by Robert Chen, who joined the company in 2016 after working at TechGiant for 8 years.
The company currently employs 250 people across its 4 locations and had a revenue
of $75 million in the last fiscal year. Acme is planning to go public in 2024
with an estimated valuation of $500 million.
"""
async def run_kg_pipeline_with_auto_schema() -> None:
"""Run the SimpleKGPipeline with automatic schema extraction from text input."""
# Define Neo4j connection
uri = os.getenv("NEO4J_URI", "neo4j://localhost:7687")
user = os.getenv("NEO4J_USER", "neo4j")
password = os.getenv("NEO4J_PASSWORD", "password")
# Initialize the Neo4j driver
driver = neo4j.GraphDatabase.driver(uri, auth=(user, password))
# Create the LLM instance
llm = OpenAILLM(
model_name="gpt-5",
)
# Create the embedder instance
embedder = OpenAIEmbeddings()
try:
# Create a SimpleKGPipeline instance without providing a schema
# This will trigger automatic schema extraction
kg_builder = SimpleKGPipeline(
llm=llm,
driver=driver,
embedder=embedder,
from_file=False, # Using raw text input, not PDF
)
# Run the pipeline on the text
await kg_builder.run_async(text=TEXT)
finally:
# Close connections
await llm.aclose()
driver.close()
async def main() -> None:
"""Run the example."""
await run_kg_pipeline_with_auto_schema()
if __name__ == "__main__":
asyncio.run(main())

View File

@@ -0,0 +1,109 @@
{
"version_": "1",
"template_": "SimpleKGPipeline",
"neo4j_config": {
"params_": {
"uri": {
"resolver_": "ENV",
"var_": "NEO4J_URI"
},
"user": {
"resolver_": "ENV",
"var_": "NEO4J_USER"
},
"password": {
"resolver_": "ENV",
"var_": "NEO4J_PASSWORD"
}
}
},
"llm_config": {
"class_": "OpenAILLM",
"params_": {
"api_key": {
"resolver_": "ENV",
"var_": "OPENAI_API_KEY"
},
"model_name": "gpt-5"
}
},
"embedder_config": {
"class_": "OpenAIEmbeddings",
"params_": {
"api_key": {
"resolver_": "ENV",
"var_": "OPENAI_API_KEY"
}
}
},
"from_file": false,
"schema": {
"node_types": [
"Person",
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
"relationship_types": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
"patterns": [
[
"Person",
"PARENT_OF",
"Person"
],
[
"Person",
"HEIR_OF",
"House"
],
[
"House",
"RULES",
"Planet"
]
]
},
"text_splitter": {
"class_": "text_splitters.fixed_size_splitter.FixedSizeSplitter",
"params_": {
"chunk_size": 100,
"chunk_overlap": 10
}
},
"perform_entity_resolution": true
}

View File

@@ -0,0 +1,59 @@
version_: "1"
template_: SimpleKGPipeline
neo4j_config:
params_:
uri:
resolver_: ENV
var_: NEO4J_URI
user:
resolver_: ENV
var_: NEO4J_USER
password:
resolver_: ENV
var_: NEO4J_PASSWORD
llm_config:
class_: OpenAILLM
params_:
api_key:
resolver_: ENV
var_: OPENAI_API_KEY
model_name: gpt-5
embedder_config:
class_: OpenAIEmbeddings
params_:
api_key:
resolver_: ENV
var_: OPENAI_API_KEY
from_file: false
schema:
node_types:
- label: Person
- label: House
description: Family the person belongs to
properties:
- name: name
type: STRING
- label: Planet
properties:
- name: name
type: STRING
- name: weather
type: STRING
relationship_types:
- label: PARENT_OF
- label: HEIR_OF
description: Used for inheritor relationship between father and sons
- label: RULES
properties:
- name: fromYear
type: INTEGER
patterns:
- ["Person", "PARENT_OF", "Person"]
- ["Person", "HEIR_OF", "House"]
- ["House", "RULES", "Planet"]
text_splitter:
class_: text_splitters.fixed_size_splitter.FixedSizeSplitter
params_:
chunk_size: 100
chunk_overlap: 10
perform_entity_resolution: true

View File

@@ -0,0 +1,115 @@
{
"version_": "1",
"template_": "SimpleKGPipeline",
"neo4j_config": {
"params_": {
"uri": {
"resolver_": "ENV",
"var_": "NEO4J_URI"
},
"user": {
"resolver_": "ENV",
"var_": "NEO4J_USER"
},
"password": {
"resolver_": "ENV",
"var_": "NEO4J_PASSWORD"
}
}
},
"llm_config": {
"class_": "OpenAILLM",
"params_": {
"api_key": {
"resolver_": "ENV",
"var_": "OPENAI_API_KEY"
},
"model_name": "gpt-5"
}
},
"embedder_config": {
"class_": "OpenAIEmbeddings",
"params_": {
"api_key": {
"resolver_": "ENV",
"var_": "OPENAI_API_KEY"
}
}
},
"from_file": true,
"schema": {
"node_types": [
"Person",
{
"label": "House",
"description": "Family the person belongs to",
"properties": [
{
"name": "name",
"type": "STRING"
}
]
},
{
"label": "Planet",
"properties": [
{
"name": "name",
"type": "STRING"
},
{
"name": "weather",
"type": "STRING"
}
]
}
],
"relationship_types": [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons"
},
{
"label": "RULES",
"properties": [
{
"name": "fromYear",
"type": "INTEGER"
}
]
}
],
"patterns": [
[
"Person",
"PARENT_OF",
"Person"
],
[
"Person",
"HEIR_OF",
"House"
],
[
"House",
"RULES",
"Planet"
]
]
},
"text_splitter": {
"class_": "text_splitters.fixed_size_splitter.FixedSizeSplitter",
"params_": {
"chunk_size": 100,
"chunk_overlap": 10
}
},
"file_loader": {
"class_": "data_loader.PdfLoader",
"run_params_": {
"fs": "http"
}
},
"perform_entity_resolution": true
}

View File

@@ -0,0 +1,47 @@
"""In this example, the pipeline is defined in a JSON ('simple_kg_pipeline_config.json')
or YAML ('simple_kg_pipeline_config.yaml') file.
According to the configuration file, some parameters will be read from the env vars
(Neo4j credentials and the OpenAI API key).
"""
import asyncio
import logging
## If env vars are in a .env file, uncomment:
## (requires pip install python-dotenv)
# from dotenv import load_dotenv
# load_dotenv()
# env vars manually set for testing:
import os
from pathlib import Path
from neo4j_graphrag.experimental.pipeline.config.runner import PipelineRunner
from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult
logging.basicConfig()
logging.getLogger("neo4j_graphrag").setLevel(logging.DEBUG)
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USER"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "password"
# os.environ["OPENAI_API_KEY"] = "sk-..."
root_dir = Path(__file__).parent
file_path = root_dir / "simple_kg_pipeline_config.yaml"
# file_path = root_dir / "simple_kg_pipeline_config.json"
# Text to process
TEXT = """The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House Atreides,
an aristocratic family that rules the planet Caladan, the rainy planet, since 10191."""
async def main() -> PipelineResult:
pipeline = PipelineRunner.from_config_file(file_path)
return await pipeline.run({"text": TEXT})
if __name__ == "__main__":
print(asyncio.run(main()))

View File

@@ -0,0 +1,45 @@
"""In this example, the pipeline is defined in a JSON ('simple_kg_pipeline_config.json')
or YAML ('simple_kg_pipeline_config.yaml') file.
According to the configuration file, some parameters will be read from the env vars
(Neo4j credentials and the OpenAI API key).
"""
import asyncio
import logging
## If env vars are in a .env file, uncomment:
## (requires pip install python-dotenv)
# from dotenv import load_dotenv
# load_dotenv()
# env vars manually set for testing:
import os
from pathlib import Path
from neo4j_graphrag.experimental.pipeline.config.runner import PipelineRunner
from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult
logging.basicConfig()
logging.getLogger("neo4j_graphrag").setLevel(logging.DEBUG)
os.environ["NEO4J_URI"] = "bolt://localhost:7687"
os.environ["NEO4J_USER"] = "neo4j"
os.environ["NEO4J_PASSWORD"] = "password"
# os.environ["OPENAI_API_KEY"] = "sk-..."
root_dir = Path(__file__).parent
file_path = root_dir / "simple_kg_pipeline_config_url.json"
# File to process
URL = "https://raw.githubusercontent.com/neo4j/neo4j-graphrag-python/c166afc4d5abc56a5686f3da46a97ed7c07da19d/examples/data/Harry%20Potter%20and%20the%20Chamber%20of%20Secrets%20Summary.pdf"
async def main() -> PipelineResult:
pipeline = PipelineRunner.from_config_file(file_path)
return await pipeline.run({"file_path": URL})
if __name__ == "__main__":
print(asyncio.run(main()))

View File

@@ -0,0 +1,74 @@
"""This example illustrates how to get started easily with the SimpleKGPipeline
and ingest PDF into a Neo4j Knowledge Graph.
This example assumes a Neo4j db is up and running. Update the credentials below
if needed.
OPENAI_API_KEY needs to be in the env vars.
"""
import asyncio
from pathlib import Path
import neo4j
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult
from neo4j_graphrag.llm import LLMInterface
from neo4j_graphrag.llm import OpenAILLM
# Neo4j db infos
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
DATABASE = "neo4j"
root_dir = Path(__file__).parents[1]
file_path = root_dir / "data" / "Harry Potter and the Chamber of Secrets Summary.pdf"
# Instantiate NodeType and RelationshipType objects. This defines the
# entities and relations the LLM will be looking for in the text.
NODE_TYPES = ["Person", "Organization", "Location"]
RELATIONSHIP_TYPES = ["SITUATED_AT", "INTERACTS", "LED_BY"]
PATTERNS = [
("Person", "SITUATED_AT", "Location"),
("Person", "INTERACTS", "Person"),
("Organization", "LED_BY", "Person"),
]
async def define_and_run_pipeline(
neo4j_driver: neo4j.Driver,
llm: LLMInterface,
) -> PipelineResult:
# Create an instance of the SimpleKGPipeline
kg_builder = SimpleKGPipeline(
llm=llm,
driver=neo4j_driver,
embedder=OpenAIEmbeddings(),
schema={
"node_types": NODE_TYPES,
"relationship_types": RELATIONSHIP_TYPES,
"patterns": PATTERNS,
},
neo4j_database=DATABASE,
)
return await kg_builder.run_async(
file_path=str(file_path),
# optional, add document metadata, each item will
# be saved as a property of the Document node
# document_metadata={"author": "J. K. Rowling"},
)
async def main() -> PipelineResult:
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
async with OpenAILLM(model_name="gpt-5") as llm:
res = await define_and_run_pipeline(driver, llm)
return res
if __name__ == "__main__":
res = asyncio.run(main())
print(res)

View File

@@ -0,0 +1,105 @@
"""This example illustrates how to get started easily with the SimpleKGPipeline
and ingest text into a Neo4j Knowledge Graph.
This example assumes a Neo4j db is up and running. Update the credentials below
if needed.
NB: when building a KG from text, no 'Document' node is created in the Knowledge Graph.
"""
import asyncio
import logging
import neo4j
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.experimental.pipeline.pipeline import PipelineResult
from neo4j_graphrag.experimental.pipeline.types.schema import (
EntityInputType,
RelationInputType,
)
from neo4j_graphrag.llm import LLMInterface
from neo4j_graphrag.llm import OpenAILLM
logging.basicConfig()
logging.getLogger("neo4j_graphrag").setLevel(logging.DEBUG)
# logging.getLogger("neo4j_graphrag").setLevel(logging.INFO)
# Neo4j db infos
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
DATABASE = "neo4j"
# Text to process
TEXT = """The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of House Atreides,
an aristocratic family that rules the planet Caladan, the rainy planet, since 10191."""
# Instantiate Entity and Relation objects. This defines the
# entities and relations the LLM will be looking for in the text.
NODE_TYPES: list[EntityInputType] = [
# entities can be defined with a simple label...
"Person",
# ... or with a dict if more details are needed,
# such as a description:
{
"label": "House",
"description": "Family the person belongs to",
"properties": [{"name": "name", "type": "STRING"}],
},
# or a list of properties the LLM will try to attach to the entity:
{"label": "Planet", "properties": [{"name": "weather", "type": "STRING"}]},
]
# same thing for relationships:
RELATIONSHIP_TYPES: list[RelationInputType] = [
"PARENT_OF",
{
"label": "HEIR_OF",
"description": "Used for inheritor relationship between father and sons",
},
{"label": "RULES", "properties": [{"name": "fromYear", "type": "INTEGER"}]},
]
PATTERNS = [
("Person", "PARENT_OF", "Person"),
("Person", "HEIR_OF", "House"),
("House", "RULES", "Planet"),
]
async def define_and_run_pipeline(
neo4j_driver: neo4j.Driver,
llm: LLMInterface,
) -> PipelineResult:
# Create an instance of the SimpleKGPipeline
kg_builder = SimpleKGPipeline(
llm=llm,
driver=neo4j_driver,
embedder=OpenAIEmbeddings(),
schema={
"node_types": NODE_TYPES,
"relationship_types": RELATIONSHIP_TYPES,
"patterns": PATTERNS,
},
from_file=False,
neo4j_database=DATABASE,
)
return await kg_builder.run_async(
text=TEXT,
# optional, specify file path for the Document node
# if not, a random name will be generated
# file_path="my_document.txt"
# optional, add document metadata, each item will
# be saved as a property of the Document node
# document_metadata={"author": "Frank Herbert"},
)
async def main() -> PipelineResult:
with neo4j.GraphDatabase.driver(URI, auth=AUTH) as driver:
async with OpenAILLM(model_name="gpt-5") as llm:
res = await define_and_run_pipeline(driver, llm)
return res
if __name__ == "__main__":
res = asyncio.run(main())
print(res)