【Agent】OpenManus-Prompt组件详细分析

1. 提示词架构概述

OpenManus 的提示词组件采用了模块化设计,为不同类型的智能体提供专门的提示词模板。每个提示词模块通常包含两种核心提示词:系统提示词(System Prompt)和下一步提示词(Next Step Prompt)。这种设计使得提示词可以独立于智能体代码进行管理和优化,同时保持了提示词与智能体之间的紧密集成。

2. 提示词类型与设计

2.1 系统提示词 (System Prompt)

设计特点

  • 定义智能体的角色、能力和行为边界
  • 设置智能体的整体行为模式和交互风格
  • 通常较为简洁,专注于角色定义
  • 在智能体初始化时设置,整个会话期间保持不变

使用场景

  • 在智能体初始化时设置基础行为模式
  • 在 LLM 调用时作为系统消息传递

2.2 下一步提示词 (Next Step Prompt)

设计特点

  • 指导智能体在每个步骤中的决策和行动
  • 提供可用工具的详细说明和使用指南
  • 通常较为详细,包含具体指令和约束
  • 在每个思考步骤中使用,可以动态更新

使用场景

  • 在每个 think 方法调用前添加到消息历史
  • 引导智能体选择合适的工具和行动
  • 提供上下文信息和决策指南

2.3 模板提示词 (Template Prompt)

设计特点

  • 包含占位符,可以在运行时动态填充
  • 支持格式化字符串语法
  • 适用于需要动态内容的场景

使用场景

  • SWE 智能体中的工作目录和文件信息
  • 规划流程中的步骤执行提示

3. 提示词调用流程

3.1 智能体初始化

python 复制代码
# 在智能体类定义中设置提示词
class Manus(ToolCallAgent):
    system_prompt: str = SYSTEM_PROMPT
    next_step_prompt: str = NEXT_STEP_PROMPT
  1. 提示词常量从相应模块导入
  2. 在智能体类定义中设置为类属性
  3. 可以在子类中覆盖或扩展

3.2 思考过程中的提示词使用

python 复制代码
# ToolCallAgent.think 方法中的提示词使用
async def think(self) -> bool:
    if self.next_step_prompt:
        user_msg = Message.user_message(self.next_step_prompt)
        self.messages += [user_msg]
    
    response = await self.llm.ask_tool(
        messages=self.messages,
        system_msgs=[Message.system_message(self.system_prompt)]
        if self.system_prompt
        else None,
        tools=self.available_tools.to_params(),
        tool_choice=self.tool_choices,
    )
  1. 如果存在下一步提示词,创建用户消息并添加到消息历史
  2. 调用 LLM 时,将系统提示词作为系统消息传递
  3. 同时传递工具参数和工具选择模式

3.3 动态提示词处理

python 复制代码
# SWEAgent.think 方法中的动态提示词处理
async def think(self) -> bool:
    # Update working directory
    self.working_dir = await self.bash.execute("pwd")
    self.next_step_prompt = self.next_step_prompt.format(
        current_dir=self.working_dir
    )
    
    return await super().think()
  1. 获取当前工作目录
  2. 使用 format 方法填充提示词模板中的占位符
  3. 调用父类的 think 方法继续处理

3.4 规划流程中的提示词使用

python 复制代码
# PlanningFlow._create_initial_plan 方法中的提示词使用
async def _create_initial_plan(self, request: str) -> None:
    # Create a system message for plan creation
    system_message = Message.system_message(
        "You are a planning assistant. Create a concise, actionable plan with clear steps. "
        "Focus on key milestones rather than detailed sub-steps. "
        "Optimize for clarity and efficiency."
    )
    
    # Create a user message with the request
    user_message = Message.user_message(
        f"Create a reasonable plan with clear steps to accomplish the task: {request}"
    )
    
    # Call LLM with PlanningTool
    response = await self.llm.ask_tool(
        messages=[user_message],
        system_msgs=[system_message],
        tools=[self.planning_tool.to_param()],
        tool_choice=ToolChoice.REQUIRED,
    )
  1. 创建特定于任务的系统消息
  2. 创建包含用户请求的用户消息
  3. 调用 LLM 时传递这些消息和工具参数

4. LLM 接口中的提示词处理

4.1 消息格式化

python 复制代码
# LLM.ask 方法中的消息处理
async def ask(
    self,
    messages: List[Union[dict, Message]],
    system_msgs: Optional[List[Union[dict, Message]]] = None,
    stream: bool = True,
    temperature: Optional[float] = None,
) -> str:
    # Format system and user messages
    if system_msgs:
        system_msgs = self.format_messages(system_msgs)
        messages = system_msgs + self.format_messages(messages)
    else:
        messages = self.format_messages(messages)
  1. 接收消息和系统消息作为参数
  2. 使用 format_messages 方法将消息转换为标准格式
  3. 将系统消息添加到消息列表的开头

4.2 工具调用提示词处理

python 复制代码
# LLM.ask_tool 方法中的工具提示词处理
async def ask_tool(
    self,
    messages: List[Union[dict, Message]],
    system_msgs: Optional[List[Union[dict, Message]]] = None,
    timeout: int = 300,
    tools: Optional[List[dict]] = None,
    tool_choice: TOOL_CHOICE_TYPE = ToolChoice.AUTO,
    temperature: Optional[float] = None,
    **kwargs,
):
    # 类似的消息处理逻辑
    # 加上工具参数和工具选择模式
    if system_msgs:
        system_msgs = self.format_messages(system_msgs)
        messages = system_msgs + self.format_messages(messages)
    else:
        messages = self.format_messages(messages)
  1. 与 ask 方法类似的消息处理
  2. 额外传递工具参数和工具选择模式
  3. 支持超时和温度等参数

OpenManus 的提示词组件设计了一个灵活、模块化的提示词。通过将提示词与代码分离,同时保持紧密集成,它实现了提示词的可维护性和可扩展性。系统提示词和下一步提示词的组合,加上动态模板能力,使智能体能够适应各种任务和环境,同时保持一致的行为模式和交互风格。

相关推荐
天天向上杰15 分钟前
地基Prompt提示常用方式
人工智能·prompt·提示词
zhongken25935 分钟前
AI智能混剪工具:AnKo打造高效创作的利器!
人工智能·ai·ai编程·ai网站·ai工具·ai软件·ai平台
风华浪浪2 小时前
提示词工程(Prompt Engineering)
人工智能·prompt
Elastic 中国社区官方博客3 小时前
拆解 “ES 已死“ 伪命题:Agentic RAG 时代搜索引擎的终极形态
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
雷渊3 小时前
java版本管理工具-jenv
后端·架构
我爱鸿蒙开发4 小时前
一文带你深入了解Stage模型
前端·架构·harmonyos
一个处女座的程序猿O(∩_∩)O4 小时前
DeepSeek与人工智能:技术演进、架构解析与未来展望
人工智能·架构
JinSo4 小时前
国际化探索:提升开发体验与灵活性
前端·javascript·架构
奔袭的算法工程师4 小时前
TI的Doppler-Azimuth架构(TI文档)
人工智能·深度学习·目标检测·架构·自动驾驶
LTPP4 小时前
Hyperlane:基于Rust的高性能Web框架,QPS突破32万!🚀
后端·面试·架构