🪞 OpenClaw Agent自我反思

世界上有一种成长叫自我反思,Agent做完一件事,停下来想一想:我做得对吗?能更好吗?...

"这个答案对吗?"我盯着Agent的输出,心里没底。传统Agent只会给答案,不会质疑自己。但会反思的Agent会说:"让我检查一下...哦,我发现一个问题,修正后是..."——这才是靠谱的Agent。

📋 功能介绍

🎯 自我反思的核心价值

反思类型 说明 效果
输出校验 检查输出是否满足要求 减少错误输出
工具反思 评估工具选择是否正确 提高工具准确率
推理验证 检查推理链条是否合理 避免逻辑漏洞
质量评估 评估回答质量 提升输出质量
改进迭代 发现问题并自我修正 持续优化

💡 反思模式

🚀 使用方法

1. ReAct模式配置

# ReAct模式 - 边想边做边反思
agents:
  react-agent:
    name: 反思型助手
    
    # 启用ReAct模式
    reasoning:
      mode: react
      show_thoughts: true  # 显示思考过程
      
    prompt: |
      你是一个会反思的Agent。
      
      遵循 ReAct 模式:
      1. Thought: 思考当前应该做什么
      2. Action: 执行动作(调用工具)
      3. Observation: 观察结果
      4. Reflection: 反思这一步是否正确
      5. 循环直到完成任务
      
      示例:
      Thought: 用户想查天气,我需要调用天气API
      Action: weather_query(city="北京")
      Observation: 返回北京明天气温25度
      Reflection: 获取成功,信息完整,可以回答
      Answer: 北京明天25度,晴朗

2. Reflexion模式配置

# Reflexion模式 - 完成后反思
agents:
  reflexion-agent:
    name: 深度反思助手
    
    reasoning:
      mode: reflexion
      
      # 反思配置
      reflection:
        enabled: true
        max_iterations: 3  # 最多反思3轮
        
        # 反思触发条件
        triggers:
          - output_length < 10   # 输出太短
          - confidence < 0.8     # 置信度低
          - has_error            # 有错误
          
        # 反思存储
        memory:
          enabled: true
          store_failures: true   # 记录失败案例
          store_successes: true  # 记录成功案例

3. 双重检查配置

# 两个Agent交叉验证
agents:
  primary-agent:
    name: 主Agent
    
  checker-agent:
    name: 检查Agent
    
# 工作流
workflows:
  double-check:
    steps:
      - agent: primary-agent
        task: "完成任务"
        
      - agent: checker-agent
        task: |
          检查上一个Agent的输出:
          1. 是否正确?
          2. 是否完整?
          3. 有没有更好的方案?
          
          如果有问题,提供修正建议。
          
      - condition: "{{ checker_feedback.needs_fix }}"
        agent: primary-agent
        task: "根据检查反馈修正输出"

4. 评分反馈配置

# 自我评分
agents:
  scoring-agent:
    name: 评分助手
    
    reasoning:
      self_scoring:
        enabled: true
        
        # 评分维度
        dimensions:
          - name: 准确性
            weight: 0.4
            criteria: "信息是否正确"
            
          - name: 完整性
            weight: 0.3
            criteria: "是否覆盖所有要点"
            
          - name: 清晰度
            weight: 0.3
            criteria: "表达是否清晰"
            
        # 及格线
        passing_score: 0.7
        
        # 不及格时的动作
        on_fail:
          - retry           # 重试
          - ask_for_help    # 求助用户

✨ 最佳实践

💡 反思设计要点
⚠️ 常见问题
📝 ReAct实战案例
# ReAct执行过程示例
用户: 北京和上海明天的天气差多少?

Thought 1: 需要分别查询北京和上海的天气
Action 1: weather_query(city="北京")
Observation 1: 北京明天25度,晴

Thought 2: 获取到北京天气,现在查上海
Action 2: weather_query(city="上海")  
Observation 2: 上海明天28度,多云

Thought 3: 两个城市天气都获取到了
Reflection: 我需要计算温差并给出分析
Answer: 
- 北京明天25度,晴
- 上海明天28度,多云
- 温差:上海比北京高3度
- 建议:两地出行都不需要带伞

💻 代码示例

ReAct模式

from openclaw import Agent

agent = Agent(
    name="reflective-agent",
    reasoning="react"  # 启用ReAct
)

result = agent.run(
    "帮我分析这个产品定价是否合理",
    show_thoughts=True  # 显示思考过程
)

# 输出示例:
# Thought: 需要了解产品定价和竞品信息
# Action: web_search("产品定价")
# Observation: 找到相关定价信息...
# Thought: 现在对比竞品定价
# Reflection: 需要更多竞品数据
# Action: web_search("竞品定价")
# ...
# Answer: 根据分析,定价合理,因为...

Reflexion模式

from openclaw import Agent
from openclaw.reasoning import Reflexion

agent = Agent(
    name="reflexion-agent",
    reasoning=Reflexion(
        max_iterations=3,
        store_memory=True
    )
)

# 第一次尝试
result = agent.run("写一个冒泡排序")

# Agent会自我检查
# Reflection: "这个实现有问题,没有处理边界情况"
# 重试...
# Reflection: "现在正确了"

print(result.content)
print(f"反思次数: {result.reflections}")

自定义反思器

from openclaw.reasoning import Reflector

class CodeReflector(Reflector):
    """代码质量反思器"""
    
    def reflect(self, output: str, context: dict) -> dict:
        issues = []
        
        # 检查代码风格
        if 'TODO' in output:
            issues.append("包含TODO注释,需要完善")
            
        # 检查错误处理
        if 'try' not in output and 'except' not in output:
            issues.append("缺少异常处理")
            
        # 检查测试
        if 'def test_' not in output:
            issues.append("缺少单元测试")
            
        return {
            "needs_fix": len(issues) > 0,
            "issues": issues,
            "score": 1 - len(issues) * 0.2
        }

# 使用自定义反思器
agent = Agent(
    name="code-agent",
    reasoning=CodeReflector()
)

反思记忆利用

# 从历史反思中学习
agent = Agent(
    name="learning-agent",
    reasoning=Reflexion(
        learn_from_reflections=True,
        reflection_memory="./reflections.json"
    )
)

# 查看学习到的经验
agent.show_learned_lessons()
# 输出:
# - 天气查询要同时查多个城市
# - 代码要包含错误处理
# - 数据分析要说明数据来源

# 这些经验会在后续任务中自动应用

🔗 相关链接

📊 反思效果对比

模式 准确率提升 Token成本 适用场景
无反思 - 1x 简单任务
ReAct +15% 1.3x 复杂推理
Reflexion +25% 1.5x 高质量输出
双重检查 +30% 2x 关键任务