跳至主要内容

如何将运行时参数附加到 Runnable

先决条件

有时我们想要在 Runnable 内调用 RunnableSequence,其中使用并非序列中先前 Runnable 输出的一部分的常量参数,并且这些参数也不是用户输入的一部分。我们可以使用 Runnable.bind() 方法提前设置这些参数。

绑定停止序列

假设我们有一个简单的提示 + 模型链

yarn add @langchain/openai @langchain/core
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 runnableWithStop = prompt
.pipe(model.bind({ stop: ["SOLUTION"] }))
.pipe(new StringOutputParser());

const shorterResponse = await runnableWithStop.invoke({
equation_statement: "x raised to the third plus seven equals 12",
});

console.log(shorterResponse);
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 modelWithTools = new ChatOpenAI({ model: "gpt-4o" }).bind({ tools });

await modelWithTools.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。

接下来,您可能对我们的操作指南感兴趣,内容是关于 将数据传递到链中


本页对您有帮助吗?


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