SearchApi 工具
SearchApi
工具将您的代理和链连接到互联网。
Search API 的包装器。当您需要回答有关当前事件的问题时,此工具非常方便。
用法
输入应该是搜索查询。
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/core
yarn add @langchain/openai @langchain/core
pnpm add @langchain/openai @langchain/core
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 参考
- ChatOpenAI 来自
@langchain/openai
- AgentExecutor 来自
langchain/agents
- ChatPromptTemplate 来自
@langchain/core/prompts
- RunnableSequence 来自
@langchain/core/runnables
- AgentFinish 来自
@langchain/core/agents
- AgentAction 来自
@langchain/core/agents
- BaseMessageChunk 来自
@langchain/core/messages
- SearchApi 来自
@langchain/community/tools/searchapi