告别繁琐攻略,AI小分队一键搞定旅行计划!尼泊尔三日游只是开始,你的下一站由AI决定
概述:AutoGen——AI界的“乐高大师”
AutoGen,这款开源AI智能体框架,简直就是AI界的“乐高大师”!它不仅能帮你轻松搭建事件驱动、分布式、可扩展的智能体应用,还能让你像拼积木一样,快速构建专属AI助手。无论你是AI小白还是技术大牛,AutoGen都能让你玩转AI世界!
核心亮点:AutoGen集成了强大的大型语言模型(LLM),不仅能解决复杂问题,还能帮你搞定日常琐事,甚至让AI之间“聊”得热火朝天,持续学习新技能。简直就是AI界的“社交达人”!
目标:本着“游戏中娱乐,实战中学习”的精神,我们将通过一个超有趣的实战案例——AgentChat旅行计划,带你深入理解AutoGen的架构设计
本着游戏中娱乐,实战中学习的目标, 我们下面我们基于实战案例AgentChat 旅行计划,针对AutoGen的架构设计的知识点进行说明
2.框架架构
在进入实战之前,先来了解一下AutoGen的“三层魔法”架构:
- Core(核心层) :基于Actor模型,支持异步消息传递和事件驱动的工作流。简单来说,就是AI们的“神经系统”。
- AgentChat(高层API) :任务驱动的高层API,抽象了核心层的复杂概念,让你像搭积木一样轻松构建AI应用。
- Extensions(扩展包) :集成了第三方系统(如OpenAI模型客户端和Azure代码执行器),让你的AI应用更加强大。
本着游戏中娱乐,实战中学习的目标, 我们下面我们基于实战案例针对AutoGen的架构设计的知识点进行说明
3. 旅行计划案例实战:AI小分队出击!
AutoGen的模块化设计让它能轻松应对各种复杂场景。比如,内容创作助手和质量评估专家可以联手打造爆款文章,而这一切都得益于大型语言模型(LLM)的加持。几年前,这种设计还叫“组件化”,如今AutoGen已经把它升级到了“智能化、自动化、拟人化”的新高度!
AgentChat:高级API的核心角色:在AutoGen框架中,AgentChat作为高级API,扮演着类似于TensorFlow中Keras的角色。它抽象了底层的复杂性,为开发者提供了简洁易用的接口,使得构建复杂的多智能体协作系统变得更加高效。接下来,我们将通过一个旅行计划系统的实战示例,深入说明AutoGen框架的核心概念。
3.1 实战:旅行计划系统 场景描述
在这个示例中,我们将使用AgentChat构建一个智能旅行计划系统。该系统由多个AI智能体协作完成,每个智能体都有特定的角色,共同生成一份全面的旅行行程。以下是实现这一目标的核心步骤:
多智能体协作的旅行计划系统 : 该系统由四个专门的AI智能体组成,每个智能体都有独特的角色和功能,共同协作生成一份全面且个性化的旅行计划。
3.1.1 智能体角色与功能
-
行程规划智能体(planner_agent) :
- 角色:负责根据用户需求生成初步的旅行计划。
- 功能:基于用户输入(如目的地、时间、预算等),提供详细的行程建议。
- 系统提示:
You are a helpful assistant that can suggest a travel plan for a user based on their request.
-
本地活动推荐智能体(local_agent) :
- 角色:提供目的地本地化的活动和景点推荐。
- 功能:根据目的地信息,推荐真实且有趣的本地活动或景点,并利用上下文信息优化推荐。
- 系统提示:
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) :
- 角色:提供语言和沟通方面的实用建议。
- 功能:审查旅行计划,提供关于如何应对语言或沟通挑战的重要提示。如果计划已包含语言建议,则会评估其合理性。
- 系统提示:
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) :
- 角色:整合所有智能体的建议,生成最终的旅行计划。
- 功能:汇总行程规划、本地活动推荐和语言建议,确保最终计划完整且一致。当计划完成后,输出完整计划并终止流程。
- 系统提示:
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.
3.1.2 Agent协作流程
- 用户输入:用户提供旅行需求(如目的地、时间、预算等)。
- 行程规划:
planner_agent根据用户需求生成初步行程。 - 本地活动推荐:
local_agent基于目的地信息,推荐本地活动和景点。 - 语言支持:
language_agent审查行程,提供语言和沟通方面的建议。 - 计划整合:
travel_summary_agent汇总所有建议,生成最终旅行计划并输出
3.2 实战:旅行计划系统案例 code落地
基于案例的场景描述,该系统核心为
- 系统智能体角色定义
- 智能体协作完成用户任务
3.2.1 系统智能体角色定义
一个完整的智能体包括以下几部分
- 智能体大脑制定:智能体思维决策依赖大模型,model_client 作为脑机接口可以有效接入现有行业内领先的大模型
- 智能体角色设定: 基于大模型的 Prompt: You are a helpful assistant that can suggest a travel plan for a user based on their request
- 智能体说明:A helpful assistant that can plan trips.
下面为具体的代码实现,其他的智能体不在累述,最后会有完整 code
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.",
)
3.2.1 多系统智能体协作
智能体小分队已成立,团队成员已就位,请 RoundRobinGroupChat指示!!!
RoundRobinGroupChat作为旅行计划制定行动的指挥官,负责本次行动的计划制定,任务分配和成员调度,他将成员之间成流水线式协作
- 1.planner_agent 负责根据用户需求生成初步的旅行计划,并交付local_agent
- 2.local_agent 提供目的地本地化的活动和景点推荐,并利用上下文优化,之后交付language_agent进行下一步
- 3.language_agent 提供语言和沟通方面的实用建议,最后交付travel_summary_agent 汇总整个计划
整个计划完成后termination_condition
group_chat = RoundRobinGroupChat(
[planner_agent, local_agent, language_agent, travel_summary_agent], termination_condition=termination
)
开始行动 1.金主芭比 提出要求: 针对加德满都制定未来 3 天的旅游计划
await Console(group_chat.run_stream(task="Plan a 3 day trip to Nepal.并翻译成中文
2.RoundRobinGroupChat 指挥官调度执行智能体成员小分队,流水式协作,完成计划,并基于终端展示计划
中文翻译:尼泊尔三日游语言与沟通建议
你的尼泊尔三日游计划非常完善,涵盖了加德满都、巴德岗和帕坦的主要文化和历史景点。然而,计划中没有明确提到语言或沟通方面的挑战,这对于前往尼泊尔的旅行者来说非常重要。以下是一些关键建议,帮助你在旅途中克服语言障碍,确保沟通顺畅:............
3.4 多智能体旅行计划完整 code
import asyncio
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
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
)
async def main() -> None:
await Console(group_chat.run_stream(task="Plan a 3 day trip to Nepal.并翻译成中文输出"))
if __name__ == "__main__":
asyncio.run(main())
运行程序:旅行计划智能体协作小分队,计划制定完成
4 总结
AutoGen的核心竞争力在于其强大的大型语言模型(LLM),不仅能解决问题,还能让AI之间高效协作,持续学习新技能。通过旅行计划系统的实战案例,我们已经掌握了AutoGen的核心概念:
针对 AutoGen,我们重点了解了以下两个核心概念:
-
智能体:
AssistantAgent,执行特定任务的AI助手。 -
指挥官:
RoundRobinGroupChat,调度多个智能体协作完成任务。
接下来,我们将通过更多实战案例,深入探索AutoGen的潜力。无论是游戏中的娱乐场景,还是实战中的复杂任务,AutoGen都能让你轻松应对!
记住: “游戏中娱乐,实战中学习” ,这才是掌握AutoGen的正确姿势!