@@ -38,43 +38,63 @@ Follow installation instructions [here](https://pygraphviz.github.io/documentati
38
38
39
39
### Knowledge graph construction
40
40
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
+
41
45
``` python
46
+ from neo4j import GraphDatabase
47
+ from neo4j_graphrag.embeddings import OpenAIEmbeddings
42
48
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
43
49
from neo4j_graphrag.llm.openai_llm import OpenAILLM
44
50
51
+ # Connect to Neo4j database
52
+ URI = " neo4j://localhost:7687"
53
+ AUTH = (" neo4j" , " password" )
54
+ driver = GraphDatabase.driver(URI , auth = AUTH )
55
+
45
56
# 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 " ]
48
59
potential_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 " )
52
63
]
53
64
65
+ # Instantiate an Embedder object
66
+ embedder = OpenAIEmbeddings(model = " text-embedding-3-large" )
67
+
54
68
# Instantiate the LLM
55
69
llm = OpenAILLM(
56
70
model_name = " gpt-4o" ,
57
71
model_params = {
58
72
" max_tokens" : 2000 ,
59
73
" response_format" : {" type" : " json_object" },
74
+ " temperature" : 0 ,
60
75
},
61
76
)
62
77
63
- # Create an instance of the SimpleKGPipeline
78
+ # Instantiate the SimpleKGPipeline
64
79
kg_builder = SimpleKGPipeline(
65
80
llm = llm,
66
81
driver = driver,
67
- embedder = OpenAIEmbeddings() ,
82
+ embedder = embedder ,
68
83
entities = entities,
69
84
relations = relations,
85
+ on_error = " CONTINUE" ,
86
+ from_pdf = False ,
70
87
)
71
88
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
+ )
76
93
```
77
94
95
+ Example knowledge graph created using the above code:
96
+
97
+ ![ Example knowledge graph] ( images/kg_construction.svg )
78
98
79
99
80
100
### Creating a vector index
0 commit comments