From a8cf01a00c7a841eaa2b54c62714ca3811018ac8 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 13 Oct 2025 21:21:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=86=85=E7=BD=AE=E7=BD=91?= =?UTF-8?q?=E9=A1=B5=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8E=A5=E5=85=A5=E7=99=BE=E5=BA=A6=20AI=20=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/config/default.py | 11 ++++++++- packages/web_searcher/main.py | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 8831c7e14..b7c860fe0 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -57,6 +57,7 @@ "web_search": False, "websearch_provider": "default", "websearch_tavily_key": [], + "webseach_baidu_app_builder_key": "", "web_search_link": False, "display_reasoning_text": False, "identifier": False, @@ -2063,7 +2064,7 @@ "provider_settings.websearch_provider": { "description": "网页搜索提供商", "type": "string", - "options": ["default", "tavily"], + "options": ["default", "tavily", "baidu_ai_search"], }, "provider_settings.websearch_tavily_key": { "description": "Tavily API Key", @@ -2074,6 +2075,14 @@ "provider_settings.websearch_provider": "tavily", }, }, + "provider_settings.websearch_baidu_app_builder_key": { + "description": "百度千帆智能云 APP Builder API Key", + "type": "string", + "hint": "参考:https://console.bce.baidu.com/iam/#/iam/apikey/list", + "condition": { + "provider_settings.websearch_provider": "baidu_ai_search", + }, + }, "provider_settings.web_search_link": { "description": "显示来源引用", "type": "bool", diff --git a/packages/web_searcher/main.py b/packages/web_searcher/main.py index 2bcebc21f..77541af9b 100644 --- a/packages/web_searcher/main.py +++ b/packages/web_searcher/main.py @@ -52,6 +52,8 @@ def __init__(self, context: star.Context) -> None: except Exception as e: logger.error(f"google search init error: {e}, disable google search") + self.baidu_initialized = False + async def _tidy_text(self, text: str) -> str: """清理文本,去除空格、换行符等""" return text.strip().replace("\n", " ").replace("\r", " ").replace(" ", " ") @@ -225,6 +227,30 @@ async def search_from_search_engine( return ret + async def ensure_baidu_ai_search_mcp(self, umo: str | None = None): + if self.baidu_initialized: + return + cfg = self.context.get_config(umo=umo) + key = cfg.get("provider_settings", {}).get( + "websearch_baidu_app_builder_key", "" + ) + if not key: + raise ValueError( + "Error: Baidu AI Search API key is not configured in AstrBot." + ) + self.baidu_initialized = True + func_tool_mgr = self.context.get_llm_tool_manager() + await func_tool_mgr.enable_mcp_server( + "baidu_ai_search", + config={ + "transport": "sse", + "url": f"http://appbuilder.baidu.com/v2/ai_search/mcp/sse?api_key={key}", + "headers": {}, + "timeout": 30, + }, + ) + logger.info("Successfully initialized Baidu AI Search MCP server.") + @llm_tool(name="fetch_url") async def fetch_website_content(self, event: AstrMessageEvent, url: str) -> str: """fetch the content of a website with the given web url @@ -371,6 +397,7 @@ async def edit_web_search_tools( tool_set.add_tool(fetch_url_t) tool_set.remove_tool("web_search_tavily") tool_set.remove_tool("tavily_extract_web_page") + tool_set.remove_tool("AIsearch") elif provider == "tavily": web_search_tavily = func_tool_mgr.get_func("web_search_tavily") tavily_extract_web_page = func_tool_mgr.get_func("tavily_extract_web_page") @@ -380,3 +407,17 @@ async def edit_web_search_tools( tool_set.add_tool(tavily_extract_web_page) tool_set.remove_tool("web_search") tool_set.remove_tool("fetch_url") + tool_set.remove_tool("AIsearch") + elif provider == "baidu_ai_search": + try: + await self.ensure_baidu_ai_search_mcp(event.unified_msg_origin) + aisearch_tool = func_tool_mgr.get_func("AIsearch") + if not aisearch_tool: + raise ValueError("Cannot get Baidu AI Search MCP tool.") + tool_set.add_tool(aisearch_tool) + tool_set.remove_tool("web_search") + tool_set.remove_tool("fetch_url") + tool_set.remove_tool("web_search_tavily") + tool_set.remove_tool("tavily_extract_web_page") + except Exception as e: + logger.error(f"Cannot Initialize Baidu AI Search MCP Server: {e}") From 0f11583bd76764efc9232347cbf834fdeaeb5774 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 13 Oct 2025 21:26:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=AD=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=B8=BA=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=E9=94=AE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/config/default.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index b7c860fe0..2d5ce7bf3 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -57,7 +57,7 @@ "web_search": False, "websearch_provider": "default", "websearch_tavily_key": [], - "webseach_baidu_app_builder_key": "", + "websearch_baidu_app_builder_key": "", "web_search_link": False, "display_reasoning_text": False, "identifier": False, From 14d355275634debd394ff5c24f6a71d1cf947209 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Tue, 14 Oct 2025 09:33:42 +0800 Subject: [PATCH 3/3] Fix Baidu AI Search initialization logic --- packages/web_searcher/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web_searcher/main.py b/packages/web_searcher/main.py index 77541af9b..90c3f502d 100644 --- a/packages/web_searcher/main.py +++ b/packages/web_searcher/main.py @@ -238,7 +238,6 @@ async def ensure_baidu_ai_search_mcp(self, umo: str | None = None): raise ValueError( "Error: Baidu AI Search API key is not configured in AstrBot." ) - self.baidu_initialized = True func_tool_mgr = self.context.get_llm_tool_manager() await func_tool_mgr.enable_mcp_server( "baidu_ai_search", @@ -249,6 +248,7 @@ async def ensure_baidu_ai_search_mcp(self, umo: str | None = None): "timeout": 30, }, ) + self.baidu_initialized = True logger.info("Successfully initialized Baidu AI Search MCP server.") @llm_tool(name="fetch_url")