AutoGen 多智能体实战:订单 + 物流查询案例

张开发
2026/4/21 19:49:44 15 分钟阅读

分享文章

AutoGen 多智能体实战:订单 + 物流查询案例
1. 案例组件使用情况总结组件名称对应代码位置备注UserProxyAgentself.user_proxy用户代理 / 用户替身。它是系统的 “手脚”负责发起任务、真实执行工具调用并接收工具返回结果。AssistantAgentself.order_agent 、logistics_agent、summary_agent业务智能体 / 大脑。定义了 3 个核心角色1.订单专员调用订单查询工具2.物流专员调用物流查询工具3.客服主管汇总智能体汇总结果生成最终回复GroupChatself.groupchat智能体容器 / 会议室。存储所有参与的智能体列表同时维护对话历史上下文保证多智能体能在同一语境下协作。GroupChatManagerself.manager流程调度器 / 会议主持人。负责发言顺序、驱动流程运转会依据对话上下文智能决定下一位发言人。控制对话轮次防止流程卡死。initiate_chat()self.user_proxy.initiate_chat对话启动入口。由UserProxyAgent调用作为整个多智能体流程的启动按钮下达任务指令。整体流程启动脚本↓UserProxyAgent用户代理发起任务↓GroupChatManager会议主持人调度群组对话↓GroupChat会议室↓AssistantAgent智能体分工思考 → 决定调用工具↓UserProxyAgent真正执行调用工具↓结果返回 → 汇总 → 输出最终回复2. AutoGen完整案例 AutoGen 多智能体案例订单 物流查询 核心流程用户发起查询 → 订单智能体 → 物流智能体 → 主管汇总 → 输出结果 importautogenfromautogen.agentchatimportregister_function# LLM 模型配置 LLM_CONFIG{config_list:[{model:your-model-name,# 大模型名称api_key:your-api-key,# API密钥base_url:your-base-url,# 接口地址}],temperature:0.1,}# 业务工具类模拟接口 classOrderTools:订单查询工具_order_db{ORD001:{status:待发货,amount:199}}defget_order_info(self,order_id):infoself._order_db.get(order_id)ifinfoisNone:returnf订单{order_id}不存在returnf订单{order_id}状态{info[status]}金额{info[amount]}元classLogisticsTools:物流查询工具_logistics_db{ORD001:{status:揽收完成,carrier:顺丰}}defget_logistics_info(self,order_id):infoself._logistics_db.get(order_id)ifinfoisNone:returnf物流信息{order_id}不存在returnf物流{order_id}状态{info[status]}承运商{info[carrier]}# AutoGen 多智能体核心类 classAutoGenOrderAgent:def__init__(self):# ------------------------------------------------------ 【用户代理】user_proxy 组件人类替身负责发起任务、执行工具、终止对话 # ------------------------------------------------------self.user_proxyautogen.UserProxyAgent(name用户代理,# 智能体名称human_input_modeNEVER,# 全自动执行无需人工输入max_consecutive_auto_reply5,# 最大连续自动回复次数is_termination_msglambdax:any(# 明确的终止标志只要含 问题已解决..字样。kwinx.get(content,)forkwin[问题已解决,结束,完成]),code_execution_config{use_docker:False},# 关闭Docker避免报错)# ------------------------------------------------------ 【助手智能体】AssistantAgent 组件分工处理订单/物流/汇总任务 # ------------------------------------------------------# 订单查询智能体self.order_agentautogen.AssistantAgent(name订单专员,system_message你负责调用订单查询工具返回订单信息,llm_configLLM_CONFIG,)# 物流查询智能体self.logistics_agentautogen.AssistantAgent(name物流专员,system_message你负责调用物流查询工具返回物流信息,llm_configLLM_CONFIG,)# 汇总智能体self.summary_agentautogen.AssistantAgent(name客服主管,system_message汇总订单和物流信息生成友好的最终回复。回复末尾必须包含问题已解决、结束、完成,# 必须告知“终止字符”否则多智能体会无限对话直到触达 max_turns/max_round 硬性截断浪费 token 且答案可能不完整llm_configLLM_CONFIG,)# ------------------------------------------------------ 【工具注册】绑定智能体与业务工具函数 # ------------------------------------------------------# 在 __init__ 中一次性创建工具实例避免重复创建self.order_toolsOrderTools()self.logistics_toolsLogisticsTools()# 包装工具函数defget_order_info(order_id:str)-str:returnself.order_tools.get_order_info(order_id)defget_logistics_info(order_id:str)-str:returnself.logistics_tools.get_logistics_info(order_id)# 使用 register_function 将工具函数同时注册到调用者和执行者register_function(get_order_info,# 工具函数callerself.order_agent,# 调用者订单智能体executorself.user_proxy,# 执行者用户代理description根据订单ID查询订单信息,# 工具描述)register_function(get_logistics_info,# 工具函数callerself.logistics_agent,# 调用者物流智能体executorself.user_proxy,# 执行者用户代理description根据订单ID查询物流信息,# 工具描述)# ------------------------------------------------------ 【群组聊天】GroupChat 组件定义参与智能体 协作规则 # ------------------------------------------------------self.groupchatautogen.GroupChat(agents[self.order_agent,# 订单查询智能体self.logistics_agent,# 物流查询智能体self.summary_agent,# 汇总智能体self.user_proxy,# 用户代理执行工具],messages[],# 对话历史初始为空max_round10,# 最大对话轮次speaker_selection_methodauto,# 自动选择下一个发言人allow_repeat_speakerTrue,# 允许同一智能体连续发言避免流程卡死)# ------------------------------------------------------ 【聊天管理器】GroupChatManager 组件控制多智能体发言顺序 # ------------------------------------------------------self.managerautogen.GroupChatManager(groupchatself.groupchat,# 绑定群组llm_configLLM_CONFIG,# 绑定大模型配置)# 执行查询任务defrun(self,order_idORD001):# ------------------------------------------------------ 【启动多智能体对话】 user_proxy.initiate_chat # ------------------------------------------------------# 任务指令task_msgf请查询订单{order_id}先查订单、再查物流最后客服主管汇总回复chat_resultself.user_proxy.initiate_chat(recipientself.manager,# 接收方群组管理器messagetask_msg,# 任务指令max_turns10,# 最大对话轮次summary_methodlast_msg,# 指定 summary 取最后一条消息一般最后一条消息就是汇总的最终结果也可以按照官方推荐的获取最终结果写法)# 返回最终汇总结果returnchat_result# 启动运行 if__name____main__:resultAutoGenOrderAgent().run(order_idORD001)# 打印最终结果print(\n*50)print( 会话ID方便以后做日志追踪、查历史:)print(result.chat_id)print( 智能体汇总回复)print(result.summary)print(*50)

更多文章