跳到主要内容

如何在使用工具调用时使用少样本提示

对于更复杂的工具使用,在提示中添加少样本示例非常有用。我们可以通过将包含 ToolCalls 和相应 ToolMessagesAIMessages 添加到提示来实现这一点。

首先定义模型和计算器工具

import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({ model: "gpt-4o", temperature: 0 });

/**
* Note that the descriptions here are crucial, as they will be passed along
* to the model along with the class name.
*/
const calculatorSchema = z.object({
operation: z
.enum(["add", "subtract", "multiply", "divide"])
.describe("The type of operation to execute."),
number1: z.number().describe("The first number to operate on."),
number2: z.number().describe("The second number to operate on."),
});

const calculatorTool = tool(
async ({ operation, number1, number2 }) => {
// Functions must return strings
if (operation === "add") {
return `${number1 + number2}`;
} else if (operation === "subtract") {
return `${number1 - number2}`;
} else if (operation === "multiply") {
return `${number1 * number2}`;
} else if (operation === "divide") {
return `${number1 / number2}`;
} else {
throw new Error("Invalid operation.");
}
},
{
name: "calculator",
description: "Can perform mathematical operations.",
schema: calculatorSchema,
}
);

const llmWithTools = llm.bindTools([calculatorTool]);

我们的计算器可以处理常见的加、减、乘、除运算。但如果我们询问新的数学运算符 🦜 会发生什么?

让我们看看在简单使用它时会发生什么

const res = await llmWithTools.invoke("What is 3 🦜 12");

console.log(res.content);
console.log(res.tool_calls);

[
{
name: 'calculator',
args: { operation: 'multiply', number1: 3, number2: 12 },
type: 'tool_call',
id: 'call_I0oQGmdESpIgcf91ej30p9aR'
}
]

它不太清楚如何将 🦜 解释为运算符,它默认使用 multiply。现在,让我们尝试以制造消息的形式提供一些示例来引导它朝 divide 方向发展

import { HumanMessage, AIMessage, ToolMessage } from "@langchain/core/messages";

const res = await llmWithTools.invoke([
new HumanMessage("What is 333382 🦜 1932?"),
new AIMessage({
content:
"The 🦜 operator is shorthand for division, so we call the divide tool.",
tool_calls: [
{
id: "12345",
name: "calculator",
args: {
number1: 333382,
number2: 1932,
operation: "divide",
},
},
],
}),
new ToolMessage({
tool_call_id: "12345",
content: "The answer is 172.558.",
}),
new AIMessage("The answer is 172.558."),
new HumanMessage("What is 6 🦜 2?"),
new AIMessage({
content:
"The 🦜 operator is shorthand for division, so we call the divide tool.",
tool_calls: [
{
id: "54321",
name: "calculator",
args: {
number1: 6,
number2: 2,
operation: "divide",
},
},
],
}),
new ToolMessage({
tool_call_id: "54321",
content: "The answer is 3.",
}),
new AIMessage("The answer is 3."),
new HumanMessage("What is 3 🦜 12?"),
]);

console.log(res.tool_calls);
[
{
name: 'calculator',
args: { number1: 3, number2: 12, operation: 'divide' },
type: 'tool_call',
id: 'call_O6M4yDaA6s8oDqs2Zfl7TZAp'
}
]

我们可以看到,它现在以正确的方式将 🦜divide 运算符等同起来!


此页面对您有帮助吗?


您也可以留下详细的反馈 在 GitHub 上.