An integration package connecting Kozmograph and LangChain
Project description
[!IMPORTANT]
This repository has been merged into the Kozmograph AI Toolkit monorepo to avoid duplicating tools.
It will be deleted in one month—please follow the Langchain integration there for all future development, and feel free to open issues or PRs in that repo.
🦜️🔗 LangChain Kozmograph
This package contains the LangChain integration with Kozmograph graph database.
📦 Installation
pip install -U langchain-kozmograph
💻 Integration features
Kozmograph
The Kozmograph class is a wrapper around the database client that supports the
query operation.
import os
from langchain_kozmograph.graphs.kozmograph import Kozmograph
url = os.getenv("KOZMOGRAPH_URI", "bolt://localhost:7687")
username = os.getenv("KOZMOGRAPH_USERNAME", "")
password = os.getenv("KOZMOGRAPH_PASSWORD", "")
graph = Kozmograph(url=url, username=username, password=password, refresh_schema=False)
results = graph.query("MATCH (n) RETURN n LIMIT 1")
print(results)
KozmographQAChain
The KozmographQAChain class enables natural language interactions with a Kozmograph database.
It uses an LLM and the database's schema to translate a user's question into a Cypher query, which is executed against the database.
The resulting data is then sent along with the user's question to the LLM to generate a natural language response.
import os
from langchain_kozmograph.graphs.kozmograph import Kozmograph
from langchain_kozmograph.chains.graph_qa import KozmographQAChain
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("KOZMOGRAPH_URI", "bolt://localhost:7687")
username = os.getenv("KOZMOGRAPH_USERNAME", "")
password = os.getenv("KOZMOGRAPH_PASSWORD", "")
graph = Kozmograph(url=url, username=username, password=password, refresh_schema=False)
chain = KozmographQAChain.from_llm(
ChatOpenAI(temperature=0),
graph=graph,
model_name="gpt-4-turbo",
allow_dangerous_requests=True,
)
response = chain.invoke("Is there a any Person node in the dataset?")
result = response["result"].lower()
print(result)
Kozmograph toolkit
The KozmographToolkit contains different tools agents can leverage to perform specific tasks the user has given them. Toolkit
needs a database object and LLM access since different tools leverage different operations.
Currently supported tools:
- QueryKozmographTool - Basic Cypher query execution tool
import os
import pytest
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langchain_kozmograph import KozmographToolkit
from langchain_kozmograph.graphs.kozmograph import Kozmograph
from langgraph.prebuilt import create_react_agent
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "")
url = os.getenv("KOZMOGRAPH_URI", "bolt://localhost:7687")
username = os.getenv("KOZMOGRAPH_USERNAME", "")
password = os.getenv("KOZMOGRAPH_PASSWORD", "")
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
db = Kozmograph(url=url, username=username, password=password)
toolkit = KozmographToolkit(db=db, llm=llm)
agent_executor = create_react_agent(
llm, toolkit.get_tools(), prompt="You will get a cypher query, try to execute it on the Kozmograph database."
)
example_query = "MATCH (n) WHERE n.name = 'Jon Snow' RETURN n"
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
last_event = None
for event in events:
last_event = event
event["messages"][-1].pretty_print()
print(last_event)
🧪 Test
Install the test dependencies to run the tests:
- Install dependencies
poetry install --with test,test_integration
-
Start Kozmograph in the background.
-
Create an
.envfile that points to Kozmograph and OpenAI API
KOZMOGRAPH_URI=bolt://localhost:7687
KOZMOGRAPH_USERNAME=
KOZMOGRAPH_PASSWORD=
OPENAI_API_KEY=your_openai_api_key
Run tests
Run the unit tests using:
make tests
Run the integration test using:
make integration_tests
🧹 Code Formatting and Linting
Install the codespell, lint, and typing dependencies to lint and format your code:
poetry install --with codespell,lint,typing
To format your code, run:
make format
To lint it, run:
make lint
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_kozmograph-0.1.1.tar.gz.
File metadata
- Download URL: langchain_kozmograph-0.1.1.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2febeb3d557c7ef3a6af60b825f6a2e9b6d04b3f6450fdd144d5164edcb465e
|
|
| MD5 |
66c3937a6e8b3de05c0f40452e80a8b7
|
|
| BLAKE2b-256 |
a996adbe242b22d9d806915412d1348b7703b8f000f2e8e3dc5ae313dbe83a97
|
File details
Details for the file langchain_kozmograph-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langchain_kozmograph-0.1.1-py3-none-any.whl
- Upload date:
- Size: 20.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d5e656d3061e1cde65a76f468abdcf2977672e08ec28dc5dcd802e816f9913e
|
|
| MD5 |
9ae3c8d27aea2678772d57aaa08fbe8c
|
|
| BLAKE2b-256 |
a3f46d9d4b5b44668097a9a93ee6e6380cd0ec87a82c5a2aced94634d988d4f4
|