feat: 更新 Gemini 模型列表并优化思考过程展示

This commit is contained in:
yinpeng
2024-12-25 16:04:09 +08:00
parent 9578704cfd
commit a059c4d586
4 changed files with 23 additions and 18 deletions
+3 -2
View File
@@ -29,8 +29,9 @@ async def list_models(
logger.info("Handling Gemini models list request") logger.info("Handling Gemini models list request")
api_key = await key_manager.get_next_working_key() api_key = await key_manager.get_next_working_key()
logger.info(f"Using API key: {api_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") @router.post("/models/{model_name}:generateContent")
async def generate_content( async def generate_content(
+1 -1
View File
@@ -18,7 +18,6 @@ logger = get_openai_logger()
security_service = SecurityService(settings.ALLOWED_TOKENS, settings.AUTH_TOKEN) security_service = SecurityService(settings.ALLOWED_TOKENS, settings.AUTH_TOKEN)
key_manager = KeyManager(settings.API_KEYS) key_manager = KeyManager(settings.API_KEYS)
model_service = ModelService(settings.MODEL_SEARCH) model_service = ModelService(settings.MODEL_SEARCH)
chat_service = ChatService(settings.BASE_URL, key_manager)
embedding_service = EmbeddingService(settings.BASE_URL) embedding_service = EmbeddingService(settings.BASE_URL)
@@ -42,6 +41,7 @@ async def chat_completion(
authorization: str = Header(None), authorization: str = Header(None),
token: str = Depends(security_service.verify_authorization), token: str = Depends(security_service.verify_authorization),
): ):
chat_service = ChatService(settings.BASE_URL, key_manager)
logger.info("-" * 50 + "chat_completion" + "-" * 50) logger.info("-" * 50 + "chat_completion" + "-" * 50)
logger.info(f"Handling chat completion request for model: {request.model}") logger.info(f"Handling chat completion request for model: {request.model}")
logger.info(f"Request: \n{request.model_dump_json(indent=2)}") logger.info(f"Request: \n{request.model_dump_json(indent=2)}")
+1 -1
View File
@@ -10,7 +10,7 @@ class Settings(BaseSettings):
TOOLS_CODE_EXECUTION_ENABLED: bool = False TOOLS_CODE_EXECUTION_ENABLED: bool = False
SHOW_SEARCH_LINK: bool = True SHOW_SEARCH_LINK: bool = True
SHOW_THINKING_PROCESS: bool = True SHOW_THINKING_PROCESS: bool = True
AUTH_TOKEN: str AUTH_TOKEN: str = ""
def __init__(self): def __init__(self):
super().__init__() super().__init__()
+18 -14
View File
@@ -96,7 +96,7 @@ class ChatService:
if self.thinking_first: if self.thinking_first:
self.thinking_first = False self.thinking_first = False
self.thinking_status = True self.thinking_status = True
text = "【思考过程】\n\n" + parts[0].get("text") text = "> thinking\n\n" + parts[0].get("text")
else: else:
text = parts[0].get("text") text = parts[0].get("text")
@@ -105,15 +105,15 @@ class ChatService:
if self.thinking_first: if self.thinking_first:
self.thinking_first = False self.thinking_first = False
text = ( text = (
"【思考过程】\n\n" "> thinking\n\n"
+ parts[0].get("text") + parts[0].get("text")
+ "\n---\n【输出结果】\n\n" + "\n\n---\n> output\n\n"
+ parts[1].get("text") + parts[1].get("text")
) )
else: else:
text = ( text = (
parts[0].get("text") parts[0].get("text")
+ "\n---\n【输出结果】\n\n" + "\n\n---\n> output\n\n"
+ parts[1].get("text") + parts[1].get("text")
) )
else: else:
@@ -182,9 +182,7 @@ class ChatService:
"index": 0, "index": 0,
"message": { "message": {
"role": "assistant", "role": "assistant",
"content": response["candidates"][0]["content"]["parts"][0][ "content": response["candidates"][0]["content"]["parts"][0]["text"],
"text"
],
}, },
"finish_reason": finish_reason, "finish_reason": finish_reason,
} }
@@ -200,14 +198,20 @@ class ChatService:
candidate = response["candidates"][0] candidate = response["candidates"][0]
if "thinking" in model: if "thinking" in model:
if settings.SHOW_THINKING_PROCESS: if settings.SHOW_THINKING_PROCESS:
text = ( if len(candidate["content"]["parts"]) == 2:
"【思考过程】\n\n" text = (
+ candidate["content"]["parts"][0]["text"] "> thinking\n\n"
+ "\n---\n【输出结果】\n\n" + candidate["content"]["parts"][0]["text"]
+ candidate["content"]["parts"][1]["text"] + "\n\n---\n> output\n\n"
) + candidate["content"]["parts"][1]["text"]
)
else:
text = candidate["content"]["parts"][0]["text"]
else: 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: else:
text = candidate["content"]["parts"][0]["text"] text = candidate["content"]["parts"][0]["text"]