跳到主要内容

如何取消执行

前提条件

本指南假定您熟悉以下概念

在构建长时间运行的链或 LangGraph 代理时,您可能希望在用户离开您的应用或提交新查询等情况下中断执行。

LangChain 表达式语言 (LCEL) 支持通过运行时 signal 选项中止正在进行的 runnables。

兼容性

内置信号支持需要 @langchain/core>=0.2.20。请点击此处查看升级指南

注意:诸如聊天模型或检索器之类的个别集成可能缺少或具有不同的中止执行的实现。本指南中描述的信号支持将应用于链的步骤之间。

为了了解其工作原理,请构建一个链,例如下面执行检索增强生成的链。它通过首先使用 Tavily 搜索网络来回答问题,然后将结果传递给聊天模型以生成最终答案

选择您的聊天模型

安装依赖项

提示

请参阅 此部分,了解有关安装集成包的通用说明.

yarn add @langchain/groq 

添加环境变量

GROQ_API_KEY=your-api-key

实例化模型

import { ChatGroq } from "@langchain/groq";

const model = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0
});
import { TavilySearchAPIRetriever } from "@langchain/community/retrievers/tavily_search_api";
import type { Document } from "@langchain/core/documents";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";

const formatDocsAsString = (docs: Document[]) => {
return docs.map((doc) => doc.pageContent).join("\n\n");
};

const retriever = new TavilySearchAPIRetriever({
k: 3,
});

const prompt = ChatPromptTemplate.fromTemplate(`
Use the following context to answer questions to the best of your ability:

<context>
{context}
</context>

Question: {question}`);

const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocsAsString),
question: new RunnablePassthrough(),
},
prompt,
llm,
new StringOutputParser(),
]);

如果您正常调用它,您可以看到它返回最新的信息

await chain.invoke("what is the current weather in SF?");
Based on the provided context, the current weather in San Francisco is:

Temperature: 17.6°C (63.7°F)
Condition: Sunny
Wind: 14.4 km/h (8.9 mph) from WSW direction
Humidity: 74%
Cloud cover: 15%

The information indicates it's a sunny day with mild temperatures and light winds. The data appears to be from August 2, 2024, at 17:00 local time.

现在,让我们提前中断它。初始化一个 AbortController 并将其 signal 属性传递到链执行中。为了说明取消尽快发生的事实,设置 100 毫秒的超时

const controller = new AbortController();

const startTimer = console.time("timer1");

setTimeout(() => controller.abort(), 100);

try {
await chain.invoke("what is the current weather in SF?", {
signal: controller.signal,
});
} catch (e) {
console.log(e);
}

console.timeEnd("timer1");
Error: Aborted
at EventTarget.<anonymous> (/Users/jacoblee/langchain/langchainjs/langchain-core/dist/utils/signal.cjs:19:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:825:20)
at EventTarget.dispatchEvent (node:internal/event_target:760:26)
at abortSignal (node:internal/abort_controller:370:10)
at AbortController.abort (node:internal/abort_controller:392:5)
at Timeout._onTimeout (evalmachine.<anonymous>:7:29)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
timer1: 103.204ms

您可以看到执行在 100 毫秒多一点后结束。查看此 LangSmith 追踪,您可以看到模型从未被调用。

流式传输

您也可以在流式传输时传递信号。这使您可以更好地控制在 for await... of 循环中使用 break 语句来取消当前运行,这仅在最终输出已经开始流式传输后才会触发。以下示例使用 break 语句 - 请注意取消发生前经过的时间

const startTimer2 = console.time("timer2");

const stream = await chain.stream("what is the current weather in SF?");

for await (const chunk of stream) {
console.log("chunk", chunk);
break;
}

console.timeEnd("timer2");
chunk
timer2: 3.990s

现在将其与使用信号进行比较。请注意,您需要将流包装在 try/catch 块中

const controllerForStream = new AbortController();

const startTimer3 = console.time("timer3");

setTimeout(() => controllerForStream.abort(), 100);

try {
const streamWithSignal = await chain.stream(
"what is the current weather in SF?",
{
signal: controllerForStream.signal,
}
);
for await (const chunk of streamWithSignal) {
console.log(chunk);
break;
}
} catch (e) {
console.log(e);
}

console.timeEnd("timer3");
Error: Aborted
at EventTarget.<anonymous> (/Users/jacoblee/langchain/langchainjs/langchain-core/dist/utils/signal.cjs:19:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:825:20)
at EventTarget.dispatchEvent (node:internal/event_target:760:26)
at abortSignal (node:internal/abort_controller:370:10)
at AbortController.abort (node:internal/abort_controller:392:5)
at Timeout._onTimeout (evalmachine.<anonymous>:7:38)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
timer3: 100.684ms

此页对您有帮助吗?


您也可以留下详细的反馈 在 GitHub 上.