logologo
  • AI Interviewer
  • Features
  • AI Tools
  • FAQs
  • Jobs
logologo

Transform your hiring process with AI-powered interviews. Screen candidates faster and make better hiring decisions.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • AI Pre-Screening

Procodebase © 2025. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

LangGraph Example with Javascript: Simple Chatbot with Memory

author
Generated by
ProCodebase AI

11/12/2024

langchain

Sign in to read full article

Introduction to LangChain LangGraph

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.

Why Use LangGraph?

  1. Flexibility: Easily adapt your AI workflows to changing conditions.
  2. Statefulness: Maintain context across multiple interactions.
  3. Modularity: Build complex systems from simple, reusable components.

Getting Started with LangGraph in JavaScript

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.

A Simple Chatbot with Memory

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.

Breaking Down the Code

  1. We import necessary modules and initialize our language model.
  2. We create a StateGraph with channels for history and human input.
  3. We add a "chat" node that processes input and generates responses.
  4. We set up the graph structure, allowing for continuous conversation or ending.
  5. We compile the graph into an executable application.
  6. Finally, we demonstrate usage with two interactions.

Advanced Features: Conditional Flows

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:

  1. Query Classification: We use a classifier node to determine the type of query.
  2. Conditional Routing: Based on the classification, we route to different nodes.
  3. Specialized Handling: Different types of queries are processed by specialized nodes.

Best Practices for LangGraph

  1. Keep Nodes Focused: Each node should have a single, clear responsibility.
  2. Use Channels Wisely: Leverage channels to pass important state information between nodes.
  3. Error Handling: Implement robust error handling within nodes to prevent graph execution failures.
  4. Testing: Create unit tests for individual nodes and integration tests for the entire graph.

Conclusion

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.

Popular Tags

langchainlanggraphjavascript

Share now!

Like & Bookmark!

Related Collections

  • Mastering Vector Databases and Embeddings for AI-Powered Apps

    08/11/2024 | Generative AI

  • ChromaDB Mastery: Building AI-Driven Applications

    12/01/2025 | Generative AI

  • CrewAI Multi-Agent Platform

    27/11/2024 | Generative AI

  • Generative AI: Unlocking Creative Potential

    31/08/2024 | Generative AI

  • Intelligent AI Agents Development

    25/11/2024 | Generative AI

Related Articles

  • Implementing Natural Language Processing in Multi-Agent Systems

    12/01/2025 | Generative AI

  • Building a Semantic Search Engine Using Vector Databases

    08/11/2024 | Generative AI

  • Voice Synthesis Fundamentals

    06/10/2024 | Generative AI

  • Retrieval-Augmented Generation (RAG)

    03/12/2024 | Generative AI

  • Demystifying Large Language Model Internals

    06/10/2024 | Generative AI

  • Unleashing the Power of Retrieval Augmented Generation in AI Agents

    25/11/2024 | Generative AI

  • Exploring Different Types of Vector Databases and Their Use Cases in Generative AI

    08/11/2024 | Generative AI

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design