Open djaffer opened 1 month ago
The issue with the AgentExecutor
not working for Searxng or DuckDuckgo in LangChain.js might be due to the tools not being properly integrated or recognized within the AgentExecutor
. Here is an example of how to correctly instantiate and use the AgentExecutor
with the SearxngSearch
tool:
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor } from "langchain/agents";
import { BaseMessageChunk } from "@langchain/core/messages";
import { AgentAction, AgentFinish } from "@langchain/core/agents";
import { RunnableSequence } from "@langchain/core/runnables";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { SearxngSearch } from "@langchain/community/tools/searxng_search";
const model = new ChatOpenAI({
maxTokens: 1000,
model: "gpt-4",
});
// `apiBase` will be automatically parsed from .env file, set "SEARXNG_API_BASE" in .env,
const tools = [
new SearxngSearch({
params: {
format: "json", // Do not change this, format other than "json" is will throw error
engines: "google",
},
// Custom Headers to support rapidAPI authentication Or any instance that requires custom headers
headers: {},
}),
];
const prefix = ChatPromptTemplate.fromMessages([
[
"ai",
"Answer the following questions as best you can. In your final answer, use a bulleted list markdown format.",
],
["human", "{input}"],
]);
// Replace this with your actual output parser.
const customOutputParser = (
input: BaseMessageChunk
): AgentAction | AgentFinish => ({
log: "test",
returnValues: {
output: input,
},
});
// Replace this placeholder agent with your actual implementation.
const agent = RunnableSequence.from([prefix, model, customOutputParser]);
const executor = AgentExecutor.fromAgentAndTools({
agent,
tools,
});
console.log("Loaded agent.");
const input = `What is Langchain? Describe in 50 words`;
console.log(`Executing with input "${input}"...`);
const result = await executor.invoke({ input });
console.log(result);
/**
* Langchain is a framework for developing applications powered by language models, such as chatbots, Generative Question-Answering, summarization, and more. It provides a standard interface, integrations with other tools, and end-to-end chains for common applications. Langchain enables data-aware and powerful applications.
*/
Ensure that SearxngSearch
is correctly implemented and extends the Tool
class, and that it is properly recognized by the AgentExecutor
. If these tools are not correctly implemented or recognized, the AgentExecutor
will not be able to use them, leading to the issue you are experiencing [1][2].
To continue talking to Dosu, mention @dosu.
What is the update on this?
The issue with the AgentExecutor
not calling the _call
function in your setup might be due to the customOutputParser
not correctly parsing the output into a format that the AgentExecutor
can use. Ensure that the customOutputParser
function returns an object that matches the AgentAction
or AgentFinish
interface.
Here's an example of how to correctly implement the customOutputParser
:
const customOutputParser = (text: string): AgentAction | AgentFinish => {
if (text.includes("Final Answer:")) {
const parts = text.split("Final Answer:");
const input = parts[parts.length - 1].trim();
const finalAnswers = { output: input };
return { log: text, returnValues: finalAnswers };
}
const match = /Action: (.*)\nAction Input: (.*)/s.exec(text);
if (!match) {
throw new Error(`Could not parse LLM output: ${text}`);
}
return {
tool: match[1].trim(),
toolInput: match[2].trim().replace(/^"+|"+$/g, ""),
log: text,
};
};
Ensure that the SearxngSearch
tool is correctly implemented and extends the Tool
class. The AgentExecutor
should be able to recognize and use it if it is properly configured.
If the issue persists, you might want to check the verbose logs to get more insights into the execution process. This can help identify any problems with tool invocation or output parsing.
For more details on how the SearxngSearch
tool handles API requests and responses, you can refer to the implementation in the LangChain.js repository [1][2].
To continue talking to Dosu, mention @dosu.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
The toolchain does not get to _call
System Info