The new langchain-kuzu integration package is now available on PyPI! This package unites the powerful capabilities of LangChain with Kùzu's cutting-edge graph database, enabling seamless transformation of unstructured text into structured graphs. Whether you're a data scientist, developer, or ai enthusiast, this integration simplifies complex tasks like entity extraction, graph creation, and natural language queries. Let's explore what makes this package a game-changer for your data workflows.
Learning objectives
- Understand the capabilities of the LangChain-Kùzu integration to transform unstructured text into structured graph databases.
- Learn how to define graph schemas, including nodes and relationships, tailored to your specific data needs.
- Master the process of creating, updating, and querying charts using Kùzu and LangChain's LLM-based tools.
- Explore natural language queries of graph databases with LangChain's GraphQACain to gain intuitive insights into your data.
- Discover advanced features such as dynamic schema updates, custom LLM pairing, and flexible data import options into Kùzu.
This article was published as part of the Data Science Blogathon.
Quick Kuzu Installation
To get started, simply install the package on Google Co.:
pip install -U langchain-kuzu langchain-openai langchain-experimental
This installation includes dependencies for LangChain and Kùzu, along with support for LLM such as OpenAI's GPT models. If you prefer other LLM providers, you can install their respective LangChain-compatible Python packages.
Why choose LangChain-Kùzu for your projects?
If you work with unstructured text data and want to create graph-based representations, this package is designed for you.
Key features include:
- Customizable schemes– Define and extract specific entities and relationships from text data effortlessly.
- Text to Graphic Transformation: Harness the power of LLMs to structure meaningful graphics from plain text.
- Query in natural language: Query graphs intuitively using natural language, powered by LangChain's GraphQACain.
- Seamless integration: Quickly connect LangChain's LLM capabilities with Kùzu for a unified workflow.
Let's look at a practical example to see this integration in action.
Create a chart from unstructured text
First create a Kùzu database on your local machine and connect to it:
import kuzu
db = kuzu.Database("test_db")
conn = kuzu.Connection(db)
Getting started with LangChain-Kùzu
Kùzu's integration with LangChain makes it convenient to create and update graphs from unstructured text, and also query graphs through a Text2Cypher pipeline that uses the power of LangChain's LLM chains. To start, we create a KuzuGraph object that uses the database object we created earlier in combination with the KuzuGraph constructor.
from langchain_kuzu.graphs.kuzu_graph import KuzuGraph
graph = KuzuGraph(db, allow_dangerous_requests=True)
Let's imagine that we want to transform the following text into a graphic:
- “Tim Cook is the CEO of Apple. “Apple is headquartered in California.”
text = "Tim Cook is the CEO of Apple. Apple has its headquarters in California."
Step 1: define the graphic scheme
First, define the types of entities (nodes) and relationships you want to include.
# Define schema
allowed_nodes = ("Person", "Company", "Location")
allowed_relationships = (
("Person", "IS_CEO_OF", "Company"),
("Company", "HAS_HEADQUARTERS_IN", "Location"),
)
Step 2: Transform text into graphic documents
Use the LLMGraphTransformer class to process text in structured graphic documents:
from langchain_core.documents import 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'), # noqa: F821
allowed_nodes=allowed_nodes,
allowed_relationships=allowed_relationships,
)
documents = (Document(page_content=text))
graph_documents = llm_transformer.convert_to_graph_documents(documents)
Step 3: Add graphic documents to Kùzu
Upload graphic documents to Kùzu for further use:
from langchain_kuzu.graphs.kuzu_graph import KuzuGraph
graph = KuzuGraph(db)
graph.add_graph_documents(graph_documents, include_source=True, allow_dangerous_requests= True)
graph_documents(:2)
Note: In the KuzuGraph method, set the 'allow_dangerous_requests' parameter to True if you get an error.
Production:
(GraphDocument(nodes=(Node(id='Tim Cook', type="Person", properties={}),
Node(id='Apple', type="Company", properties={}), Node(id='California', \
type="Location", properties={})), relationships=(Relationship(source=Node(id='Tim
Cook', type="Person", properties={}), target=Node(id='Apple', type="Company",
properties={}), type="IS_CEO_OF", properties={}),
Relationship(source=Node(id='Apple', type="Company", properties={}),
target=Node(id='California', type="Location", properties={}),
type="HAS_HEADQUARTERS_IN", properties={})), source=Document(metadata={},
page_content="Tim Cook is the CEO of Apple. Apple has its headquarters in
California.")))
Consulting the graph
With the KuzuQAChainyou can consult the graph using natural language:
# Add the graph document to the graph
graph.add_graph_documents(
graph_documents,
include_source=True,
)
from langchain_kuzu.chains.graph_qa.kuzu import KuzuQAChain
# 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'), # noqa: F821
graph=graph,
verbose=True,
allow_dangerous_requests=True,
)
chain.invoke("Where is Apple headquartered?")
Production:
> Entering new KuzuQAChain chain...
Generated Cypher:
MATCH (c:Company {id: 'Apple'})-(:HAS_HEADQUARTERS_IN)->(l:Location) RETURN l
Full Context:
({'l': {'_id': {'offset': 0, 'table': 1}, '_label': 'Location', 'id': 'California', 'type': 'entity'}})
> Finished chain.
{'query': 'Where is Apple headquartered?',
'result': 'Apple is headquartered in California.'}
Unlocking advanced features
The LangChain-Kùzu integration offers several advanced features to improve your workflows:
- Dynamic schema updates– Automatically updates schematics when the chart is updated.
- Custom LLM Matching: Use separate LLMs for Cypher generation and response generation to optimize performance.
- Complete graphic inspection– Easily inspect nodes, relationships, and schemas with intuitive commands.
Kùzu is a high-performance, embeddable graph database built for modern applications. Highlights include:
- Encrypted query support: Declaratively query property graphs using Cypher.
- Integrated architecture: runs in process without the need to configure the server.
- Flexible data import– Handles data from various formats such as CSV, JSON, and relational databases.
Explore more in the kùzu documentation.
Getting started with LangChain-Kùzu
To start your journey:
- Install the package: Start with pip install langchain-kuzu.
- Define your graphic scheme: adapt it to your specific needs.
- Leverage LLMs: Use LangChain tools to effortlessly create and query charts.
Visit the PyPI page for more detailed examples and updates. Don't forget to highlight our repository on GitHub and share your feedback – your contributions drive our progress!
Conclusion
The langchain-kuzu integration redefines the way you interact with unstructured data. Whether transforming text into structured graphs or querying those graphs with natural language, this package opens up powerful possibilities for gaining insights from ai-powered data. Try it today and discover a more intuitive way to work with graph data!
Key takeaways
- The LangChain-Kùzu integration simplifies the transformation of unstructured text into structured graph databases effortlessly.
- Define customizable graphical schemas to extract meaningful entities and relationships tailored to your data.
- Leverage LangChain LLMs for intuitive natural language queries and text-to-graph conversion.
- Kùzu offers dynamic schema updates, seamless integration, and support for Cypher queries for improved workflows.
- This integration allows ai enthusiasts, developers, and data scientists to unlock valuable insights from graph data.
Frequently asked questions
A. Simply run the command pip install langchain-kuzu. Make sure you have Python 3.7 or later installed on your system.
A. The package supports OpenAI GPT models and can be extended to other LangChain-compatible LLM providers.
A. Yes, you can define your own schema by specifying the nodes and relationships you want to extract from the text.
A. The schema is automatically updated when you invoke the chain. However, you can manually call the refresh_schema() method on the KuzuGraph object.
A. Absolutely! You can configure separate LLMs for these tasks by specifying the cypher_llm and qa_llm parameters in the KuzuQAChain object.
A. Kùzu supports data from CSV, JSON, and relational databases, making it very versatile.
The media shown in this article is not the property of Analytics Vidhya and is used at the author's discretion.