SearchApi tool
The SearchApi
tool connects your agents and chains to the internet.
A wrapper around the Search API. This tool is handy when you need to answer questions about current events.
Usage
Input should be a search query.
- npm
- Yarn
- pnpm
npm install @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
import { 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);
API Reference:
- ChatOpenAI from
@langchain/openai
- AgentExecutor from
langchain/agents
- ChatPromptTemplate from
@langchain/core/prompts
- RunnableSequence from
@langchain/core/runnables
- AgentFinish from
@langchain/core/agents
- AgentAction from
@langchain/core/agents
- BaseMessageChunk from
@langchain/core/messages
- SearchApi from
@langchain/community/tools/searchapi
Related
- Tool conceptual guide
- Tool how-to guides