Skip to main content

SearchApi 工具

SearchApi 工具可以将你的智能体(agents)和链(chains)连接到互联网。

这是对搜索API的一个封装。当你需要回答与时事相关的问题时,这个工具会非常有用。

使用方法

输入应为一个搜索查询。

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install @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({
model: "gpt-4o-mini",
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:

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.