OpenManus 的提示词组件采用了模块化设计,为不同类型的智能体提供专门的提示词模板。每个提示词模块通常包含两种核心提示词:系统提示词(System Prompt)和下一步提示词(Next Step Prompt)。这种设计使得提示词可以独立于智能体代码进行管理和优化,同时保持了提示词与智能体之间的紧密集成。
设计特点:
使用场景:
设计特点:
使用场景:
think
方法调用前添加到消息历史设计特点:
使用场景:
# 在智能体类定义中设置提示词
class Manus(ToolCallAgent):
system_prompt: str = SYSTEM_PROMPT
next_step_prompt: str = NEXT_STEP_PROMPT
# 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,
)
# 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()
# 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,
)
# 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)
# 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)
OpenManus 的提示词组件设计了一个灵活、模块化的提示词。通过将提示词与代码分离,同时保持紧密集成,它实现了提示词的可维护性和可扩展性。系统提示词和下一步提示词的组合,加上动态模板能力,使智能体能够适应各种任务和环境,同时保持一致的行为模式和交互风格。