From 33b64ddf39ea03c46b07b6e0f09a376b6cec50ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=A6=E7=BE=BD?= Date: Mon, 9 Jun 2025 03:55:59 +0000 Subject: [PATCH 1/4] feat: enhance tool selection logic for Gemini model versions --- .../core/provider/sources/gemini_source.py | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 2ff017c34..edd591d87 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -142,19 +142,32 @@ class ProviderGoogleGenAI(Provider): modalities = ["Text"] tool_list = None + model_name = self.get_model() native_coderunner = self.provider_config.get("gm_native_coderunner", False) native_search = self.provider_config.get("gm_native_search", False) - if native_coderunner: - tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] + if "gemini-2.5" in model_name: + if native_coderunner: + tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] if native_search: - logger.warning("已启用代码执行工具,搜索工具将被忽略") - if tools: - logger.warning("已启用代码执行工具,函数工具将被忽略") - elif native_search: - tool_list = [types.Tool(google_search=types.GoogleSearch())] - if tools: - logger.warning("已启用搜索工具,函数工具将被忽略") + if tool_list: + tool_list.append(types.Tool(google_search=types.GoogleSearch())) + else: + tool_list = [types.Tool(google_search=types.GoogleSearch())] + elif "gemini-2.0-lite" in model_name: + if native_coderunner or native_search: + logger.warning("gemini-2.0-lite 不支持代码执行和搜索工具,将忽略这些设置") + tool_list = None + else: + if native_coderunner: + tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] + if native_search: + logger.warning("已启用代码执行工具,搜索工具将被忽略") + elif native_search: + tool_list = [types.Tool(google_search=types.GoogleSearch())] + + if tools and tool_list: + logger.warning("已启用原生工具,函数工具将被忽略") elif tools and (func_desc := tools.get_func_desc_google_genai_style()): tool_list = [ types.Tool(function_declarations=func_desc["function_declarations"]) From 7fe1c1ec890f2f4970101a22ef26c8c3192500d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=A6=E7=BE=BD?= Date: Mon, 9 Jun 2025 04:54:24 +0000 Subject: [PATCH 2/4] feat: add URL context feature to Gemini model configuration --- astrbot/core/config/default.py | 6 +++ .../core/provider/sources/gemini_source.py | 53 ++++++++++++++----- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 6af27337b..fb7ca32db 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -1024,6 +1024,12 @@ CONFIG_METADATA_2 = { "hint": "启用后所有函数工具将全部失效", "obvious_hint": True, }, + "gm_url_context": { + "description": "启用URL上下文功能", + "type": "bool", + "hint": "启用后所有函数工具将全部失效", + "obvious_hint": True, + }, "gm_safety_settings": { "description": "安全过滤器", "type": "object", diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index edd591d87..12736532a 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -141,30 +141,58 @@ class ProviderGoogleGenAI(Provider): logger.warning("流式输出不支持图片模态,已自动降级为文本模态") modalities = ["Text"] - tool_list = None + tool_list = [] model_name = self.get_model() native_coderunner = self.provider_config.get("gm_native_coderunner", False) native_search = self.provider_config.get("gm_native_search", False) + url_context = self.provider_config.get("gm_url_context", False) if "gemini-2.5" in model_name: if native_coderunner: - tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] - if native_search: - if tool_list: + tool_list.append(types.Tool(code_execution=types.ToolCodeExecution())) + if native_search: + logger.warning("代码执行工具与搜索工具互斥,已忽略搜索工具") + if url_context: + logger.warning( + "代码执行工具与URL上下文工具互斥,已忽略URL上下文工具" + ) + else: + if native_search: tool_list.append(types.Tool(google_search=types.GoogleSearch())) - else: - tool_list = [types.Tool(google_search=types.GoogleSearch())] + + if url_context: + if hasattr(types, "UrlContext"): + tool_list.append(types.Tool(web_context=types.UrlContext())) + else: + logger.warning( + "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包" + ) + elif "gemini-2.0-lite" in model_name: - if native_coderunner or native_search: - logger.warning("gemini-2.0-lite 不支持代码执行和搜索工具,将忽略这些设置") - tool_list = None + if native_coderunner or native_search or url_context: + logger.warning( + "gemini-2.0-lite 不支持代码执行、搜索工具和URL上下文,将忽略这些设置" + ) + tool_list = None + else: if native_coderunner: - tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] + tool_list.append(types.Tool(code_execution=types.ToolCodeExecution())) if native_search: - logger.warning("已启用代码执行工具,搜索工具将被忽略") + logger.warning("代码执行工具与搜索工具互斥,已忽略搜索工具") elif native_search: - tool_list = [types.Tool(google_search=types.GoogleSearch())] + tool_list.append(types.Tool(google_search=types.GoogleSearch())) + + if url_context and not native_coderunner: + if hasattr(types, "UrlContext"): + tool_list.append(types.Tool(web_context=types.UrlContext())) + else: + logger.warning( + "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包" + ) + + if not tool_list: + tool_list = None if tools and tool_list: logger.warning("已启用原生工具,函数工具将被忽略") @@ -172,6 +200,7 @@ class ProviderGoogleGenAI(Provider): tool_list = [ types.Tool(function_declarations=func_desc["function_declarations"]) ] + return types.GenerateContentConfig( system_instruction=system_instruction, temperature=temperature, From e79487dd5f8a12ea9691c6c460cdb3ffdc8e0beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=A6=E7=BE=BD?= Date: Mon, 9 Jun 2025 05:03:15 +0000 Subject: [PATCH 3/4] fix: add missing config --- astrbot/core/config/default.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index fb7ca32db..664f8d371 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -620,6 +620,7 @@ CONFIG_METADATA_2 = { "gm_resp_image_modal": False, "gm_native_search": False, "gm_native_coderunner": False, + "gm_url_context": False, "gm_safety_settings": { "harassment": "BLOCK_MEDIUM_AND_ABOVE", "hate_speech": "BLOCK_MEDIUM_AND_ABOVE", From f77c4538433ecb15349511a7b8e5ce31cf22dd55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=A6=E7=BE=BD?= Date: Tue, 10 Jun 2025 00:20:35 +0000 Subject: [PATCH 4/4] fix: clean code --- astrbot/core/provider/sources/gemini_source.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 12736532a..c16b39415 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -162,7 +162,7 @@ class ProviderGoogleGenAI(Provider): if url_context: if hasattr(types, "UrlContext"): - tool_list.append(types.Tool(web_context=types.UrlContext())) + tool_list.append(types.Tool(url_context=types.UrlContext())) else: logger.warning( "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包" @@ -185,7 +185,7 @@ class ProviderGoogleGenAI(Provider): if url_context and not native_coderunner: if hasattr(types, "UrlContext"): - tool_list.append(types.Tool(web_context=types.UrlContext())) + tool_list.append(types.Tool(url_context=types.UrlContext())) else: logger.warning( "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包"