Add docs for tags (#1648)

This commit is contained in:
Nuno Campos
2023-06-14 11:43:45 +01:00
committed by GitHub
parent 6e9d342a6e
commit 590e21a07d
4 changed files with 16 additions and 6 deletions
+5 -1
View File
@@ -4,7 +4,7 @@ import CodeBlock from "@theme/CodeBlock";
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).
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
@@ -45,6 +45,10 @@ import VerboseExample from "@examples/callbacks/docs_verbose.ts";
- 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
## Tags
You can add tags to your callbacks by passing a `tags` argument to the `call()`/`run()`/`apply()` methods. This is useful for filtering your logs, eg. if you want to log all requests made to a specific LLMChain, you can add a tag, and then filter your logs by that tag. You can pass tags to both constructor and request callbacks, see the examples above for details. These tags are then passed to the `tags` argument of the "start" callback methods, ie. [`handleLLMStart`](../../api/callbacks/interfaces/CallbackHandlerMethods#handlellmstart), [`handleChatModelStart`](../../api/callbacks/interfaces/CallbackHandlerMethods#handlechatmodelstart), [`handleChainStart`](../../api/callbacks/interfaces/CallbackHandlerMethods#handlechainstart), [`handleToolStart`](../../api/callbacks/interfaces/CallbackHandlerMethods#handletoolstart).
## Backgrounding callbacks
By default callbacks run in-line with the your chain/LLM run. This means that if you have a slow callback you can see an impact on the overall latency of your runs. You can make callbacks not be awaited by setting the environment variable `LANGCHAIN_CALLBACKS_BACKGROUND=true`. This will cause the callbacks to be run in the background, and will not impact the overall latency of your runs. When you do this you might need to await all pending callbacks before exiting your application. You can do this with the following method:
@@ -3,6 +3,8 @@ import { OpenAI } from "langchain/llms/openai";
const llm = new OpenAI({
temperature: 0,
// These tags will be attached to all calls made with this LLM.
tags: ["example", "callbacks", "constructor"],
// This handler will be used for all calls made with this LLM.
callbacks: [new ConsoleCallbackHandler()],
});
@@ -5,7 +5,10 @@ const llm = new OpenAI({
temperature: 0,
});
// This handler will be used only for this call.
const response = await llm.call("1 + 1 =", undefined, [
new ConsoleCallbackHandler(),
]);
const response = await llm.call(
"1 + 1 =",
// These tags will be attached only to this call to the LLM.
{ tags: ["example", "callbacks", "request"] },
// This handler will be used only for this call.
[new ConsoleCallbackHandler()]
);
@@ -39,7 +39,8 @@ export class MyCustomChain extends BaseChain implements MyCustomChainInputs {
const result = await this.llm.generatePrompt(
[promptValue],
{},
runManager?.getChild()
// This tag "a-tag" will be attached to this inner LLM call
runManager?.getChild("a-tag")
);
// If you want to log something about this run, you can do so by calling