SearchAPI Example Not Working #5273
-
Checked other resources
Commit to Help
Example Codeimport { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { AgentFinish, AgentAction } from "@langchain/core/agents";
import { BaseMessageChunk } from "@langchain/core/messages";
import { SearchApi } from "@langchain/community/tools/searchapi";
const model = new ChatOpenAI({
temperature: 0,
});
const tools = [
new SearchApi(process.env.SEARCHAPI_API_KEY, {
engine: "google_news",
}),
];
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,
});
const res = await executor.invoke({
input: "What's happening in Ukraine today?",
});
console.log(res); DescriptionI'm attempting to use the SearchAPI tool according to the documentation. I have copied the example exactly, but the tool is never invoked. {
input: "What's happening in Ukraine today?",
output: AIMessageChunk {
lc_serializable: true,
lc_kwargs: {
content: 'As of today, the situation in Ukraine is constantly evolving. Here are some key points:\n' +
'\n' +
'- Ukraine continues to face ongoing conflict in the eastern regions of Donetsk and Luhansk, where fighting between Ukrainian forces and Russian-backed separatists persists.\n' +
'- The issue of Crimea, which was annexed by Russia in 2014, remains a point of contention between Ukraine and Russia.\n' +
'- Ukraine is also dealing with political challenges, including corruption and economic struggles.\n' +
'- The country is working towards closer ties with the European Union and NATO, while also navigating its relationship with Russia.\n' +
'\n' +
"Please note that the situation in Ukraine can change rapidly, so it's important to stay updated with the latest news.",
additional_kwargs: {},
response_metadata: [Object],
tool_call_chunks: [],
tool_calls: [],
invalid_tool_calls: []
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'As of today, the situation in Ukraine is constantly evolving. Here are some key points:\n' +
'\n' +
'- Ukraine continues to face ongoing conflict in the eastern regions of Donetsk and Luhansk, where fighting between Ukrainian forces and Russian-backed separatists persists.\n' +
'- The issue of Crimea, which was annexed by Russia in 2014, remains a point of contention between Ukraine and Russia.\n' +
'- Ukraine is also dealing with political challenges, including corruption and economic struggles.\n' +
'- The country is working towards closer ties with the European Union and NATO, while also navigating its relationship with Russia.\n' +
'\n' +
"Please note that the situation in Ukraine can change rapidly, so it's important to stay updated with the latest news.",
name: undefined,
additional_kwargs: {},
response_metadata: { prompt: 0, completion: 0, finish_reason: 'stop' },
tool_calls: [],
invalid_tool_calls: [],
tool_call_chunks: []
}
} When i replace the input with something more targeted, like "what are today's headlines", the tool is also not invoked, & OpenAI responds with a message that it does not have real-time data. {
input: "What are today's headlines?",
output: AIMessageChunk {
lc_serializable: true,
lc_kwargs: {
content: "I'm sorry, but I am not able to provide real-time information or current news headlines. I recommend checking a reliable news source or website for the most up-to-date information.",
additional_kwargs: {},
response_metadata: [Object],
tool_call_chunks: [],
tool_calls: [],
invalid_tool_calls: []
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: "I'm sorry, but I am not able to provide real-time information or current news headlines. I recommend checking a reliable news source or website for the most up-to-date information.",
name: undefined,
additional_kwargs: {},
response_metadata: { prompt: 0, completion: 0, finish_reason: 'stop' },
tool_calls: [],
invalid_tool_calls: [],
tool_call_chunks: []
}
} What am i missing? Thank you. System Info
macos 14.4.1 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
It appears the issue with the Next, verify the compatibility of your Node.js and Yarn versions. LangChain JS library requires Node.js version 18 or higher and Yarn version 3.4.1. Using versions outside these requirements could lead to unexpected behavior. Also, review the tool invocation logic within your application. The Lastly, there's a known issue in LangChain JS version 0.1.36 related to the handling of the API key in the To diagnose further, consider adding logging statements at various points in the process to trace the execution flow and identify where it might be failing. Additionally, ensure that all dependencies are correctly set up as per the LangChain documentation and the provided example.
|
Beta Was this translation helpful? Give feedback.
-
@dosu If i replace the |
Beta Was this translation helpful? Give feedback.
To address the issue of the
SearchApi
tool not being invoked in your LangChain application, it's crucial to understand that tools are invoked based on the agent's plan, which dynamically determines the sequence of actions, including tool usage. This means that simply including a tool in the tools array doesn't guarantee its invocation. The agent must be explicitly programmed to use the tool in response to certain conditions or inputs.For your specific case, consider using a more direct approach with a tool designed for web searches, such as
TavilySearchResults
from the@langchain/community/tools/tavily_search
module. Here's how you can integrate it: