From 9d5a99583d7f01e24ca0b8d1d54c4debfe62ad40 Mon Sep 17 00:00:00 2001 From: yinpeng <2291314224@qq.com> Date: Wed, 25 Dec 2024 00:36:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20Gemini=20=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=80=9D=E8=80=83=E8=BF=87=E7=A8=8B=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/config.py | 3 +- app/services/chat_service.py | 82 +++++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 85c49cd..cba379b 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -9,7 +9,8 @@ class Settings(BaseSettings): MODEL_SEARCH: List[str] = ["gemini-2.0-flash-exp"] TOOLS_CODE_EXECUTION_ENABLED: bool = False SHOW_SEARCH_LINK: bool = True - + SHOW_THINKING_PROCESS: bool = True + class Config: env_file = ".env" diff --git a/app/services/chat_service.py b/app/services/chat_service.py index 90ff3bd..1954181 100644 --- a/app/services/chat_service.py +++ b/app/services/chat_service.py @@ -72,6 +72,7 @@ class ChatService: self.base_url = base_url self.key_manager = key_manager self.thinking_first = True + self.thinking_status = False def convert_gemini_response_to_openai( self, @@ -90,31 +91,49 @@ class ChatService: parts = content.get("parts", []) if "thinking" in model: - if len(parts) == 1: - if self.thinking_first: - self.thinking_first = False - text = "\nπŸ€” **思考过程** πŸ€”\n---\n```\n" + parts[ - 0 - ].get("text") - else: - text = parts[0].get("text") - elif len(parts) == 2: - if self.thinking_first: - self.thinking_first = False - text = ( - "\nπŸ€” **思考过程** πŸ€”\n---\n```\n" - + parts[0].get("text") - + "\n```\n---\n" - + parts[1].get("text") - ) - else: - text = ( - parts[0].get("text") - + "\n```\n---\n" - + parts[1].get("text") - ) + if settings.SHOW_THINKING_PROCESS: + if len(parts) == 1: + if self.thinking_first: + self.thinking_first = False + self.thinking_status = True + text = "【思考过程】\n\n" + parts[0].get("text") + else: + text = parts[0].get("text") + + if len(parts) == 2: + self.thinking_status = False + if self.thinking_first: + self.thinking_first = False + text = ( + "【思考过程】\n\n" + + parts[0].get("text") + + "\n---\nγ€θΎ“ε‡Ίη»“ζžœγ€‘\n\n" + + parts[1].get("text") + ) + else: + text = ( + parts[0].get("text") + + "\n---\nγ€θΎ“ε‡Ίη»“ζžœγ€‘\n\n" + + parts[1].get("text") + ) else: - text = "" + if len(parts) == 1: + if self.thinking_first: + self.thinking_first = False + self.thinking_status = True + text = "" + elif self.thinking_status: + text = "" + else: + text = parts[0].get("text") + + if len(parts) == 2: + self.thinking_status = False + if self.thinking_first: + self.thinking_first = False + text = parts[1].get("text") + else: + text = parts[1].get("text") else: if "text" in parts[0]: text = parts[0].get("text") @@ -180,12 +199,15 @@ class ChatService: if response.get("candidates"): candidate = response["candidates"][0] if "thinking" in model: - text = ( - "\nπŸ€” **思考过程** πŸ€”\n---\n```\n" - + candidate["content"]["parts"][0]["text"] - + "\n```\n---\n" - + candidate["content"]["parts"][1]["text"] - ) + if settings.SHOW_THINKING_PROCESS: + text = ( + "【思考过程】\n\n" + + candidate["content"]["parts"][0]["text"] + + "\n---\nγ€θΎ“ε‡Ίη»“ζžœγ€‘\n\n" + + candidate["content"]["parts"][1]["text"] + ) + else: + text = candidate["content"]["parts"][1]["text"] else: text = candidate["content"]["parts"][0]["text"]