5.2 C
New York
Friday, January 17, 2025

LangChain-Kùzu Integration


The brand new langchain-kuzu integration bundle is now obtainable on PyPI! This bundle 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 a knowledge 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 bundle a game-changer in your knowledge workflows.

Studying Targets

  • Perceive the capabilities of the LangChain-Kùzu integration for reworking unstructured textual content into structured graph databases.
  • Discover ways 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 printed as part of the Information Science Blogathon.

Fast Set up of Kuzu

To get began, merely set up the bundle 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 assist for LLMs like OpenAI’s GPT fashions. In case you choose different LLM suppliers, you may set up their respective Python packages supported by LangChain.

Why Select LangChain-Kùzu for Your Initiatives?

In case you work with unstructured textual content knowledge and wish to create graph-based representations, this bundle 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 likewise 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 remodel the next textual content right into a graph:

  • “Tim Cook dinner is the CEO of Apple. Apple has its headquarters in California.”
text to graph: LangChain-Kùzu Integration
textual content = "Tim Cook dinner is the CEO of Apple. Apple has its headquarters in California."

Step1: Outline the Graph Schema

First, outline the kinds 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: Rework 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]

Be aware: In KuzuGraph methodology, set ‘allow_dangerous_requests’ parameter to True should 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 may 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?',
 'end result': '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: Routinely 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 Assist: Declaratively question property graphs utilizing Cypher.
  • Embedded Structure: Run in-process with out requiring server setup.
  • Versatile Information Import: Deal with knowledge from numerous 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 Bundle : 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 neglect 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 bundle unlocks highly effective prospects for AI-driven knowledge insights. Strive it at this time and uncover a extra intuitive solution 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 assist 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

Q1. How do I set up the langchain-kuzu bundle?

A. Merely run the command pip set up langchain-kuzu. Guarantee you will have Python 3.7 or later put in in your system.

Q2. What LLMs are supported by the bundle?

A. The bundle helps OpenAI’s GPT fashions and will be prolonged to different LLM suppliers supported by LangChain.

Q3. Can I take advantage of a customized schema for my graph?

A. Sure, you may outline your personal schema by specifying the nodes and relationships you wish to extract from the textual content.

This autumn. What ought to I do if my graph schema doesn’t replace after including paperwork?

A. The schema refreshes robotically once you invoke the chain. Nonetheless, you may manually name the refresh_schema() methodology on the KuzuGraph object.

Q5. Is it doable to make use of completely different LLMs for Cypher era and reply era?

A. Completely! You’ll be able to configure separate LLMs for these duties by specifying cypher_llm and qa_llm parameters within the KuzuQAChain object.

Q6. What codecs are supported for importing knowledge into Kùzu?

A. Kùzu helps knowledge from CSV, JSON, and relational databases, making it extremely versatile.

The media proven on this article is just not owned by Analytics Vidhya and is used on the Creator’s discretion.

Hello! I am Adarsh, a Enterprise Analytics graduate from ISB, presently deep into analysis and exploring new frontiers. I am tremendous enthusiastic about knowledge science, AI, and all of the progressive methods they will remodel industries. Whether or not it is constructing fashions, engaged on knowledge pipelines, or diving into machine studying, I like experimenting with the most recent tech. AI is not simply my curiosity, it is the place I see the longer term heading, and I am at all times excited to be part of that journey!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles