This project has been archived by its maintainers, and is no longer receiving any updates.
🦜️🔗 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)
"""
)
Metadata
Release files for langchain-kuzu 0.4.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| langchain_kuzu-0.4.2.tar.gz | 13.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_kuzu-0.4.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.6 kB
Release files / langchain_kuzu-0.4.2.tar.gz
| Download URL | langchain_kuzu-0.4.2.tar.gz |
|---|---|
| Size | 13.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aad5785f8c132195077c616dea76573de8f0b0d4aa84244b07fddc3e7495a5b5
|
|
BLAKE2b-256 checksum How to use checksums |
d36cdfba0ca47ca43cd27a27df0aaf277e2d8ff6fb173c3d4d9228bb9c53fd37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / langchain_kuzu-0.4.2-py3-none-any.whl
| Download URL | langchain_kuzu-0.4.2-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dcc23bc5b5d98132d69d00a48e2d98f6762e7ccd419e3b5ea0c0d1b502b63946
|
|
BLAKE2b-256 checksum How to use checksums |
a2d24553743b0531f3163ca5abdc82c092284d6beda4cfc0f99aea3f2c5c29cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for langchain-kuzu 0.4.2
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.