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

[LangChain] 20. Tools配置

在实际开发中,经常还有如下的需求:

  1. 禁止模型调用某个工具
  2. 强制调用某个工具
  3. 注册多个函数工具

禁止模型调用工具

默认情况下,模型会自行决定是否使用 tool。但有时你希望它只能用自己的知识回答,怎么办?

你可以通过 tool_choice: "none" 禁用工具调用。

const result = await openai.chat.completions.create({model: "gpt-3.5-turbo-1106",messages: [{ role: "user", content: "北京天气如何?" }],tools, // 工具注册了,但不会被调用tool_choice: "none", // 显式禁止调用工具
});

强制调用工具

有时候你希望无论用户说什么,模型都必须调用某个函数

思考🤔什么场景有这样的需求?

  1. 系统内部函数(写日志、上报埋点):比如用户输入“哈哈哈”,你并不在乎他问了啥,但系统强制调用 logUserInteraction(),确保每次对话都会记录到数据库里。
  2. 必须走翻译函数:如果你做一个“统一英文客服”,无论用户输入中英文,都必须调用 translateToEnglish(),再交给后续处理。
  3. 结构化场景
    • 提取表单信息:无论用户说“我的名字是张三”还是“我来自北京”,你都强制调用 extractUserProfile(),把内容整理成 {name: "张三", city: "北京"}
    • 生成 SQL:即使用户说“hi”,也要求模型调用 toSQL(),这样就能保持统一的 SQL 输出。
const res = await openai.chat.completions.create({model: "gpt-3.5-turbo-1106",messages,tools,tool_choice: {type: "function",function: {name: "getCurrentWeather", // 强制调用该函数},},
});

注册多个工具

很多实际场景中,我们的机器人不仅能查天气,还可能支持查时间、查汇率、订机票……

tools 支持同时注册多个函数。模型会根据用户提问智能选择对应函数,无需额外配置。

额外需注意字段

parallel_tool_calls

是否允许模型在一次回复里并行提出多个 tool 调用。默认是开启的。

注意,parallel_tool_calls 只决定“能不能一次要多个工具”;用哪个工具仍由 tool_choice(或模型自动选择)决定。还有就是,parallel_tool_calls: false 只是禁止并行提出多个调用,也就是模型通常会只给一个 tool_call。它不保证“一轮就结束”。模型仍可能在下一轮继续提别的工具。

description

tools.function 中,description 字段能显著影响模型选择工具的行为。如果你注册多个工具,要写清楚每个工具的用途和限制,否则模型可能误选。

import OpenAI from "openai";
import { tools, getCurrentWeather, getCurrentTime } from "./tools.js";
import dotenv from "dotenv";
dotenv.config();const openai = new OpenAI({apiKey: process.env.API_KEY,
});// 1. 用户提的问题
const messages = [{role: "user",content: "北京今天的天气怎么样?另外,现在几点了?",},
];// 2. 将问题 + 工具箱一起给模型,模型判断是否使用工具
const res = await openai.chat.completions.create({model: "gpt-3.5-turbo-1106",messages,tools,parallel_tool_calls: false,
});console.dir(res.choices[0], { depth: null });const assistanMsg = res.choices[0].message;// 不需要调用工具的情况
if (!assistanMsg.tool_calls?.length) {console.log("模型未调用任何工具,直接回复的内容:", assistanMsg.content);process.exit(0);
}// 下面是需要调用工具的流程
messages.push(assistanMsg);// 构建一个本地的工具箱
const funcs = {getCurrentWeather,getCurrentTime,
};for (const call of assistanMsg.tool_calls) {const {id: tool_call_id,function: { name, arguments: args },} = call;const fn = funcs[name];if (!fn) {messages.push({role: "tool",tool_call_id,name,content: JSON.stringify({error: `${name}的工具不存在`,}),});continue;}const toolResult = await fn(args);// 针对工具调用返回的结果做一个简单的日志const content =typeof toolResult === "string" ? toolResult : JSON.stringify(toolResult);console.log(`已执行 ${name}(${JSON.stringify(args)}), 返回: ${content}`);// 将工具调用结果和之前的会话组装起来,再给大模型messages.push({role: "tool",tool_call_id,name,content,});
}const finalRes = await openai.chat.completions.create({model: "gpt-3.5-turbo-1106",messages,tools,
});
// console.log("最终回答:", finalRes.choices[0].message.content);
console.log(finalRes.choices[0].message.tool_calls);
// 外部工具
export function getCurrentWeather({ location, unit = "celsius" }) {const weather_info = {location, // 查询的城市名称temperature: "22",unit, // 温度的单位("celsius" 或 "fahrenheit")forecast: ["晴朗 ☀️", "微风 🌬️"], // 天气简单的描述};return JSON.stringify(weather_info);
}// 工具:获取当前时间
export function getCurrentTime({ format = "locale" }) {switch (format) {case "iso":return new Date().toISOString(); // ISO 格式:2025-06-24T09:00:00.000Zcase "locale":return new Date().toLocaleString(); // 本地格式:2025/6/24 17:00:00case "string":return new Date().toString(); // 英文字符串格式:Tue Jun 24 2025 17:00:00 GMT+0800default:return "不支持的 format 类型,请传入 iso / locale / string";}
}// 工具箱
export const tools = [{type: "function",function: {name: "getCurrentWeather",description: "获取指定城市当前的天气情况",parameters: {type: "object",properties: {location: {type: "string",description: "城市名称,例如:北京、上海、成都",},unit: {type: "string",enum: ["celsius", "fahrenheit"],description: "温度单位,可选:摄氏度或者华氏度",},},required: ["location"],},},},{type: "function",function: {name: "getCurrentTime",description: "获取当前时间(可选格式)",parameters: {type: "object",properties: {format: {type: "string",enum: ["iso", "locale", "string"],},},required: ["format"],},},},
];

-EOF-

http://www.jsqmd.com/news/43149/

相关文章:

  • XHORSE XZKA82EN Hyundai Kia Key Shell - 5pcs/lot for Replacement Repair
  • jdk linux 下载 64位
  • jdk linux 64 安装
  • CSP-S 2022 游记
  • jdk 1.7 linux 安装
  • jdk 1.6 64 linux
  • 网络分析模型十
  • 网络分析模型九
  • 251117
  • Boost Key Programming Speed with CG A10-3+1 HON.D Style 4-Button Remote (5pcs) for CGDI K2
  • 合并和部分保存与鼠标的使用
  • 抖音视频批量提取工具(增加新功能 ,新功能介绍),通过关键词搜索进行视频提取下载软件
  • 打开保存各种格式文件
  • FunASR 快速上手
  • JDBC与MySQL交互有哪些安全措施
  • 网络分析模型八
  • java执行linux 命令
  • 主机开v*n 虚拟机共享v*n
  • 2025-11-18 vue3+ts项目报错:TypeError: Failed to fetch dynamically imported==》script没有指定使用lang=ts
  • 2025 年 11 月新风系统厂家推荐排行榜,沈阳/大连/鞍山/哈尔滨/内蒙古,电竞网咖/酒店/棋牌室/KTV/别墅/学校/诊所/养殖基地,全热交换/除湿/加湿/静音/防冷凝水/节能/耐用/口碑好
  • 2025 年 11 月无尘投料站厂家推荐排行榜,自动无尘投料站,真空无尘投料站,吨袋无尘投料站,高效无尘投料站公司推荐
  • alpha阶段工作总结11.17
  • 绘图区右键上下文菜单快捷键设置
  • javac linux
  • java 调用linux命令
  • 西门子v4.0问题
  • java 读取文件linux文件
  • java 读取linux 文件
  • java 上传 linux
  • 从接雨水问题看双指针与递归:藏在“收缩”里的分治智慧