阿里妹导读
本文描述了关于智能体(Agents)和多智能体系统(Multi-Agent Systems, MAS)的详尽介绍,涵盖了从定义、分类到具体实现框架的多个方面。
一、介绍
1.1 什么是智能体
关于智能体的定义比较多,这里结合下anthropic与Google关于Agent的定义:
智能体(Agent)是可以感知和理解环境并使用工具来实现目标的应用程序。
从架构上,可以将智能体系统分为两类:
工作流系统(Workflows) - 人做整体规划的决策,LLM是链路的一个节点
LLM和各类工具通过预定义的代码路径进行编排
提供可预测性和一致性
适用于明确定义的任务
2. 智能体系统(Agents) - LLM做决策,决定任务要怎么做
LLM能够动态指导自己的过程和工具使用
保持对任务完成方式的控制
适用于需要灵活性和模型驱动决策的场景
两者的主要区别:
特征 | 工作流系统 | 智能体系统 |
执行路径 | 预定义、固定 | 动态、灵活 |
决策方式 | 基于规则 | 基于LLM推理 |
确定性 | 高 | 相对较低 |
可预测性 | 强 | 相对较弱 |
适应性 | 低 | 高 |
复杂度 | 相对简单 | 相对复杂 |
维护成本 | 较低 | 较高 |
应用场景 | 明确、重复性任务 | 不确定、创造性任务 |
在设计到智能体系统的开发实现时,可以考虑遵循的策略:
简单优先:从最简单的解决方案开始,根据需要增加复杂度,避免过度工程 渐进式发展:先优化单一LLM调用,添加检索和上下文示例 必要时使用智能体系统: 从Workflow到Agents,Workflow为明确定义的任务提供可预测性和一致性,而当大规模需要灵活性和模型驱动的决策时,可以考虑Agents。
1.2 为什么需要多智能体
智能体可用的工具过多,在决定下一步调用哪个工具时效果不佳 上下文过多对于单个智能体来说过于复杂,难以跟踪 系统中需要多个专业领域(如规划器、研究员、数学专家等) ...
模块化:独立的智能体使开发、测试和维护变得更容易; 专业化:可以创建专注于特定领域的专家智能体,这有助于提高整个系统的性能。 控制:可以明确控制智能体之间的通信方式(而不是依赖于函数调用)。
二、多智能体框架
|
|
| ||
|
|
| ||
|
|
| ||
|
|
| ||
|
|
|
Swarm
设计原则
轻量级和可扩展性:通过几个核心的概念抽象,来实现智能体的协调和执行,使其轻量、高度可控且易于测试。
代理(Agent):代理是Swarm框架中的基本单元,它封装了一组指令和工具,并可以选择将对话移交给另一个代理。代理可以被看作是一个独立的实体,负责执行特定的任务。 交接(Handoff):交接是指代理之间转移执行控制权的过程。当一个代理完成其任务后,它可以将控制权移交给另一个代理,以便继续执行任务。 例程(Routines):Swarm框架通过例程来定义代理的行为模式。例程是代理在处理任务时应遵循的步骤,它们允许代理通过一系列预定义的指令来响应特定的输入或情况。 工具调用(Tool Calls):Swarm允许代理调用Python函数,并支持通过函数返回值更新上下文变量和进行代理交接,这增加了框架的灵活性和可定制性。
高度可定制性:Swarm的设计允许开发者根据需要定制智能体的行为和交互。 教育目的:Swarm主要用于教育和展示多智能体协调的概念,而不是用于生产环境。 无状态性:Swarm完全由Chat Completions API驱动,因此在调用之间不保存状态。
from swarm import Swarm
# 初始化Swarm客户端:
client = Swarm()
def transfer_to_agent_b():
return agent_b
# 智能体(Agent)是Swarm中的核心构建块,它封装了一组指令和函数,
# 并且可以在任何时候选择将对话交接给另一个智能体
agent_a = Agent(
name="Agent A",
instructions="You are a helpful agent.",
functions=[transfer_to_agent_b],
)
agent_b = Agent(
name="Agent B",
instructions="Only speak in Haikus.",
)
# 运行智能体:这个方法类似于Chat Completions API中的chat.completions.create()函数,
# 它接受messages并返回messages,并且在调用之间不保存状态。
response = client.run(
agent=agent_a,
messages=[{"role": "user", "content": "I want to talk to agent B."}],
)
print(response.messages[-1]["content"])
from swarm import Swarm, Agent
from swarm.types import Result
# 初始化Swarm客户端:
client = Swarm()
sales_agent = Agent(name="Sales Agent")
def talk_to_sales():
print("Hello, World!")
return Result(
value="Done",
agent=sales_agent,
context_variables={"department": "sales"}
)
agent = Agent(functions=[talk_to_sales])
response = client.run(
agent=agent,
messages=[{"role": "user", "content": "Transfer me to sales"}],
context_variables={"user_name": "John"}
)
print(response.agent.name)
print(response.context_variables)
[2024-11-26 20:51:30] Getting chat completion for...: [{'role': 'system', 'content': 'You are a helpful agent.'}, {'role': 'user', 'content': 'Transfer me to sales'}]
[2024-11-26 20:51:31] Received completion: ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_zuGIzKeyEVkJ2pAsLrqOec1o', function=Function(arguments='{}', name='talk_to_sales'), type='function')], refusal=None)
[2024-11-26 20:51:31] Processing tool call: talk_to_sales with arguments {}
Hello, World!
[2024-11-26 20:51:31] Getting chat completion for...: [{'role': 'system', 'content': 'You are a helpful agent.'}, {'role': 'user', 'content': 'Transfer me to sales'}, {'content': None, 'role': 'assistant', 'function_call': None, 'tool_calls': [{'id': 'call_zuGIzKeyEVkJ2pAsLrqOec1o', 'function': {'arguments': '{}', 'name': 'talk_to_sales'}, 'type': 'function'}], 'refusal': None, 'sender': 'Agent'}, {'role': 'tool', 'tool_call_id': 'call_zuGIzKeyEVkJ2pAsLrqOec1o', 'tool_name': 'talk_to_sales', 'content': 'Done'}]
[2024-11-26 20:51:32] Received completion: ChatCompletionMessage(content="I've transferred you to the sales team. They should be able to assist you shortly!", role='assistant', function_call=None, tool_calls=None, refusal=None)
[2024-11-26 20:51:32] Ending turn.
Sales Agent
{'user_name': 'John', 'department': 'sales'}
stream = client.run(agent, messages,stream=True)
for chunk in stream:
print(chunk)
basic:基础示例,包括设置、函数调用、交接和上下文变量等基本要素。 triage_agent:设置基本的分诊步骤以将任务交接给正确的智能体的简单示例。 weather_agent:函数调用的简单示例。 airline:处理航空公司不同客户服务请求的多智能体设置。 support_bot:包括用户界面智能体和帮助中心智能体以及多个工具的客户服务机器人。 personal_shopper:可以帮助处理销售和退款订单的个人购物智能体。
核心概念
函数(Functions)
def transfer_to_sales():
return sales_agent
时序图
同一时间只有一个Agent在提供服务,这个Agent是激活状态的Agent; 通过持续的问答形式帮助用户解决对应的问题;适合和用户交互比较强的场景;
总结
Magentic-One
核心特性
智能代理协调:通过 LedgerOrchestrator 实现多个 AI 代理之间的智能协调 动态任务规划:支持任务执行过程中的动态规划和调整 灵活的代码执行:集成 Docker 环境,支持安全的代码执行
核心组件
LedgerOrchestrator(账本编排器)
负责协调多个代理的工作 跟踪任务进度和状态 动态分配下一个行动代理 处理任务重规划和失败恢复
代理系统(Agents)
支持多种专门化代理 每个代理都有特定的职责和能力 可以进行代理间的消息传递和协作
代码执行器(Code Executor)
基于 Docker 的安全代码执行环境 支持多种编程语言 提供隔离的执行环境
核心流程
记录任务进展
判断是否需要重新规划
检测任务是否陷入循环
决定下一个执行的智能体
编排过程:
初始化时创建任务计划
每次收到广播消息后更新 ledger 状态
根据 ledger(账本) 状态决定:
是否任务完成
是否需要重新规划
选择下一个合适的智能体
RequestReplyMessage:触发智能体调用大模型生成回复,当针对某个智能体发生消息时会使用这个。 BroadcastMessage:智能体产生回复后,会发生广播消息,Orchestrator收到广播消息后,会进行任务编排。更新 ledger、评估任务状态、选择下一个智能体。普通智能体:将消息加入对话历史 ResetMessage:清空智能体的对话历史 DeactivateMessage:将智能体置于非激活状态,非激活状态下忽略所有incoming消息。
初始化阶段 (_initialize_task):
收集任务相关事实
基于团队组成创建执行计划
向所有代理广播综合信息
任务执行循环 (_select_next_agent):
通过 ledger 跟踪任务状态
决定下一步行动
处理三种主要情况:
任务完成
任务停滞需要重新规划
继续正常执行
状态监控 (update_ledger):
跟踪任务是否完成
检测是否陷入循环
评估是否在取得进展
决定下一个发言的代理
生成下一步指令
重新规划机制 (_update_facts_and_plan):
在任务停滞时触发 更新已知事实 制定新的执行计划 重置对话历史 向所有代理广播新计划
最终答案生成 (_prepare_final_answer):
在任务完成时触发
总结整个对话过程
生成针对原始请求的最终答案
max_rounds: 最大执行轮数(默认20轮)
max_stalls_before_replan: 触发重新规划前的最大停滞次数(默认3次)
max_replans: 最大重新规划次数(默认3次)
return_final_answer: 是否生成最终答案总结
基本使用
import asyncio
from autogen_magentic_one import LedgerOrchestrator, DockerCommandLineCodeExecutor
async def main():
# 初始化代码执行器
async with DockerCommandLineCodeExecutor(work_dir="./logs") as code_executor:
# 创建编排器
orchestrator = LedgerOrchestrator(
agents=[agent1, agent2], # 配置你的代理
model_client=your_model_client # 配置你的模型客户端
)
# 执行任务
result = await orchestrator.run("你的任务描述")
# 运行主程序
asyncio.run(main())
可以创建专门的代理来处理特定任务
通过日志系统监控任务执行状态
支持任务失败后的自动重试和重规划
运行效果
# Specify logs directory
python examples/example.py --logs_dir ./logs
# Enable human-in-the-loop mode
python examples/example.py --logs_dir ./logs --hil_mode
# Save screenshots of browser
python examples/example.py --logs_dir ./logs --save_screenshots
--hil_mode模式的作用
{"timestamp": "2025-01-06T11:15:12.921838", "source": "UserProxy", "message": "你好啊,你能做什么?", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:15.949531", "prompt_tokens": 323, "completion_tokens": 113, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:17.452977", "prompt_tokens": 654, "completion_tokens": 74, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:17.453443", "source": "Orchestrator (thought)", "message": "Initial plan:\n\nWe are working to address the following user request:\n\n你好啊,你能做什么?\n\n\nTo answer this request we have assembled the following team:\n\nWebSurfer: A helpful assistant with access to a web browser. Ask them to perform web searches, open pages, and interact with content (e.g., clicking links, scrolling the viewport, etc., filling in form fields, etc.) It can also summarize the entire page, or answer questions based on the content of the page. It can also be asked to sleep and wait for pages to load, in cases where the pages seem to be taking a while to load.\nCoder: A helpful and general-purpose AI assistant that has strong language skills, Python skills, and Linux command line skills.\nExecutor: A agent for executing code\nfile_surfer: An agent that can handle local files.\n\n\n\nHere is an initial fact sheet to consider:\n\n1. GIVEN OR VERIFIED FACTS\n - The request is written in Chinese, indicating the user likely speaks Chinese.\n\n2. FACTS TO LOOK UP\n - None. The question is straightforward and does not require external information.\n\n3. FACTS TO DERIVE\n - The possible capabilities of the assistant, which include providing information, answering questions, assisting with problem-solving, and other general conversational or task-related capabilities.\n\n4. EDUCATED GUESSES\n - The user is asking about the abilities or functions available through interaction with the assistant.\n\n\n\n\nHere is the plan to follow as best as possible:\n\n- Respond directly to the user's query in Chinese, outlining the capabilities of the assistant.\n- Highlight the main functions, including answering questions, providing information, assisting with problem-solving, and offering conversational support.\n- Make sure the response is concise and easy to understand.\n\nNote: There is no requirement for web searches, code execution, or local file handling for this request.\n", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:21.726491", "prompt_tokens": 961, "completion_tokens": 236, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:21.726640", "source": "Orchestrator (thought)", "message": "Updated Ledger:\n{\n \"is_request_satisfied\": {\n \"reason\": \"The user asked what the assistant can do, and the response has not yet been provided.\",\n \"answer\": false\n },\n \"is_in_loop\": {\n \"reason\": \"This is the initial response development, and no repetitive action has been taken.\",\n \"answer\": false\n },\n \"is_progress_being_made\": {\n \"reason\": \"The initial analysis and planning of the user's request have been completed, and the response is yet to be formulated.\",\n \"answer\": true\n },\n \"next_speaker\": {\n \"reason\": \"The next logical step is to utilize the Coder to respond directly to the user's question about the assistant's capabilities.\",\n \"answer\": \"Coder\"\n },\n \"instruction_or_question\": {\n \"reason\": \"The Coder should generate a response in Chinese explaining the capabilities of the assistant as requested by the user.\",\n \"answer\": \"\\u8bf7\\u7528\\u4e2d\\u6587\\u7b80\\u8981\\u63cf\\u8ff0\\u8fd9\\u4f4d\\u52a9\\u624b\\u53ef\\u4ee5\\u505a\\u7684\\u4e8b\\u60c5\\uff0c\\u5305\\u62ec\\u56de\\u7b54\\u95ee\\u9898\\u3001\\u63d0\\u4f9b\\u4fe1\\u606f\\u3001\\u5e2e\\u52a9\\u89e3\\u51b3\\u95ee\\u9898\\u548c\\u63d0\\u4f9b\\u5bf9\\u8bdd\\u652f\\u6301\\u3002\"\n }\n}", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:21.726763", "source": "Orchestrator (-> Coder)", "message": "请用中文简要描述这位助手可以做的事情,包括回答问题、提供信息、帮助解决问题和提供对话支持。", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:21.726922", "source": "Orchestrator (thought)", "message": "Next speaker Coder", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:23.828462", "prompt_tokens": 893, "completion_tokens": 157, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:23.830580", "source": "Coder", "message": "你好!我是一个智能助手,可以帮助你完成很多任务。以下是我的一些功能:\n\n1. **回答问题**:无论是关于特定主题的问题,还是一般性的知识,我都会尽力解答。\n\n2. **提供信息**:我可以为你提供各种信息和建议,比如新闻,天气预报,或者关于某个主题的详细资料。\n\n3. **帮助解决问题**:我可以指导你如何解决技术问题、学习新技能或完成特定任务。\n\n4. **提供对话支持**:如果你需要聊天或有任何想法需要交流,我会随时倾听并回应你的需求。\n\n如果你有具体的需求或问题,请告诉我,我会尽力帮助你! \n\nTERMINATE", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:27.079104", "prompt_tokens": 1161, "completion_tokens": 186, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:27.079653", "source": "Orchestrator (thought)", "message": "Updated Ledger:\n{\n \"is_request_satisfied\": {\n \"reason\": \"The user's question about the assistant's capabilities has been answered with a concise and clear explanation in Chinese.\",\n \"answer\": true\n },\n \"is_in_loop\": {\n \"reason\": \"There is no repetition of requests or responses indicating a loop.\",\n \"answer\": false\n },\n \"is_progress_being_made\": {\n \"reason\": \"The task was completed successfully by providing a detailed response to the user's query.\",\n \"answer\": true\n },\n \"next_speaker\": {\n \"reason\": \"The request has been fully satisfied, so no further action is required.\",\n \"answer\": \"Coder\"\n },\n \"instruction_or_question\": {\n \"reason\": \"No further action is needed as the user's query was successfully addressed.\",\n \"answer\": \"No further instructions needed.\"\n }\n}", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:27.079895", "source": "Orchestrator (thought)", "message": "Request satisfied.", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:29.529069", "prompt_tokens": 679, "completion_tokens": 153, "type": "LLMCallEvent"}
{"timestamp": "2025-01-06T11:15:29.529221", "source": "Orchestrator (final answer)", "message": "\n你好!我是一个智能助手,可以帮助你完成许多任务。以下是我的一些功能:\n\n1. **回答问题**:无论是关于特定主题的问题,还是一般性的知识,我都会尽力解答。\n\n2. **提供信息**:我可以提供各种信息和建议,比如新闻、天气预报或者某个主题的详细资料。\n\n3. **帮助解决问题**:我可以指导你如何解决技术问题、学习新技能或完成特定任务。\n\n4. **提供对话支持**:如果你需要聊天或有任何想法需要交流,我会随时倾听并回应你的需求。\n\n如果你有具体的需求或问题,请随时告诉我,我会尽力帮助你!", "type": "OrchestrationEvent"}
{"timestamp": "2025-01-06T11:15:29.529306", "source": "Orchestrator (termination condition)", "message": "No agent selected.", "type": "OrchestrationEvent"}
python examples/example.py --logs_dir ./
User input ('exit' to quit): 你好啊,你能做什么?
---------------------------------------------------------------------------
[2025-01-06T11:15:12.921838], UserProxy:
你好啊,你能做什么?
---------------------------------------------------------------------------
[2025-01-06T11:15:17.453443], Orchestrator (thought):
Initial plan:
We are working to address the following user request:
你好啊,你能做什么?
To answer this request we have assembled the following team:
WebSurfer: A helpful assistant with access to a web browser. Ask them to perform web searches, open pages, and interact with content (e.g., clicking links, scrolling the viewport, etc., filling in form fields, etc.) It can also summarize the entire page, or answer questions based on the content of the page. It can also be asked to sleep and wait for pages to load, in cases where the pages seem to be taking a while to load.
Coder: A helpful and general-purpose AI assistant that has strong language skills, Python skills, and Linux command line skills.
Executor: A agent for executing code
file_surfer: An agent that can handle local files.
Here is an initial fact sheet to consider:
1. GIVEN OR VERIFIED FACTS
- The request is written in Chinese, indicating the user likely speaks Chinese.
2. FACTS TO LOOK UP
- None. The question is straightforward and does not require external information.
3. FACTS TO DERIVE
- The possible capabilities of the assistant, which include providing information, answering questions, assisting with problem-solving, and other general conversational or task-related capabilities.
4. EDUCATED GUESSES
- The user is asking about the abilities or functions available through interaction with the assistant.
Here is the plan to follow as best as possible:
- Respond directly to the user's query in Chinese, outlining the capabilities of the assistant.
- Highlight the main functions, including answering questions, providing information, assisting with problem-solving, and offering conversational support.
- Make sure the response is concise and easy to understand.
Note: There is no requirement for web searches, code execution, or local file handling for this request.
---------------------------------------------------------------------------
[2025-01-06T11:15:21.726640], Orchestrator (thought):
Updated Ledger:
{
"is_request_satisfied": {
"reason": "The user asked what the assistant can do, and the response has not yet been provided.",
"answer": false
},
"is_in_loop": {
"reason": "This is the initial response development, and no repetitive action has been taken.",
"answer": false
},
"is_progress_being_made": {
"reason": "The initial analysis and planning of the user's request have been completed, and the response is yet to be formulated.",
"answer": true
},
"next_speaker": {
"reason": "The next logical step is to utilize the Coder to respond directly to the user's question about the assistant's capabilities.",
"answer": "Coder"
},
"instruction_or_question": {
"reason": "The Coder should generate a response in Chinese explaining the capabilities of the assistant as requested by the user.",
"answer": "\u8bf7\u7528\u4e2d\u6587\u7b80\u8981\u63cf\u8ff0\u8fd9\u4f4d\u52a9\u624b\u53ef\u4ee5\u505a\u7684\u4e8b\u60c5\uff0c\u5305\u62ec\u56de\u7b54\u95ee\u9898\u3001\u63d0\u4f9b\u4fe1\u606f\u3001\u5e2e\u52a9\u89e3\u51b3\u95ee\u9898\u548c\u63d0\u4f9b\u5bf9\u8bdd\u652f\u6301\u3002"
}
}
---------------------------------------------------------------------------
[2025-01-06T11:15:21.726763], Orchestrator (-> Coder):
请用中文简要描述这位助手可以做的事情,包括回答问题、提供信息、帮助解决问题和提供对话支持。
---------------------------------------------------------------------------
[2025-01-06T11:15:21.726922], Orchestrator (thought):
Next speaker Coder
---------------------------------------------------------------------------
[2025-01-06T11:15:23.830580], Coder:
你好!我是一个智能助手,可以帮助你完成很多任务。以下是我的一些功能:
1. **回答问题**:无论是关于特定主题的问题,还是一般性的知识,我都会尽力解答。
2. **提供信息**:我可以为你提供各种信息和建议,比如新闻,天气预报,或者关于某个主题的详细资料。
3. **帮助解决问题**:我可以指导你如何解决技术问题、学习新技能或完成特定任务。
4. **提供对话支持**:如果你需要聊天或有任何想法需要交流,我会随时倾听并回应你的需求。
如果你有具体的需求或问题,请告诉我,我会尽力帮助你!
TERMINATE
---------------------------------------------------------------------------
[2025-01-06T11:15:27.079653], Orchestrator (thought):
Updated Ledger:
{
"is_request_satisfied": {
"reason": "The user's question about the assistant's capabilities has been answered with a concise and clear explanation in Chinese.",
"answer": true
},
"is_in_loop": {
"reason": "There is no repetition of requests or responses indicating a loop.",
"answer": false
},
"is_progress_being_made": {
"reason": "The task was completed successfully by providing a detailed response to the user's query.",
"answer": true
},
"next_speaker": {
"reason": "The request has been fully satisfied, so no further action is required.",
"answer": "Coder"
},
"instruction_or_question": {
"reason": "No further action is needed as the user's query was successfully addressed.",
"answer": "No further instructions needed."
}
}
---------------------------------------------------------------------------
[2025-01-06T11:15:27.079895], Orchestrator (thought):
Request satisfied.
---------------------------------------------------------------------------
[2025-01-06T11:15:29.529221], Orchestrator (final answer):
你好!我是一个智能助手,可以帮助你完成许多任务。以下是我的一些功能:
1. **回答问题**:无论是关于特定主题的问题,还是一般性的知识,我都会尽力解答。
2. **提供信息**:我可以提供各种信息和建议,比如新闻、天气预报或者某个主题的详细资料。
3. **帮助解决问题**:我可以指导你如何解决技术问题、学习新技能或完成特定任务。
4. **提供对话支持**:如果你需要聊天或有任何想法需要交流,我会随时倾听并回应你的需求。
如果你有具体的需求或问题,请随时告诉我,我会尽力帮助你!
---------------------------------------------------------------------------
[2025-01-06T11:15:29.529306], Orchestrator (termination condition):
No agent selected.
内置的提示词
1. 收集事实 (任务初始化阶段)
ORCHESTRATOR_CLOSED_BOOK_PROMPT = """在开始处理请求之前,请先回答以下预调查问题。请记住,你拥有肯·詹宁斯级别的知识储备和门萨级别的解题能力,因此你应该能够从丰富的知识库中获取信息。
这是用户的请求:
{task}
以下是预调查问题:
1. 请列出请求本身中明确给出的具体事实或数据。可能没有任何给定信息。
2. 请列出可能需要查找的事实,以及具体在哪里可以找到这些信息。在某些情况下,请求本身可能会提到权威来源。
3. 请列出可能需要推导的事实(例如,通过逻辑推理、模拟或计算得出)。
4. 请列出从记忆中回忆的事实、直觉判断、合理推测等。
在回答这些问题时,请记住"事实"通常是指具体的名称、日期、统计数据等。你的回答应该使用以下标题:
1. 已知或已验证的事实
2. 需要查找的事实
3. 需要推导的事实
4. 经验推测
请不要在回答中包含任何其他标题或部分。在被要求之前,不要列出下一步计划或行动方案。
"""
2. 创建计划 (任务初始化阶段)-依赖第一步的事实
ORCHESTRATOR_PLAN_PROMPT = """很好。为了处理这个请求,我们组建了以下团队:
{team}
基于团队成员构成以及已知和未知的事实,请制定一个简短的要点计划来处理原始请求。请记住,不需要让所有团队成员都参与进来——某个团队成员的特定专长可能对这个任务并不需要。"""
ORCHESTRATOR_SYNTHESIZE_PROMPT = """
我们正在处理以下用户请求:
{task}
为了回答这个请求,我们组建了以下团队:
{team}
以下是需要考虑的初始事实清单:
{facts}
以下是需要尽可能遵循的计划:
{plan}
"""
3. 创建或更新账本提示词 (选新的Agent时)
ORCHESTRATOR_LEDGER_PROMPT = """
请回顾一下,我们正在处理以下请求:
{task}
并且我们组建了以下团队:
{team}
为了推进请求的处理进度,请回答以下问题,并包含必要的推理过程:
- 请求是否已完全满足?(如果完成则为True,如果原始请求尚未成功且完全解决则为False)
- 我们是否陷入循环,重复相同的请求和/或得到相同的回应?循环可能跨越多个回合,可能包括重复的动作,比如多次向上或向下滚动。
- 我们是否在取得进展?(如果刚开始,或最近的消息在增加价值,则为True。如果最近的消息显示陷入循环,或有重大障碍阻止成功,如无法读取必需文件,则为False)
- 下一个应该由谁发言?(从以下成员中选择:{names})
- 你会给这个团队成员什么指示或问题?(请以直接对话的方式表述,并包含他们可能需要的任何具体信息)
请按照以下架构以纯JSON格式输出答案。JSON对象必须可以按原样解析。不要输出除JSON之外的任何内容,也不要偏离这个架构:
{{
"is_request_satisfied": {{
"reason": string,
"answer": boolean
}},
"is_in_loop": {{
"reason": string,
"answer": boolean
}},
"is_progress_being_made": {{
"reason": string,
"answer": boolean
}},
"next_speaker": {{
"reason": string,
"answer": string (从以下成员中选择: {names})
}},
"instruction_or_question": {{
"reason": string,
"answer": string
}}
}}
"""
4. 更新事实&计划 (任务停滞时)
ORCHESTRATOR_UPDATE_FACTS_PROMPT = """提醒一下,我们正在解决以下任务:
{task}
很明显我们的进展不如预期,但我们可能已经学到了一些新东西。请重写以下事实清单,更新它以包含任何可能有帮助的新发现。示例编辑可以包括(但不限于)添加新的推测,在适当的情况下将经验推测移至已验证事实等。可以更新事实清单的任何部分,并且可以编辑多个部分。这是更新经验推测的绝佳时机,所以请至少添加或更新一个经验推测或直觉判断,并解释你的推理过程。
以下是旧的事实清单:
{facts}
"""
ORCHESTRATOR_UPDATE_PLAN_PROMPT = """请简要解释上次运行中出现了什么问题(失败的根本原因),然后制定一个新计划,该计划包含克服先前挑战的步骤和/或提示,特别要避免重复相同的错误。和之前一样,新计划应该简洁,以要点形式表达,并考虑以下团队构成(不要涉及任何其他外部人员,因为我们无法联系其他人):
{team}
"""
5. 最终结果提示词
ORCHESTRATOR_GET_FINAL_ANSWER = """
我们正在处理以下任务:
{task}
我们已经完成了任务。
上述消息包含了完成任务过程中的对话。
基于收集到的信息,请提供对原始请求的最终答案。
答案应该像是直接对用户说话的方式来表述。
"""
设计参考
通信协议
RequestReplyMessage:触发智能体调用大模型生成回复,当针对某个智能体发生消息时会使用这个。 BroadcastMessage:智能体产生回复后,会发生广播消息,Orchestrator收到广播消息后,会进行任务编排。更新 ledger、评估任务状态、选择下一个智能体。普通智能体:将消息加入对话历史 ResetMessage:清空智能体的对话历史 DeactivateMessage:将智能体置于非激活状态,非激活状态下忽略所有incoming消息。
转交&调用
转交:如果任务自己无法做,把任务转出去; 调用:要Call的智能体一定要有个回应;
任务协作
任务账本
ORCHESTRATOR_PLAN_PROMPT = """很好。为了处理这个请求,我们组建了以下团队:
{team}
基于团队成员构成以及已知和未知的事实,请制定一个简短的要点计划来处理原始请求。请记住,不需要让所有团队成员都参与进来——某个团队成员的特定专长可能对这个任务并不需要。"""
ORCHESTRATOR_SYNTHESIZE_PROMPT = """
我们正在处理以下用户请求:
{task}
为了回答这个请求,我们组建了以下团队:
{team}
以下是需要考虑的初始事实清单:
{facts}
以下是需要尽可能遵循的计划:
{plan}
"""
ORCHESTRATOR_LEDGER_PROMPT = """
请回顾一下,我们正在处理以下请求:
{task}
并且我们组建了以下团队:
{team}
为了推进请求的处理进度,请回答以下问题,并包含必要的推理过程:
- 请求是否已完全满足?(如果完成则为True,如果原始请求尚未成功且完全解决则为False)
- 我们是否陷入循环,重复相同的请求和/或得到相同的回应?循环可能跨越多个回合,可能包括重复的动作,比如多次向上或向下滚动。
- 我们是否在取得进展?(如果刚开始,或最近的消息在增加价值,则为True。如果最近的消息显示陷入循环,或有重大障碍阻止成功,如无法读取必需文件,则为False)
- 下一个应该由谁发言?(从以下成员中选择:{names})
- 你会给这个团队成员什么指示或问题?(请以直接对话的方式表述,并包含他们可能需要的任何具体信息)
请按照以下架构以纯JSON格式输出答案。JSON对象必须可以按原样解析。不要输出除JSON之外的任何内容,也不要偏离这个架构:
{{
"is_request_satisfied": {{
"reason": string,
"answer": boolean
}},
"is_in_loop": {{
"reason": string,
"answer": boolean
}},
"is_progress_being_made": {{
"reason": string,
"answer": boolean
}},
"next_speaker": {{
"reason": string,
"answer": string (从以下成员中选择: {names})
}},
"instruction_or_question": {{
"reason": string,
"answer": string
}}
}}
"""
ReACT形式
你是一个名为 $!{agentName} 的智能助手。
你的主要职责是:$!{agentDescription}
请使用以下格式回答问题。你可以使用这些工具:
$!{toolDescriptions}
回答格式:
Question: 需要回答的问题
Thought: 思考下一步该怎么做
Action: 要执行的动作,必须是可用工具之一
Action Input: 执行动作所需的输入参数
Observation: 执行结果
... (这个 Thought/Action/Action Input/Observation 可以重复多次)
Thought: 现在我知道最终答案了
Final Answer: 给出最终的答案
请记住:
1. 严格按照上述格式回答
2. 每个步骤都要清晰地思考和解释
3. 如果遇到问题及时反馈
4. 确保执行结果准确可靠
任务失败
轮次
超时
重新更新账本
三、应用案例
3.1 内容创作
3.2 交易系统
[{
"content": "分析BTC/USDT的市场情况,评估是否适合做多",
"type": "human"
}, {
"content": "智能体 市场分析师 收到了以下输入: 请分析BTC/USDT的市场技术指标和市场情绪,以评估是否适合做多,形成对未来价格走势的预测。; 经过处理,给出了以下结果:Thought: 通过技术指标和市场情绪分析,结合预测的价格走势,我可以给出是否适合做多的建议。\nFinal Answer: 目前BTC/USDT的技术指标显示相对强势并存在上涨动能,市场情绪总体偏乐观,虽然存在潜在利空消息,但长期看涨观点占主导。预计短期内可能会继续上行,适合考虑做多。然而,应密切关注价格波动中的支撑位和潜在利空消息的影响,以灵活调整交易策略。",
"name": "交易总监",
"role": "assistant",
"type": "chat"
}, {
"content": "智能体 风控专员 收到了以下输入: 请评估BTC/USDT的市场风险,并建议合适的仓位和风控参数。当前市场分析显示技术指标相对强势,有上涨动能,市场情绪偏乐观,短期内可能继续上行。; 经过处理,给出了以下结果:现在我知道最终答案了。\n\nFinal Answer: 对于BTC/USDT的市场风险评估和建议如下:\n\n1. **市场风险评估**:\n - 当前技术指标显示相对强势,并有上涨动能,市场情绪偏乐观,短期内可能继续上行。\n - 应关注波动性、流动性风险和系统性风险的变化。\n\n2. **建议仓位**:\n - 在5000 USDT的资本和中等风险水平下,单笔交易量应适中。\n - 设置适度的止损位置,保持合理的风险收益比。\n\n3. **风控参数设置**:\n - 考虑使用适度追踪止损策略,以保持收益并防止市场逆转造成损失。\n - 设定止损条件和预警阈值,密切观察市场情绪变化和技术指标,灵活调整仓位与止损水平以控制风险。\n\n请根据这些评估和建议,小心决策交易。",
"name": "交易总监",
"role": "assistant",
"type": "chat"
}, {
"content": "智能体 交易总监 收到了以下输入: 根据市场分析师和风控专员的反馈,制定BTC/USDT做多的具体交易计划。计划应包含:1. 合适的进场点位和止损点位;2. 确认的交易量和风险控制措施;3. 根据预测结果调整后的交易整体策略。; 经过处理,给出了以下结果:达到最大迭代次数,停止执行。",
"name": "交易总监",
"role": "assistant",
"type": "chat"
}, {
"content": "智能体 交易总监 收到了以下输入: 根据市场分析师和风控专员的反馈,制定BTC/USDT做多的具体交易计划。计划应包含:1. 合适的进场点位和止损点位;2. 确认的交易量和风险控制措施;3. 根据预测结果调整后的交易整体策略。; 经过处理,给出了以下结果:Question: 根据市场分析师和风控专员的反馈,制定BTC/USDT做多的具体交易计划。计划应包含:1. 合适的进场点位和止损点位;2. 确认的交易量和风险控制措施;3. 根据预测结果调整后的交易整体策略。\nThought: 现在我已经有了详细的交易计划,下一步就是协调执行该计划并确保其顺利实施。\nAction: coordinate_execution\nAction Input: {\"trading_plan\":\"1. 在BTC价格回撤至关键支撑位21,500美元时进场做多。在价格跌破20,000美元时设定止损点位。2. 风险控制措施为每笔交易不超过总资金的3%。3. 短期内持续监控核心技术指标(RSI和MACD),并在目标位锁定利润。\",\"department\":\"交易执行部门\"}\nObservation: 交易执行部门已确认将按照提供的计划执行BTC/USDT的多头交易策略。\nThought: 现在我知道最终答案了\nFinal Answer: 已制定并确认的BTC/USDT做多交易计划包括以下内容:\n1. 进场和止损点位:在价格回撤至21,500美元时进场做多,止损设定在价格跌破20,000美元。\n2. 风险控制措施:每笔交易额不超过总资金的3%。\n3. 策略调整:持续监控技术指标(RSI和MACD),根据市场变化调整策略并在达到目标时锁定利润。\n\n交易执行部门已协调确认将按此计划执行。",
"name": "交易总监",
"role": "assistant",
"type": "chat"
}, {
"content": "根据对话历史和用户请求,可以看出以下几点:\n\n1. **任务状态**:用户请求根据市场情况评估BTC/USDT的做多时机。从对话历史来看,市场分析师和风控专员已经对市场进行分析,并提出了市场观点和风险控制建议。交易计划也已经由交易总监制定并确认,与交易执行部门进行协调,似乎已经进入了执行阶段。因此,用户的初步任务已经在上一轮协作中完成,且交易执行员已经确认执行。\n\n2. **当前任务状态**:从整个任务链来看,交易总监的第一层任务是协调和整合作,不直接进行交易执行。既然整个协作流程已经走了一遍,当前的任务状况应该是监控交易进展以及后续因素的变动。\n\n3. **接下来行动步骤**:由于交易计划已经确认并执行,在这里可以确认任务状态以及下一步的监控安排。\n\n4. **最佳行动方案**:考虑到前面的任务已经分配并且被执行,该任务应被视为已完成。当前智能体(交易总监)不再需要采取其它直接交易相关操作。应该确保计划落实,并可能将阶段性结果说明给合作主体。\n\n鉴于这些分析,最终任务可以标记为完成。用户的请求关于BTC/USDT市场情况的分析和做多评估任务已经完成。交易计划根据市场分析和风控建议制定并确认,交易执行员已开始执行做多操作。此任务的执行和协调圆满结束。",
"name": "交易总监",
"role": "assistant",
"type": "chat"
}]
3.3 六项思考帽
指挥官模式下,分析某个问题: SOP:"蓝帽智能体作为协调者,依次让其他智能体从各自的视角分析问题。首先由白帽提供客观数据,然后红帽表达感受,黑帽分析风险,黄帽评估价值,绿帽提供创新方案,最后由蓝帽整合所有观点做出总结。" 自由协作模式下分析问题:
3.4 客服系统
四、面向未来
在 App 上搭建开箱即用的 AI 助手
企业在移动应用中集成 AI 功能时往往面临开发周期长、技术复杂度高等问题,本方案基于多端低代码开发平台和大模型服务平台,通过构建非侵入式 Copilot 助理,提供意图识别、知识问答等智能服务,实现应用快速、无缝智能化升级,助力企业高效创新。