LangChain LangGraph is a game-changing addition to the LangChain ecosystem, designed to empower developers in creating sophisticated, stateful AI workflows. It's particularly useful for applications that require complex decision-making processes or multi-step interactions.
First, let's set up our environment:
npm install langchain @langchain/community
Now, let's dive into a simple example to demonstrate LangGraph's capabilities.
We'll create a chatbot that remembers previous interactions:
import { ChatOpenAI } from "@langchain/openai"; import { HumanMessage, AIMessage } from "@langchain/core/messages"; import { StateGraph, END } from "langchain/graphs"; const model = new ChatOpenAI({ temperature: 0 }); const graph = new StateGraph({ channels: { history: [], human_input: "", }, }); graph.addNode("chat", async (data) => { const history = data.history; const human_input = data.human_input; const messages = [ ...history, new HumanMessage(human_input), ]; const response = await model.invoke(messages); return { response: response.content, history: [...history, new HumanMessage(human_input), new AIMessage(response.content)] }; }); graph.setEntryPoint("chat"); graph.addEdge("chat", "chat"); graph.addEdge("chat", END); const app = graph.compile(); // Usage const result1 = await app.invoke({ human_input: "Hello! My name is Alice." }); console.log(result1.response); const result2 = await app.invoke({ human_input: "What's my name?" }); console.log(result2.response);
In this example, we've created a simple stateful chatbot. The StateGraph
maintains the conversation history, allowing the bot to remember previous interactions.
StateGraph
with channels for history and human input.LangGraph really shines when dealing with complex, conditional workflows. Let's enhance our chatbot to handle different types of queries:
import { ChatOpenAI } from "@langchain/openai"; import { HumanMessage, AIMessage } from "@langchain/core/messages"; import { StateGraph, END } from "langchain/graphs"; import { StructuredOutputParser } from "langchain/output_parsers"; const model = new ChatOpenAI({ temperature: 0 }); const parser = StructuredOutputParser.fromNamesAndDescriptions({ queryType: "The type of query (general, weather, or exit)", response: "The response to the query", }); const graph = new StateGraph({ channels: { history: [], human_input: "", }, }); graph.addNode("classifier", async (data) => { const response = await model.invoke([ new HumanMessage(`Classify the following query into 'general', 'weather', or 'exit': "${data.human_input}"`), ]); const { queryType } = await parser.parse(response.content); return { queryType }; }); graph.addNode("generalChat", async (data) => { // ... (similar to previous chat node) }); graph.addNode("weatherChat", async (data) => { // Simulate weather API call return { response: `The weather is sunny today in ${data.human_input}.` }; }); graph.setEntryPoint("classifier"); graph.addEdge("classifier", "generalChat", (data) => data.queryType === "general"); graph.addEdge("classifier", "weatherChat", (data) => data.queryType === "weather"); graph.addEdge("classifier", END, (data) => data.queryType === "exit"); graph.addEdge("generalChat", "classifier"); graph.addEdge("weatherChat", "classifier"); const app = graph.compile(); // Usage await app.invoke({ human_input: "Hello, how are you?" }); await app.invoke({ human_input: "What's the weather like in New York?" }); await app.invoke({ human_input: "Goodbye!" });
This enhanced version demonstrates:
LangChain LangGraph offers a powerful way to create dynamic, stateful AI workflows in JavaScript. By breaking complex processes into manageable nodes and leveraging the graph structure, you can build highly flexible and maintainable AI applications.
25/11/2024 | Generative AI
28/09/2024 | Generative AI
06/10/2024 | Generative AI
03/12/2024 | Generative AI
27/11/2024 | Generative AI
06/10/2024 | Generative AI
25/11/2024 | Generative AI
06/10/2024 | Generative AI
06/10/2024 | Generative AI
28/09/2024 | Generative AI
03/12/2024 | Generative AI
06/10/2024 | Generative AI