ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

LangChain.js 如何用 bindTools 与 JsonOutputToolsParser 从文本中抽取结构化实体

2026/9/14 7:57:22 拓冰建站 浏览量
LangChain.js 如何用 bindTools 与 JsonOutputToolsParser 从文本中抽取结构化实体 LangChain.js 如何用 bindTools 与 JsonOutputToolsParser 从文本中抽取结构化实体【免费下载链接】langchainjsThe agent engineering platform项目地址: https://gitcode.com/GitHub_Trending/la/langchainjs假设你有一段自由文本需要把其中的实体人名、年龄、身高、发色等抽成结构化的 JSON 对象。LangChain.js 仓库中给出了可直接运行的做法用ChatOpenAI或ChatAnthropic的bindTools把 Zod schema 绑定为函数让模型按 schema 返回工具调用再用JsonOutputToolsParser把这些工具调用解析成普通 JSON 数组。整条链路不需要执行任何工具工具调用在这里纯粹充当结构化输出的载体。主要示例代码在 examples/src/extraction/openai_tool_calling_extraction.ts解析器实现在 json_output_tools_parsers.ts。准备条件一个 Node.js 项目安装核心包与 OpenAI 集成包安装方式来自 langchain-openai 包 READMEnpm install langchain/openai langchain/core设置 OpenAI API Key 环境变量export OPENAI_API_KEYyour-api-key示例代码使用 Zod 3 的子路径导入zod/v3而 examples 包 的devDependencies中是zod: ^4.3.6peerDependencies 允许^3.25.76 || ^4所以还需要安装npm install zod另外仓库约定所有 LangChain 包依赖同一个langchain/core实例README 建议在package.json中为pnpm/npm/yarn添加对应的 overrides 字段固定版本。主路径用 OpenAI 模型抽取实体下面是仓库示例文件的完整代码可直接保存为.ts文件后用tsx或ts-node运行import { z } from zod/v3; import { ChatOpenAI } from langchain/openai; import { ChatPromptTemplate } from langchain/core/prompts; import { JsonOutputToolsParser } from langchain/core/output_parsers/openai_tools; const EXTRACTION_TEMPLATE Extract and save the relevant entities mentioned \ in the following passage together with their properties. If a property is not present and is not required in the function parameters, do not include it in the output.; const prompt ChatPromptTemplate.fromMessages([ [system, EXTRACTION_TEMPLATE], [human, {input}], ]); const person z.object({ name: z.string().describe(The persons name), age: z.string().describe(The persons age), }); const model new ChatOpenAI({ model: gpt-3.5-turbo-1106, temperature: 0, }).bindTools([ { name: person, description: A person, schema: person, }, ]); const parser new JsonOutputToolsParser(); const chain prompt.pipe(model).pipe(parser); const res await chain.invoke({ input: jane is 2 and bob is 3, }); console.log(res);各环节的作用Zod schemaperson定义要抽取的实体结构和字段说明。bindTools接收{ name, description, schema }形式的工具定义name会成为解析结果中的实体类型标识。ChatPromptTemplate系统消息固定抽取规则缺失且非必填的属性不要输出人类消息用{input}占位运行时传入待抽取的文本。prompt.pipe(model).pipe(parser)标准的 Runnable 管道——提示词渲染后发给绑定工具的模型模型输出交给解析器。new ChatOpenAI({ model: gpt-3.5-turbo-1106, temperature: 0 })示例用低温度保证抽取稳定。模型名可按你的账户可用模型替换。结果验证示例文件注释中给出的运行输出文档示例实际内容会随模型输出略有差异[ { name: person, arguments: { name: jane, age: 2 } }, { name: person, arguments: { name: bob, age: 3 } } ]判断抽取是否完成的依据有两条都来自仓库文档结果数量openai_tools.int.test.ts 中有一个 Extraction 集成测试用同样的bindToolsJsonOutputToolsParser链路让模型输出两个笑话断言expect(res.length).toBe(2)。也就是说解析结果是数组其长度对应模型返回的工具调用条数——你可以按待抽取实体的预期数量断言。字段结构按 解析器源码每个解析结果的字段为{ type, args, id? }其中type是工具名示例中为personargs是按 schema 解析出的对象仅当构造解析器时传returnId: true时才带上id。Anthropic 变体示例注释中的输出正是这种结构[ { type: person, args: { name: Alex, height: 5, hairColor: blonde } }, { type: person, args: { name: Claudia, height: 6, hairColor: brunette } } ]如果消息里没有tool_callsparsePartialResult返回空数组[]工具参数不是合法 JSON 时parseToolCall会抛出OutputParserException。可选分支一用 Anthropic 模型并强制选择工具同样的模式适用于ChatAnthropic仓库示例见 anthropic_tools/extraction.ts需要额外安装langchain/anthropic。与 OpenAI 主路径相比有两个差异import { z } from zod/v3; import { ChatAnthropic } from langchain/anthropic; import { PromptTemplate } from langchain/core/prompts; import { JsonOutputToolsParser } from langchain/core/output_parsers/openai_tools; const prompt PromptTemplate.fromTemplate(EXTRACTION_TEMPLATE); const schema z.object({ name: z.string().describe(The name of a person), height: z.number().describe(The persons height), hairColor: z.optional(z.string()).describe(The persons hair color), }); const model new ChatAnthropic({ temperature: 0.1, model: claude-3-sonnet-20240229, }) .bindTools([ { name: person, description: Extracts the relevant people from the passage., schema, }, ]) .withConfig({ // Can also set to auto to let the model choose a tool tool_choice: { type: tool, name: person, }, }); const chain await prompt.pipe(model).pipe(new JsonOutputToolsParser()); const response await chain.invoke({ input: Alex is 5 feet tall. Claudia is 1 foot taller than Alex and jumps higher than him. Claudia is a brunette and Alex is blonde., });用z.optional(...)声明可缺失字段主路径示例里则靠提示词约束缺失属性不输出。通过.withConfig({ tool_choice: { type: tool, name: person } })强制模型调用指定工具示例注释说明也可以设为auto让模型自行选择。chain是await出来的因为withConfig返回 Promise。可选分支二只关心单一工具时用 JsonOutputKeyToolsParser如果你的场景只绑定了一个抽取工具解析器源码中还有JsonOutputKeyToolsParser构造参数为{ keyName, returnSingle?, zodSchema?, serializableSchema?, returnId? }。它会过滤出type keyName的调用默认只返回argsreturnSingle: true时返回第一个结果而不是数组提供zodSchema或serializableSchema时会对结果做 schema 校验校验失败同样抛出OutputParserException。限制该链路依赖模型返回工具调用消息模型没有产生tool_calls时解析结果为空数组需要按你自己的任务逻辑判断是文本中确实没有实体还是模型未按约定输出。JsonOutputToolsParser不校验字段内容如args是否符合 schema要做 schema 校验应使用带zodSchema的JsonOutputKeyToolsParser或在拿到args后自行用 Zod 解析。示例中的模型名gpt-3.5-turbo-1106、claude-3-sonnet-20240229来自仓库示例文件实际运行时替换为你账户可访问的模型。【免费下载链接】langchainjsThe agent engineering platform项目地址: https://gitcode.com/GitHub_Trending/la/langchainjs创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考