This project demonstrates how to build intelligent agents that can extract and maintain knowledge graphs from conversations. It consists of two main POCs:
-
Knowledge Graph Extraction Agent - Extracts structured information from text
-
Conversational Knowledge Graph Agent - Maintains a knowledge graph during a conversation
The project uses Large Language Models (LLMs) to extract structured information from unstructured text and visualize it as a knowledge graph. The agents can:
Extract entities and relationships from text
- Build and maintain a knowledge graph during conversations
- Visualize the knowledge graph using interactive HTML
- Python 3.9+
- DeepSeek API key (or another LLM provider)
- Clone this repository
- Install the required packages:
pip install -r requirements.txt
- Create a .env file in the root directory with your API key:
DEEPSEEK_API_KEY=your_api_key_here
knowledge-graph-extraction-agent.ipynb
- Demonstrates basic knowledge graph extraction from textconversational-kg-extraction-agent.ipynb
- Builds a conversational agent that maintains a knowledge graphconversational_kg_extraction_agent.py
- Python script version of the conversational agentassets/prompts/task-prompt.txt
- System prompt for the conversational agent
Knowledge Graph Extraction The knowledge-graph-extraction-agent.ipynb notebook demonstrates how to extract a knowledge graph from a static text. It:
- Takes an unstructured text input
- Extracts entities and relationships
- Visualizes the knowledge graph
Conversational Knowledge Graph Agent The conversational-kg-extraction-agent.ipynb notebook creates an interactive agent named "Emma" that: Engages in conversation with the user Extracts information about the user during the conversation Builds and maintains a knowledge graph of user information Visualizes the knowledge graph in real-time
Running the Conversational Agent You can run the conversational agent in two ways: Using the Jupyter Notebook: Open conversational-kg-extraction-agent.ipynb Run all cells Interact with the agent in the notebook Using the Python Script: If you've made changes to the notebook, run the last cell to generate the Python script Run the script from the command line:
python conversational_kg_extraction_agent.py
If you want to modify the agent:
- Make your changes in the conversational-kg-extraction-agent.ipynb notebook
- Run the last cell to generate an updated Python script
- Run the script to test your changes
The project uses Pydantic models to define the structure of the knowledge graph:
- Node - Represents entities with labels and types
- Edge - Represents relationships between entities
- Graph - Contains nodes and edges
- InferenceResponse - The structured output from the LLM
The knowledge graph is visualized using:
- pyvis - Creates an interactive HTML visualization
- The visualization is saved as knowledge_graph.html and can be opened in any browser
When you run the conversational agent, it will:
- Ask for your name
- Engage in conversation
- Extract information about you
- Build a knowledge graph
- Generate an interactive visualization of the graph
You can exit the conversation by typing /exit.
- The agent uses the DeepSeek API for inference
- The instructor library is used to get structured outputs from the LLM
- The knowledge graph is updated in real-time as new information is discovered
This project is open source and available for educational and personal use under the MIT license.