forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nc/callbacks docs (langchain-ai#1012)
* Add docs on how/when to use callbacks * Update "create custom handler" section * Update hierarchy * Update constructor for BaseChain to allow receiving an object with args, rather than positional args Doing this in a backwards compat way, ie. still supporting old positional args * Remove requirement to implement serialize method in subcalsses of BaseChain to make it easier to subclass (until we work more on serialization) * Fix code block api docs section for renamed imports * Add docs and examples on creating custom chains, small reorg to chain docs * Split callbacks docs into multiple pages, add docs on using callbacks in subclasses * Fix broken links * Fix one more * Fix tsc transpiling issue TSC was defining this property before the `super` call * Lint * Add example of __run * Update * Typo
- Loading branch information
Showing
34 changed files
with
315 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
docs/docs/modules/chains/index_related_chains/conversational_retrieval.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
docs/docs/modules/chains/index_related_chains/retrieval_qa.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Creating callback handlers | ||
|
||
## Creating a custom handler | ||
|
||
You can also create your own handler by implementing the `BaseCallbackHandler` interface. This is useful if you want to do something more complex than just logging to the console, eg. send the events to a logging service. As an example here is a simple implementation of a handler that logs to the console: | ||
|
||
import CustomHandlerExample from "@examples/callbacks/custom_handler.ts"; | ||
|
||
<CodeBlock language="typescript">{CustomHandlerExample}</CodeBlock> | ||
|
||
You could then use it as described in the [section](#built-in-handlers) above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
sidebar_label: Callbacks in custom Chains | ||
--- | ||
|
||
# Callbacks in custom Chains/Agents | ||
|
||
LangChain is designed to be extensible. You can add your own custom Chains and Agents to the library. This page will show you how to add callbacks to your custom Chains and Agents. | ||
|
||
## Adding callbacks to custom Chains | ||
|
||
When you create a custom chain you can easily set it up to use the same callback system as all the built-in chains. See this guide for more information on how to [create custom chains and use callbacks inside them](../../modules/chains#subclassing-basechain). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# Events / Callbacks | ||
|
||
LangChain provides a callback system that allows you to hook into the various stages of your LLM application. This is useful for logging, [monitoring](../tracing), [streaming](../../modules/models/llms/additional_functionality#streaming-responses), and other tasks. | ||
|
||
You can subscribe to these events by using the `callbacks` argument available throughout the API. This method accepts a list of handler objects, which are expected to implement one or more of the methods described in the [API docs](../../api/callbacks/interfaces/CallbackHandlerMethods). | ||
|
||
## Dive deeper | ||
|
||
import DocCardList from "@theme/DocCardList"; | ||
|
||
<DocCardList /> | ||
|
||
## How to use callbacks | ||
|
||
The `callbacks` argument is available on most objects throughout the API (Chains, Models, Tools, Agents, etc.) in two different places: | ||
|
||
- **Constructor callbacks**: defined in the constructor, eg. `new LLMChain({ callbacks: [handler] })`, which will be used for all calls made on that object, and will be scoped to that object only, eg. if you pass a handler to the `LLMChain` constructor, it will not be used by the Model attached to that chain. | ||
- **Request callbacks**: defined in the `call()`/`run()`/`apply()` methods used for issuing a request, eg. `chain.call({ input: '...' }, [handler])`, which will be used for that specific request only, and all sub-requests that it contains (eg. a call to an LLMChain triggers a call to a Model, which uses the same handler passed in the `call()` method). | ||
|
||
The `verbose` argument is available on most objects throughout the API (Chains, Models, Tools, Agents, etc.) as a constructor argument, eg. `new LLMChain({ verbose: true })`, and it is equivalent to passing a `ConsoleCallbackHandler` to the `callbacks` argument of that object and all child objects. This is useful for debugging, as it will log all events to the console. | ||
|
||
### When do you want to use each of these? | ||
|
||
- Constructor callbacks are most useful for use cases such as logging, monitoring, etc., which are _not specific to a single request_, but rather to the entire chain. For example, if you want to log all the requests made to an LLMChain, you would pass a handler to the constructor. | ||
- Request callbacks are most useful for use cases such as streaming, where you want to stream the output of a single request to a specific websocket connection, or other similar use cases. For example, if you want to stream the output of a single request to a websocket, you would pass a handler to the `call()` method | ||
|
||
## Usage examples | ||
|
||
### Built-in handlers | ||
|
||
LangChain provides a few built-in handlers that you can use to get started. These are available in the `langchain/callbacks` module. The most basic handler is the `ConsoleCallbackHandler`, which simply logs all events to the console. In the future we will add more default handlers to the library. Note that when the `verbose` flag on the object is set to `true`, the `ConsoleCallbackHandler` will be invoked even without being explicitly passed in. | ||
|
||
import ConsoleExample from "@examples/callbacks/console_handler.ts"; | ||
|
||
<CodeBlock language="typescript">{ConsoleExample}</CodeBlock> | ||
|
||
### One-off handlers | ||
|
||
You can create a one-off handler inline by passing a plain object to the `callbacks` argument. This object should implement the [`CallbackHandlerMethods`](../../api/callbacks/interfaces/CallbackHandlerMethods) interface. This is useful if eg. you need to create a handler that you will use only for a single request, eg to stream the output of an LLM/Agent/etc to a websocket. | ||
|
||
import StreamingExample from "@examples/models/llm/llm_streaming.ts"; | ||
|
||
<CodeBlock language="typescript">{StreamingExample}</CodeBlock> | ||
|
||
### Multiple handlers | ||
|
||
We offer a method on the `CallbackManager` class that allows you to create a one-off handler. This is useful if eg. you need to create a handler that you will use only for a single request, eg to stream the output of an LLM/Agent/etc to a websocket. | ||
|
||
This is a more complete example that passes a `CallbackManager` to a ChatModel, and LLMChain, a Tool, and an Agent. | ||
|
||
import AgentExample from "@examples/agents/streaming.ts"; | ||
|
||
<CodeBlock language="typescript">{AgentExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { BaseCallbackHandler } from "langchain/callbacks"; | ||
import { AgentAction, AgentFinish, ChainValues } from "langchain/schema"; | ||
|
||
export class MyCallbackHandler extends BaseCallbackHandler { | ||
name = "MyCallbackHandler"; | ||
|
||
async handleChainStart(chain: { name: string }) { | ||
console.log(`Entering new ${chain.name} chain...`); | ||
} | ||
|
||
async handleChainEnd(_output: ChainValues) { | ||
console.log("Finished chain."); | ||
} | ||
|
||
async handleAgentAction(action: AgentAction) { | ||
console.log(action.log); | ||
} | ||
|
||
async handleToolEnd(output: string) { | ||
console.log(output); | ||
} | ||
|
||
async handleText(text: string) { | ||
console.log(text); | ||
} | ||
|
||
async handleAgentEnd(action: AgentFinish) { | ||
console.log(action.log); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { CallbackManagerForChainRun } from "langchain/callbacks"; | ||
import { BaseChain as _ } from "langchain/chains"; | ||
import { BaseMemory } from "langchain/memory"; | ||
import { ChainValues } from "langchain/schema"; | ||
|
||
abstract class BaseChain { | ||
memory?: BaseMemory; | ||
|
||
/** | ||
* Run the core logic of this chain and return the output | ||
*/ | ||
abstract _call( | ||
values: ChainValues, | ||
runManager?: CallbackManagerForChainRun | ||
): Promise<ChainValues>; | ||
|
||
/** | ||
* Return the string type key uniquely identifying this class of chain. | ||
*/ | ||
abstract _chainType(): string; | ||
|
||
/** | ||
* Return the list of input keys this chain expects to receive when called. | ||
*/ | ||
abstract get inputKeys(): string[]; | ||
|
||
/** | ||
* Return the list of output keys this chain will produce when called. | ||
*/ | ||
abstract get outputKeys(): string[]; | ||
} |
Oops, something went wrong.