Skip to main content

LangChain integration for the Graphiti knowledge graph system.

Project description

🦜🔗 Graphiti for LangChain

PyPI version CI/CD Status License: MIT

langchain-graphiti is a production-grade, high-quality LangChain integration for the Graphiti knowledge graph system. It provides a robust set of tools and retrievers that empower LangChain agents with long-term, evolving memory and sophisticated graph-based reasoning capabilities.

This library allows your agents to dynamically build and query a knowledge graph, transforming unstructured information into structured, queryable memory that persists across sessions.


Core Concepts

At its core, this integration bridges the gap between LangChain's powerful agentic frameworks and Graphiti's ability to create real-time knowledge graphs.

  • Graphiti: An open-source framework that ingests unstructured data (like conversations or documents) and uses LLMs to automatically extract entities and relationships, building a rich knowledge graph.
  • LangChain Integration: This library exposes Graphiti's functionality through standard LangChain interfaces (BaseTool, BaseRetriever), allowing agents to seamlessly read from, write to, and manage the knowledge graph as part of their workflow.

The result is an AI agent that doesn't just operate on short-term context but learns, remembers, and reasons over a persistent, interconnected web of information.

Features

  • Comprehensive Toolset: A full suite of tools for adding, searching, and managing information in the knowledge graph.
  • Advanced Retrievers: Powerful retrievers that leverage Graphiti's hybrid search (text, vector, and graph traversal) for highly relevant and context-aware information retrieval.
  • Robust Client: A thread-safe, async-first client with built-in health checks, connection management, and error handling.
  • Production-Ready: Designed with a focus on quality, robustness, and meticulous engineering for reliable deployment.
  • Seamless Integration: Built to feel like a native part of the LangChain ecosystem.

Installation

pip install langchain-graphiti

You will also need to install extras depending on the LLM and database providers you intend to use:

# Example for OpenAI and Neo4j
pip install "langchain-graphiti[openai,neo4j]"

See pyproject.toml for a full list of available extras.

Quick Start

Here's a simple example of how to get up and running with langchain-graphiti. This example uses langchain-openai and a local Neo4j database.

import asyncio
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

from langchain_graphiti import GraphitiClient
from langchain_graphiti.tools import create_basic_agent_tools
from langchain_graphiti.config import LLMProvider, DriverProvider, OpenAIConfig, Neo4jConfig

# 1. Configure your providers
llm_config = OpenAIConfig()  # Assumes OPENAI_API_KEY is in your environment
driver_config = Neo4jConfig() # Assumes NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD are set

# 2. Create the GraphitiClient
# This client manages the connection to your knowledge graph
client = GraphitiClient.from_factory(
    llm_provider=LLMProvider.OPENAI,
    driver_provider=DriverProvider.NEO4J,
    llm_config=llm_config,
    driver_config=driver_config,
)

# 3. Create the agent tools
# These are the functions the agent can call to interact with the graph
tools = create_basic_agent_tools(client)

# 4. Set up your LangChain agent
llm = ChatOpenAI(model="gpt-4o")
agent_executor = create_react_agent(llm, tools)

async def main():
    # Let's teach the agent something new
    await agent_executor.ainvoke({
        "messages": [("user", "Add this fact: 'Project Phoenix is managed by Alice.'")]
    })

    # Now, let's ask a question that requires recalling that fact
    response = await agent_executor.ainvoke({
        "messages": [("user", "Who manages Project Phoenix?")]
    })

    print(response["messages"][-1].content)
    # Expected output will mention that Alice manages Project Phoenix.

    # Clean up the client connection
    await client.close()

if __name__ == "__main__":
    asyncio.run(main())

Advanced Usage

Using the GraphitiRetriever

The GraphitiRetriever is a powerful tool for fetching documents from your knowledge graph. It can be used in any standard LangChain RAG (Retrieval-Augmented Generation) chain.

from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_graphiti import GraphitiRetriever

# Assume 'client' is an initialized GraphitiClient
retriever = GraphitiRetriever(client=client, k=5)

template = """
Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# This will search the graph for context related to the question
result = rag_chain.invoke("What were the key outcomes of the Q3 budget meeting?")
print(result)

Using the Full Toolset

For agents that require more advanced control over the knowledge graph, you can use the create_agent_tools factory to get the complete set of tools.

from langchain_graphiti.tools import create_agent_tools

# Get all available tools
advanced_tools = create_agent_tools(client)

# Create an agent with full capabilities
advanced_agent = create_react_agent(llm, advanced_tools)

# This agent can now perform advanced operations like:
# - Building communities
# - Removing specific episodes
# - Adding structured triplets directly
# - Optimizing the database with indices

Contributing

We welcome contributions to langchain-graphiti! Whether it's reporting a bug, suggesting a new feature, or submitting a pull request, your help is valued.

Please see the CONTRIBUTING.md file for detailed guidelines on how to contribute to the project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_graphiti-0.1.0.tar.gz (27.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_graphiti-0.1.0-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file langchain_graphiti-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_graphiti-0.1.0.tar.gz
  • Upload date:
  • Size: 27.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_graphiti-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a875b4331a774d9716b90d7dc6b8f0ffe20677a9f04b8e5bd627f3e589a39115
MD5 c3fb784fab77f5344945aa1e1ef603ae
BLAKE2b-256 325b770978c3415c1c9e75c9056dedfbafa074d01f7b1ff0d5b26c0539d1857c

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_graphiti-0.1.0.tar.gz:

Publisher: deploy.yml on dev-mirzabicer/langchain_graphiti

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_graphiti-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_graphiti-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b195f14a54932ebf5dac2d25e5197ece1bd874322bcfeda5632a62c4ba7a60ae
MD5 4f7b455f900960283359fa931c320147
BLAKE2b-256 6f26e59311fd33791f6e76ac4e884ce462db29b2bc07f9146376a41bc449617c

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_graphiti-0.1.0-py3-none-any.whl:

Publisher: deploy.yml on dev-mirzabicer/langchain_graphiti

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page