Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hk-Gosuto committed Oct 12, 2024
1 parent 3f78a64 commit d781d61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
- ⚠ 注意:已知在 vercel 环境下会出现调用不稳定的情况 https://github.com/Hk-Gosuto/ChatGPT-Next-Web-LangChain/issues/89#issuecomment-1868887904

- 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)
Expand Down
11 changes: 5 additions & 6 deletions app/api/langchain-tools/duckduckgo_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { decode } from "html-entities";
import { convert as htmlToText } from "html-to-text";
import { Tool } from "@langchain/core/tools";

const API_PROXY_PREFIX = process.env.DDG_API_PROXY_PREFIX ?? "";

const SEARCH_REGEX =
/DDG\.pageLayout\.load\('d',(\[.+\])\);DDG\.duckbar\.load\('images'/;
const IMAGES_REGEX =
Expand Down Expand Up @@ -325,7 +327,7 @@ async function search(
};

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();

Expand Down Expand Up @@ -369,7 +371,7 @@ async function search(
description: decode(search.a),
rawDescription: search.a,
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,
bang,
});
Expand Down Expand Up @@ -456,7 +458,7 @@ function queryString(query: Record<string, string>) {
async function getVQD(query: string, ia = "web") {
try {
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();
return VQD_REGEX.exec(data)![1];
Expand Down Expand Up @@ -516,16 +518,13 @@ export class DuckDuckGo extends Tool {
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 }) => htmlToText(description))
.join("\n\n");

return results;
}

Expand Down

0 comments on commit d781d61

Please sign in to comment.