@@ -38,43 +38,63 @@ Follow installation instructions [here](https://pygraphviz.github.io/documentati
3838
3939### Knowledge graph construction
4040
41+ ** NOTE: The [ APOC core library] ( https://neo4j.com/labs/apoc/ ) must be installed in your Neo4j instance in order to use this feature**
42+
43+ Assumption: Neo4j running
44+
4145``` python
46+ from neo4j import GraphDatabase
47+ from neo4j_graphrag.embeddings import OpenAIEmbeddings
4248from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
4349from neo4j_graphrag.llm.openai_llm import OpenAILLM
4450
51+ # Connect to Neo4j database
52+ URI = " neo4j://localhost:7687"
53+ AUTH = (" neo4j" , " password" )
54+ driver = GraphDatabase.driver(URI , auth = AUTH )
55+
4556# Instantiate Entity and Relation objects
46- entities = [" PERSON " , " ORGANIZATION " , " LOCATION " ]
47- relations = [" SITUATED_AT " , " INTERACTS " , " LED_BY " ]
57+ entities = [" Person " , " House " , " Planet " ]
58+ relations = [" PARENT_OF " , " HEIR_OF " , " RULES " ]
4859potential_schema = [
49- (" PERSON " , " SITUATED_AT " , " LOCATION " ),
50- (" PERSON " , " INTERACTS " , " PERSON " ),
51- (" ORGANIZATION " , " LED_BY " , " PERSON " ),
60+ (" Person " , " PARENT_OF " , " Person " ),
61+ (" Person " , " HEIR_OF " , " House " ),
62+ (" House " , " RULES " , " Planet " )
5263]
5364
65+ # Instantiate an Embedder object
66+ embedder = OpenAIEmbeddings(model = " text-embedding-3-large" )
67+
5468# Instantiate the LLM
5569llm = OpenAILLM(
5670 model_name = " gpt-4o" ,
5771 model_params = {
5872 " max_tokens" : 2000 ,
5973 " response_format" : {" type" : " json_object" },
74+ " temperature" : 0 ,
6075 },
6176)
6277
63- # Create an instance of the SimpleKGPipeline
78+ # Instantiate the SimpleKGPipeline
6479kg_builder = SimpleKGPipeline(
6580 llm = llm,
6681 driver = driver,
67- embedder = OpenAIEmbeddings() ,
82+ embedder = embedder ,
6883 entities = entities,
6984 relations = relations,
85+ on_error = " CONTINUE" ,
86+ from_pdf = False ,
7087)
7188
72- await kg_builder.run_async(text = """
73- Albert Einstein was a German physicist born in 1879 who wrote many groundbreaking
74- papers especially about general relativity and quantum mechanics.
75- """ )
89+ await kg_builder.run_async(
90+ text = """ "The son of Duke Leto Atreides and the Lady Jessica, Paul is the heir of
91+ House Atreides, an aristocratic family that rules the planet Caladan. """
92+ )
7693```
7794
95+ Example knowledge graph created using the above code:
96+
97+ ![ Example knowledge graph] ( images/kg_construction.svg )
7898
7999
80100### Creating a vector index
0 commit comments