Cancelling requests
You can cancel a request by passing a signal
option when you call the model. For example, for OpenAI:
- npm
- Yarn
- pnpm
npm install @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
const model = new ChatOpenAI({ temperature: 1 });
const controller = new AbortController();
// Call `controller.abort()` somewhere to cancel the request.
const res = await model.invoke(
[
new HumanMessage(
"What is a good name for a company that makes colorful socks?"
),
],
{ signal: controller.signal }
);
console.log(res);
/*
'\n\nSocktastic Colors'
*/
API Reference:
- ChatOpenAI from
@langchain/openai
- HumanMessage from
@langchain/core/messages
Note, this will only cancel the outgoing request if the underlying provider exposes that option. LangChain will cancel the underlying request if possible, otherwise it will cancel the processing of the response.