mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-13 20:53:45 +08:00
feat: 支持duckduckgo搜索插件
This commit is contained in:
32
app/api/langchain-tools/duckduckgo.ts
Normal file
32
app/api/langchain-tools/duckduckgo.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { SafeSearchType, search } from "duck-duck-scrape";
|
||||
import { convert as htmlToText } from "html-to-text";
|
||||
import { Tool } from "langchain/tools";
|
||||
|
||||
export class DuckDuckGo extends Tool {
|
||||
name = "duckduckgo_search";
|
||||
maxResults = 4;
|
||||
|
||||
/** @ignore */
|
||||
async _call(input: string) {
|
||||
const searchResults = await search(input, {
|
||||
safeSearch: SafeSearchType.OFF,
|
||||
});
|
||||
|
||||
if (searchResults.noResults) {
|
||||
return "No good search result found";
|
||||
}
|
||||
|
||||
const results = searchResults.results
|
||||
.slice(0, this.maxResults)
|
||||
.map(
|
||||
({ title, description, url }) =>
|
||||
`title:${title}\ncontent:${htmlToText(description)}\nurl:${url}`,
|
||||
)
|
||||
.join("\n\n");
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
description =
|
||||
"a search engine. useful for when you need to answer questions about current events. input should be a search query.";
|
||||
}
|
||||
Reference in New Issue
Block a user