Contents
Open Contents
基本设计模式
1. ReAct 模式
- 模仿人类解决问题的过程:思考(分析、规划)→ 行动(执行工具调用或环境交互)→ 观察(获取反馈)→ 再思考,循环往复。
- 动态适应不确定性和新信息,避免一次性规划导致的错误。
- 通过“Action”调用外部工具(如
search(Newton's laws)),并接收工具返回的“Observation”。
示例 Prompt 模板:
manual_react = f"""
Question: What profession does Nicholas Ray and Elia Kazan have in common?
Thought: I need to search Nicholas Ray and Elia Kazan, find their professions, then find the profession they have in common.
Action: Search[Nicholas Ray]
Observation: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16, 1979) was an American film director, screenwriter, and actor best known for the 1955 film Rebel Without a Cause.
Thought: Professions of Nicholas Ray are director, screenwriter, and actor. I need to search Elia Kazan next and find his professions.
Action: Search[Elia Kazan]
Observation: Elia Kazan was an American film and theatre director, producer, screenwriter and actor.
Thought: Professions of Elia Kazan are director, producer, screenwriter, and actor. So profession Nicholas Ray and Elia Kazan have in common is director, screenwriter, and actor.
Action: Finish[director, screenwriter, actor]
"""
2. Plan-and-Solve 模式
- 强调先制定完整计划,再按计划执行,适合结构化强、确定性高的任务。
- 规划器(Planner):生成多步计划。
- Replanner:任务执行后根据进展重新规划。
- 执行器(Executor):根据计划调用工具完成任务。
3. Reason without Observation 模式
- 仅依赖预训练知识或给定上下文,不调用外部工具。
- 通过 链式推理(Chain-of-Thought, CoT) 生成答案,类似人类“心算”。
- Planner:生成依赖链式计划。
- Worker:遍历任务并分配变量。
- Solver:整合输出为最终答案。
4. LLMCompiler 模式
- 将自然语言任务编译为可执行工作流,将复杂任务分解为原子操作,通过结构化编排(如 DAG)实现高效执行。
- 强调静态优化和并行化执行。
示例任务分解:
找三篇关于AI安全的论文,总结核心观点并对比异同,最后生成中文报告
分解为:search×3 → summarize×3 → compare → translate
5. Basic Reflection 模式
- 通过模拟师生交互来改进生成内容。
- 学生(Generator) 生成内容,老师(Reflector) 批改并反馈,学生根据反馈修改。
- 强调迭代和反馈提升内容质量。
Prompt 示例:
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are an essay assistant tasked with writing excellent 5-paragraph essays."
" Generate the best essay possible for the user’s request."
" If the user provides critique, respond with a revised version of your previous attempts.",
),
MessagesPlaceholder(variable_name="messages"),
]
)
reflection_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a teacher grading an essay submission. Generate critique and recommendations for the user’s submission."
" Provide detailed recommendations, including requests for length, depth, style, etc.",
),
MessagesPlaceholder(variable_name="messages"),
]
)
reflect = reflection_prompt | llm
6. Reflexion 模式
- Reflexion: Language Agents with Verbal Reinforcement Learning (NeurIPS 2023)
- 在 Basic Reflection 基础上,引入外部数据评估回答准确性,强制生成缺失和多余方面的内容,反思更具建设性。
- 回答前进行反思和批判性思考,回答后再有针对性修改。
7. Language Agent Tree Search 模式
- 基于 蒙特卡罗树搜索(MCTS) 的推理决策框架。
- 以思考序列为节点,使用语言模型评估节点价值,并根据环境反馈更新。
- 具备自我反思功能,可从失败轨迹中学习提升决策能力。
MCP
MCP (Model Context Protocol,模型上下文协议)定义了应用程序和 AI 模型之间交换上下文信息的方式。这使得开发者能够以一致的方式将各种数据源、工具和功能连接到 AI 模型(一个中间协议层),就像 USB-C 让不同设备能够通过相同的接口连接一样。MCP 的目标是创建一个通用标准,使 AI 应用程序的开发和集成变得更加简单和统一。
MCP 三大核心组件
- Host:如 Claude Desktop,负责接收用户提问并与 Claude 模型交互。
- Client:内置于 Host,负责与 MCP Server 建立连接。
- Server:如文件系统 MCP Server,执行实际操作并返回结果。
流程示例:
- 用户提问 → Claude Desktop (Host)
- Claude 模型决定需要文件信息 → MCP Client 连接
- 文件系统 MCP Server 执行操作 → 返回结果
- Claude 生成回答 → 显示在 Claude Desktop
Claude(模型)如何确定使用哪些工具?
- 调用过程分两步:
- 由 LLM(Claude)确定使用哪些 MCP Server。
- 执行对应的 MCP Server 并对结果进行处理。
- 模型通过 prompt 确定可用工具: 将工具使用描述以文本形式传递给模型,模型结合实时情况选择工具。