mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-26 07:30:18 +09:00
feat: #311
This commit is contained in:
parent
3f78a6425e
commit
d781d61907
@ -91,6 +91,12 @@
|
|||||||
|
|
||||||
- DuckDuckGo
|
- DuckDuckGo
|
||||||
|
|
||||||
|
- 环境变量:`DDG_API_PROXY_PREFIX`
|
||||||
|
|
||||||
|
配置后将在 DuckDuckGo 插件相关接口前拼接配置内容,如:`DDG_API_PROXY_PREFIX=https://example.com/` 则最终请求为:`https://example.com/https://duckduckgo.com`
|
||||||
|
|
||||||
|
可以结合类似 1234567Yang/cf-proxy-ex 这类代理项目来实现 DuckDuckGo 插件相关接口的代理
|
||||||
|
|
||||||
- 计算
|
- 计算
|
||||||
- [Calculator](https://api.js.langchain.com/classes/langchain_tools_calculator.Calculator.html)
|
- [Calculator](https://api.js.langchain.com/classes/langchain_tools_calculator.Calculator.html)
|
||||||
- [WolframAlpha](https://api.js.langchain.com/classes/langchain_tools.WolframAlphaTool.html)
|
- [WolframAlpha](https://api.js.langchain.com/classes/langchain_tools.WolframAlphaTool.html)
|
||||||
|
@ -2,6 +2,8 @@ import { decode } from "html-entities";
|
|||||||
import { convert as htmlToText } from "html-to-text";
|
import { convert as htmlToText } from "html-to-text";
|
||||||
import { Tool } from "@langchain/core/tools";
|
import { Tool } from "@langchain/core/tools";
|
||||||
|
|
||||||
|
const API_PROXY_PREFIX = process.env.DDG_API_PROXY_PREFIX ?? "";
|
||||||
|
|
||||||
const SEARCH_REGEX =
|
const SEARCH_REGEX =
|
||||||
/DDG\.pageLayout\.load\('d',(\[.+\])\);DDG\.duckbar\.load\('images'/;
|
/DDG\.pageLayout\.load\('d',(\[.+\])\);DDG\.duckbar\.load\('images'/;
|
||||||
const IMAGES_REGEX =
|
const IMAGES_REGEX =
|
||||||
@ -325,7 +327,7 @@ async function search(
|
|||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`https://links.duckduckgo.com/d.js?${queryString(queryObject)}`,
|
`${API_PROXY_PREFIX}https://links.duckduckgo.com/d.js?${queryString(queryObject)}`,
|
||||||
);
|
);
|
||||||
const data = await response.text();
|
const data = await response.text();
|
||||||
|
|
||||||
@ -369,7 +371,7 @@ async function search(
|
|||||||
description: decode(search.a),
|
description: decode(search.a),
|
||||||
rawDescription: search.a,
|
rawDescription: search.a,
|
||||||
hostname: search.i,
|
hostname: search.i,
|
||||||
icon: `https://external-content.duckduckgo.com/ip3/${search.i}.ico`,
|
icon: `${API_PROXY_PREFIX}https://external-content.duckduckgo.com/ip3/${search.i}.ico`,
|
||||||
url: search.u,
|
url: search.u,
|
||||||
bang,
|
bang,
|
||||||
});
|
});
|
||||||
@ -456,7 +458,7 @@ function queryString(query: Record<string, string>) {
|
|||||||
async function getVQD(query: string, ia = "web") {
|
async function getVQD(query: string, ia = "web") {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`https://duckduckgo.com/?${queryString({ q: query, ia })}`,
|
`${API_PROXY_PREFIX}https://duckduckgo.com/?${queryString({ q: query, ia })}`,
|
||||||
);
|
);
|
||||||
const data = await response.text();
|
const data = await response.text();
|
||||||
return VQD_REGEX.exec(data)![1];
|
return VQD_REGEX.exec(data)![1];
|
||||||
@ -516,16 +518,13 @@ export class DuckDuckGo extends Tool {
|
|||||||
const searchResults = await search(input, {
|
const searchResults = await search(input, {
|
||||||
safeSearch: SafeSearchType.OFF,
|
safeSearch: SafeSearchType.OFF,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (searchResults.noResults) {
|
if (searchResults.noResults) {
|
||||||
return "No good search result found";
|
return "No good search result found";
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = searchResults.results
|
const results = searchResults.results
|
||||||
.slice(0, this.maxResults)
|
.slice(0, this.maxResults)
|
||||||
.map(({ title, description, url }) => htmlToText(description))
|
.map(({ title, description, url }) => htmlToText(description))
|
||||||
.join("\n\n");
|
.join("\n\n");
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user