Skip to main content

回调

¥Callbacks

LangChain 提供了一个回调系统,允许你连接到 LLM 应用的各个阶段。这对于日志记录、监控、流式传输和其他任务很有用。

¥LangChain provides a callback system that allows you to hook into the various stages of your LLM application. This is useful for logging, monitoring, streaming, and other tasks.

你可以使用 API 中提供的 callbacks 参数订阅这些事件。此参数是处理程序对象列表,这些对象预计将实现下面更详细描述的一个或多个方法。

¥You can subscribe to these events by using the callbacks argument available throughout the API. This argument is list of handler objects, which are expected to implement one or more of the methods described below in more detail.

回调事件

¥Callback events

EventEvent TriggerAssociated Method
Chat model startWhen a chat model startshandleChatModelStart
LLM startWhen a llm startshandleLlmStart
LLM new tokenWhen an llm OR chat model emits a new tokenhandleLlmNewToken
LLM endsWhen an llm OR chat model endshandleLlmEnd
LLM errorsWhen an llm OR chat model errorshandleLlmError
Chain startWhen a chain starts runninghandleChainStart
Chain endWhen a chain endshandleChainEnd
Chain errorWhen a chain errorshandleChainError
Tool startWhen a tool starts runninghandleToolStart
Tool endWhen a tool endshandleToolEnd
Tool errorWhen a tool errorshandleToolError
Retriever startWhen a retriever startshandleRetrieverStart
Retriever endWhen a retriever endshandleRetrieverEnd
Retriever errorWhen a retriever errorshandleRetrieverError

回调处理程序

¥Callback handlers

在运行时,LangChain 会配置一个合适的回调管理器(例如 CallbackManager),当事件触发时,该管理器将负责在每个 "registered" 回调处理程序上调用相应的方法。

¥During run-time LangChain configures an appropriate callback manager (e.g., CallbackManager) which will be responsible for calling the appropriate method on each "registered" callback handler when the event is triggered.

传递回调函数

¥Passing callbacks

callbacks 属性在整个 API(模型、工具、代理等)中的大多数对象上都可用,但只在两个不同的地方可用:

¥The callbacks property is available on most objects throughout the API (Models, Tools, Agents, etc.) in two different places:

  • 请求时间回调:除了输入数据外,在请求时也传递。适用于所有标准 Runnable 对象。这些回调会被定义它们的对象的所有子对象继承。例如,await chain.invoke({ number: 25 }, { callbacks: [handler] })

    ¥Request time callbacks: Passed at the time of the request in addition to the input data. Available on all standard Runnable objects. These callbacks are INHERITED by all children of the object they are defined on. For example, await chain.invoke({ number: 25 }, { callbacks: [handler] }).

  • 构造函数回调:const chain = new TheNameOfSomeChain({ callbacks: [handler] })。这些回调作为参数传递给对象的构造函数。回调的作用域仅限于定义它们的对象,并且不会被该对象的任何子对象继承。

    ¥Constructor callbacks: const chain = new TheNameOfSomeChain({ callbacks: [handler] }). These callbacks are passed as arguments to the constructor of the object. The callbacks are scoped only to the object they are defined on, and are not inherited by any children of the object.

danger

构造函数回调的作用域仅限于它们所定义的对象。它们不会被对象的子对象继承。

¥Constructor callbacks are scoped only to the object they are defined on. They are not inherited by children of the object.

如果你正在创建自定义链或可运行对象,则需要记住将请求时间回调传播到任何子对象。

¥If you're creating a custom chain or runnable, you need to remember to propagate request time callbacks to any child objects.

对于某些模型,你可以尝试显式传递 相关操作指南 参数来排除任何环境变量问题,例如:

¥For specifics on how to use callbacks, see the relevant how-to guides here.