Spaces:
Sleeping
Sleeping
fix backend to support gpt-5-mini
Browse files- app/main.py +53 -7
app/main.py
CHANGED
|
@@ -29,11 +29,38 @@ except ImportError:
|
|
| 29 |
API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 30 |
MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-pro")
|
| 31 |
GEMINI_SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL")
|
| 32 |
-
|
|
|
|
| 33 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 34 |
USE_OPENAI = os.getenv("USE_OPENAI", "").lower() == "true"
|
| 35 |
PORT = int(os.getenv("PORT", "7860"))
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
gemini_client = genai.Client(api_key=API_KEY) if API_KEY else None
|
| 38 |
gpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI and USE_OPENAI) else None
|
| 39 |
|
|
@@ -162,6 +189,22 @@ def _build_responses_input(system: str, contents: Any) -> List[Dict[str, Any]]:
|
|
| 162 |
{"role": "user", "content": _build_openai_content(contents, for_chat=False)},
|
| 163 |
]
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def _extract_chat_content(resp: Any) -> str:
|
| 167 |
content = resp.choices[0].message.content
|
|
@@ -199,17 +242,20 @@ def _invoke_gpt_model(model: str, system: str, contents: Any) -> str:
|
|
| 199 |
if not gpt_client:
|
| 200 |
raise RuntimeError("GPT client is not configured")
|
| 201 |
messages = _build_chat_messages(system, contents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
try:
|
| 203 |
resp = gpt_client.chat.completions.create(model=model, messages=messages)
|
| 204 |
return _extract_chat_content(resp)
|
| 205 |
except Exception as err:
|
| 206 |
-
|
| 207 |
-
if "only supported in v1/responses" not in message:
|
| 208 |
raise
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
)
|
| 213 |
return _extract_responses_content(resp)
|
| 214 |
|
| 215 |
|
|
|
|
| 29 |
API_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 30 |
MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-pro")
|
| 31 |
GEMINI_SMALL_MODEL = os.getenv("GEMINI_SMALL_MODEL")
|
| 32 |
+
DEFAULT_OPENAI_SMALL_MODEL = "gpt-4o-mini"
|
| 33 |
+
OPENAI_SMALL_MODEL = os.getenv("OPENAI_SMALL_MODEL") or DEFAULT_OPENAI_SMALL_MODEL
|
| 34 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 35 |
USE_OPENAI = os.getenv("USE_OPENAI", "").lower() == "true"
|
| 36 |
PORT = int(os.getenv("PORT", "7860"))
|
| 37 |
|
| 38 |
+
_OPENAI_RESPONSES_MODELS_ENV = os.getenv("OPENAI_RESPONSES_MODELS", "")
|
| 39 |
+
RESPONSES_API_MODEL_NAMES = {"gpt-5-mini"}
|
| 40 |
+
if _OPENAI_RESPONSES_MODELS_ENV:
|
| 41 |
+
RESPONSES_API_MODEL_NAMES.update(
|
| 42 |
+
model.strip().lower()
|
| 43 |
+
for model in _OPENAI_RESPONSES_MODELS_ENV.split(",")
|
| 44 |
+
if model.strip()
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
_OPENAI_RESPONSES_PREFIXES_ENV = os.getenv("OPENAI_RESPONSES_PREFIXES", "")
|
| 48 |
+
_RESPONSES_API_MODEL_PREFIXES = ["gpt-5"]
|
| 49 |
+
if _OPENAI_RESPONSES_PREFIXES_ENV:
|
| 50 |
+
_RESPONSES_API_MODEL_PREFIXES.extend(
|
| 51 |
+
prefix.strip().lower()
|
| 52 |
+
for prefix in _OPENAI_RESPONSES_PREFIXES_ENV.split(",")
|
| 53 |
+
if prefix.strip()
|
| 54 |
+
)
|
| 55 |
+
RESPONSES_API_MODEL_PREFIXES = tuple(_RESPONSES_API_MODEL_PREFIXES)
|
| 56 |
+
RESPONSES_API_ERROR_HINTS = (
|
| 57 |
+
"only supported in v1/responses",
|
| 58 |
+
"use the responses api",
|
| 59 |
+
"use the responses endpoint",
|
| 60 |
+
"please call the responses api",
|
| 61 |
+
"please use the responses endpoint",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
gemini_client = genai.Client(api_key=API_KEY) if API_KEY else None
|
| 65 |
gpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI and USE_OPENAI) else None
|
| 66 |
|
|
|
|
| 189 |
{"role": "user", "content": _build_openai_content(contents, for_chat=False)},
|
| 190 |
]
|
| 191 |
|
| 192 |
+
def _requires_responses_api(model: str) -> bool:
|
| 193 |
+
lowered = (model or "").lower()
|
| 194 |
+
if not lowered:
|
| 195 |
+
return False
|
| 196 |
+
if lowered in RESPONSES_API_MODEL_NAMES:
|
| 197 |
+
return True
|
| 198 |
+
return any(
|
| 199 |
+
prefix and lowered.startswith(prefix)
|
| 200 |
+
for prefix in RESPONSES_API_MODEL_PREFIXES
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def _should_use_responses_fallback(err: Exception) -> bool:
|
| 205 |
+
message = str(err).lower()
|
| 206 |
+
return any(hint in message for hint in RESPONSES_API_ERROR_HINTS)
|
| 207 |
+
|
| 208 |
|
| 209 |
def _extract_chat_content(resp: Any) -> str:
|
| 210 |
content = resp.choices[0].message.content
|
|
|
|
| 242 |
if not gpt_client:
|
| 243 |
raise RuntimeError("GPT client is not configured")
|
| 244 |
messages = _build_chat_messages(system, contents)
|
| 245 |
+
responses_input: Optional[List[Dict[str, Any]]] = None
|
| 246 |
+
if _requires_responses_api(model):
|
| 247 |
+
responses_input = _build_responses_input(system, contents)
|
| 248 |
+
resp = gpt_client.responses.create(model=model, input=responses_input)
|
| 249 |
+
return _extract_responses_content(resp)
|
| 250 |
try:
|
| 251 |
resp = gpt_client.chat.completions.create(model=model, messages=messages)
|
| 252 |
return _extract_chat_content(resp)
|
| 253 |
except Exception as err:
|
| 254 |
+
if not _should_use_responses_fallback(err):
|
|
|
|
| 255 |
raise
|
| 256 |
+
if responses_input is None:
|
| 257 |
+
responses_input = _build_responses_input(system, contents)
|
| 258 |
+
resp = gpt_client.responses.create(model=model, input=responses_input)
|
|
|
|
| 259 |
return _extract_responses_content(resp)
|
| 260 |
|
| 261 |
|