Replies: 2 comments
-
To stop the execution of an agent inside a dynamic tool in LangChain JS, you can use the import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { OpenAI } from "@langchain/openai";
import { Calculator } from "@langchain/community/tools/calculator";
import { SerpAPI } from "@langchain/community/tools/serpapi";
const model = new OpenAI({ temperature: 0 });
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
});
const controller = new AbortController();
// Call `controller.abort()` somewhere to cancel the request.
setTimeout(() => {
controller.abort();
}, 2000);
try {
const input = `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`;
const result = await executor.invoke({ input, signal: controller.signal });
} catch (e) {
console.log(e);
/*
Error: Cancel: canceled
at file:///Users/nuno/dev/langchainjs/langchain/dist/util/async_caller.js:60:23
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at RetryOperation._fn (/Users/nuno/dev/langchainjs/node_modules/p-retry/index.js:50:12) {
attemptNumber: 1,
retriesLeft: 6
}
*/
} In this example, the Note that 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 [1][2][3]. |
Beta Was this translation helpful? Give feedback.
-
@valdo99 What happens if I use return_direct? |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
im using langchain js to create an agent for shopping.
but in some cases i need to stop the agent executor because i have custom business logics that doesnt need anymore the generation...
System Info
langchain@0.2.4 | MIT | deps: 16 | versions: 276
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/
keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores
dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.2.4.tgz
.shasum: a85295e2510425cc5f4aca32bc9adeda88f6918d
.integrity: sha512-zBsBuNREn/3IlWvIQqhQ2iqf6JJhyjjsB1Db/keDkcgThPI3EcblC1pqAXU2BIKHmpNUkHBR2bAUok5+xtgOcw==
.unpackedSize: 4.0 MB
dependencies:
@langchain/core: ~0.2.0 js-tiktoken: ^1.0.12 langsmith: ~0.1.30 uuid: ^9.0.0
@langchain/openai: ~0.1.0 js-yaml: ^4.1.0 ml-distance: ^4.0.0 yaml: ^2.2.1
@langchain/textsplitters: ~0.0.0 jsonpointer: ^5.0.1 openapi-types: ^12.1.3 zod-to-json-schema: ^3.22.3
binary-extensions: ^2.2.0 langchainhub: ~0.0.8 p-retry: 4 zod: ^3.22.4
maintainers:
dist-tags:
latest: 0.2.4 next: 0.2.3-rc.0
published 16 hours ago by jacoblee93 jacoblee93@gmail.com
Beta Was this translation helpful? Give feedback.
All reactions