Getting Started with LangGraph: Building a Basic Chatbot

LangGraph is not a new framework independent of LangChain, but rather an extension library built on top of LLM and LangChain, enabling seamless collaboration with existing chains in LangChain.

LangGraph can coordinate multiple Chains, Agents, Tools, etc., to collaboratively perform Q&A tasks that depend on external tools and databases with feedback.

We will first create a simple chatbot using LangGraph. This chatbot will directly respond to user messages. Although simple, it will illustrate the core concepts of building with LangGraph. By the end of this section, you will have built a basic chatbot.

First, create a <span>StateGraph</span>. The <span>StateGraph</span> object defines the structure of our chatbot as a “state machine”. We will add nodes to represent the llm and functions that the chatbot can call, and edges to specify how the bot transitions between these functions.

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages

class State(TypedDict):
    # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
    messages: Annotated[list, add_messages]

graph_builder = StateGraph(State)

Note

When defining the graph, the first thing you need to do is define the <span>State</span>. The <span>State</span> contains the schema of the graph and the reducer functions that specify how updates are applied to the state. In our example, <span>State</span> is a <span>TypedDict</span> with a single key <span>messages</span>. The <span>messages</span> key is annotated with the <span>add_messages</span> reducer function, which tells LangGraph to append new messages to the existing list rather than overwrite it. Unannotated state keys will be overwritten with each update, storing only the latest value. See this conceptual guide for more information about state, reducers, and other low-level concepts.

Now our graph knows two things:

  • Each node we define will receive the current <span>State</span> as input and return a value that updates that state.
  • <span>messages</span>
    will be appended to the current list rather than directly overwritten. This is conveyed through the pre-built <span>add_messages</span> function in the <span>Annotated</span> syntax.

Next, add a “chatbot” node. Nodes represent working units. They are typically regular Python functions.

import os

from langchain_community.chat_models import ChatZhipuAI


os.environ["ZHIPUAI_API_KEY"] = "your_zhipu_api"


llm = ChatZhipuAI(
    model="glm-4-flash",
    temperature=0.5,
)

def chatbot(state: State):
    return {"messages": [llm.invoke(state["messages"])]}

# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
graph_builder.add_node("chatbot", chatbot)

Note<span>chatbot</span> node function takes the current <span>State</span> as input and returns a dictionary containing an updated <span>messages</span> list (located under the key “messages”). This is the basic pattern for all LangGraph node functions.

The <span>add_messages</span> function in our <span>State</span> appends the response message from the llm to any existing messages in the state.

Next, add an entry point. This tells our graph where to start working each time it runs.

graph_builder.add_edge(START, "chatbot")

Similarly, set a finish point. This indicates that the graph can exit whenever this node is run.

graph_builder.add_edge("chatbot", END)

Finally, we will need to be able to run our graph. To do this, call “compile()” on the graph builder. This will create a “CompiledGraph” that we can call our state on.

graph = graph_builder.compile()

You can visualize the graph using the <span>get_graph</span> method and a “draw” method (like <span>draw_ascii</span> or <span>draw_png</span>). Each draw method requires additional dependencies.

from IPython.display import Image, display

try:
    display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
    # This requires some extra dependencies and is optional
    pass

Now let’s run the chatbot!

Tip: You can exit the chat loop anytime by typing “quit”, “exit”, or “q”.

def stream_graph_updates(user_input: str):
    for event in graph.stream({"messages": [("user", user_input)]}):
        for value in event.values():
            print("Assistant:", value["messages"][-1].content)


while True:
    try:
        user_input = input("User: ")
        if user_input.lower() in ["quit", "exit", "q"]:
            print("Goodbye!")
            break

        stream_graph_updates(user_input)
    except:
        # fallback if input() is not available
        user_input = "What do you know about LangGraph?"
        print("User: " + user_input)
        stream_graph_updates(user_input)
        break

Example Output:

User: 你好
Assistant: 你好👋!很高兴见到你,有什么可以帮助你的吗?
User: 介绍下你的背景
Assistant: Hello! I'm an AI assistant developed based on a large language model. My background is as follows:

1. **Development Team**: I was developed by a team of computer scientists and engineers with extensive experience in natural language processing and artificial intelligence.

2. **Technical Foundation**: My core technology is based on deep learning, particularly neural networks. I was trained on a large amount of text data to understand and generate natural language.

3. **Training Data**: My training data comes from various texts on the internet, including books, news, articles, social media posts, etc. This data covers a wide range of topics and fields.

4. **Functions**: I have various functions such as Q&A, text generation, machine translation, sentiment analysis, etc. I can help users solve problems, provide information, and engage in creative tasks.

5. **Goals**: My goal is to become an AI assistant for users, providing them with convenient and efficient services.

6. **Compliance Principles**: In providing services, I strictly adhere to relevant laws and regulations and ethical standards, respecting user privacy and protecting user information security.

7. **Continuous Improvement**: I will continue to learn and optimize to provide better services.

I hope the above information is helpful to you! If you have any questions, feel free to ask me anytime.
User: What do you know about LangGraph?
Assistant: LangGraph is a term that could refer to several different things depending on the context. It could be a specific software tool, a research project, a concept in computational linguistics, or a term used in a particular academic field. Here are a few possibilities:

1. **Software Tool**: LangGraph could refer to a software application designed for graph-based analysis of linguistic data. Such a tool might be used to visualize and analyze the relationships between words, concepts, or linguistic structures in a network graph format.

2. **Research Project**: In the field of computational linguistics or natural language processing, LangGraph might be the name of a research project focused on the creation and analysis of language graphs. These graphs could represent the structure of a language, the relationships between words, or the semantic networks of concepts.

3. **Concept in Computational Linguistics**: LangGraph could be a conceptual framework for understanding the structure of language as a graph. This would involve mapping linguistic elements (like words, phrases, or sentences) as nodes and the relationships between them as edges.

4. **Graph Theory in Language Studies**: In a more general sense, LangGraph might simply refer to the application of graph theory to the study of language, where the graph represents the structure of language data.

Without more specific information, it's difficult to provide a detailed description of what LangGraph refers to. If you have additional context or details about LangGraph, please provide them, and I can offer a more accurate explanation.

Congratulations! You have built your first chatbot using LangGraph. This bot can engage in basic conversation by accepting user input and generating responses using LLM.

Leave a Comment