Spaces:
Paused
Paused
Feat: Fallback to Google gemini-3-flash-preview on primary LLM proxy error
Browse files260 unmodified lines261262263264265266267268269270271272273274275276277278279280281282283284285286287260 unmodified lines# To make the log cleaner, we remove the messages from the logged parameterslogged_params = {k: v for k, v in chat_api_params.items() if k != "messages"}if "response_format" in chat_api_params:# to enforce the response format via pydantic, we need to use a different methodif "stream" in chat_api_params:del chat_api_params["stream"]logger.debug(f"Calling LLM model (using .parse too) with these parameters: {logged_params}. Not showing 'messages' parameter.")# complete messagelogger.debug(f" --> Complete messages sent to LLM: {chat_api_params['messages']}")result_message = self.client.beta.chat.completions.parse(**chat_api_params)return result_messageelse:logger.debug(f"Calling LLM model with these parameters: {logged_params}. Not showing 'messages' parameter.")return self.client.chat.completions.create(**chat_api_params)def _is_reasoning_model(self, model):return "o1" in model or "o3" in model260 unmodified lines261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297260 unmodified lines# To make the log cleaner, we remove the messages from the logged parameterslogged_params = {k: v for k, v in chat_api_params.items() if k != "messages"}# --- GOOGLE FALLBACK INJECTION ---import osfrom openai import OpenAItry:if "response_format" in chat_api_params:if "stream" in chat_api_params: del chat_api_params["stream"]result_message = self.client.beta.chat.completions.parse(**chat_api_params)return result_messageelse:return self.client.chat.completions.create(**chat_api_params)except Exception as e:logger.warning(f"Primary model call failed ({e}). Falling back to Google gemini-3-flash-preview...")google_client = OpenAI(api_key=os.environ.get("GOOGLE_API_KEY", "missing_google_key"),base_url="https://generativelanguage.googleapis.com/v1beta/openai/")chat_api_params["model"] = "gemini-3-flash-preview"# Remove parameters Google OpenAI compat endpoint might not supportif "response_format" in chat_api_params:del chat_api_params["response_format"]if "reasoning_effort" in chat_api_params:del chat_api_params["reasoning_effort"]if "max_completion_tokens" in chat_api_params:chat_api_params["max_tokens"] = chat_api_params.pop("max_completion_tokens")return google_client.chat.completions.create(**chat_api_params)# ----------------------------------def _is_reasoning_model(self, model):return "o1" in model or "o3" in model
tinytroupe/openai_utils.py+30-20