Skip to main content

An integration package connecting Kùzu, an embedded graph database, and LangChain

Project description

🦜️🔗 LangChain Kùzu

This package contains the LangChain integration with Kùzu, an embeddable property graph database.

📦 Installation

pip install -U langchain-kuzu

Usage

Kùzu's integration with LangChain makes it convenient to create and update graphs from unstructured text, and also to query graphs via a Text2Cypher pipeline that utilizes the power of LangChain's LLM chains. In this section, we'll cover an example of how to use the KuzuGraph and KuzuQAChain classes to create a graph, add nodes and relationships, and query the graph in conjunction with OpenAI's LLMs.

Let's install some additional dependencies:

pip install -U langchain-openai langchain-experimental

Creating a graph

text = "Tim Cook is the CEO of Apple. Apple has its headquarters in California."

First define the LLM to use for graph extraction, and the schema for the graph.

# Define schema
allowed_nodes = ["Person", "Company", "Location"]
allowed_relationships = [
    ("Person", "IS_CEO_OF", "Company"),
    ("Company", "HAS_HEADQUARTERS_IN", "Location"),
]

The LLMGraphTransformer class is used to convert the text into a graph document.

from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_openai import ChatOpenAI

# Define the LLMGraphTransformer
llm_transformer = LLMGraphTransformer(
    llm=ChatOpenAI(model="gpt-4o-mini", temperature=0, api_key=OPENAI_API_KEY),
    allowed_nodes=allowed_nodes,
    allowed_relationships=allowed_relationships,
)
from langchain_core.documents import Document

# Convert the given text into graph documents
documents = [Document(page_content=text)]
graph_documents = llm_transformer.convert_to_graph_documents(documents)

To ingest the graph documents directly into a Kùzu database, we can use the KuzuGraph class.

import kuzu
from langchain_kuzu.graphs.kuzu_graph import KuzuGraph

db = kuzu.Database("test_db")

# Create a graph object
graph = KuzuGraph(db, allow_dangerous_requests=True)

# Add the graph document to the graph
graph.add_graph_documents(
    graph_documents,
    include_source=True,
)

Query the graph

To query the graph, we can define a KuzuQAChain object. Then, we can invoke the chain with a query by connecting to the existing database that's stored in the test_db directory as per the previous step.

import kuzu
from langchain_kuzu.graphs.kuzu_graph import KuzuGraph
from langchain_kuzu.chains.graph_qa.kuzu import KuzuQAChain

db = kuzu.Database("test_db")
graph = KuzuGraph(db, allow_dangerous_requests=True)

# Create the KuzuQAChain with verbosity enabled to see the generated Cypher queries
chain = KuzuQAChain.from_llm(
    llm=ChatOpenAI(model="gpt-4o-mini", temperature=0.3, api_key=OPENAI_API_KEY),
    graph=graph,
    verbose=True,
    allow_dangerous_requests=True,
)

# Query the graph
queries = [
    "Who is the CEO of Apple?",
    "Where is Apple headquartered?",
]

for query in queries:
    result = chain.invoke(query)
    print(f"Query: {query}\nResult: {result}\n")

The following is the output:

> Entering new KuzuQAChain chain...
Generated Cypher:
MATCH (p:Person)-[:IS_CEO_OF]->(c:Company) WHERE c.id = 'Apple' RETURN p.id
Full Context:
[{'p.id': 'Tim Cook'}]

> Finished chain.
Query: Who is the CEO of Apple?
Result: {'query': 'Who is the CEO of Apple?', 'result': 'Tim Cook is the CEO of Apple.'}



> Entering new KuzuQAChain chain...
Generated Cypher:
MATCH (c:Company {id: "Apple"})-[:HAS_HEADQUARTERS_IN]->(l:Location) RETURN l.id
Full Context:
[{'l.id': 'California'}]

> Finished chain.
Query: Where is Apple headquartered?
Result: {'query': 'Where is Apple headquartered?', 'result': 'Apple is headquartered in California.'}

Updating the graph

You can update or mutate the graph's state by connecting to the existing database and running your own Cypher queries.

import kuzu

db = kuzu.Database("test_db")
conn = kuzu.Connection(db)

# Create a new relationship table
conn.execute("CREATE REL TABLE IS_COO_OF(FROM Person TO Company)")

# Add a new person-company relationship for Jeff Williams, the COO of Apple
conn.execute("CREATE (p:Person {id: 'Jeff Williams'})")
conn.execute(
    """
    MATCH (c:Company {id: 'Apple'}), (p:Person {id: 'Jeff Williams'})
    CREATE (p)-[:IS_COO_OF]->(c)
    """
)

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_kuzu-0.4.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

langchain_kuzu-0.4.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file langchain_kuzu-0.4.2.tar.gz.

File metadata

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

File hashes

Hashes for langchain_kuzu-0.4.2.tar.gz
Algorithm Hash digest
SHA256 aad5785f8c132195077c616dea76573de8f0b0d4aa84244b07fddc3e7495a5b5
MD5 7078f6a3c61042f6104858e472c81792
BLAKE2b-256 d36cdfba0ca47ca43cd27a27df0aaf277e2d8ff6fb173c3d4d9228bb9c53fd37

See more details on using hashes here.

File details

Details for the file langchain_kuzu-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: langchain_kuzu-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_kuzu-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dcc23bc5b5d98132d69d00a48e2d98f6762e7ccd419e3b5ea0c0d1b502b63946
MD5 6fab8024b2fce141053c10810b78d0e4
BLAKE2b-256 a2d24553743b0531f3163ca5abdc82c092284d6beda4cfc0f99aea3f2c5c29cb

See more details on using hashes here.

Supported by

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