从这篇文章开始我们开启一个新的篇章,即 AutoGen 官方教程中的 Examples
章节,如果之前将该系列笔记的所有文章都亲手执行过一次,那么这部分对你而言难度不大,甚至平均水平还没有 Advanced
章节高,所以可以抱着一个比较放松的心态来学习。
Advanced
章节回顾:
- AutoGen学习笔记系列(八)Advanced - Custom Agents
- AutoGen学习笔记系列(九)Advanced - Selector Group Chat
- AutoGen学习笔记系列(十)Advanced - Swarm
- AutoGen学习笔记系列(十一)Advanced - Magentic-One
- AutoGen学习笔记系列(十二)Advanced - Memory
- AutoGen学习笔记系列(十三)Advanced - Logging
- AutoGen学习笔记系列(十四)Advanced - Serializing Components
首先还是对Advanced
章节中的内容进行一个简短的回顾,其主要有以下几个重点:
- 对于自定义的 Agent 其必须对一个属性与两个函数进行重写,同时也介绍了
run()
传入task为空时的处理机制以及理由; SelectorGroupChat
能够将整个Team中的上下文传递给LLM,让LLM来决定与哪个Agent对话,每个Agent都是平等的;Swarm
能够让Team中Agent主动放弃task并将上下文和子任务转移给其他Agent;Magentic-One
是一个很新的子库,其特点在于有一个编排器对task进行拆分,并且专注于开放网络和本地资源,但存在一定风险;ListMemory
提供一个RAG方法,能够在task执行之前传入一些有价值的知识以帮助LLM更有针对性地回答;- 终止条件、Agent、Team三个类型的序列化与反序列化主要作用是传递配置,而非实例,很类似
Tutorial
中的Managing State
;
这篇笔记瞄准的是 Examples
章节中的第一个例子 Travel Planning
,官网链接如下:
- 官网链接:https://microsoft.github.io/autogen/stable/user-guide/agentchat-user-guide/examples/travel-planning.html ;
Travel Planning
该demo主要用 RoundRobinGroupChat
这个类,对应的是 Tutorial
章节中的 Teams
部分,参考博客如下:
整个demo的结构为 planner_agent
调用 local_agent
, language_agent
, travel_summary_agent
这三个来完成旅行规划,总体的控制权在 planner_agent
手上。
- planner_agent:对整个旅行计划进行规划。
planner_agent = AssistantAgent(
"planner_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can plan trips.",
system_message="You are a helpful assistant that can suggest a travel plan for a user based on their request.",
)
- local_agent:提供代选地点。
local_agent = AssistantAgent(
"local_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A local assistant that can suggest local activities or places to visit.",
system_message="You are a helpful assistant that can suggest authentic and interesting local activities or places to visit for a user and can utilize any context information provided.",
)
- language_agent:提供处理目标地点的语言习俗。
language_agent = AssistantAgent(
"language_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can provide language tips for a given destination.",
system_message="You are a helpful assistant that can review travel plans, providing feedback on important/critical tips about how best to address language or communication challenges for the given destination. If the plan already includes language tips, you can mention that the plan is satisfactory, with rationale.",
)
- travel_summary_agent:对整个计划进行总结。
travel_summary_agent = AssistantAgent(
"travel_summary_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can summarize the travel plan.",
system_message="You are a helpful assistant that can take in all of the suggestions and advice from the other agents and provide a detailed final travel plan. You must ensure that the final plan is integrated and complete. YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN. When the plan is complete and all perspectives are integrated, you can respond with TERMINATE.",
)
完整代码如下:
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
import os, asyncio
os.environ["OPENAI_API_KEY"] = "你的OpenAI API Key"
planner_agent = AssistantAgent(
"planner_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can plan trips.",
system_message="You are a helpful assistant that can suggest a travel plan for a user based on their request.",
)
local_agent = AssistantAgent(
"local_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A local assistant that can suggest local activities or places to visit.",
system_message="You are a helpful assistant that can suggest authentic and interesting local activities or places to visit for a user and can utilize any context information provided.",
)
language_agent = AssistantAgent(
"language_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can provide language tips for a given destination.",
system_message="You are a helpful assistant that can review travel plans, providing feedback on important/critical tips about how best to address language or communication challenges for the given destination. If the plan already includes language tips, you can mention that the plan is satisfactory, with rationale.",
)
travel_summary_agent = AssistantAgent(
"travel_summary_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
description="A helpful assistant that can summarize the travel plan.",
system_message="You are a helpful assistant that can take in all of the suggestions and advice from the other agents and provide a detailed final travel plan. You must ensure that the final plan is integrated and complete. YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN. When the plan is complete and all perspectives are integrated, you can respond with TERMINATE.",
)
termination = TextMentionTermination("TERMINATE")
group_chat = RoundRobinGroupChat(
[planner_agent, local_agent, language_agent, travel_summary_agent], termination_condition=termination
)
asyncio.run(
Console(group_chat.run_stream(task="Plan a 3 day trip to Nepal."))
)
运行结果:
$ python demo.py