AI能否像人类一样从错误中学习?反思型Agent系统不仅能生成回答,还会主动审视自己的输出,找出问题并持续改进。
反思策略本质上就是让LLM对自己的行为进行自我批评。有时反思器还会调用外部工具或检索系统来提升批评的准确性。这样一来系统输出的就不再是一次性的回答,而是经过多轮生成-审阅循环优化后的结果。
目前主流的反思系统主要分为三类:
基础Reflection Agent比较轻量,就是简单的生成器加反思器循环。生成器负责起草、反思器负责批评,然后生成器根据反馈进行修订。这种方式在很多编辑类任务中效果不错。
Reflexion Agent更加结构化,会在可追踪的日志中记录历史行为、假设和反思内容。特别适合那些需要从多次失败中汲取经验的问题求解场景。
语言Agent树搜索(LATS)采用搜索策略探索多条行动路径,对结果进行反思,然后裁剪或保留有前景的分支。在规划和多步推理任务中表现最佳。
本文重点讨论前两种:Reflection和Reflexion,并用LangChain与LangGraph来实现完整的工作流程。
基础Reflection Agent的工作原理
Reflection Agent的核心在于两个角色之间的互动:
生成器负责起草初始回答,反思器则审查这个草稿,指出缺陷并提出改进建议。
这种循环会进行几轮,每一轮都让输出变得更加精炼和可靠。AI实际上在实时学习自己的错误,就像作家根据编辑意见反复修改稿件一样。
接下来用LangGraph构建一个LinkedIn帖子生成的Reflection Agent。LangGraph专门用于创建自我改进的AI系统,能够模拟人类的反思思维过程——Agent不会止步于第一稿,而是持续打磨直到内容足够优秀。
这个演示会展示如何设置生成器和反思器角色,使用LangChain进行结构化提示,并通过LangGraph将所有组件编织成一个迭代反馈循环。
动手构建Reflection Agent
先从LinkedIn内容创建Agent入手,实现基础的Reflection模式。流程很直接:Agent起草帖子,独立的"反思器"对其进行评析,然后系统根据反馈修订内容。
环境配置
我们这里按需逐步引入,保持学习流程的清晰度。首先用
.env
文件设置API集成的环境变量:
ANTHROPIC_API_KEY="your-anthropic-api-key"
LANGCHAIN_API_KEY="your-langchain-api-key" # optional
LANGCHAIN_TRACING_V2=True # optional
LANGCHAIN_PROJECT="multi-agent-swarm" # optional
然后将这些加载到notebook中:
from langchain_anthropic import ChatAnthropic
from dotenv import load_dotenv
load_dotenv()
load_dotenv(dotenv_path="../.env", override=True) # mention the .env path
Initialize Anthropic model
llm = ChatAnthropic(
model="claude-3-7-sonnet-latest", # Claude model ID
temperature=0,
# max_tokens=1024
)
这里选择Anthropic的claude-3–7-sonnet-latest作为对话模型。当然也可以换成其他LLM,LangChain支持相当广泛的集成。
更多案例:
newsmth.net/nForum/#!article/PieLove/2920274
att.newsmth.net/nForum/#!article/PieLove/2920274
newsmth.net/nForum/#!article/PieLove/2920273
att.newsmth.net/nForum/#!article/PieLove/2920273
newsmth.net/nForum/#!article/PieLove/2920272
att.newsmth.net/nForum/#!article/PieLove/2920272
newsmth.net/nForum/#!article/PieLove/2920271
att.newsmth.net/nForum/#!article/PieLove/2920271
newsmth.net/nForum/#!article/PieLove/2920270
att.newsmth.net/nForum/#!article/PieLove/2920270
newsmth.net/nForum/#!article/PieLove/2920269
att.newsmth.net/nForum/#!article/PieLove/2920269
newsmth.net/nForum/#!article/PieLove/2920268
att.newsmth.net/nForum/#!article/PieLove/2920268
newsmth.net/nForum/#!article/PieLove/2920267
att.newsmth.net/nForum/#!article/PieLove/2920267
newsmth.net/nForum/#!article/PieLove/2920266
att.newsmth.net/nForum/#!article/PieLove/2920266
newsmth.net/nForum/#!article/PieLove/2920265
att.newsmth.net/nForum/#!article/PieLove/2920265
newsmth.net/nForum/#!article/PieLove/2920264
att.newsmth.net/nForum/#!article/PieLove/2920264
newsmth.net/nForum/#!article/PieLove/2920263
att.newsmth.net/nForum/#!article/PieLove/2920263
newsmth.net/nForum/#!article/PieLove/2920262
att.newsmth.net/nForum/#!article/PieLove/2920262
newsmth.net/nForum/#!article/PieLove/2920261
att.newsmth.net/nForum/#!article/PieLove/2920261
newsmth.net/nForum/#!article/PieLove/2920260