Skip to content

Commit

Permalink
Updated knowledge graph construction example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
alexthomas93 committed Oct 7, 2024
1 parent f63a375 commit f3125eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
44 changes: 32 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,64 @@ Follow installation instructions [here](https://pygraphviz.github.io/documentati

### Knowledge graph construction

**NOTE: The [APOC core library](https://neo4j.com/labs/apoc/) must be installed in your Neo4j instance in order to use this feature**

Assumption: Neo4j running

```python
from neo4j import GraphDatabase
from neo4j_graphrag.embeddings import OpenAIEmbeddings
from neo4j_graphrag.experimental.pipeline.kg_builder import SimpleKGPipeline
from neo4j_graphrag.llm.openai_llm import OpenAILLM

# Connect to Neo4j database
URI = "neo4j://localhost:7687"
AUTH = ("neo4j", "password")
driver = GraphDatabase.driver(URI, auth=AUTH)

# Instantiate Entity and Relation objects
entities = ["PERSON", "ORGANIZATION", "LOCATION"]
relations = ["SITUATED_AT", "INTERACTS", "LED_BY"]
entities = ["Book", "Genre", "Person", "Date"]
relations = ["AUTHORED_BY", "CATEGORIZED_BY", "PUBLISHED_ON"]
potential_schema = [
("PERSON", "SITUATED_AT", "LOCATION"),
("PERSON", "INTERACTS", "PERSON"),
("ORGANIZATION", "LED_BY", "PERSON"),
("Book", "AUTHORED_BY", "Person"),
("Book", "CATEGORIZED_BY", "Genre"),
("Book", "PUBLISHED_ON", "Date"),
]

# Instantiate an Embedder object
embedder = OpenAIEmbeddings(model="text-embedding-3-large")

# Instantiate the LLM
llm = OpenAILLM(
model_name="gpt-4o",
model_params={
"max_tokens": 2000,
"response_format": {"type": "json_object"},
"temperature": 0,
},
)

# Create an instance of the SimpleKGPipeline
# Instantiate the SimpleKGPipeline
kg_builder = SimpleKGPipeline(
llm=llm,
driver=driver,
embedder=OpenAIEmbeddings(),
file_path=file_path,
embedder=embedder,
entities=entities,
relations=relations,
on_error="CONTINUE",
from_pdf=False,
)

await kg_builder.run_async(text="""
Albert Einstein was a German physicist born in 1879 who wrote many groundbreaking
papers especially about general relativity and quantum mechanics.
""")
await kg_builder.run_async(
text="""Dune is a 1965 epic science fiction novel by American author Frank Herbert,
originally published as two separate serials (1963-64 novel Dune World and
1965 novel Prophet of Dune) in Analog magazine."""
)
```

Example knowledge graph created using the above code:

![Example knowledge graph](images/kg_construction.svg)


### Creating a vector index
Expand Down
1 change: 1 addition & 0 deletions images/kg_construction.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f3125eb

Please sign in to comment.