跳至主要内容

如何在运行时传递回调

先决条件

本指南假设您熟悉以下概念

在许多情况下,在运行对象时传递处理程序会更有利。当我们使用 CallbackHandlers 通过 `callbacks` 关键字参数执行运行时,这些回调将由参与执行的所有嵌套对象发出。例如,当处理程序传递到代理时,它将用于与代理及其执行中涉及的所有对象(在本例中为工具和 LLM)相关的所有回调。

这使我们不必将处理程序手动附加到每个单独的嵌套对象。以下是如何使用 LangChain 的内置 ConsoleCallbackHandler 的示例。

import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatAnthropic } from "@langchain/anthropic";

const handler = new ConsoleCallbackHandler();

const prompt = ChatPromptTemplate.fromTemplate(`What is 1 + {number}?`);
const model = new ChatAnthropic({
model: "claude-3-sonnet-20240229",
});

const chain = prompt.pipe(model);

await chain.invoke({ number: "2" }, { callbacks: [handler] });
[chain/start] [1:chain:RunnableSequence] Entering Chain run with input: {
"number": "2"
}
[chain/start] [1:chain:RunnableSequence > 2:prompt:ChatPromptTemplate] Entering Chain run with input: {
"number": "2"
}
[chain/end] [1:chain:RunnableSequence > 2:prompt:ChatPromptTemplate] [1ms] Exiting Chain run with output: {
"lc": 1,
"type": "constructor",
"id": [
"langchain_core",
"prompt_values",
"ChatPromptValue"
],
"kwargs": {
"messages": [
{
"lc": 1,
"type": "constructor",
"id": [
"langchain_core",
"messages",
"HumanMessage"
],
"kwargs": {
"content": "What is 1 + 2?",
"additional_kwargs": {},
"response_metadata": {}
}
}
]
}
}
[llm/start] [1:chain:RunnableSequence > 3:llm:ChatAnthropic] Entering LLM run with input: {
"messages": [
[
{
"lc": 1,
"type": "constructor",
"id": [
"langchain_core",
"messages",
"HumanMessage"
],
"kwargs": {
"content": "What is 1 + 2?",
"additional_kwargs": {},
"response_metadata": {}
}
}
]
]
}
[llm/end] [1:chain:RunnableSequence > 3:llm:ChatAnthropic] [766ms] Exiting LLM run with output: {
"generations": [
[
{
"text": "1 + 2 = 3",
"message": {
"lc": 1,
"type": "constructor",
"id": [
"langchain_core",
"messages",
"AIMessage"
],
"kwargs": {
"content": "1 + 2 = 3",
"tool_calls": [],
"invalid_tool_calls": [],
"additional_kwargs": {
"id": "msg_01SGGkFVbUbH4fK7JS7agerD",
"type": "message",
"role": "assistant",
"model": "claude-3-sonnet-20240229",
"stop_sequence": null,
"usage": {
"input_tokens": 16,
"output_tokens": 13
},
"stop_reason": "end_turn"
},
"response_metadata": {
"id": "msg_01SGGkFVbUbH4fK7JS7agerD",
"model": "claude-3-sonnet-20240229",
"stop_sequence": null,
"usage": {
"input_tokens": 16,
"output_tokens": 13
},
"stop_reason": "end_turn"
}
}
}
}
]
],
"llmOutput": {
"id": "msg_01SGGkFVbUbH4fK7JS7agerD",
"model": "claude-3-sonnet-20240229",
"stop_sequence": null,
"usage": {
"input_tokens": 16,
"output_tokens": 13
},
"stop_reason": "end_turn"
}
}
[chain/end] [1:chain:RunnableSequence] [778ms] Exiting Chain run with output: {
"lc": 1,
"type": "constructor",
"id": [
"langchain_core",
"messages",
"AIMessage"
],
"kwargs": {
"content": "1 + 2 = 3",
"tool_calls": [],
"invalid_tool_calls": [],
"additional_kwargs": {
"id": "msg_01SGGkFVbUbH4fK7JS7agerD",
"type": "message",
"role": "assistant",
"model": "claude-3-sonnet-20240229",
"stop_sequence": null,
"usage": {
"input_tokens": 16,
"output_tokens": 13
},
"stop_reason": "end_turn"
},
"response_metadata": {
"id": "msg_01SGGkFVbUbH4fK7JS7agerD",
"model": "claude-3-sonnet-20240229",
"stop_sequence": null,
"usage": {
"input_tokens": 16,
"output_tokens": 13
},
"stop_reason": "end_turn"
}
}
}
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: "1 + 2 = 3",
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {
id: "msg_01SGGkFVbUbH4fK7JS7agerD",
type: "message",
role: "assistant",
model: "claude-3-sonnet-20240229",
stop_sequence: null,
usage: { input_tokens: 16, output_tokens: 13 },
stop_reason: "end_turn"
},
response_metadata: {}
},
lc_namespace: [ "langchain_core", "messages" ],
content: "1 + 2 = 3",
name: undefined,
additional_kwargs: {
id: "msg_01SGGkFVbUbH4fK7JS7agerD",
type: "message",
role: "assistant",
model: "claude-3-sonnet-20240229",
stop_sequence: null,
usage: { input_tokens: 16, output_tokens: 13 },
stop_reason: "end_turn"
},
response_metadata: {
id: "msg_01SGGkFVbUbH4fK7JS7agerD",
model: "claude-3-sonnet-20240229",
stop_sequence: null,
usage: { input_tokens: 16, output_tokens: 13 },
stop_reason: "end_turn"
},
tool_calls: [],
invalid_tool_calls: []
}

如果模块已存在关联的回调,则这些回调将与运行时传递的任何回调一起运行。

下一步

您现在已经学习了如何在运行时传递回调。

接下来,查看本节中的其他操作指南,例如如何创建自己的 自定义回调处理程序


本页内容是否有帮助?


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