From 67dbc3a70c617cf7ddafd56d8b91992a1713ca07 Mon Sep 17 00:00:00 2001 From: MCjiaozi <1503773566@qq.com> Date: Tue, 5 Aug 2025 15:31:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E5=BF=BD=E7=95=A5=20Gemini=20?= =?UTF-8?q?=E2=80=9C=E8=BF=94=E5=9B=9E=E5=86=85=E5=AE=B9=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E2=80=9D=E7=9A=84=E9=94=99=E8=AF=AF=E6=B6=88=E6=81=AF=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=20OpenAI=20=E5=92=8C=20Gemini=20=E7=9A=84=E6=AD=A4?= =?UTF-8?q?=E7=B1=BB=E6=B6=88=E6=81=AF=E4=BB=A5=20info=20=E7=BA=A7?= =?UTF-8?q?=E5=88=AB=E8=BE=93=E5=87=BA=E5=9C=A8=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E4=B8=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/provider/sources/gemini_source.py | 39 ++++++++++--------- .../core/provider/sources/openai_source.py | 3 ++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 56526c121..eaffdd8c3 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -344,30 +344,31 @@ def _process_content_parts( if not result_parts: logger.debug(result.candidates) - raise Exception("API 返回的内容为空。") + logger.info("API 可能返回了空消息。") chain = [] part: types.Part + if result_parts: # 避免遍历None导致报错 # 暂时这样Fallback - if all( - part.inline_data and part.inline_data.mime_type.startswith("image/") - for part in result_parts - ): - chain.append(Comp.Plain("这是图片")) - for part in result_parts: - if part.text: - chain.append(Comp.Plain(part.text)) - elif part.function_call: - llm_response.role = "tool" - llm_response.tools_call_name.append(part.function_call.name) - llm_response.tools_call_args.append(part.function_call.args) - # gemini 返回的 function_call.id 可能为 None - llm_response.tools_call_ids.append( - part.function_call.id or part.function_call.name - ) - elif part.inline_data and part.inline_data.mime_type.startswith("image/"): - chain.append(Comp.Image.fromBytes(part.inline_data.data)) + if all( + part.inline_data and part.inline_data.mime_type.startswith("image/") + for part in result_parts + ): + chain.append(Comp.Plain("这是图片")) + for part in result_parts: + if part.text: + chain.append(Comp.Plain(part.text)) + elif part.function_call: + llm_response.role = "tool" + llm_response.tools_call_name.append(part.function_call.name) + llm_response.tools_call_args.append(part.function_call.args) + # gemini 返回的 function_call.id 可能为 None + llm_response.tools_call_ids.append( + part.function_call.id or part.function_call.name + ) + elif part.inline_data and part.inline_data.mime_type.startswith("image/"): + chain.append(Comp.Image.fromBytes(part.inline_data.data)) return MessageChain(chain=chain) async def _query(self, payloads: dict, tools: FuncCall) -> LLMResponse: diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index 14c2da2de..b575f9a18 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -214,6 +214,9 @@ async def parse_openai_completion( logger.error(f"API 返回的 completion 无法解析:{completion}。") raise Exception(f"API 返回的 completion 无法解析:{completion}。") + if llm_response.completion_text == "": + logger.info("API 可能返回了空消息。") + llm_response.raw_completion = completion return llm_response From bf1727441179ab2596a865022fe69049d3d97e96 Mon Sep 17 00:00:00 2001 From: MCjiaozi <1503773566@qq.com> Date: Tue, 5 Aug 2025 17:23:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=20Gemini=20?= =?UTF-8?q?=E7=A9=BA=E6=B6=88=E6=81=AF=E8=BF=94=E5=9B=9E=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=20OpenAI=20=E5=92=8C=20Gemini=20=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B6=88=E6=81=AF=E6=8F=90=E7=A4=BA=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BB=A5=20debug=20=E7=BA=A7=E5=88=AB=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=9C=A8=E6=8E=A7=E5=88=B6=E5=8F=B0=E4=B8=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/provider/sources/gemini_source.py | 40 +++++++++---------- .../core/provider/sources/openai_source.py | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index eaffdd8c3..0e145905c 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -344,31 +344,31 @@ def _process_content_parts( if not result_parts: logger.debug(result.candidates) - logger.info("API 可能返回了空消息。") + logger.debug("API 可能返回了空消息。") + return MessageChain(chain=[]) chain = [] part: types.Part - if result_parts: # 避免遍历None导致报错 # 暂时这样Fallback - if all( - part.inline_data and part.inline_data.mime_type.startswith("image/") - for part in result_parts - ): - chain.append(Comp.Plain("这是图片")) - for part in result_parts: - if part.text: - chain.append(Comp.Plain(part.text)) - elif part.function_call: - llm_response.role = "tool" - llm_response.tools_call_name.append(part.function_call.name) - llm_response.tools_call_args.append(part.function_call.args) - # gemini 返回的 function_call.id 可能为 None - llm_response.tools_call_ids.append( - part.function_call.id or part.function_call.name - ) - elif part.inline_data and part.inline_data.mime_type.startswith("image/"): - chain.append(Comp.Image.fromBytes(part.inline_data.data)) + if all( + part.inline_data and part.inline_data.mime_type.startswith("image/") + for part in result_parts + ): + chain.append(Comp.Plain("这是图片")) + for part in result_parts: + if part.text: + chain.append(Comp.Plain(part.text)) + elif part.function_call: + llm_response.role = "tool" + llm_response.tools_call_name.append(part.function_call.name) + llm_response.tools_call_args.append(part.function_call.args) + # gemini 返回的 function_call.id 可能为 None + llm_response.tools_call_ids.append( + part.function_call.id or part.function_call.name + ) + elif part.inline_data and part.inline_data.mime_type.startswith("image/"): + chain.append(Comp.Image.fromBytes(part.inline_data.data)) return MessageChain(chain=chain) async def _query(self, payloads: dict, tools: FuncCall) -> LLMResponse: diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index b575f9a18..9ce5315ff 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -215,7 +215,7 @@ async def parse_openai_completion( raise Exception(f"API 返回的 completion 无法解析:{completion}。") if llm_response.completion_text == "": - logger.info("API 可能返回了空消息。") + logger.debug("API 可能返回了空消息。") llm_response.raw_completion = completion From f7b5f7219e533967e3bbdb5d64d7f76c5f4d30d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E4=BB=AC=E7=9A=84=E9=A5=BA=E5=AD=90?= <64739528+MCjiaozi@users.noreply.github.com> Date: Tue, 5 Aug 2025 17:33:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E5=88=A0=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/provider/sources/gemini_source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index 0e145905c..09faffbd7 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -356,7 +356,7 @@ def _process_content_parts( for part in result_parts ): chain.append(Comp.Plain("这是图片")) - for part in result_parts: + for part in result_parts: if part.text: chain.append(Comp.Plain(part.text)) elif part.function_call: