介绍
大家好,这次给大家分享的内容是微软AutoGen框架的核心功能Termination(中止)。那么今天分享的内容又是什么呢?我们直接进入正题。
Termination
在前面的分享中中,我们探讨了如何定义智能体,并将它们组织成能够解决任务的团队。然而,一次运行可能会永远持续下去,在许多情况下,我们需要知道何时停止它们。这就是终止条件的作用。
AgentChat通过提供一个基础的TerminationCondition类以及几个从它继承的实现类,支持多种终止条件。
终止条件是一个可调用对象,它接受自上次调用该条件以来的一系列AgentEvent或ChatMessage对象作为参数。如果对话应该终止,它将返回一个StopMessage;否则,返回None。一旦达到终止条件,在再次使用它之前,必须通过调用reset()来重置。
关于终止条件,有一些重要事项需要注意:
- 它们是有状态的,但在每次运行(run() 或run_stream())结束后会自动重置。
- 可以使用与(AND)和或(OR)运算符对它们进行组合。
博主笔记:对于群组聊天团队(即RoundRobinGroupChat、SelectorGroupChat和Swarm),终止条件会在每个智能体回复后调用。虽然一个回复可能包含多个内部消息,但团队针对单个回复中的所有消息仅调用一次终止条件。因此,该条件是使用自上次调用以来消息的 “增量序列” 来调用的。
内置终止条件:
- 最大消息数终止(MaxMessageTermination):在生成指定数量的消息后停止,这些消息包括智能体消息和任务消息。
- 文本提及终止(TextMentionTermination):当消息中提及特定文本或字符串时(例如 “TERMINATE”)停止。
- 令牌使用量终止(TokenUsageTermination):当使用了一定数量的提示令牌或完成令牌时停止。这要求智能体在其消息中报告令牌使用情况。
- 超时终止(TimeoutTermination):在经过指定的秒数时长后停止。
- 交接终止(HandoffTermination):当请求交接给特定目标时停止。交接消息可用于构建诸如 “Swarm” 之类的模式。当你希望在智能体交接给应用程序或用户时暂停运行,并允许他们提供输入时,此条件很有用。
- 源匹配终止(SourceMatchTermination):在特定智能体回复后停止。
- 外部终止(ExternalTermination):支持从运行外部以编程方式控制终止。这对于用户界面集成很有用(例如,聊天界面中的 “停止” 按钮)。
- 停止消息终止(StopMessageTermination):当智能体生成停止消息时停止。
为了展示终止条件的特点,我们将创建一个由两个智能体组成的团队:一个主要智能体负责文本生成,另一个评判智能体则对生成的文本进行审查并提供反馈。
创建智能体代码演示
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(
model="gpt-3.5-turbo",
temperature=1,
# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。
)
# 创建主智能体。
primary_agent = AssistantAgent(
"primary",
model_client=model_client,
system_message="You are a helpful AI assistant.",
)
# 创建评判智能体。
critic_agent = AssistantAgent(
"critic",
model_client=model_client,
system_message="Provide constructive feedback for every message. Respond with 'APPROVE' to when your feedbacks are addressed.",
)
让我们探究一下,每次调用run或run_stream后,终止条件是如何自动重置的,这使得团队能够从上一次中断的地方继续进行对话。
run_stream代码演示
max_msg_termination = MaxMessageTermination(max_messages=3)
round_robin_team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=max_msg_termination)
async def main() -> None:
await Console(round_robin_team.run_stream(task="Write a unique, Haiku about the weather in Beijing"), output_stats=True)
asyncio.run(main())
创建智能体并执行完整代码
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
model_client = OpenAIChatCompletionClient(
model="gpt-3.5-turbo",
temperature=1,
# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。
)
# 创建主智能体。
primary_agent = AssistantAgent(
"primary",
model_client=model_client,
system_message="You are a helpful AI assistant.",
)
# 创建评判智能体。
critic_agent = AssistantAgent(
"critic",
model_client=model_client,
system_message="Provide constructive feedback for every message. Respond with 'APPROVE' to when your feedbacks are addressed.",
)
max_msg_termination = MaxMessageTermination(max_messages=3)
round_robin_team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=max_msg_termination)
async def main() -> None:
await Console(round_robin_team.run_stream(task="Write a unique, Haiku about the weather in Beijing"), output_stats=True)
asyncio.run(main())
创建智能体并执行运行结果
---------- user ----------
Write a unique, Haiku about the weather in Beijing
---------- primary ----------
Amidst smoggy skies,
A gentle breeze whispers by,
Beijing's weather sighs.
[Prompt tokens: 30, Completion tokens: 23]
---------- critic ----------
I like how you captured the essence of Beijing's weather through your Haiku. The imagery is vivid and the mood is well conveyed. Great job!
[Prompt tokens: 74, Completion tokens: 30]
---------- Summary ----------
Number of messages: 3
Finish reason: Maximum number of messages 3 reached, current message count: 3
Total prompt tokens: 104
Total completion tokens: 53
Duration: 61.66 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
model_client = OpenAIChatCompletionClient(
model="gpt-3.5-turbo",
temperature=1,
# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。
)
# 创建主智能体。
primary_agent = AssistantAgent(
"primary",
model_client=model_client,
system_message="You are a helpful AI assistant.",
)
# 创建评判智能体。
critic_agent = AssistantAgent(
"critic",
model_client=model_client,
system_message="Provide constructive feedback for every message. Respond with 'APPROVE' to when your feedbacks are addressed.",
)
max_msg_termination = MaxMessageTermination(max_messages=3)
round_robin_team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=max_msg_termination)
async def main() -> None:
await Console(round_robin_team.run_stream(task="Write a unique, Haiku about the weather in Beijing"), output_stats=True)
await Console(round_robin_team.run_stream(), output_stats=True)
asyncio.run(main())
继续对话运行结果
---------- user ----------
Write a unique, Haiku about the weather in Beijing
---------- primary ----------
Clouds cover the sky,
Rain whispers on ancient streets,
Beijing's mood shifts.
[Prompt tokens: 30, Completion tokens: 18]
---------- critic ----------
I appreciate the imagery and vivid emotions in your Haiku about Beijing's weather. The contrast between the clouds and rain captures the mood beautifully. Well done!
[Prompt tokens: 69, Completion tokens: 31]
---------- Summary ----------
Number of messages: 3
Finish reason: Maximum number of messages 3 reached, current message count: 3
Total prompt tokens: 99
Total completion tokens: 49
Duration: 31.83 seconds
---------- primary ----------
Thank you for your kind feedback! I'm glad you enjoyed the Haiku. If you have any more requests or feedback, feel free to let me know.
[Prompt tokens: 92, Completion tokens: 32]
---------- critic ----------
You're welcome! Your writing is strong and engaging. Keep up the great work and continue exploring your creativity through poetry. If you want to experiment with different styles or themes, feel free to do so! APPROVE
[Prompt tokens: 145, Completion tokens: 43]
---------- primary ----------
Thank you for the encouragement and approval! I'm glad you appreciate the creativity. I'll continue to explore different styles and themes in poetry. If you have any specific preferences or ideas for future poems, feel free to share them.
[Prompt tokens: 180, Completion tokens: 46]
---------- Summary ----------
Number of messages: 3
Finish reason: Maximum number of messages 3 reached, current message count: 3
Total prompt tokens: 417
Total completion tokens: 121
Duration: 32.37 seconds
进程已结束,退出代码为 0
团队从上一次中断处继续,让主智能体对反馈作出回应。
接下来,我们展示如何使用与(&)和或(|)运算符组合终止条件,以创建更复杂的终止逻辑。例如,我们将创建一个团队,该团队在生成10条消息后,或者当评判者智能体认可一条消息时就停止。
中止逻辑完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(
model="gpt-3.5-turbo",
temperature=1,
# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。
)
# 创建主智能体。
primary_agent = AssistantAgent(
"primary",
model_client=model_client,
system_message="You are a helpful AI assistant.",
)
# 创建评判智能体。
critic_agent = AssistantAgent(
"critic",
model_client=model_client,
system_message="Provide constructive feedback for every message. Respond with 'APPROVE' to when your feedbacks are addressed.",
)
max_msg_termination = MaxMessageTermination(max_messages=10)
text_termination = TextMentionTermination("APPROVE")
combined_termination = max_msg_termination | text_termination
round_robin_team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=combined_termination)
async def main() -> None:
await Console(round_robin_team.run_stream(task="Write a unique, Haiku about the weather in Beijing"), output_stats=True)
asyncio.run(main())
中止逻辑运行结果
---------- user ----------
Write a unique, Haiku about the weather in Beijing
---------- primary ----------
Skies of Beijing gray,
Whispers of wind in the air,
Autumn's chill holds sway.
[Prompt tokens: 30, Completion tokens: 21]
---------- critic ----------
I love the imagery and mood you captured in these lines. The seasonal reference to autumn adds depth to the haiku. Well done! Just a small note: consider the syllable count in the third line, where "Autumn's chill holds sway". It may have one extra syllable. Great work overall!
How about trying to rephrase the last line while maintaining the essence of the haiku, to stay within the 5-7-5 syllable structure?
APPROVE
[Prompt tokens: 72, Completion tokens: 100]
---------- Summary ----------
Number of messages: 3
Finish reason: Text 'APPROVE' mentioned
Total prompt tokens: 102
Total completion tokens: 121
Duration: 62.42 seconds
进程已结束,退出代码为 0
在评判者智能体认可该消息后,对话停止,尽管即使生成了10条消息,对话也可能会停止。
另外,如果我们希望仅在两个条件都满足时才停止运行,我们可以使用与(&)运算符。
多个中止条件代码演示
combined_termination = max_msg_termination & text_termination
多个中止条件完整代码
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination, TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
model_client = OpenAIChatCompletionClient(
model="gpt-4o",
temperature=1,
# api_key="sk-...", # 如果已设置OPENAI_API_KEY环境变量,此步骤可选。
)
# 创建主智能体。
primary_agent = AssistantAgent(
"primary",
model_client=model_client,
system_message="You are a helpful AI assistant.",
)
# 创建评判智能体。
critic_agent = AssistantAgent(
"critic",
model_client=model_client,
system_message="Provide constructive feedback for every message. Respond with 'APPROVE' to when your feedbacks are addressed.",
)
max_msg_termination = MaxMessageTermination(max_messages=10)
text_termination = TextMentionTermination("APPROVE")
combined_termination = max_msg_termination & text_termination
round_robin_team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=combined_termination)
async def main() -> None:
await Console(round_robin_team.run_stream(task="Write a unique, Haiku about the weather in Beijing"), output_stats=True)
asyncio.run(main())
多个中止条件运行结果
---------- user ----------
Write a unique, Haiku about the weather in Beijing
---------- primary ----------
Autumn whispers soft,
Beijing sky in golden hue,
Leaves dance with cool breeze.
[Prompt tokens: 30, Completion tokens: 20]
---------- critic ----------
This is a lovely Haiku that captures the essence of Beijing's autumn weather with imagery and emotion. The use of "whispers soft" adds a gentle and calming tone, while "golden hue" perfectly describes the visual aspect of the season. However, consider using "sky" in a more descriptive way, perhaps by mentioning how it changes with the weather or interacts with the leaves. This could enhance the overall imagery of the poem.
Is there anything else you'd like to refine or focus on regarding this Haiku?
[Prompt tokens: 71, Completion tokens: 104]
---------- primary ----------
Thank you for the feedback. Here's a revised version that incorporates a more descriptive use of "sky":
Autumn whispers soft,
Beijing's sky shifts to deep gold,
Leaves sway with cool breeze.
Let me know if there's anything else you'd like to adjust or explore!
[Prompt tokens: 166, Completion tokens: 56]
---------- critic ----------
This revised version of the Haiku effectively incorporates the more descriptive use of the sky with "shifts to deep gold," which enhances the imagery and creates a vivid picture of the setting. By describing the sky's transformation, you've added depth to the poem that complements the gentle movement of the leaves. Overall, this Haiku now captures the atmosphere of Beijing's autumn beautifully. Excellent work!
APPROVE
[Prompt tokens: 243, Completion tokens: 80]
---------- critic ----------
Your response is gracious and uplifting, and you demonstrate excellent enthusiasm to continue collaborating. Wonderful job expressing positivity and willingness to create more!
APPROVE
[Prompt tokens: 474, Completion tokens: 29]
---------- primary ----------
Thank you so much for the positive feedback! I'm here to help and create with you anytime. If you have more ideas, questions, or projects, just reach out. Happy to be on this creative journey with you! 😊
[Prompt tokens: 494, Completion tokens: 45]
---------- Summary ----------
Number of messages: 10
Finish reason: Text 'APPROVE' mentioned, Maximum number of messages 10 reached, current message count: 10
Total prompt tokens: 2580
Total completion tokens: 461
Duration: 132.83 seconds
进程已结束,退出代码为 0
说明
如果大家在运行上述代码的时候有AutoGen相关的提示或报错(例如:该参数不存在,没有此类方法等),请尝试更新一下AutoGen,博主在分享这篇博文的时候AutoGen的版本是0.4.5稳定版。
安装或更新命令
pip install -U "autogen-agentchat" "autogen-ext[openai,azure]"
另外大家要根据业务需求设置使用的LLM,不一定要按照我给大家分享代码中的设置来,如果只是为了测试并看运行结果可直接复制粘贴代码。
结束
好了,以上就是本次分享的全部内容,大家理解了吗?没关系,博主再帮助大家分析下今天的内容。
在微软AutoGen框架中,“Termination” 指的是终止机制或终止条件,用于决定对话或任务何时结束。
终止条件的类型
- 基于消息数量:可以设定当生成或交互的消息达到一定数量时,对话或任务自动终止。比如设置为生成 10 条消息后就停止,这在控制对话长度或限定任务执行步数等场景中很有用,能避免无限制的交互或处理,提高效率和资源利用率。
- 基于特定条件满足:例如当特定的智能体(如评判者智能体)对生成的消息表示认可时,就满足了终止条件。这意味着只有当消息内容达到了特定的质量标准或符合特定要求,对话才会停止,有助于保证输出结果的质量和准确性。
- 基于时间限制:设置一个时间阈值,当对话或任务执行的时间超过这个阈值时,无论当前进展如何,都将终止。这可以防止因某些原因导致的程序长时间运行,避免资源被过度占用,同时也能提高系统的响应速度和稳定性。
终止机制的作用
- 资源管理:合理的终止条件能有效控制计算资源的使用,避免因无限循环或长时间运行导致的资源耗尽问题,确保系统能够稳定、高效地运行,同时可以提高系统的并发处理能力,让系统能够同时处理多个任务或对话。
- 结果控制:通过设定明确的终止条件,可以确保生成的结果在一定的质量和范围之内,避免生成不相关或无意义的内容,提高任务执行的准确性和可靠性,使输出结果更符合用户的预期和需求。
- 提高效率:及时终止不必要的对话或任务,能够节省时间和计算资源,让系统更快地响应用户的请求,提高整个系统的运行效率和用户体验。
到这里,我想大家应该有很深的理解了吧,如果还没有理解,博主建议大家可以结合上面的完整代码在本地电脑再次进行调试,只要经过多测调试后,我相信大家肯定会理解这部分内容的。请大家多去大胆尝试和使用,博主也希望本次分享能够对大家有所帮助。如果大家对博主分享的内容感兴趣或有帮助,请点赞和关注,博主会持续更新关于微软AutoGen更多和更有趣的内容。