ReAct Agent 中的对话历史管理¶
消息历史记录会迅速增长并超出 LLM 的上下文窗口大小,无论您是构建具有多次对话轮次的聊天机器人还是具有多次工具调用的代理系统。有几种策略可以管理消息历史记录:
要在 create_react_agent
中管理消息历史记录,您需要定义一个 pre_model_hook
函数或 可运行对象,它接受图状态并返回状态更新:
-
修剪示例:
from langchain_core.messages.utils import ( trim_messages, count_tokens_approximately ) from langgraph.prebuilt import create_react_agent # 此函数将在每次调用 LLM 节点之前被调用 def pre_model_hook(state): trimmed_messages = trim_messages( state["messages"], strategy="last", token_counter=count_tokens_approximately, max_tokens=384, start_on="human", end_on=("human", "tool"), ) # 您可以在 `llm_input_messages` 或 # `messages` 键下返回更新后的消息(请参阅下面的注释) return {"llm_input_messages": trimmed_messages} checkpointer = InMemorySaver() agent = create_react_agent( model, tools, pre_model_hook=pre_model_hook, checkpointer=checkpointer, )
-
摘要示例:
from langmem.short_term import SummarizationNode from langchain_core.messages.utils import count_tokens_approximately from langgraph.prebuilt.chat_agent_executor import AgentState from langgraph.checkpoint.memory import InMemorySaver from typing import Any model = ChatOpenAI(model="gpt-4o") summarization_node = SummarizationNode( token_counter=count_tokens_approximately, model=model, max_tokens=384, max_summary_tokens=128, output_messages_key="llm_input_messages", ) class State(AgentState): # 注意:我们添加了这个键来跟踪之前的摘要信息 # 以确保我们不会对每次 LLM 调用都进行摘要处理 context: dict[str, Any] checkpointer = InMemorySaver() graph = create_react_agent( model, tools, pre_model_hook=summarization_node, state_schema=State, checkpointer=checkpointer, )
Important
- 要在图状态中**保持原始消息历史记录不变**,并且**仅将更新后的历史记录作为 LLM 的输入传递**,请在
llm_input_messages
键下返回更新后的消息。 - 要使用更新后的历史记录**覆盖图状态中的原始消息历史记录**,请在
messages
键下返回更新后的消息。
要覆盖 messages
键,您需要执行以下操作:
设置¶
首先,我们来安装所需的包并设置我们的 API 密钥
import getpass
import os
def _set_env(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"{var}: ")
_set_env("OPENAI_API_KEY")
为 LangGraph 开发设置 LangSmith
注册 LangSmith,以便快速发现问题并提高 LangGraph 项目的性能。LangSmith 允许您使用跟踪数据来调试、测试和监控您使用 LangGraph 构建的 LLM 应用——在此处详细了解如何开始。
保持原始消息历史记录不变¶
让我们来构建一个 ReAct agent,其中一个步骤可以管理对话历史:当历史的长度超过指定的 token 数量时,我们将调用 trim_messages
工具,该工具将在满足 LLM 提供商约束的同时缩减历史记录。
更新的消息历史有两种方式可以应用到 ReAct agent 中:
- 保持原始消息历史不变,将更新后的历史**仅作为输入传递给 LLM**
- 用更新后的历史覆盖原始消息历史
我们先来实现第一种。首先,我们需要为我们的 agent 定义模型和工具:
API Reference: ChatOpenAI
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o", temperature=0)
def get_weather(location: str) -> str:
"""Use this to get weather information."""
if any([city in location.lower() for city in ["nyc", "new york city"]]):
return "It might be cloudy in nyc, with a chance of rain and temperatures up to 80 degrees."
elif any([city in location.lower() for city in ["sf", "san francisco"]]):
return "It's always sunny in sf"
else:
return f"I am not sure what the weather is in {location}"
tools = [get_weather]
现在,让我们来实现 pre_model_hook
,它将被添加为一个新节点,并在每次调用 LLM 的节点(即 agent
节点)**之前**被调用。
我们的实现将包装 trim_messages
调用,并将修剪后的消息返回给 llm_input_messages
。这样可以**在图状态中保持原始消息历史不变**,并将更新后的历史**仅作为 LLM 的输入**传递。
API Reference: create_react_agent | InMemorySaver | trim_messages | count_tokens_approximately
from langgraph.prebuilt import create_react_agent
from langgraph.checkpoint.memory import InMemorySaver
from langchain_core.messages.utils import (
trim_messages,
count_tokens_approximately,
)
# This function will be added as a new node in ReAct agent graph
# that will run every time before the node that calls the LLM.
# The messages returned by this function will be the input to the LLM.
def pre_model_hook(state):
trimmed_messages = trim_messages(
state["messages"],
strategy="last",
token_counter=count_tokens_approximately,
max_tokens=384,
start_on="human",
end_on=("human", "tool"),
)
return {"llm_input_messages": trimmed_messages}
checkpointer = InMemorySaver()
graph = create_react_agent(
model,
tools,
pre_model_hook=pre_model_hook,
checkpointer=checkpointer,
)
我们还将定义一个实用程序来 nicely 地渲染代理输出:
def print_stream(stream, output_messages_key="llm_input_messages"):
for chunk in stream:
for node, update in chunk.items():
print(f"Update from node: {node}")
messages_key = (
output_messages_key if node == "pre_model_hook" else "messages"
)
for message in update[messages_key]:
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
print("\n\n")
现在让我们运行该代理并执行一些不同的查询,以达到指定的 max tokens 限制:
config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("user", "What's the weather in NYC?")]}
result = graph.invoke(inputs, config=config)
inputs = {"messages": [("user", "What's it known for?")]}
result = graph.invoke(inputs, config=config)
我们来看看到目前为止消息历史中有多少令牌:
可以看到,我们快要达到 max_tokens
的阈值了,所以在下一次调用时,我们应该会看到 pre_model_hook
启动并修剪消息历史。我们再运行一次:
inputs = {"messages": [("user", "where can i find the best bagel?")]}
print_stream(graph.stream(inputs, config=config, stream_mode="updates"))
Update from node: pre_model_hook
================================ Human Message =================================
What's it known for?
================================== Ai Message ==================================
New York City is known for a variety of iconic landmarks, cultural institutions, and vibrant neighborhoods. Some of the most notable features include:
1. **Statue of Liberty**: A symbol of freedom and democracy, located on Liberty Island.
2. **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere.
3. **Central Park**: A large public park offering a natural retreat in the middle of the city.
4. **Empire State Building**: An iconic skyscraper offering panoramic views of the city.
5. **Broadway**: Famous for its world-class theater productions.
6. **Wall Street**: The financial hub of the United States.
7. **Museums**: Including the Metropolitan Museum of Art, Museum of Modern Art (MoMA), and the American Museum of Natural History.
8. **Diverse Cuisine**: A melting pot of cultures offering a wide range of culinary experiences.
9. **Cultural Diversity**: A rich tapestry of cultures and communities from around the world.
10. **Fashion**: A global fashion capital, hosting events like New York Fashion Week.
These are just a few highlights of what makes New York City a unique and vibrant place.
================================ Human Message =================================
where can i find the best bagel?
Update from node: agent
================================== Ai Message ==================================
New York City is famous for its bagels, and there are several places renowned for serving some of the best. Here are a few top spots where you can find excellent bagels in NYC:
1. **Ess-a-Bagel**: Known for their large, chewy bagels with a variety of spreads and toppings.
2. **Russ & Daughters**: A classic spot offering traditional bagels with high-quality smoked fish and cream cheese.
3. **H&H Bagels**: Famous for their fresh, hand-rolled bagels.
4. **Murray’s Bagels**: Offers a wide selection of bagels and spreads, with a no-toasting policy to preserve freshness.
5. **Absolute Bagels**: Known for their authentic, fluffy bagels and a variety of cream cheese options.
6. **Tompkins Square Bagels**: Offers creative bagel sandwiches and a wide range of spreads.
7. **Bagel Hole**: Known for their smaller, denser bagels with a crispy crust.
Each of these places has its own unique style and flavor, so it might be worth trying a few to find your personal favorite!
pre_model_hook
节点现在如预期般只返回了最后 3 条消息。然而,现有的消息历史并未受影响:
updated_messages = graph.get_state(config).values["messages"]
assert [(m.type, m.content) for m in updated_messages[: len(messages)]] == [
(m.type, m.content) for m in messages
]
覆盖原始消息历史¶
现在,我们将 pre_model_hook
修改为**覆盖**图状态中的消息历史记录。为此,我们将返回 messages
键下的更新后的消息。我们还将包含一个特殊的 RemoveMessage(REMOVE_ALL_MESSAGES)
对象,它告诉 create_react_agent
从图状态中删除之前的消息:
API Reference: RemoveMessage
from langchain_core.messages import RemoveMessage
from langgraph.graph.message import REMOVE_ALL_MESSAGES
def pre_model_hook(state):
trimmed_messages = trim_messages(
state["messages"],
strategy="last",
token_counter=count_tokens_approximately,
max_tokens=384,
start_on="human",
end_on=("human", "tool"),
)
# NOTE that we're now returning the messages under the `messages` key
# We also remove the existing messages in the history to ensure we're overwriting the history
return {"messages": [RemoveMessage(REMOVE_ALL_MESSAGES)] + trimmed_messages}
checkpointer = InMemorySaver()
graph = create_react_agent(
model,
tools,
pre_model_hook=pre_model_hook,
checkpointer=checkpointer,
)
现在,让我们使用与之前相同的查询来运行代理:
config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("user", "What's the weather in NYC?")]}
result = graph.invoke(inputs, config=config)
inputs = {"messages": [("user", "What's it known for?")]}
result = graph.invoke(inputs, config=config)
messages = result["messages"]
inputs = {"messages": [("user", "where can i find the best bagel?")]}
print_stream(
graph.stream(inputs, config=config, stream_mode="updates"),
output_messages_key="messages",
)
Update from node: pre_model_hook
================================ Remove Message ================================
================================ Human Message =================================
What's it known for?
================================== Ai Message ==================================
New York City is known for a variety of iconic landmarks, cultural institutions, and vibrant neighborhoods. Some of the most notable features include:
1. **Statue of Liberty**: A symbol of freedom and democracy, located on Liberty Island.
2. **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere.
3. **Central Park**: A large public park offering a natural oasis amidst the urban environment.
4. **Empire State Building**: An iconic skyscraper offering panoramic views of the city.
5. **Broadway**: Famous for its world-class theater productions and musicals.
6. **Wall Street**: The financial hub of the United States, located in the Financial District.
7. **Museums**: Including the Metropolitan Museum of Art, Museum of Modern Art (MoMA), and the American Museum of Natural History.
8. **Diverse Cuisine**: A melting pot of cultures, offering a wide range of international foods.
9. **Cultural Diversity**: Known for its diverse population and vibrant cultural scene.
10. **Brooklyn Bridge**: An iconic suspension bridge connecting Manhattan and Brooklyn.
These are just a few highlights, as NYC is a city with endless attractions and activities.
================================ Human Message =================================
where can i find the best bagel?
Update from node: agent
================================== Ai Message ==================================
New York City is famous for its bagels, and there are several places renowned for serving some of the best. Here are a few top spots where you can find delicious bagels in NYC:
1. **Ess-a-Bagel**: Known for its large, chewy bagels and a wide variety of spreads and toppings. Locations in Midtown and the East Village.
2. **Russ & Daughters**: A historic appetizing store on the Lower East Side, famous for its bagels with lox and cream cheese.
3. **Absolute Bagels**: Located on the Upper West Side, this spot is popular for its fresh, fluffy bagels.
4. **Murray’s Bagels**: Known for its traditional, hand-rolled bagels. Located in Greenwich Village.
5. **Tompkins Square Bagels**: Offers a wide selection of bagels and creative cream cheese flavors. Located in the East Village.
6. **Bagel Hole**: A small shop in Park Slope, Brooklyn, known for its classic, no-frills bagels.
7. **Leo’s Bagels**: Located in the Financial District, known for its authentic New York-style bagels.
Each of these places has its own unique style and flavor, so it might be worth trying a few to find your personal favorite!
pre_model_hook
节点再次返回了最后 3 条消息。但是,这一次,消息历史在图状态中也被修改了:
updated_messages = graph.get_state(config).values["messages"]
assert (
# First 2 messages in the new history are the same as last 2 messages in the old
[(m.type, m.content) for m in updated_messages[:2]]
== [(m.type, m.content) for m in messages[-2:]]
)
总结消息历史记录¶
最后,让我们采用一种不同的策略来管理消息历史记录——总结。与截断一样,您可以选择保持原始消息历史记录不变或覆盖它。下面的示例将仅显示前者。
我们将使用预构建的 langmem
库中的 SummarizationNode
。一旦消息历史记录达到 token 限制,总结节点将总结较早的消息,以确保它们符合 max_tokens
。
API Reference: AgentState
from langmem.short_term import SummarizationNode
from langgraph.prebuilt.chat_agent_executor import AgentState
from typing import Any
model = ChatOpenAI(model="gpt-4o")
summarization_model = model.bind(max_tokens=128)
summarization_node = SummarizationNode(
token_counter=count_tokens_approximately,
model=summarization_model,
max_tokens=384,
max_summary_tokens=128,
output_messages_key="llm_input_messages",
)
class State(AgentState):
# NOTE: we're adding this key to keep track of previous summary information
# to make sure we're not summarizing on every LLM call
context: dict[str, Any]
checkpointer = InMemorySaver()
graph = create_react_agent(
# limit the output size to ensure consistent behavior
model.bind(max_tokens=256),
tools,
pre_model_hook=summarization_node,
state_schema=State,
checkpointer=checkpointer,
)
config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("user", "What's the weather in NYC?")]}
result = graph.invoke(inputs, config=config)
inputs = {"messages": [("user", "What's it known for?")]}
result = graph.invoke(inputs, config=config)
inputs = {"messages": [("user", "where can i find the best bagel?")]}
print_stream(graph.stream(inputs, config=config, stream_mode="updates"))
Update from node: pre_model_hook
================================ System Message ================================
Summary of the conversation so far: The user asked about the current weather in New York City. In response, the assistant provided information that it might be cloudy, with a chance of rain, and temperatures reaching up to 80 degrees.
================================ Human Message =================================
What's it known for?
================================== Ai Message ==================================
New York City, often referred to as NYC, is known for its:
1. **Landmarks and Iconic Sites**:
- **Statue of Liberty**: A symbol of freedom and democracy.
- **Central Park**: A vast green oasis in the middle of the city.
- **Empire State Building**: Once the tallest building in the world, offering stunning views of the city.
- **Times Square**: Known for its bright lights and bustling atmosphere.
2. **Cultural Institutions**:
- **Broadway**: Renowned for theatrical performances and musicals.
- **Metropolitan Museum of Art** and **Museum of Modern Art (MoMA)**: World-class art collections.
- **American Museum of Natural History**: Known for its extensive exhibits ranging from dinosaurs to space exploration.
3. **Diverse Neighborhoods and Cuisine**:
- NYC is famous for having a melting pot of cultures, reflected in neighborhoods like Chinatown, Little Italy, and Harlem.
- The city offers a wide range of international cuisines, from street food to high-end dining.
4. **Financial District**:
- Home to Wall Street, the New York Stock Exchange (NYSE), and other major financial institutions.
5. **Media and Entertainment**:
- Major hub for television, film, and media, with numerous studios and networks based there.
6. **Fashion**:
- Often referred to as one of the "Big Four" fashion capitals, hosting events like New York Fashion Week.
7. **Sports**:
- Known for its passionate sports culture with teams like the Yankees (MLB), Mets (MLB), Knicks (NBA), and Rangers (NHL).
These elements, among others, contribute to NYC's reputation as a vibrant and dynamic city.
================================ Human Message =================================
where can i find the best bagel?
Update from node: agent
================================== Ai Message ==================================
Finding the best bagel in New York City can be subjective, as there are many beloved spots across the city. However, here are some renowned bagel shops you might want to try:
1. **Ess-a-Bagel**: Known for its chewy and flavorful bagels, located in Midtown and Stuyvesant Town.
2. **Bagel Hole**: A favorite for traditionalists, offering classic and dense bagels, located in Park Slope, Brooklyn.
3. **Russ & Daughters**: A legendary appetizing store on the Lower East Side, famous for their bagels with lox.
4. **Murray’s Bagels**: Located in Greenwich Village, known for their fresh and authentic New York bagels.
5. **Absolute Bagels**: Located on the Upper West Side, they’re known for their fresh, fluffy bagels with a variety of spreads.
6. **Tompkins Square Bagels**: In the East Village, famous for their creative cream cheese options and fresh bagels.
7. **Zabar’s**: A landmark on the Upper West Side known for their classic bagels and smoked fish.
Each of these spots offers a unique take on the classic New York bagel experience, and trying several might be the best way to discover your personal favorite!