LangGraph là một khung để xây dựng các ứng dụng LLM có trạng thái, là lựa chọn phù hợp để xây dựng các Tác nhân ReAct (Tư duy và Hành động).
Các tác nhân ReAct kết hợp quá trình suy luận LLM với việc thực thi hành động. Họ suy nghĩ, sử dụng các công cụ và hành động lặp lại dựa trên những quan sát để đạt được mục tiêu của người dùng, đồng thời linh hoạt điều chỉnh phương pháp của mình. Được giới thiệu trong "ReAct: Synergizing Reasoning and Acting in Language Models" (2023), mẫu này cố gắng phản ánh khả năng giải quyết vấn đề linh hoạt, giống như con người thay vì quy trình làm việc cứng nhắc.
Mặc dù LangGraph cung cấp một tác nhân ReAct tạo sẵn (create_react_agent
), nhưng tác nhân này sẽ phát huy tác dụng khi bạn cần kiểm soát và tuỳ chỉnh nhiều hơn cho các hoạt động triển khai ReAct.
LangGraph mô hình hoá các tác nhân dưới dạng biểu đồ bằng cách sử dụng 3 thành phần chính:
State
: Cấu trúc dữ liệu dùng chung (thường làTypedDict
hoặcPydantic BaseModel
) đại diện cho ảnh chụp nhanh hiện tại của ứng dụng.Nodes
: Mã hoá logic của các tác nhân. Các hàm này nhận Trạng thái hiện tại làm dữ liệu đầu vào, thực hiện một số phép tính hoặc hiệu ứng phụ và trả về một Trạng thái đã cập nhật, chẳng hạn như lệnh gọi LLM hoặc lệnh gọi công cụ.Edges
: Xác địnhNode
tiếp theo để thực thi dựa trênState
hiện tại, cho phép logic có điều kiện và chuyển đổi cố định.
Nếu chưa có Khoá API, bạn có thể lấy một khoá miễn phí tại Google AI Studio.
pip install langgraph langchain-google-genai geopy requests
Đặt khoá API trong biến môi trường GEMINI_API_KEY
.
import os
# Read your API key from the environment variable or set it manually
api_key = os.getenv("GEMINI_API_KEY")
Để hiểu rõ hơn về cách triển khai tác nhân ReAct bằng LangGraph, hãy xem xét một ví dụ thực tế. Bạn sẽ tạo một tác nhân đơn giản có mục tiêu là sử dụng một công cụ để tìm thông tin thời tiết hiện tại cho một vị trí cụ thể.
Đối với tác nhân thời tiết này, State
của tác nhân sẽ cần duy trì nhật ký cuộc trò chuyện đang diễn ra (dưới dạng danh sách tin nhắn) và bộ đếm cho số bước đã thực hiện để minh hoạ thêm về việc quản lý trạng thái.
LangGraph cung cấp một trình trợ giúp tiện lợi, add_messages
, để cập nhật danh sách tin nhắn trong trạng thái. Thành phần này hoạt động như một trình giảm, nghĩa là lấy danh sách hiện tại và thư mới, sau đó trả về một danh sách kết hợp. Phương thức này xử lý thông tin cập nhật theo mã thông báo một cách thông minh và mặc định là hành vi "chỉ thêm" cho các thông báo mới, riêng biệt.
from typing import Annotated,Sequence, TypedDict
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages # helper function to add messages to the state
class AgentState(TypedDict):
"""The state of the agent."""
messages: Annotated[Sequence[BaseMessage], add_messages]
number_of_steps: int
Tiếp theo, bạn xác định công cụ thời tiết.
from langchain_core.tools import tool
from geopy.geocoders import Nominatim
from pydantic import BaseModel, Field
import requests
geolocator = Nominatim(user_agent="weather-app")
class SearchInput(BaseModel):
location:str = Field(description="The city and state, e.g., San Francisco")
date:str = Field(description="the forecasting date for when to get the weather format (yyyy-mm-dd)")
@tool("get_weather_forecast", args_schema=SearchInput, return_direct=True)
def get_weather_forecast(location: str, date: str):
"""Retrieves the weather using Open-Meteo API for a given location (city) and a date (yyyy-mm-dd). Returns a list dictionary with the time and temperature for each hour."""
location = geolocator.geocode(location)
if location:
try:
response = requests.get(f"https://api.open-meteo.com/v1/forecast?latitude={location.latitude}&longitude={location.longitude}&hourly=temperature_2m&start_date={date}&end_date={date}")
data = response.json()
return {time: temp for time, temp in zip(data["hourly"]["time"], data["hourly"]["temperature_2m"])}
except Exception as e:
return {"error": str(e)}
else:
return {"error": "Location not found"}
tools = [get_weather_forecast]
Tiếp theo, bạn khởi chạy mô hình và liên kết các công cụ với mô hình.
from datetime import datetime
from langchain_google_genai import ChatGoogleGenerativeAI
# Create LLM class
llm = ChatGoogleGenerativeAI(
model= "gemini-2.5-pro-preview-05-06",
temperature=1.0,
max_retries=2,
google_api_key=api_key,
)
# Bind tools to the model
model = llm.bind_tools([get_weather_forecast])
# Test the model with tools
res=model.invoke(f"What is the weather in Berlin on {datetime.today()}?")
print(res)
Bước cuối cùng trước khi bạn có thể chạy tác nhân là xác định các nút và cạnh. Trong ví dụ này, bạn có hai nút và một cạnh.
– Nút call_tool
thực thi phương thức công cụ. LangGraph có một nút tạo sẵn cho việc này có tên là ToolNode.
– Nút call_model
sử dụng model_with_tools
để gọi mô hình.
– Cạnh should_continue
quyết định việc gọi công cụ hay mô hình.
Số lượng nút và cạnh không cố định. Bạn có thể thêm bao nhiêu nút và cạnh tùy thích vào biểu đồ. Ví dụ: bạn có thể thêm một nút để thêm đầu ra có cấu trúc hoặc nút tự xác minh/tự phản ánh để kiểm tra đầu ra của mô hình trước khi gọi công cụ hoặc mô hình.
from langchain_core.messages import ToolMessage
from langchain_core.runnables import RunnableConfig
tools_by_name = {tool.name: tool for tool in tools}
# Define our tool node
def call_tool(state: AgentState):
outputs = []
# Iterate over the tool calls in the last message
for tool_call in state["messages"][-1].tool_calls:
# Get the tool by name
tool_result = tools_by_name[tool_call["name"]].invoke(tool_call["args"])
outputs.append(
ToolMessage(
content=tool_result,
name=tool_call["name"],
tool_call_id=tool_call["id"],
)
)
return {"messages": outputs}
def call_model(
state: AgentState,
config: RunnableConfig,
):
# Invoke the model with the system prompt and the messages
response = model.invoke(state["messages"], config)
# We return a list, because this will get added to the existing messages state using the add_messages reducer
return {"messages": [response]}
# Define the conditional edge that determines whether to continue or not
def should_continue(state: AgentState):
messages = state["messages"]
# If the last message is not a tool call, then we finish
if not messages[-1].tool_calls:
return "end"
# default to continue
return "continue"
Giờ đây, bạn đã có tất cả các thành phần để tạo tác nhân. Hãy cùng kết hợp các thành phần này.
from langgraph.graph import StateGraph, END
# Define a new graph with our state
workflow = StateGraph(AgentState)
# 1. Add our nodes
workflow.add_node("llm", call_model)
workflow.add_node("tools", call_tool)
# 2. Set the entrypoint as `agent`, this is the first node called
workflow.set_entry_point("llm")
# 3. Add a conditional edge after the `llm` node is called.
workflow.add_conditional_edges(
# Edge is used after the `llm` node is called.
"llm",
# The function that will determine which node is called next.
should_continue,
# Mapping for where to go next, keys are strings from the function return, and the values are other nodes.
# END is a special node marking that the graph is finish.
{
# If `tools`, then we call the tool node.
"continue": "tools",
# Otherwise we finish.
"end": END,
},
)
# 4. Add a normal edge after `tools` is called, `llm` node is called next.
workflow.add_edge("tools", "llm")
# Now we can compile and visualize our graph
graph = workflow.compile()
Bạn có thể trực quan hoá biểu đồ bằng phương thức draw_mermaid_png
.
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))
Bây giờ, hãy chạy tác nhân.
from datetime import datetime
# Create our initial message dictionary
inputs = {"messages": [("user", f"What is the weather in Berlin on {datetime.today()}?")]}
# call our graph with streaming to see the steps
for state in graph.stream(inputs, stream_mode="values"):
last_message = state["messages"][-1]
last_message.pretty_print()
Giờ đây, bạn có thể tiếp tục cuộc trò chuyện, chẳng hạn như hỏi thông tin thời tiết ở một thành phố khác hoặc yêu cầu Trợ lý so sánh thông tin thời tiết.
state["messages"].append(("user", "Would it be in Munich warmer?"))
for state in graph.stream(state, stream_mode="values"):
last_message = state["messages"][-1]
last_message.pretty_print()