From a23d5be056a96da724fdcbbf4b0c059400677fd2 Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Wed, 23 Apr 2025 12:49:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=87=8F=E5=B0=91=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=E6=9D=A1=E4=BB=B6=E5=92=8C=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/provider/sources/gemini_source.py | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index fe9cc4346..a175a3d68 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -194,6 +194,12 @@ class ProviderGoogleGenAI(Provider): image_bytes = base64.b64decode(url.split(",", 1)[1]) return types.Part.from_bytes(data=image_bytes, mime_type=mime_type) + def append_or_extend(contents: list[types.Content], part: list[types.Part], content_cls: type[types.Content]) -> None: + if contents and isinstance(contents[-1], content_cls): + contents[-1].parts.extend(part) + else: + contents.append(content_cls(parts=part)) + gemini_contents: List[types.Content] = [] native_tool_enabled = any( [ @@ -214,19 +220,12 @@ class ProviderGoogleGenAI(Provider): ] else: parts = [create_text_part(content)] - - if gemini_contents and isinstance(gemini_contents[-1], types.UserContent): - gemini_contents[-1].parts.extend(parts) - else: - gemini_contents.append(types.UserContent(parts=parts)) + append_or_extend(gemini_contents, parts, types.UserContent) elif role == "assistant": if content: parts = [types.Part.from_text(text=content)] - if gemini_contents and isinstance(gemini_contents[-1], types.ModelContent): - gemini_contents[-1].parts.extend(parts) - else: - gemini_contents.append(types.ModelContent(parts=parts)) + append_or_extend(gemini_contents, parts, types.ModelContent) elif not native_tool_enabled and "tool_calls" in message : parts = [ types.Part.from_function_call( @@ -235,10 +234,7 @@ class ProviderGoogleGenAI(Provider): ) for tool in message["tool_calls"] ] - if gemini_contents and isinstance(gemini_contents[-1], types.ModelContent): - gemini_contents[-1].parts.extend(parts) - else: - gemini_contents.append(types.ModelContent(parts=parts)) + append_or_extend(gemini_contents, parts, types.ModelContent) else: logger.warning("assistant 角色的消息内容为空,已添加空格占位") if native_tool_enabled and "tool_calls" in message: @@ -246,10 +242,7 @@ class ProviderGoogleGenAI(Provider): "检测到启用Gemini原生工具,且上下文中存在函数调用,建议使用 /reset 重置上下文" ) parts = [types.Part.from_text(text=" ")] - if gemini_contents and isinstance(gemini_contents[-1], types.ModelContent): - gemini_contents[-1].parts.extend(parts) - else: - gemini_contents.append(types.ModelContent(parts=parts)) + append_or_extend(gemini_contents, parts, types.ModelContent) elif role == "tool" and not native_tool_enabled: parts = [ @@ -261,10 +254,7 @@ class ProviderGoogleGenAI(Provider): }, ) ] - if gemini_contents and isinstance(gemini_contents[-1], types.UserContent): - gemini_contents[-1].parts.extend(parts) - else: - gemini_contents.append(types.UserContent(parts=parts)) + append_or_extend(gemini_contents, parts, types.UserContent) if gemini_contents and isinstance(gemini_contents[0], types.ModelContent): gemini_contents.pop()