Graphiti: Building Real-Time Knowledge Graphs for AI Agents
At its core, getzep/graphiti is a library designed to help you create and manage knowledge graphs. But it's not just any knowledge graph; it's optimized for real-time interaction and for use with AI agents.
Think of a knowledge graph as a super-organized, interconnected web of information. Instead of just a bunch of isolated facts, a knowledge graph shows you how different pieces of information relate to each other. For example, it might show that "New York City" is the "capital of" "New York State," and "New York State" "contains" "the Statue of Liberty."
When we add "real-time" and "AI agents" to the mix, it means
Real-Time
The graph can be updated and queried very quickly, which is crucial for AI agents that need up-to-the-minute information to make decisions or answer questions.
AI Agents
This tool is specifically built to help AI agents (like chatbots, autonomous systems, or decision-making AI) understand and use complex information more effectively. It gives them a structured way to access and reason about the world.
From a software engineer's perspective, getzep/graphiti is incredibly powerful because it helps us overcome several challenges when building AI applications
Enhanced AI Understanding & Reasoning
Beyond Keyword Search
Traditional RAG (Retrieval Augmented Generation) often relies on keyword matching. Graphiti allows AI agents to understand the relationships between concepts, leading to much more nuanced and accurate responses. For example, if you ask an AI about "who created the iPhone," a knowledge graph can directly link "iPhone" to "Apple" and "Steve Jobs," rather than just finding documents with those words.
Contextual Awareness
AI agents can use the graph to maintain a consistent and evolving understanding of the conversation or task at hand, preventing them from losing context.
Handling Complex Information
Structured Data
It provides a structured way to represent unstructured data. Imagine trying to make sense of a long conversation or a massive document; a knowledge graph can extract key entities and their relationships, making it digestible for an AI.
Scalability
As your application grows and the amount of information increases, a knowledge graph offers a robust way to manage and query that data efficiently.
Real-Time Decision Making
Dynamic Updates
For applications where information changes frequently (e.g., stock prices, current events, user preferences), Graphiti allows for real-time updates to the knowledge graph, ensuring your AI agents are always working with the freshest data.
Proactive Behavior
With a constantly updated knowledge graph, AI agents can become more proactive, identifying patterns or anomalies as they emerge in the data.
Debugging & Explainability
Traceable Reasoning
When an AI agent makes a decision or provides an answer, you can often trace its "thought process" by examining the paths it took through the knowledge graph. This is invaluable for debugging and understanding why your AI is behaving in a certain way.
The getzep/graphiti library is designed to be integrated into your Python applications. Here's a general overview of the steps involved
Installation
You'll typically install it via pip
pip install graphiti
Core Concepts
You'll be working with
Nodes (Entities)
These are the individual pieces of information (e.g., "Apple," "iPhone," "Steve Jobs").
Edges (Relationships)
These connect the nodes and describe their relationship (e.g., "developed," "founded by," "is a type of").
Graph Database
Graphiti likely integrates with or provides abstractions over a graph database (like Neo4j, ArangoDB, or even a simpler in-memory graph) to store and query the graph.
Integrating with AI Agents (RAG)
The real magic happens when you integrate Graphiti with your AI agents, especially for RAG workflows. Instead of just retrieving raw text, your agent can
Extract Entities and Relationships
From a user query or a document, extract key entities and how they relate.
Query the Knowledge Graph
Use these extracted entities to query the knowledge graph for relevant, structured information.
Augment Prompts
Inject this structured information from the graph into your AI model's prompt, giving it a much richer and more accurate context.
Let's imagine a simple scenario where we want to build a knowledge graph about companies and their products.
# This is a conceptual example, actual API might vary slightly
# Refer to the official getzep/graphiti documentation for precise API usage.
from graphiti import KnowledgeGraph, Node, Edge
# 1. Initialize your Knowledge Graph
# In a real application, you might connect to a persistent graph database here.
# For simplicity, let's imagine an in-memory graph.
graph = KnowledgeGraph()
# 2. Add Nodes (Entities)
apple = Node("Company", name="Apple Inc.")
iphone = Node("Product", name="iPhone")
macbook = Node("Product", name="MacBook")
tim_cook = Node("Person", name="Tim Cook")
steve_jobs = Node("Person", name="Steve Jobs")
graph.add_node(apple)
graph.add_node(iphone)
graph.add_node(macbook)
graph.add_node(tim_cook)
graph.add_node(steve_jobs)
# 3. Add Edges (Relationships)
graph.add_edge(Edge(apple, "produces", iphone))
graph.add_edge(Edge(apple, "produces", macbook))
graph.add_edge(Edge(tim_cook, "is_ceo_of", apple))
graph.add_edge(Edge(steve_jobs, "founded", apple))
graph.add_edge(Edge(steve_jobs, "developed", iphone))
# 4. Querying the Graph (for AI Agent use)
# Example Query 1: What products does Apple produce?
# An AI agent could formulate this query based on a user's question.
# In a real scenario, this would be more sophisticated (e.g., using graph traversal algorithms).
print("\n--- Products produced by Apple ---")
for product_node in graph.get_connected_nodes(apple, "produces"):
print(f"- {product_node.properties['name']}")
# Example Query 2: Who founded Apple?
print("\n--- Who founded Apple? ---")
for founder_node in graph.get_connected_nodes(apple, "founded_by", reverse=True): # Assuming 'founded_by' is the inverse
print(f"- {founder_node.properties['name']}")
# --- How an AI Agent might use this for RAG ---
def answer_question_with_graph(question: str):
# This is highly simplified. A real agent would use NLP to extract entities/relations.
if "products of Apple" in question.lower():
products = [node.properties['name'] for node in graph.get_connected_nodes(apple, "produces")]
return f"Apple produces: {', '.join(products)}."
elif "who founded Apple" in question.lower():
founders = [node.properties['name'] for node in graph.get_connected_nodes(apple, "founded_by", reverse=True)]
return f"Apple was founded by: {', '.join(founders)}."
else:
return "I can only answer questions about Apple's products and founders based on my current knowledge graph."
print("\n--- AI Agent Interaction ---")
print(f"User: What products does Apple produce?")
print(f"AI: {answer_question_with_graph('What products does Apple produce?')}")
print(f"\nUser: Who founded Apple?")
print(f"AI: {answer_question_with_graph('Who founded Apple?')}")
print(f"\nUser: What is the capital of France?")
print(f"AI: {answer_question_with_graph('What is the capital of France?')}")
Explanation of the Sample Code
We define Node and Edge objects to represent our entities and their relationships.
We add these to a KnowledgeGraph instance.
The get_connected_nodes function (hypothetical, but common in graph libraries) allows us to traverse the graph and find related information.
The answer_question_with_graph function demonstrates how an AI agent could use this queried information to answer questions. Instead of just searching for keywords in a document, it's directly leveraging the structured relationships in the graph.
To truly leverage getzep/graphiti, you'll want to dive into their official documentation. It will cover
Setting up a persistent graph database
For real-world applications, you'll need a database to store your graph.
More advanced querying
Learning about graph traversal algorithms (e.g., breadth-first search, depth-first search) and graph query languages (like Cypher for Neo4j, or AQL for ArangoDB if Graphiti uses them).
Integration with LLMs and NLP
How to effectively extract entities and relationships from unstructured text using NLP techniques and then feed them into your graph.
Real-time updates and change tracking
How to manage an evolving knowledge graph.