From 739f09059e587317b5b6fa7960a6765daf78174a Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Sun, 13 Apr 2025 12:43:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BAGemini=E5=8E=9F=E7=94=9F?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=89=A7=E8=A1=8C=E5=99=A8=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E6=9C=89=E9=99=90=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/config/default.py | 7 +++++++ astrbot/core/provider/sources/gemini_source.py | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 459ff622a..e31eb5570 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -528,6 +528,7 @@ CONFIG_METADATA_2 = { "model": "gemini-2.0-flash-exp", }, "gm_resp_image_modal": False, + "gm_native_coderunner": False, "gm_safety_settings": { "harassment": "BLOCK_MEDIUM_AND_ABOVE", "hate_speech": "BLOCK_MEDIUM_AND_ABOVE", @@ -704,6 +705,12 @@ CONFIG_METADATA_2 = { "type": "bool", "hint": "启用后,将支持返回图片内容。需要模型支持,否则会报错。具体支持模型请查看 Google Gemini 官方网站。温馨提示,如果您需要生成图片,请关闭 `启用群员识别` 配置获得更好的效果。", }, + "gm_native_coderunner": { + "description": "启用原生代码执行器", + "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 580363069..905289987 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -126,12 +126,14 @@ class ProviderGoogleGenAI(Provider): modalities = ["Text"] tool_list = None - if tools: - func_desc = tools.get_func_desc_google_genai_style() - if func_desc: - tool_list = [ - types.Tool(function_declarations=func_desc["function_declarations"]) - ] + if self.provider_config.get("gm_native_coderunner", False): + if tools: + logger.warning("Gemini原生代码执行器已启用,函数工具将被忽略") + tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] + elif tools and (func_desc := tools.get_func_desc_google_genai_style()): + tool_list = [ + types.Tool(function_declarations=func_desc["function_declarations"]) + ] return types.GenerateContentConfig( system_instruction=system_instruction, @@ -252,6 +254,10 @@ class ProviderGoogleGenAI(Provider): chain.append(Comp.Plain("这是图片")) for part in result_parts: if part.text: + if part.executable_code: + part.executable_code = None + if part.code_execution_result: + part.code_execution_result = None chain.append(Comp.Plain(part.text)) elif part.function_call: llm_response.role = "tool"