如何将运行时参数附加到 Runnable
先决条件
本指南假设您熟悉以下概念
有时我们想在一个 Runnable
中调用一个 RunnableSequence,该 RunnableSequence 具有不属于序列中前一个 Runnable 输出的常量参数,也不属于用户输入。我们可以使用 Runnable.bind()
方法预先设置这些参数。
绑定停止序列
假设我们有一个简单的提示 + 模型链
提示
- npm
- yarn
- pnpm
npm i @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"Write out the following equation using algebraic symbols then solve it. Use the format\n\nEQUATION:...\nSOLUTION:...\n\n",
],
["human", "{equation_statement}"],
]);
const model = new ChatOpenAI({ temperature: 0 });
const runnable = prompt.pipe(model).pipe(new StringOutputParser());
const res = await runnable.invoke({
equation_statement: "x raised to the third plus seven equals 12",
});
console.log(res);
EQUATION: x^3 + 7 = 12
SOLUTION:
Subtract 7 from both sides:
x^3 = 5
Take the cube root of both sides:
x = ∛5
并且想要使用某些 stop
单词调用模型以缩短输出,这在某些类型的提示技术中很有用。虽然我们可以将一些参数传递到构造函数中,但其他运行时参数使用 .bind()
方法如下
const runnable = prompt
.pipe(model.bind({ stop: "SOLUTION" }))
.pipe(new StringOutputParser());
const res = await runnable.invoke({
equation_statement: "x raised to the third plus seven equals 12",
});
console.log(res);
EQUATION: x^3 + 7 = 12
您可以绑定到 Runnable 的内容将取决于调用它时可以传递的额外参数。
附加 OpenAI 工具
另一个常见用例是工具调用。虽然您通常应该使用 .bind_tools()
方法来调用工具模型,但您也可以直接绑定提供程序特定的参数,如果您想要更低级别的控制
const tools = [
{
type: "function",
function: {
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
},
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["location"],
},
},
},
];
const model = new ChatOpenAI({ model: "gpt-4o" }).bind({ tools });
await model.invoke("What's the weather in SF, NYC and LA?");
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "",
tool_calls: [
{
name: "get_current_weather",
args: { location: "San Francisco, CA" },
id: "call_iDKz4zU8PKBaaIT052fJkMMF"
},
{
name: "get_current_weather",
args: { location: "New York, NY" },
id: "call_niQwZDOqO6OJTBiDBFG8FODc"
},
{
name: "get_current_weather",
args: { location: "Los Angeles, CA" },
id: "call_zLXH2cDVQy0nAVC0ViWuEP4m"
}
],
invalid_tool_calls: [],
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_iDKz4zU8PKBaaIT052fJkMMF",
type: "function",
function: [Object]
},
{
id: "call_niQwZDOqO6OJTBiDBFG8FODc",
type: "function",
function: [Object]
},
{
id: "call_zLXH2cDVQy0nAVC0ViWuEP4m",
type: "function",
function: [Object]
}
]
},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "",
name: undefined,
additional_kwargs: {
function_call: undefined,
tool_calls: [
{
id: "call_iDKz4zU8PKBaaIT052fJkMMF",
type: "function",
function: {
name: "get_current_weather",
arguments: '{"location": "San Francisco, CA"}'
}
},
{
id: "call_niQwZDOqO6OJTBiDBFG8FODc",
type: "function",
function: {
name: "get_current_weather",
arguments: '{"location": "New York, NY"}'
}
},
{
id: "call_zLXH2cDVQy0nAVC0ViWuEP4m",
type: "function",
function: {
name: "get_current_weather",
arguments: '{"location": "Los Angeles, CA"}'
}
}
]
},
response_metadata: {
tokenUsage: { completionTokens: 70, promptTokens: 82, totalTokens: 152 },
finish_reason: "tool_calls"
},
tool_calls: [
{
name: "get_current_weather",
args: { location: "San Francisco, CA" },
id: "call_iDKz4zU8PKBaaIT052fJkMMF"
},
{
name: "get_current_weather",
args: { location: "New York, NY" },
id: "call_niQwZDOqO6OJTBiDBFG8FODc"
},
{
name: "get_current_weather",
args: { location: "Los Angeles, CA" },
id: "call_zLXH2cDVQy0nAVC0ViWuEP4m"
}
],
invalid_tool_calls: []
}
下一步
您现在了解了如何将运行时参数绑定到 Runnable。
接下来,您可能对有关 通过链传递数据 的操作指南感兴趣。