Quick Start: Using LangChain and LangGraph for Multi-Agent Construction

  • • Hello everyone, I am student Xiao Zhang, sharing AI knowledge and practical cases daily.

  • • Welcome to like + follow 👏, continuous learning, and continuous output of valuable content.

  • • +v: jasper_8017 let’s communicate 💬 and progress together 💪.

Article Overview in the Official Account

Quick Start: Using LangChain and LangGraph for Multi-Agent Construction

So far, I have systematically studied two intelligent agent frameworks: AutoGPT and MetaGPT. Today, let’s take a look at another multi-agent framework: LangGraph.

0. Introduction

LangGraph is a multi-agent execution framework built on top of LangChain. It extends the LangChain expression language to coordinate multiple chains (or participants) in a cyclic manner across multiple computation steps.

To elaborate further: LangChain and its expression language (LCEL) provide technical support for developers to build custom chains. From a graph perspective, these chains are directed acyclic graphs (DAGs). However, in practical applications, users expect to construct cyclic graphs. This is the purpose of LangGraph: to help users better and more conveniently construct cyclic graphs.

One of the core concepts of LangGraph is the state. Each execution of the graph creates a state that is passed between the nodes in the graph, and each node updates this state after execution. Thus, LangGraph presents a mechanism similar to a state machine.

1. Quick Start

1.1 Installing LangGraph

pip install langgraph

1.2 Experience It – Quick Demo

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.graph import END, MessageGraph

model = ChatOpenAI(temperature=0)

graph = MessageGraph()

graph.add_node("oracle", model)
graph.add_edge("oracle", END)

graph.set_entry_point("oracle")

runnable = graph.compile()

response = runnable.invoke(HumanMessage("What is 1 + 1?"))

print(response)

Output:

Quick Start: Using LangChain and LangGraph for Multi-Agent Construction

1.3 Demo Code Explanation

1.3.1 Analyzing the Steps to Use LangGraph

After a brief experience, let’s look at its basic usage.

(1) First, create a graph: graph = MessageGraph()

(2) Then, add nodes to the graph: graph.add_node("oracle", model), the name of this node is “oracle“, and the content of the node is model, which is the interface encapsulation of the OpenAI large model.

(3) Next, add edges: graph.add_edge("oracle", END), adding an edge from “oracle” to END. END is a built-in node indicating the end.

(4) Then, set the entry point: graph.set_entry_point("oracle")

(5) Compile the graph: runnable = graph.compile(), the compile function solidifies the graph, and it cannot be changed anymore.

(6) Run: the familiar invoke function.

In summary, the steps are: create a graph —> add nodes —> add edges —> set the starting node —> compile the graph —> run.

The steps are clear, and the process of generating the graph is simple, focusing on the basic elements of the graph: nodes and edges.

1.3.2 Observing the State of LangGraph from the Results

Looking at the running result (as shown above), it includes HumanMessage and AIMessage. This is what is called state transfer. The input is a HumanMessage, written into the state and passed to the oracle node, then executed, and the oracle node returns a result. This result updates the state and is passed to END, which outputs the final state.

Thus, the state of LangGraph is a list of a series of Messages.

1.4 Node Forms

In the above example, we added a model as a node: graph.add_node("oracle", model)

It can also be a function:

def call_oracle(messages: list):
    return model.invoke(message)

graph.add_node("oracle", call_oracle)

1.4.1 Can a Node Be a Chain?

Consider a question: can a Chain be added as a node in LangGraph? For instance, I have the following Chain:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant named {name} who always speaks in pirate dialect"),
    MessagesPlaceholder(variable_name="messages"),
])

chain = prompt | model

Can I add this chain to the graph using graph.add_node("oracle", chain)? My understanding is: a node cannot add a Chain because the input received by a Chain is a Dict type, while the state passed between nodes is a List, not a Dict.

2. References

  • • https://python.langchain.com/docs/langgraph

  • • https://mp.weixin.qq.com/s/R4tvoOY3AFNHypvVoOKMsQ

If you find this article helpful, please give it a like and follow ~~~

  • • Hello everyone, I am student Xiao Zhang, sharing AI knowledge and practical cases daily.

  • • Welcome to like + follow 👏, continuous learning, and continuous output of valuable content.

  • • +v: jasper_8017 let’s communicate 💬 and progress together 💪.

Article Overview in the Official Account

Quick Start: Using LangChain and LangGraph for Multi-Agent Construction

Leave a Comment