chrisammon3000 / dspy-neo4j-knowledge-graph

LLM-driven automated knowledge graph construction from text using DSPy and Neo4j.
144 stars 17 forks source link

dspy-neo4j-knowledge-graph

LLM-driven automated knowledge graph construction from text using DSPy and Neo4j.

Knowledge Graph

Project Structure

dspy-neo4j-knowledge-graph/
├── README.md
├── examples
├── requirements.txt
├── run.py
└── src

Description

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.

Optimized Schema Context

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.

Quick Start

  1. Clone the repository.
  2. Create a Python virtual environment and install the required packages.
  3. Create a .env file and add the required environment variables.
  4. Run Neo4j using Docker.
  5. Run python3 run.py and paste your text in the prompt.
  6. Navigate to http://localhost:7474/browser/ to view the Knowledge Graph in Neo4j Browser.

Installation

Prerequisites

Environment Variables

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>

Python Virtual Environment

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

Usage

Run Neo4j using Docker.

docker run \
    --name dspy-kg \
    --publish=7474:7474 \
    --publish=7687:7687 \
    --env "NEO4J_AUTH=none" \
    neo4j:5.15

Clean Up

Stop and remove the Neo4j container.

docker stop dspy-kg
docker rm dspy-kg

Deactivate the Python virtual environment.

deactivate
rm -rf .venv

License

This project is licensed under the MIT License - see the LICENSE file for details.

References

Contact

Primary Contact: @chrisammon3000