diff --git a/app/services/gemini_chat_service.py b/app/services/gemini_chat_service.py index 0ee8772..58df814 100644 --- a/app/services/gemini_chat_service.py +++ b/app/services/gemini_chat_service.py @@ -53,7 +53,7 @@ class GeminiChatService: return { "contents": payload.get("contents", []), "tools": self._build_tools(model, payload), - "safetySettings": self._get_safety_settings(), + "safetySettings": self._get_safety_settings(model), "generationConfig": payload.get("generationConfig", {}), "systemInstruction": payload.get("systemInstruction", []) } @@ -78,12 +78,20 @@ class GeminiChatService: return True return False - def _get_safety_settings(self) -> List[Dict[str, str]]: + def _get_safety_settings(self, model: str) -> List[Dict[str, str]]: """获取安全设置""" + if "2.0" in model and model != "gemini-2.0-flash-thinking-exp": + return [ + {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_ONLY_HIGH"} + ] return [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, - {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"} + {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_ONLY_HIGH"} ] \ No newline at end of file diff --git a/app/services/openai_chat_service.py b/app/services/openai_chat_service.py index ca67bae..a1a6cb5 100644 --- a/app/services/openai_chat_service.py +++ b/app/services/openai_chat_service.py @@ -100,7 +100,7 @@ class OpenAIChatService: "topK": request.top_k }, "tools": self._build_tools(request, messages), - "safetySettings": self._get_safety_settings() + "safetySettings": self._get_safety_settings(request.model) } def _build_tools(self, request: ChatRequest, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: @@ -125,12 +125,20 @@ class OpenAIChatService: return True return False - def _get_safety_settings(self) -> List[Dict[str, str]]: + def _get_safety_settings(self, model: str) -> List[Dict[str, str]]: """获取安全设置""" + if "2.0" in model and model != "gemini-2.0-flash-thinking-exp": + return [ + {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "OFF"}, + {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_ONLY_HIGH"} + ] return [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, - {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_NONE"} + {"category": "HARM_CATEGORY_CIVIC_INTEGRITY", "threshold": "BLOCK_ONLY_HIGH"} ] \ No newline at end of file