Skip to main content

🦜️🔗 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:

  1. 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:

  1. Install dependencies
poetry install --with test,test_integration
  1. Start Kozmograph in the background.

  2. Create an .env file 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

Metadata

Release files for langchain-kozmograph 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-kozmograph 0.1.1
File Size Uploaded Provenance
langchain_kozmograph-0.1.1.tar.gz 16.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-kozmograph 0.1.1
File Interpreter ABI Platform
langchain_kozmograph-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.0 kB

Release files / langchain_kozmograph-0.1.1.tar.gz

Download URL langchain_kozmograph-0.1.1.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b2febeb3d557c7ef3a6af60b825f6a2e9b6d04b3f6450fdd144d5164edcb465e
BLAKE2b-256 checksum
How to use checksums
a996adbe242b22d9d806915412d1348b7703b8f000f2e8e3dc5ae313dbe83a97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / langchain_kozmograph-0.1.1-py3-none-any.whl

Download URL langchain_kozmograph-0.1.1-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6d5e656d3061e1cde65a76f468abdcf2977672e08ec28dc5dcd802e816f9913e
BLAKE2b-256 checksum
How to use checksums
a3f46d9d4b5b44668097a9a93ee6e6380cd0ec87a82c5a2aced94634d988d4f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for langchain-kozmograph 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page