当前位置: 首页 > news >正文

[LangChain] 21. LangChain中创建Agent

LangChain Hub

LangChain Hub 是一个托管 Prompt 与 Chain 配置的集中仓库(可以理解成 Hugging Face Hub 的“提示词版”)。

它允许开发者:

  • 上传 / 分享 自己的 prompt、chain 配置;
  • 下游用户可以直接通过一个 key (author/name) 拉取使用;
  • 便于复用、标准化,而不是大家都在项目里复制粘贴 prompt 字符串。

官方定位:“Prompt templates hosting and sharing service”。

核心 API 就两个:

pull(identifier: string):从 Hub 拉取指定资源(prompt 或 chain)。

  • 参数 identifier 的形式通常是:
    • "hwchase17/react"
    • "hwchase17/openai-tools-agent"
    • "org_name/custom_prompt"
  • 返回值取决于资源类型:
    • 如果是 prompt,就返回 PromptTemplateChatPromptTemplate 对象;
    • 如果是 chain,就返回 chain runnable。

push(...):把本地的 prompt / chain 上传到 Hub(需配置账户权限),JS/TS 里用得少,更多是官方/团队场景。

得到的提示词:

Answer the following questions as best you can. You have access to the following tools:{tools}Use the following format:Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!Question: {input}
Thought:{agent_scratchpad}
  • Question:自动填充成用户输入;
  • Thought:模型写下推理思路(“我需要先查天气,然后算…”);(CoT:Chain of Thought)
  • Action:必须严格写成工具名之一([{tool_names}] 会替换成 weather, calculator 之类);
  • Action Input:传给工具的参数(字符串);
  • Observation:执行工具的返回结果;
  • 循环:Thought → Action → Action Input → Observation 可以重复多次;
  • Final Answer:最终答案,直接写给用户。

这就是 ReAct 循环(Reasoning + Acting)

Begin!Question: {input}
Thought:{agent_scratchpad}
  • {input}:用户真实提问,比如“北京今天的天气如何?”;

  • {agent_scratchpad}:ReAct 的关键变量,用来记录之前所有的 Action/Observation 历史。

    • 第一次调用时是空;

    • 之后每轮会把上一轮的 Action/Observation 填进去,再交给模型继续思考。

Tools

前面介绍的模型携带 Tools:

const modelWithTool = new ChatOpenAI({model: "gpt-3.5-turbo-1106",apiKey: process.env.API_KEY,temperature: 0,
}).withConfig({tools: [{type: "function",function: {name: "getCurrentWeather",description: "获取指定城市当前天气",parameters: getCurrentWeatherJsonSchema,},},],
});

这是 OpenAI API 的原生 Function Calling / Tool Calling 配置。在 底层 LLM 请求里,直接把工具符合 OpenAI 规范的 schema 传给模型。特点:

  • 完全贴近 OpenAI 官方的 tools: [...] 格式(type: "function" + JSON Schema)。
  • 模型生成的 tool_calls 会自动遵守 schema;
  • 这是模型原生能力,不需要 ReAct prompt。

这里要使用的 Tool 稍微有些不同,需要一个能被 Agent 消费的 Tool。

在 LangChain.js 中,提供了一个 Tool 工具,能够生成给 Agent 消费的 Tool 工具:

import { tool } from "@langchain/core/tools";
tool(方法(具体的业务逻辑),{name: "xxx",description: "xxx",schema: "xxx"
})

示例:

import { tool } from "@langchain/core/tools";
import { z } from "zod";const weatherTool = tool(async (city) => `${city}:多云转晴,28℃ / 20℃(示例数据)`,{name: "weather",description: "查询城市天气。输入城市名。",schema: z.string(),}
);

上例中得到的 weatherTool 是一个符合 LangChain Tool 接口的对象,可被 AgentExecutor/ createReactAgent/ createToolCallingAgent 直接使用。

createReactAgent

LangChain.js 中提供方法,用于创建基于 ReAct 提示法的 Agent。方法签名:

createReactAgent(params)

必填 params 参数:

  • llm:任一兼容的聊天/文本 LLM。
  • tools:工具列表(结构化工具)。
  • prompt:ReAct 模板(推荐直接从 Hub 拉 hwchase17/react)。这个 prompt 决定了该 agent 的输入变量集合。

返回值:返回的是一个 runnable,每次调用只产出两类结果之一:

  • AgentAction:表示“去调用某个工具”的决策。包含三要素:
    • tool(工具名)
    • toolInput(传给工具的输入,字符串或对象)
    • log(可读的思考/轨迹文本)
  • AgentFinish:表示“本回合已经得到最终答案”。包含:
    • returnValues(一个对象,通常至少有 output 字段)
    • log(最终总结/轨迹)

打印出来的 agent,大致是如下的形式:

RunnableSequence (name: "ReactAgent")├─ first:  RunnableAssign( mapper = RunnableMap(...) )├─ middle: [ PromptTemplate(...), ChatOpenAI(...) ]└─ last:   ReActSingleInputOutputParser(toolNames=[...])===> 输入 → Assign预处理 → 模板渲染 → LLM补全 → ReAct输出解析 → (AgentAction | AgentFinish)
  • RunnableSequence:LCEL 的“管道”。把多个 runnable 依次串起来。

    • first:把原始输入整理成 Prompt 需要的变量。
    • middle:
      • 第 1 项:从 Hub 拉下来的 ReAct 模板
      • 第 2 项:LLM 调用
    • last:ReAct 专用输出解析器。它会:
      • 检查模型的文本是否遵守规范(有 Action:Action Input:Final Answer: 等)。
      • 如果检测到 Final Answer: :产出 AgentFinish(结束)。
      • 否则解析出 Action + Action Input :产出 AgentAction
  • singleAction: true:这个 Agent 每次只产出一个结果:

    • 要么一个工具动作(AgentAction
    • 要么终止(AgentFinish

AgentExecutor

AgentExecutor,顾名思义,就是 agent 执行器,它把你已经构造好的 agent 和 tools 结合起来,负责外层的 ReAct 式决策循环:反复“让 agent 规划一步 → 调工具 → 回填观察 → 再问 agent”,直到结束。

具体的运行过程:

  1. 调用 agent:agent 会返回 AgentAction(要调哪个工具+参数)或 AgentFinish(结束)
  2. 执行工具:当拿到 AgentAction 时,AgentExecutor 根据 action.tool 找到对应 tool,校验 toolInput,执行并得到 observation。
  3. 记录中间步骤:把 { action, observation } 追加成一条 AgentStep,同时把它格式化进 agent_scratchpad,供下一轮给 agent 使用。
  4. 判断是否继续:如果 agent 返回 AgentFinish,或到达最大迭代次数,则停止并产出最终结果。官方提供了一个 shouldContinueGetter() 用来判断是否继续(基于已执行轮数)。

对应的配置项、方法等可以参阅 这里。


import { pull } from "langchain/hub";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent, AgentExecutor } from "@langchain/classic/agents";
import { weatherTool, weekdayTool } from "./tools.js";
import "dotenv/config";// Prompt
const prompt = await pull("hwchase17/react");const llm = new ChatOpenAI({model: "gpt-4o-mini",temperature: 0,
});const tools = [weatherTool, weekdayTool];const agent = await createReactAgent({llm,tools,prompt,
});const executor = new AgentExecutor({agent,tools,returnIntermediateSteps: true,maxIterations: 3,handleParsingErrors: true,
});const res = await executor.invoke({input: "What is the weather in Tokyo?",
});console.log(res);
import { tool } from "@langchain/core/tools";
import { z } from "zod";export const weatherTool = tool(async (city) => {// call the real weather apiif (!city) {return "Please provide a city name";}return `${city} is ${Math.random() > 0.5 ? "sunny" : "cloudy"}`;},{name: "weather",description: "Get the current weather in a given location",schema: z.string(),}
);export const weekdayTool = tool(async () => {return new Date().toLocaleDateString("en-US", { weekday: "long" });},{name: "weekday",description: "Get the current weekday",schema: z.string(),}
);
http://www.jsqmd.com/news/47713/

相关文章:

  • 2025年钢丝绳牵引格栅机批发厂家权威推荐榜单:抓斗清污机/耙斗清污机/移动抓斗清污机源头厂家精选
  • HBase大数据存储如何提升读写性能
  • HBase大数据存储如何应对网络延迟
  • P10683 [COTS 2024] 划分 Particija
  • 2025云南曲靖市玉溪市一对一家教辅导测评排行榜:权威推荐高性价比选择
  • 2026年盐城一对一补习机构权威推荐:靠谱辅导机构测评排行榜
  • BOM和DOM
  • 2025高粱酒纯粮食酒推荐TOP10,纯粮固态发酵酱香浓郁回甘绵长
  • 2025内蒙古兴安盟锡林郭勒盟阿拉善盟一对一家教辅导测评排行榜:优质选择推荐
  • 2025年煤矿用阻燃铠装光缆生产厂家权威推荐榜单:矿用铠装光缆/煤矿用光缆/矿用4芯光缆源头厂家精选
  • 玉树州一对一家教机构最新推荐,2026最新家教机构榜单:家长首选靠谱提分方案推荐
  • 回滚莫队模版
  • 2025年拉袋离心机订制厂家权威推荐榜单:碟式离心机/卧螺离心机/活塞推料离心机源头厂家精选
  • Linux中: 通过编译安装的方式升级 OpenSSH 服务
  • 纵观当代现状,70年代出生的人,可能别具一格
  • #题解#洛谷 P4375 Out of Sorts G #离散化#
  • hbase上如何导入python包
  • 轻薄手机推荐:不止于轻,2025 旗舰体验榜 - 详解
  • Git为什么要有submodule呢?
  • 征程 6E/M 计算平台部署指南
  • 2025年重庆废气收集处理机构权威推荐榜单:废气处理/废气治理/废气处理设备源头机构精选
  • 详细介绍:第三章 FreeRTOS 任务相关 API 函数
  • 数据库的安全与保护(下) - 实践
  • 2025年口碑好的江苏婚纱照/婚前影像/小众婚纱照/园林婚纱照/光影婚纱照/外景婚纱照/秀禾婚纱照/中式婚纱照/结婚照品牌推荐:弥素摄影领跑
  • 2025年江苏婚纱照/婚前影像/小众婚纱照/园林婚纱照/光影婚纱照/外景婚纱照/秀禾婚纱照/中式婚纱照/结婚照品牌口碑推荐榜:弥素摄影领跑行业
  • 打印机字体漏洞分析:CVE-2024-12649技术深度解析
  • 2025年11月22日
  • 2025年德商数控母线加工机实力厂家权威推荐榜单:德商母线加工机/德商铜排加工机/德商母排加工机源头厂家精选
  • 【Java后端进行ai coding实践系列】如何使用ai coding达成计划任务增删改查
  • 2025-11-21 hetao1733837的刷题记录