LLM-driven automated knowledge graph construction from text using DSPy and Neo4j.
dspy-neo4j-knowledge-graph/
├── README.md
├── examples
├── requirements.txt
├── run.py
└── src
Model entities and relationships and build a Knowledge Graph using DSPy, Neo4j, and OpenAI's GPT-4. When given a paragraph or block of text, the app uses the DSPy library and OpenAI's GPT-4 to extract entities and relationships and generate a Cypher statement which is run in Neo4j to create the Knowledge Graph.
The current graph schema is passed to the model as a list of nodes, relationships and properties in the context of the prompt. This allows the model to use elements from the existing schema and make connections between existing entities and relationships.
- Clone the repository.
- Create a Python virtual environment and install the required packages.
- Create a
.env
file and add the required environment variables. - Run Neo4j using Docker.
- Run
python3 run.py
and paste your text in the prompt. - Navigate to
http://localhost:7474/browser/
to view the Knowledge Graph in Neo4j Browser.
- Python 3.12
- OpenAI API Key
- Docker
Before you begin, make sure to create a .env
file and add your OpenAI API key.
NEO4J_URI=bolt://localhost:7687
OPENAI_API_KEY=<your-api-key>
Create a Python virtual environment and install the required packages.
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
Run Neo4j using Docker.
docker run \
--name dspy-kg \
--publish=7474:7474 \
--publish=7687:7687 \
--env "NEO4J_AUTH=none" \
neo4j:5.15
Stop and remove the Neo4j container.
docker stop dspy-kg
docker rm dspy-kg
Deactivate the Python virtual environment.
deactivate
rm -rf .venv
This project is licensed under the MIT License - see the LICENSE file for details.
Primary Contact: @chrisammon3000