跳至主要内容

Searxng 搜索工具

SearxngSearch 工具将您的代理和链连接到互联网。

该工具是对 SearxNG API 的包装,用于使用 SearxNG API 执行元搜索引擎查询。它在回答有关当前事件的问题时特别有用。

用法

npm install @langchain/openai @langchain/core
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.
*/

API 参考


此页面是否有帮助?


您也可以留下详细的反馈 在 GitHub 上.