The brand new langchain-kuzu integration package deal is now accessible on PyPI! This package deal bridges the highly effective capabilities of LangChain with Kùzu’s cutting-edge graph database, enabling seamless transformation of unstructured textual content into structured graphs. Whether or not you’re an information scientist, developer, or AI fanatic, this integration simplifies advanced duties like entity extraction, graph creation, and pure language querying. Let’s discover what makes this package deal a game-changer to your knowledge workflows.
Studying Goals
- Perceive the capabilities of the LangChain-Kùzu integration for reworking unstructured textual content into structured graph databases.
- Learn to outline graph schemas, together with nodes and relationships, tailor-made to your particular knowledge wants.
- Grasp the method of making, updating, and querying graphs utilizing Kùzu and LangChain’s LLM-driven instruments.
- Discover pure language querying of graph databases with LangChain’s GraphQAChain for intuitive knowledge insights.
- Uncover superior options like dynamic schema updates, customized LLM pairing, and versatile knowledge import choices in Kùzu.
This text was revealed as part of the Knowledge Science Blogathon.
Fast Set up of Kuzu
To get began, merely set up the package deal on Google Colab:
pip set up -U langchain-kuzu langchain-openai langchain-experimental
This set up contains dependencies for LangChain and Kùzu, together with help for LLMs like OpenAI’s GPT fashions. When you desire different LLM suppliers, you possibly can set up their respective Python packages supported by LangChain.
Why Select LangChain-Kùzu for Your Tasks?
When you work with unstructured textual content knowledge and wish to create graph-based representations, this package deal is designed for you.
Key options embrace:
- Customizable Schemas: Outline and extract particular entities and relationships from textual content knowledge effortlessly.
- Textual content-to-Graph Transformation: Leverage the facility of LLMs to construction significant graphs from uncooked textual content.
- Pure Language Querying: Question graphs intuitively utilizing pure language, powered by LangChain’s GraphQAChain.
- Seamless Integration: Rapidly join LangChain’s LLM capabilities with Kùzu for a unified workflow.
Let’s stroll by way of a sensible instance to see this integration in motion.
Making a Graph from Unstructured Textual content
First create a Kùzu database in your native machine and hook up with it:
import kuzu
db = kuzu.Database("test_db")
conn = kuzu.Connection(db)
Getting Began with LangChain-Kùzu
Kùzu’s integration with LangChain makes it handy to create and replace graphs from unstructured textual content, and in addition to question graphs by way of a Text2Cypher pipeline that makes use of the facility of LangChain’s LLM chains. To start, we create a KuzuGraph object that makes use of the database object we created above together with the KuzuGraph constructor.
from langchain_kuzu.graphs.kuzu_graph import KuzuGraph
graph = KuzuGraph(db, allow_dangerous_requests=True)
Think about we wish to rework the next textual content right into a graph:
- “Tim Cook dinner is the CEO of Apple. Apple has its headquarters in California.”
textual content = "Tim Cook dinner is the CEO of Apple. Apple has its headquarters in California."
Step1: Outline the Graph Schema
First, outline the forms of entities (nodes) and relationships you wish to embrace.
# Outline schema
allowed_nodes = ["Person", "Company", "Location"]
allowed_relationships = [
("Person", "IS_CEO_OF", "Company"),
("Company", "HAS_HEADQUARTERS_IN", "Location"),
]
Step2: Remodel Textual content into Graph Paperwork
Use the LLMGraphTransformer class to course of the textual content into structured graph paperwork:
from langchain_core.paperwork import Doc
from langchain_experimental.graph_transformers import LLMGraphTransformer
from langchain_openai import ChatOpenAI
# Outline the LLMGraphTransformer
llm_transformer = LLMGraphTransformer(
llm=ChatOpenAI(mannequin="gpt-4o-mini", temperature=0, api_key='OPENAI_API_KEY'), # noqa: F821
allowed_nodes=allowed_nodes,
allowed_relationships=allowed_relationships,
)
paperwork = [Document(page_content=text)]
graph_documents = llm_transformer.convert_to_graph_documents(paperwork)
Step3: Add Graph Paperwork to Kùzu
Load the graph paperwork into Kùzu for additional 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]
Word: In KuzuGraph technique, set ‘allow_dangerous_requests’ parameter to True when you get an error.
Output:
[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={})], supply=Doc(metadata={},
page_content="Tim Cook dinner is the CEO of Apple. Apple has its headquarters in
California."))]
Querying the Graph
With the KuzuQAChain, you possibly can question the graph utilizing pure language:
# Add the graph doc 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(mannequin="gpt-4o-mini", temperature=0.3, api_key='OPENAI_API_KEY'), # noqa: F821
graph=graph,
verbose=True,
allow_dangerous_requests=True,
)
chain.invoke("The place is Apple headquartered?")
Output:
> Getting into new KuzuQAChain chain...
Generated Cypher:
MATCH (c:Firm {id: 'Apple'})-[:HAS_HEADQUARTERS_IN]->(l:Location) RETURN l
Full Context:
[{'l': {'_id': {'offset': 0, 'table': 1}, '_label': 'Location', 'id': 'California', 'type': 'entity'}}]
> Completed chain.
{'question': 'The place is Apple headquartered?',
'consequence': 'Apple is headquartered in California.'}
Unlocking Superior Options
The LangChain-Kùzu integration presents a number of superior options to boost your workflows:
- Dynamic Schema Updates: Robotically refresh schemas when the graph is up to date.
- Customized LLM Pairing: Use separate LLMs for Cypher era and reply era to optimize efficiency.
- Complete Graph Inspection: Simply examine nodes, relationships, and schema with intuitive instructions.
Kùzu is a high-performance, embeddable graph database constructed for contemporary purposes. Key highlights embrace:
- Cypher Question Help: Declaratively question property graphs utilizing Cypher.
- Embedded Structure: Run in-process with out requiring server setup.
- Versatile Knowledge Import: Deal with knowledge from varied codecs like CSV, JSON, and relational databases.
Discover extra within the Kùzu documentation.
Getting Began with LangChain-Kùzu
To start your journey:
- Set up the Package deal : Begin with pip set up langchain-kuzu.
- Outline Your Graph Schema: Tailor it to your particular wants.
- Leverage LLMs: Use LangChain’s instruments to create and question graphs effortlessly.
Go to the PyPI web page for extra detailed examples and updates. Don’t overlook to star our repository on GitHub and share your suggestions—your enter drives our progress!
Conclusion
The langchain-kuzu integration redefines the way you work together with unstructured knowledge. Whether or not it’s reworking textual content into structured graphs or querying these graphs with pure language, this package deal unlocks highly effective potentialities for AI-driven knowledge insights. Attempt it in the present day and uncover a extra intuitive approach to work with graph knowledge!
Key Takeaways
- The LangChain-Kùzu integration simplifies reworking unstructured textual content into structured graph databases effortlessly.
- Outline customizable graph schemas to extract significant entities and relationships tailor-made to your knowledge.
- Leverage LangChain’s LLMs for intuitive pure language querying and text-to-graph conversion.
- Kùzu presents dynamic schema updates, seamless integration, and help for Cypher queries for enhanced workflows.
- This integration empowers AI lovers, builders, and knowledge scientists to unlock highly effective insights from graph knowledge.
Regularly Requested Questions
A. Merely run the command pip set up langchain-kuzu. Guarantee you have got Python 3.7 or later put in in your system.
A. The package deal helps OpenAI’s GPT fashions and will be prolonged to different LLM suppliers supported by LangChain.
A. Sure, you possibly can outline your individual schema by specifying the nodes and relationships you wish to extract from the textual content.
A. The schema refreshes mechanically once you invoke the chain. Nonetheless, you possibly can manually name the refresh_schema() technique on the KuzuGraph object.
A. Completely! You may configure separate LLMs for these duties by specifying cypher_llm and qa_llm parameters within the KuzuQAChain object.
A. Kùzu helps knowledge from CSV, JSON, and relational databases, making it extremely versatile.
The media proven on this article isn’t owned by Analytics Vidhya and is used on the Writer’s discretion.