From 1c845c5676df4dbb7d321121d5c4510fbf4f4ded Mon Sep 17 00:00:00 2001 From: Hk-Gosuto Date: Tue, 26 Dec 2023 13:23:57 +0800 Subject: [PATCH] feat: support googleCustomSearchTool --- README.md | 66 ++++++++++++++---------- app/api/langchain/tool/agent/agentapi.ts | 9 ++++ 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a25becfe3..87d875103 100644 --- a/README.md +++ b/README.md @@ -36,19 +36,53 @@ ## 主要功能 - 除插件工具外,与原项目保持一致 [ChatGPT-Next-Web 主要功能](https://github.com/Yidadaa/ChatGPT-Next-Web#主要功能) + - 支持 GPT-4V(视觉) 模型 - 需要配置对象存储服务,请参考 [对象存储服务配置指南](./docs/s3-oss.md) 配置 + - 基于 [LangChain](https://github.com/hwchase17/langchainjs) 实现的插件功能,目前支持以下插件,未来会添加更多 - - 搜索 - - [SerpAPI](https://js.langchain.com/docs/api/tools/classes/SerpAPI) - - [BingSerpAPI](https://js.langchain.com/docs/api/tools/classes/BingSerpAPI) + - 搜索(优先级:`GoogleCustomSearch > SerpAPI > BingSerpAPI > ChooseSearchEngine > DuckDuckGo`) + + - [GoogleCustomSearch](https://api.js.langchain.com/classes/langchain_tools.GoogleCustomSearch.html) + + - 环境变量: + - `GOOGLE_API_KEY` + - `GOOGLE_CSE_ID` + - 申请参考:[说明](https://stackoverflow.com/questions/37083058/programmatically-searching-google-in-python-using-custom-search) + + - [SerpAPI](https://api.js.langchain.com/classes/langchain_tools.SerpAPI.html) + + - 环境变量:`SERPAPI_API_KEY` + - 申请地址:[SerpApi: Google Search API](https://serpapi.com/) + + - [BingSerpAPI](https://api.js.langchain.com/classes/langchain_tools.BingSerpAPI.html) + + - 环境变量:`BING_SEARCH_API_KEY` + - 申请地址:[Web Search API | Microsoft Bing](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) + + - ChooseSearchEngine + + - 环境变量:`CHOOSE_SEARCH_ENGINE` + + 可选项如下: + + - google + - baidu + + - 说明:此项为直连搜索引擎,免去api试用量小的烦恼,但可能因为网络问题导致无法使用 + + - ⚠ 注意:已知在 vercel 环境下会出现调用不稳定的情况 https://github.com/Hk-Gosuto/ChatGPT-Next-Web-LangChain/issues/89#issuecomment-1868887904 + - DuckDuckGo + - 计算 - [Calculator](https://js.langchain.com/docs/api/tools_calculator/classes/Calculator) + - 网络请求 - [WebBrowser](https://js.langchain.com/docs/api/tools_webbrowser/classes/WebBrowser) - PDFBrowser - - ⚠ 仅在非vercel环境部署时可用 ⚠ + - ⚠ 仅在非 vercel 环境部署时可用 ⚠ + - 其它 - [Wiki](https://js.langchain.com/docs/api/tools/classes/WikipediaQueryRun) - DALL-E 3 @@ -60,6 +94,7 @@ - 使用本插件需要一定的专业知识,Stable Diffusion 本身的相关问题不在本项目的解答范围内,如果您确定要使用本插件请参考 [Stable Diffusion 插件配置指南](./docs/stable-diffusion-plugin-cn.md) 文档进行配置 - StableDiffusion 插件需要配置对象存储服务,请参考 [对象存储服务配置指南](./docs/s3-oss.md) 配置 - Arxiv + - 支持 Gemini-Pro 模型(同步上游仓库并修改接口为流式传输) - 以下功能目前还不支持 - **插件功能** @@ -72,11 +107,7 @@ - [x] 支持使用 DuckDuckGo 作为默认搜索引擎 - 当前配置 `SERPAPI_API_KEY` 时优先使用 `SerpAPI` 作为搜索插件,不配置时默认使用 `DuckDuckGo` 作为搜索插件。 - - 当前配置 `BING_SEARCH_API_KEY` 时优先使用 `BingSerpAPI` 作为搜索插件,不配置时默认使用 `DuckDuckGo` 作为搜索插件。 - - 优先级:`SerpAPI > BingSerpAPI > DuckDuckGo` + 不配置时默认使用 `DuckDuckGo` 作为搜索插件。 - [x] 插件列表页面开发 - [x] 支持开关指定插件 @@ -142,23 +173,6 @@ code1,code2,code3 OpanAI 密钥,你在 openai 账户页面申请的 api key。 -### `SERPAPI_API_KEY` (可选) - -[SerpApi: Google Search API](https://serpapi.com/) - -### `BING_SEARCH_API_KEY` (可选) - -[Web Search API | Microsoft Bing](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) - -### `CHOOSE_SEARCH_ENGINE` (可选) - -此项为直连搜索引擎,免去api试用量小的烦恼,但可能因为网络问题导致无法使用 - -可选项如下: - -- google -- baidu - ### `CODE` (可选) 访问密码,可选,可以使用逗号隔开多个密码。 diff --git a/app/api/langchain/tool/agent/agentapi.ts b/app/api/langchain/tool/agent/agentapi.ts index d12009838..435a4990b 100644 --- a/app/api/langchain/tool/agent/agentapi.ts +++ b/app/api/langchain/tool/agent/agentapi.ts @@ -242,6 +242,15 @@ export class AgentApi { func: async (input: string) => serpAPITool.call(input), }); } + if (process.env.GOOGLE_CSE_ID && process.env.GOOGLE_API_KEY) { + console.log("use googleCustomSearchTool"); + let googleCustomSearchTool = new langchainTools["GoogleCustomSearch"](); + searchTool = new DynamicTool({ + name: "google_custom_search", + description: googleCustomSearchTool.description, + func: async (input: string) => googleCustomSearchTool.call(input), + }); + } const tools = [];