介绍
大家好,这次给大家分享的内容是微软AutoGen框架一个重要的机制Managing State(管理状态),那么在微软AutoGen中是如何保存和加载智能体、团队以及终止条件的状态的呢?我们直接进入正题。
Managing State
在微软AutoGen中,Managing State机制主要用于管理智能体交互过程中的状态信息,包括记录对话历史、管理智能体的上下文信息、跟踪交互进度等。它是微软AutoGen的重要功能,在智能体协作、系统性能优化及用户体验提升等方面发挥着关键作用。
到目前为止,我们已经探讨了如何在多智能体应用程序中构建组件,即智能体、团队以及终止条件。在许多情况下,将这些组件的状态保存到磁盘,并在之后重新加载,是很有用的。这在 Web 应用程序中尤为重要,因为无状态的端点要响应请求,且需要从持久存储中加载应用程序的状态。
在本次分享中中,我们将演示如何保存和加载智能体、团队以及终止条件的状态。
保存和加载代理
我们可以通过在AssistantAgent上调用save_state()方法来获取智能体的状态。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClientassistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。),
)async def main() -> None:response = await assistant_agent.on_messages([TextMessage(content="Write a 3 line poem on lake tangayika", source="user")], CancellationToken())print(response.chat_message.content)asyncio.run(main())
运行结果
Lake Tanganyika so deep and wide,
Shimmering waters reflecting the African sun's pride,
Beauty and mystery beneath its smooth tide.进程已结束,退出代码为 0
我们存储assistant_agent的当前状态,并将其打印出来,看看会是什么结果。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClientassistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。),
)async def main() -> None:response = await assistant_agent.on_messages([TextMessage(content="Write a 3 line poem on lake tangayika", source="user")], CancellationToken())print(response.chat_message.content)agent_state = await assistant_agent.save_state()print(agent_state)asyncio.run(main())
运行结果
Lake Tanganyika, vast and deep,
Where waters hold secrets they keep,
A place of beauty, where dreams take flight.
{'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a 3 line poem on lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': 'Lake Tanganyika, vast and deep,\nWhere waters hold secrets they keep,\nA place of beauty, where dreams take flight.\n\n', 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}进程已结束,退出代码为 0
我们新声明一个new_assistant_agent,并加载assistant_agent保存的状态,然后对new_assistant_agent进行提问,看会有怎样的回答。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClientassistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。),
)new_assistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",),
)async def main() -> None:response = await assistant_agent.on_messages([TextMessage(content="Write a 3 line poem on lake tangayika", source="user")], CancellationToken())print(response.chat_message.content)agent_state = await assistant_agent.save_state()print(agent_state)await new_assistant_agent.load_state(agent_state)response = await new_assistant_agent.on_messages([TextMessage(content="What was the last line of the previous poem you wrote", source="user")],CancellationToken())print(response.chat_message.content)asyncio.run(main())
运行结果
Lake Tanganyika, vast and deep,
Where waters hold secrets they keep,
A place of beauty, where dreams take flight.
{'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a 3 line poem on lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': 'Lake Tanganyika, vast and deep,\nWhere waters hold secrets they keep,\nA place of beauty, where dreams take flight.\n\n', 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}
"A place of beauty, where dreams take flight."
我们看到当assistant_agent保存状态后,我们使用new_assistant_agent对其状态进行加载。然后我们对new_assistant_agent进行提问“你之前写的那首诗最后一行是什么?”,它回答了assistant_agent输出结果的最后一行"A place of beauty, where dreams take flight.",大家明白了吗?
博主笔记:对于AssistantAgent,其状态由model_context构成。如果我们要编写自定义智能体,可以考虑重写save_state() 和load_state()方法来自定义相关行为。默认的实现方式是保存和加载一个空状态。
保存和加载团队
我们可以通过调用团队的save_state方法来获取团队的状态,再通过调用团队的load_state方法将其加载回来。
当我们对一个团队调用save_state时,它会保存团队中所有智能体的状态。
我们将从创建一个仅有单个智能体的简单循环轮询群聊团队入手,然后让它创作一首诗。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient# 定义一个团队。
assistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",),
)
agent_team = RoundRobinGroupChat([assistant_agent], termination_condition=MaxMessageTermination(max_messages=2))async def main() -> None:# 运行团队(这里可能指运行由多个智能体组成的团队任务),并将消息实时输出到控制台。stream = agent_team.run_stream(task="Write a beautiful poem 3-line about lake tangayika")await Console(stream)# 保存智能体团队的状态。team_state = await agent_team.save_state()print(team_state)asyncio.run(main())
运行结果
---------- user ----------
Write a beautiful poem 3-line about lake tangayika
---------- assistant_agent ----------
Lake Tanganyika, serene and deep,
Reflecting the skies as if in endless sleep,
A place where nature's secrets silently keep.
{'type': 'TeamState', 'version': '1.0.0', 'agent_states': {'group_chat_manager/e78acd7b-f215-4d94-ad25-14047ce518c3': {'type': 'RoundRobinManagerState', 'version': '1.0.0', 'message_thread': [{'source': 'user', 'models_usage': None, 'content': 'Write a beautiful poem 3-line about lake tangayika', 'type': 'TextMessage'}, {'source': 'assistant_agent', 'models_usage': {'prompt_tokens': 29, 'completion_tokens': 28}, 'content': "Lake Tanganyika, serene and deep,\nReflecting the skies as if in endless sleep,\nA place where nature's secrets silently keep.", 'type': 'TextMessage'}], 'current_turn': 0, 'next_speaker_index': 0}, 'collect_output_messages/e78acd7b-f215-4d94-ad25-14047ce518c3': {}, 'assistant_agent/e78acd7b-f215-4d94-ad25-14047ce518c3': {'type': 'ChatAgentContainerState', 'version': '1.0.0', 'agent_state': {'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a beautiful poem 3-line about lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': "Lake Tanganyika, serene and deep,\nReflecting the skies as if in endless sleep,\nA place where nature's secrets silently keep.", 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}, 'message_buffer': []}}, 'team_id': 'e78acd7b-f215-4d94-ad25-14047ce518c3'}进程已结束,退出代码为 0
如果我们重置团队(模拟团队的实例化过程),并询问“What was the last line of the poem you wrote?”这个问题,我们会发现团队无法回答这个问题,因为它没有之前运行情况的参考信息。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient# 定义一个团队。
assistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",),
)
agent_team = RoundRobinGroupChat([assistant_agent], termination_condition=MaxMessageTermination(max_messages=2))async def main() -> None:# 运行团队(这里可能指运行由多个智能体组成的团队任务),并将消息实时输出到控制台。stream = agent_team.run_stream(task="Write a beautiful poem 3-line about lake tangayika")await Console(stream)# 保存智能体团队的状态。team_state = await agent_team.save_state()print(team_state)# 重置团队,并询问“你之前写的那首诗最后一行是什么?”stream = agent_team.run_stream(task="What was the last line of the poem you wrote?")await agent_team.reset()await Console(stream, output_stats=True)asyncio.run(main())
运行结果
---------- user ----------
Write a beautiful poem 3-line about lake tangayika
---------- assistant_agent ----------
Lake Tanganyika, sparkling blue expanse,
Nature's beauty in a peaceful trance.
Mountains embrace with a gentle dance.
{'type': 'TeamState', 'version': '1.0.0', 'agent_states': {'group_chat_manager/17ef1e0a-6038-4b25-b2ad-ff1962cba930': {'type': 'RoundRobinManagerState', 'version': '1.0.0', 'message_thread': [{'source': 'user', 'models_usage': None, 'content': 'Write a beautiful poem 3-line about lake tangayika', 'type': 'TextMessage'}, {'source': 'assistant_agent', 'models_usage': {'prompt_tokens': 29, 'completion_tokens': 26}, 'content': "Lake Tanganyika, sparkling blue expanse,\nNature's beauty in a peaceful trance.\nMountains embrace with a gentle dance.", 'type': 'TextMessage'}], 'current_turn': 0, 'next_speaker_index': 0}, 'collect_output_messages/17ef1e0a-6038-4b25-b2ad-ff1962cba930': {}, 'assistant_agent/17ef1e0a-6038-4b25-b2ad-ff1962cba930': {'type': 'ChatAgentContainerState', 'version': '1.0.0', 'agent_state': {'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a beautiful poem 3-line about lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': "Lake Tanganyika, sparkling blue expanse,\nNature's beauty in a peaceful trance.\nMountains embrace with a gentle dance.", 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}, 'message_buffer': []}}, 'team_id': '17ef1e0a-6038-4b25-b2ad-ff1962cba930'}
---------- user ----------
What was the last line of the poem you wrote?
---------- assistant_agent ----------
I'm sorry, I don't have the ability to write poetry. Would you like me to help you write a poem?
[Prompt tokens: 28, Completion tokens: 25]
---------- Summary ----------
Number of messages: 2
Finish reason: Maximum number of messages 2 reached, current message count: 2
Total prompt tokens: 28
Total completion tokens: 25
Duration: 30.67 seconds进程已结束,退出代码为 0
接下来,我们加载团队的状态,然后提出同样的问题。我们会看到,团队能够准确地给出它之前所写诗歌的最后一行。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient# 定义一个团队。
assistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",),
)
agent_team = RoundRobinGroupChat([assistant_agent], termination_condition=MaxMessageTermination(max_messages=2))async def main() -> None:# 运行团队(这里可能指运行由多个智能体组成的团队任务),并将消息实时输出到控制台。stream = agent_team.run_stream(task="Write a beautiful poem 3-line about lake tangayika")await Console(stream)# 保存智能体团队的状态。team_state = await agent_team.save_state()print(team_state)# 重置团队,并询问“你之前写的那首诗最后一行是什么?”stream = agent_team.run_stream(task="What was the last line of the poem you wrote?")await agent_team.reset()await Console(stream)print(team_state)# 加载团队状态。await agent_team.load_state(team_state)stream = agent_team.run_stream(task="What was the last line of the poem you wrote?")await Console(stream, output_stats=True)asyncio.run(main())
运行结果
---------- user ----------
Write a beautiful poem 3-line about lake tangayika
---------- assistant_agent ----------
Lake Tanganyika, serene and deep,
Reflecting the sky while the mountains steep,
Nature's beauty in its waters, secrets to keep.
{'type': 'TeamState', 'version': '1.0.0', 'agent_states': {'group_chat_manager/25f6b804-21ee-43d2-a223-caec36a300a5': {'type': 'RoundRobinManagerState', 'version': '1.0.0', 'message_thread': [{'source': 'user', 'models_usage': None, 'content': 'Write a beautiful poem 3-line about lake tangayika', 'type': 'TextMessage'}, {'source': 'assistant_agent', 'models_usage': {'prompt_tokens': 29, 'completion_tokens': 29}, 'content': "Lake Tanganyika, serene and deep,\nReflecting the sky while the mountains steep,\nNature's beauty in its waters, secrets to keep.", 'type': 'TextMessage'}], 'current_turn': 0, 'next_speaker_index': 0}, 'collect_output_messages/25f6b804-21ee-43d2-a223-caec36a300a5': {}, 'assistant_agent/25f6b804-21ee-43d2-a223-caec36a300a5': {'type': 'ChatAgentContainerState', 'version': '1.0.0', 'agent_state': {'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a beautiful poem 3-line about lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': "Lake Tanganyika, serene and deep,\nReflecting the sky while the mountains steep,\nNature's beauty in its waters, secrets to keep.", 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}, 'message_buffer': []}}, 'team_id': '25f6b804-21ee-43d2-a223-caec36a300a5'}
---------- user ----------
What was the last line of the poem you wrote?
---------- assistant_agent ----------
I didn't write it, but if you share the poem with me, I can help you identify the last line.
{'type': 'TeamState', 'version': '1.0.0', 'agent_states': {'group_chat_manager/25f6b804-21ee-43d2-a223-caec36a300a5': {'type': 'RoundRobinManagerState', 'version': '1.0.0', 'message_thread': [{'source': 'user', 'models_usage': None, 'content': 'Write a beautiful poem 3-line about lake tangayika', 'type': 'TextMessage'}, {'source': 'assistant_agent', 'models_usage': {'prompt_tokens': 29, 'completion_tokens': 29}, 'content': "Lake Tanganyika, serene and deep,\nReflecting the sky while the mountains steep,\nNature's beauty in its waters, secrets to keep.", 'type': 'TextMessage'}], 'current_turn': 0, 'next_speaker_index': 0}, 'collect_output_messages/25f6b804-21ee-43d2-a223-caec36a300a5': {}, 'assistant_agent/25f6b804-21ee-43d2-a223-caec36a300a5': {'type': 'ChatAgentContainerState', 'version': '1.0.0', 'agent_state': {'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a beautiful poem 3-line about lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': "Lake Tanganyika, serene and deep,\nReflecting the sky while the mountains steep,\nNature's beauty in its waters, secrets to keep.", 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}, 'message_buffer': []}}, 'team_id': '25f6b804-21ee-43d2-a223-caec36a300a5'}
---------- user ----------
What was the last line of the poem you wrote?
---------- assistant_agent ----------
Nature's beauty in its waters, secrets to keep.
[Prompt tokens: 81, Completion tokens: 11]
---------- Summary ----------
Number of messages: 2
Finish reason: Maximum number of messages 2 reached, current message count: 2
Total prompt tokens: 81
Total completion tokens: 11
Duration: 1.03 seconds进程已结束,退出代码为 0
持久化状态(文件或数据库)
在许多情况下,我们可能希望将团队的状态持久化到磁盘(或数据库),并在之后重新加载。状态是一个字典类型,它可以被序列化到文件中,或者写入数据库。
完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
import json# 定义一个团队。
assistant_agent = AssistantAgent(name="assistant_agent",system_message="You are a helpful assistant",model_client=OpenAIChatCompletionClient(model="gpt-3.5-turbo",),
)
agent_team = RoundRobinGroupChat([assistant_agent], termination_condition=MaxMessageTermination(max_messages=2))async def main() -> None:# 运行团队(这里可能指运行由多个智能体组成的团队任务),并将消息实时输出到控制台。stream = agent_team.run_stream(task="Write a beautiful poem 3-line about lake tangayika")await Console(stream)# 保存智能体团队的状态。team_state = await agent_team.save_state()print(team_state) # 将状态保存到硬盘with open("team_state.json", "w") as f:json.dump(team_state, f)# 从硬盘加载状态with open("team_state.json", "r") as f:team_state = json.load(f)new_agent_team = RoundRobinGroupChat([assistant_agent], termination_condition=MaxMessageTermination(max_messages=2))await new_agent_team.load_state(team_state)stream = new_agent_team.run_stream(task="What was the last line of the poem you wrote?")await Console(stream, output_stats=True)asyncio.run(main())
运行结果
---------- user ----------
Write a beautiful poem 3-line about lake tangayika
---------- assistant_agent ----------
Lake Tanganyika, serene and deep,
Reflecting the sky in its waters so steep,
Nature's beauty here forever will keep.
{'type': 'TeamState', 'version': '1.0.0', 'agent_states': {'group_chat_manager/2c5266f7-c04f-45bf-ae69-bf1910ad2994': {'type': 'RoundRobinManagerState', 'version': '1.0.0', 'message_thread': [{'source': 'user', 'models_usage': None, 'content': 'Write a beautiful poem 3-line about lake tangayika', 'type': 'TextMessage'}, {'source': 'assistant_agent', 'models_usage': {'prompt_tokens': 29, 'completion_tokens': 28}, 'content': "Lake Tanganyika, serene and deep,\nReflecting the sky in its waters so steep,\nNature's beauty here forever will keep. ", 'type': 'TextMessage'}], 'current_turn': 0, 'next_speaker_index': 0}, 'collect_output_messages/2c5266f7-c04f-45bf-ae69-bf1910ad2994': {}, 'assistant_agent/2c5266f7-c04f-45bf-ae69-bf1910ad2994': {'type': 'ChatAgentContainerState', 'version': '1.0.0', 'agent_state': {'type': 'AssistantAgentState', 'version': '1.0.0', 'llm_context': {'messages': [{'content': 'Write a beautiful poem 3-line about lake tangayika', 'source': 'user', 'type': 'UserMessage'}, {'content': "Lake Tanganyika, serene and deep,\nReflecting the sky in its waters so steep,\nNature's beauty here forever will keep. ", 'source': 'assistant_agent', 'type': 'AssistantMessage'}]}}, 'message_buffer': []}}, 'team_id': '2c5266f7-c04f-45bf-ae69-bf1910ad2994'}
---------- user ----------
What was the last line of the poem you wrote?
---------- assistant_agent ----------
Nature's beauty here forever will keep.
[Prompt tokens: 80, Completion tokens: 8]
---------- Summary ----------
Number of messages: 2
Finish reason: Maximum number of messages 2 reached, current message count: 2
Total prompt tokens: 80
Total completion tokens: 8
Duration: 30.78 seconds进程已结束,退出代码为 0
说明
如果大家在运行上述代码的时候有AutoGen相关的提示或报错(例如:该参数不存在,没有此类方法等),请尝试更新一下AutoGen,博主在分享这篇博文的时候,AutoGen的版本是0.4.6稳定版。
安装或更新命令
pip install -U "autogen-agentchat" "autogen-ext[openai,azure]"
另外大家要根据业务需求,设置使用的LLM,不一定要按照我给大家分享代码中的设置来,如果只是为了测试并看运行结果可直接复制粘贴代码(完整代码)。
结束
好了,以上就是本次分享的全部内容,不知道大家是否了解了微软AutoGen中Managing State机制以及如何的去使用该机制来达到应用效果。博主这里再给大家整理下该机制在实际使用中的具体作用:
- 记录对话历史:它能够完整地记录智能体之间的对话内容和顺序,将每一轮的提问、回答以及相关的操作都进行存储。这些记录不仅有助于追溯整个交互过程,还能为后续的分析和总结提供依据,方便用户或系统了解智能体之间的沟通情况,例如在调试或优化智能体行为时,可以通过查看对话历史来找出问题所在。
- 管理智能体的上下文信息:为每个智能体维护其特定的上下文信息,比如智能体的角色、目标、当前任务以及与当前对话相关的特定知识等。这些上下文信息能够帮助智能体更好地理解当前的对话场景,使其做出更符合逻辑和情境的回应。例如,在一个多轮的业务咨询对话中,智能体需要根据之前设定的业务场景和客户需求等上下文信息,提供准确和有针对性的建议。
- 跟踪交互进度:可以清晰地了解智能体之间交互的进展情况,比如当前处于对话的哪个阶段,是否已经完成了某个特定的任务或目标,是否需要进一步的信息来推进交互等。通过跟踪交互进度,系统能够更好地协调智能体之间的协作,确保交互按照预期的流程进行,避免出现混乱或无效的交互。
- 支持状态恢复和持久化:允许将智能体的状态进行保存和加载,以便在需要时能够恢复到之前的某个状态。这在处理复杂的多轮交互或长时间运行的任务时非常重要,当系统出现故障或需要暂停后继续执行时,可以通过加载之前保存的状态,让智能体从上次中断的地方继续进行交互,而不需要重新开始整个过程,提高了系统的稳定性和可靠性。
- 辅助决策和优化:通过对状态信息的分析和总结,系统可以获取智能体交互的各种数据和指标,如响应时间、回答准确率、交互效率等。这些数据可以用于评估智能体的性能,发现潜在的问题和优化点,从而对智能体的策略、参数等进行调整和优化,以提高智能体在后续交互中的表现和效果。
大家理解了吗?博主希望本次分享对目前正在使用AutoGen框架做大模型应用开发的同事,正在学习使用AutoGen框架的同学,还有正在研究AutoGen的管理机制或想用一样的功能却不知道如何去调用或使用的同学或同事有所启发,看看博主的这篇分享内容能不能帮助到大家,这样博主也就达到分享的目的了。博主还是那句话,请大家多去大胆的尝试和使用。对于微软AutoGen框架的主要功能介绍,目前博主就分享到这。后续博主会跟大家分享AutoGen中的进阶功能。如果大家对博主分享的内容感兴趣或有帮助,请点赞和关注,博主会持续更新关于微软AutoGen更多和更有趣的内容。