AI Personal Learning
and practical guidance

LangGraph: a framework for AI Agent construction and execution based on directed acyclic graph topology

Artificial Intelligence (AI) is a rapidly growing field. Language models have evolved to enable AI Agents to perform complex tasks and make complex decisions. However, as the skills of these Agents continue to grow, the infrastructure to support them struggles to keep up.

LangGraph, a revolutionary library designed to revolutionize the AI Agent's Build and Runtime Implementation.


LangGraph presents a new AgentBuild and RuntimeConstructive paradigm, which views Agent workflows as structures based on a cyclic graph topology.

 

What is LangGraph?

An important value proposition of LangChain is the ability to easily create custom chains. However, LangChain lacks an easy way to introduce loops in these chains. In fact, these chains are directed acyclic graphs (DAG) - Like most data orchestration frameworks.

A common pattern when people build complex LLM applications is to introduce loops in the runtime. These loops often draw on the LLM to reason about the next action in the loop. This is essentially the equivalent of running the LLM in a for loop. such systems are often referred to as Agents.

The simplest form is a loop with two steps:

  1. Call LLM to determine what action to take, or what response to give the user
  2. Perform the given operation and return to step 1.

These steps are repeated until the final response is generated. This is the loop that drives the AgentExecutor at the heart of LangChain. It's a very simple loop, but it's also the most critical because it puts almost all of the decision-making and reasoning power in the hands of the LLM.

Note that this process usually requires more control. For example, forcing the Agent to invoke a specific tool first, more control over how the tool is invoked, different prompts depending on the state the Agent is in, etc.

These are controlled processes that LangChain calls "state machines."

These state machines have the ability to loop, however, some human intervention is required in the construction of that loop.

LangGraph creates these state machines by specifying them as graphs.

LangGraph is built on top of LangChain and is fully compatible with the LangChain ecosystem.interoperability. It adds new value mainly by introducing an easy way to create loop diagrams. This is very useful in creating Agent runtimes.

This approach allows Agents to exhibit more variable and nuanced behaviors than the linear execution models of their predecessors. Using graph theory, LangGraph provides a new way to develop complex networked Agent systems.

Figure: a LangGraph for code generation

An example: dynamic question and answer system

take : The user may be providing information to the model step by step, rather than sending all the information at once.

prescription : Use a cyclic diagram:

  1. Let the LLM generate initial responses.
  2. If the answer is incomplete or more information is needed, generate a new question and continue to ask the user.
  3. until the user confirms that the problem has been resolved.

Structure of the cycle diagram

  • Nodes: Each node represents a specific task or operation, such as text generation, question answering, data processing, etc.
  • Edges: Connections between nodes indicate the order of task execution, possibly with conditional judgments.
  • Looping Path: Loops are formed when the output of a node needs to be fed back to the previous node, or when it needs to be executed repeatedly.
  • Stopping Condition: The loop diagram will set some kind of termination condition, e.g. the quality of the result meets the requirements, a set number of times limit is reached, or an externally triggered stop signal.

 

Why use LangGraph?

  • dexterity : As AI Agents evolve, developers need more control over the Agent runtime to personalize action plans and decision-making procedures.
  • The circular nature of AI reasoning : Many complex LLM applications rely on cyclic execution when using strategies such as chained reasoning.LangGraph provides a natural framework for modeling these cyclic processes.
  • multi-intelligence system : As multi-intelligence workflows become more common, the need for systems that can efficiently manage and coordinate multiple autonomous intelligences increases.

 

Use the code to see how LangGraph works

LangGraph's functionality is based on a few basic elements:

  • Nodes. These are tools for functions or Agents.
  • Edges. Define paths for execution and data flow in the Agent system, connecting nodes.
  • StateGraph. LangGraph allows persistent data to be maintained between execution cycles by managing and updating state objects, with data flowing between nodes.

Figure : Basic workflow of LangGraph

StateGraph

StateGraph is a class that represents a graph. You create a graph by passing in the state definition to initialize this class. The state definition represents a central state object that is updated over time. This state is updated by nodes in the graph, which return operations on the attributes of this state in the form of key-value stores.

import os # Import the operating system module
os.environ['OpenAI_API_KEY'] = 'hk-iwtbi1e427'
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 are of type "list". "add_messages" in Annotated defines how to update the messages.
    # In this example, messages are appended to the list, not overwritten.
    messages: Annotated[list, add_messages]
# Create an instance of StateGraph, passing in the State class
graph_builder = StateGraph(State)

Nodes

Create a StateGraph After that, you can use the graph.add_node(name, value) Syntax to add nodes. name The argument is a string that is the node name. value The argument is a function or a runnable LCEL that will be called.

This function /LCEL accepts a function that is the same as state object has a dictionary of the same form as input and outputs a dictionary that uses the state object's key to update the dictionary.

from langchain_openai import ChatOpenAI # Import ChatOpenAI from langchain_openai module
llm = ChatOpenAI( # Create instance of ChatOpenAI
    model="gpt-4o-mini", # Specify model
    temperature=0, # Set temperature
    base_url="https://api.openai-hk.com/v1" # Set the base URL
)
def chatbot(state: State):# Define chatbot function that accepts arguments of type State
    """
    Processes a chatbot request and returns a message.
    Parameters.
    state (State): contains the state of the message.
    Returns.
    dict: Dictionary of messages generated by the bot.
    """
    return {"messages": [llm.invoke(state["messages"])]} # Returns the dictionary containing the messages
# The first argument is the node name
# The second argument is the function or object being called
graph_builder.add_node("chatbot", chatbot) # Add a node to the graph with the name "chatbot"

Edges

Indicates that one of the nodes is always called after the other one

# Add an edge from START to "chatbot".
graph_builder.add_edge(START, "chatbot")
# Add an edge from "chatbot" to END.
graph_builder.add_edge("chatbot", END)  

Compile Compile

After defining our graph, we can compile it to be runnable! Expose the same methods as LangChain ( .invoke (math.) genus .stream (math.) genus .astream_log etc.), allowing it to be called in the same way as a chain.

graph = graph_builder.compile() # Compile the graph

(of a computer) run

def stream_graph_updates(user_input: str): # Defines the stream_graph_updates function to accept user input
    """
    Use user_input to stream graph update messages.
    Parameters.
    user_input (str): user input message.
    Returns.
    None
    """
    # Iterate through the graph's stream events, passing in user messages to get the model's response
    for event in graph.stream({"messages": [("user", user_input)]}): # Traverse the graph's stream events
        for value in event.values(): # Traverse through the values of the event
            print("Assistant:", value["messages"][-1].content) # Print the last message from the assistant
whileTrue.
    Try.
        user_input = input("User: ") # get user input
        if user_input.lower() in ["quit", "exit", "q"]: # Check if the user wants to quit.
            print("Goodbye!")
            break# Exit the loop
        # Call stream_graph_updates function to process user input
        stream_graph_updates(user_input)
    except: # catch exception
        # fallback if input() is not available
        user_input = "What do you know about LangGraph? "# set default user input
        print("User: " + user_input) # print user input
        stream_graph_updates(user_input) # Call the stream_graph_updates function to process the default input
        break# Exit the loop

Visualization "diagram" (optional)

Visualizing "diagrams" in the Jupyter Notebook environment

operational effect

Frameworks like LangGraph are becoming more and more important with the development of artificial intelligence.

As developers become more familiar with LangGraph features, we can expect to see more advanced AI Agents that are capable of performing more complex tasks.

In summary, LangGraph is a significant advancement in AI Agent development. It enables developers to push the limits of what is possible with AI Agents by eliminating the shortcomings of earlier systems and providing a flexible, graph-based framework for Agent construction and execution.LangGraph has the potential to significantly influence the direction of AI development in the future.

May not be reproduced without permission:Chief AI Sharing Circle " LangGraph: a framework for AI Agent construction and execution based on directed acyclic graph topology

Chief AI Sharing Circle

Chief AI Sharing Circle specializes in AI learning, providing comprehensive AI learning content, AI tools and hands-on guidance. Our goal is to help users master AI technology and explore the unlimited potential of AI together through high-quality content and practical experience sharing. Whether you are an AI beginner or a senior expert, this is the ideal place for you to gain knowledge, improve your skills and realize innovation.

Contact Us
en_USEnglish