From a059c4d586f327ecf35758c83e4b0adff77bde6b Mon Sep 17 00:00:00 2001 From: yinpeng <2291314224@qq.com> Date: Wed, 25 Dec 2024 16:04:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20Gemini=20=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=88=97=E8=A1=A8=E5=B9=B6=E4=BC=98=E5=8C=96=E6=80=9D?= =?UTF-8?q?=E8=80=83=E8=BF=87=E7=A8=8B=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/gemini_routes.py | 5 +++-- app/api/openai_routes.py | 2 +- app/core/config.py | 2 +- app/services/chat_service.py | 32 ++++++++++++++++++-------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/api/gemini_routes.py b/app/api/gemini_routes.py index 056f471..acaeb4c 100644 --- a/app/api/gemini_routes.py +++ b/app/api/gemini_routes.py @@ -29,8 +29,9 @@ async def list_models( logger.info("Handling Gemini models list request") api_key = await key_manager.get_next_working_key() logger.info(f"Using API key: {api_key}") - return model_service.get_gemini_models(api_key) - + models_json = model_service.get_gemini_models(api_key) + models_json["models"].append({"name": "models/gemini-2.0-flash-exp-search", "version": "2.0", "displayName": "Gemini 2.0 Flash Search Experimental", "description": "Gemini 2.0 Flash Search Experimental", "inputTokenLimit": 32767, "outputTokenLimit": 8192, "supportedGenerationMethods": ["generateContent", "countTokens"], "temperature": 1, "topP": 0.95, "topK": 64, "maxTemperature": 2}) + return models_json @router.post("/models/{model_name}:generateContent") async def generate_content( diff --git a/app/api/openai_routes.py b/app/api/openai_routes.py index bd0a27e..007b403 100644 --- a/app/api/openai_routes.py +++ b/app/api/openai_routes.py @@ -18,7 +18,6 @@ logger = get_openai_logger() security_service = SecurityService(settings.ALLOWED_TOKENS, settings.AUTH_TOKEN) key_manager = KeyManager(settings.API_KEYS) model_service = ModelService(settings.MODEL_SEARCH) -chat_service = ChatService(settings.BASE_URL, key_manager) embedding_service = EmbeddingService(settings.BASE_URL) @@ -42,6 +41,7 @@ async def chat_completion( authorization: str = Header(None), token: str = Depends(security_service.verify_authorization), ): + chat_service = ChatService(settings.BASE_URL, key_manager) logger.info("-" * 50 + "chat_completion" + "-" * 50) logger.info(f"Handling chat completion request for model: {request.model}") logger.info(f"Request: \n{request.model_dump_json(indent=2)}") diff --git a/app/core/config.py b/app/core/config.py index dcd2ccf..790f841 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -10,7 +10,7 @@ class Settings(BaseSettings): TOOLS_CODE_EXECUTION_ENABLED: bool = False SHOW_SEARCH_LINK: bool = True SHOW_THINKING_PROCESS: bool = True - AUTH_TOKEN: str + AUTH_TOKEN: str = "" def __init__(self): super().__init__() diff --git a/app/services/chat_service.py b/app/services/chat_service.py index 1954181..71ad742 100644 --- a/app/services/chat_service.py +++ b/app/services/chat_service.py @@ -96,7 +96,7 @@ class ChatService: if self.thinking_first: self.thinking_first = False self.thinking_status = True - text = "【思考过程】\n\n" + parts[0].get("text") + text = "> thinking\n\n" + parts[0].get("text") else: text = parts[0].get("text") @@ -105,15 +105,15 @@ class ChatService: if self.thinking_first: self.thinking_first = False text = ( - "【思考过程】\n\n" + "> thinking\n\n" + parts[0].get("text") - + "\n---\n【输出结果】\n\n" + + "\n\n---\n> output\n\n" + parts[1].get("text") ) else: text = ( parts[0].get("text") - + "\n---\n【输出结果】\n\n" + + "\n\n---\n> output\n\n" + parts[1].get("text") ) else: @@ -182,9 +182,7 @@ class ChatService: "index": 0, "message": { "role": "assistant", - "content": response["candidates"][0]["content"]["parts"][0][ - "text" - ], + "content": response["candidates"][0]["content"]["parts"][0]["text"], }, "finish_reason": finish_reason, } @@ -200,14 +198,20 @@ class ChatService: candidate = response["candidates"][0] if "thinking" in model: if settings.SHOW_THINKING_PROCESS: - text = ( - "【思考过程】\n\n" - + candidate["content"]["parts"][0]["text"] - + "\n---\n【输出结果】\n\n" - + candidate["content"]["parts"][1]["text"] - ) + if len(candidate["content"]["parts"]) == 2: + text = ( + "> thinking\n\n" + + candidate["content"]["parts"][0]["text"] + + "\n\n---\n> output\n\n" + + candidate["content"]["parts"][1]["text"] + ) + else: + text = candidate["content"]["parts"][0]["text"] else: - text = candidate["content"]["parts"][1]["text"] + if len(candidate["content"]["parts"]) == 2: + text = candidate["content"]["parts"][1]["text"] + else: + text = candidate["content"]["parts"][0]["text"] else: text = candidate["content"]["parts"][0]["text"]