Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[docs]: Docs for with listeners runnable method #3531

Merged
merged 6 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/core_docs/docs/modules/callbacks/how_to/with_listeners.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/guides/expression_language/with_listeners.ts";

# Listeners

LangChain callbacks offer a method `withListeners` which allow you to add event listeners to the following events:

- `onStart` - called when the chain starts
- `onEnd` - called when the chain ends
- `onError` - called when an error occurs

These methods accept a callback function which will be called when the event occurs. The callback function can accept two arguments:

- `input` - the input value, for example it would be `RunInput` if used with a Runnable.
- `config` - an optional config object. This can contain metadata, callbacks or any other values passed in as a config object when the chain is started.

Below is an example which demonstrates how to use the `withListeners` method:

<CodeBlock language="typescript">{Example}</CodeBlock>
59 changes: 59 additions & 0 deletions examples/src/guides/expression_language/with_listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Run } from "langchain/callbacks";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { ChatPromptTemplate } from "langchain/prompts";

const prompt = ChatPromptTemplate.fromMessages([
["ai", "You are a nice assistant."],
["human", "{question}"],
]);
const model = new ChatOpenAI({});
const chain = prompt.pipe(model);

const trackTime = () => {
let start: { startTime: number; question: string };
let end: { endTime: number; answer: string };

const handleStart = (run: Run) => {
start = {
startTime: run.start_time,
question: run.inputs.question,
};
};

const handleEnd = (run: Run) => {
if (run.end_time && run.outputs) {
end = {
endTime: run.end_time,
answer: run.outputs.content,
};
}

console.log("start", start);
console.log("end", end);
console.log(`total time: ${end.endTime - start.startTime}ms`);
};

return { handleStart, handleEnd };
};

const { handleStart, handleEnd } = trackTime();

await chain
.withListeners({
onStart: (run: Run) => {
handleStart(run);
},
onEnd: (run: Run) => {
handleEnd(run);
},
})
.invoke({ question: "What is the meaning of life?" });

/**
* start { startTime: 1701723365470, question: 'What is the meaning of life?' }
end {
endTime: 1701723368767,
answer: "The meaning of life is a philosophical question that has been contemplated and debated by scholars, philosophers, and individuals for centuries. The answer to this question can vary depending on one's beliefs, perspectives, and values. Some suggest that the meaning of life is to seek happiness and fulfillment, others propose it is to serve a greater purpose or contribute to the well-being of others. Ultimately, the meaning of life can be subjective and personal, and it is up to each individual to determine their own sense of purpose and meaning."
}
total time: 3297ms
*/