Spaces:
Running
Running
Upload patch_models.py
Browse files- patch_models.py +34 -28
patch_models.py
CHANGED
|
@@ -211,9 +211,9 @@ with open(AGENT_FILE, "w") as f:
|
|
| 211 |
f.write(content)
|
| 212 |
|
| 213 |
# === Step 2: Patch _resolve_llm_params for OpenRouter routing ===
|
| 214 |
-
# IMPORTANT:
|
| 215 |
-
#
|
| 216 |
-
#
|
| 217 |
with open(LLM_PARAMS_FILE) as f:
|
| 218 |
llm_content = f.read()
|
| 219 |
|
|
@@ -229,12 +229,11 @@ old_block = """ hf_model = normalized_model
|
|
| 229 |
new_block = """ hf_model = normalized_model
|
| 230 |
api_key = _resolve_hf_router_token(session_hf_token)
|
| 231 |
|
| 232 |
-
# === PATCH: Route openai/-prefixed models to OpenRouter ===
|
| 233 |
-
#
|
| 234 |
if normalized_model.startswith("openai/"):
|
| 235 |
-
actual_model = normalized_model[len("openai/"):]
|
| 236 |
return {
|
| 237 |
-
"model":
|
| 238 |
"api_base": "https://openrouter.ai/api/v1",
|
| 239 |
"api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
|
| 240 |
}
|
|
@@ -248,30 +247,37 @@ new_block = """ hf_model = normalized_model
|
|
| 248 |
|
| 249 |
if old_block in llm_content:
|
| 250 |
llm_content = llm_content.replace(old_block, new_block)
|
| 251 |
-
print("OK: Patched _resolve_llm_params (
|
| 252 |
-
|
| 253 |
-
#
|
| 254 |
idx = llm_content.find("if normalized_model.startswith(\"openai/\"):")
|
| 255 |
if idx >= 0:
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
| 269 |
llm_content = llm_content.replace(old_model_line, new_model_line)
|
| 270 |
-
print("OK:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
else:
|
| 272 |
-
print("
|
| 273 |
-
|
| 274 |
-
|
| 275 |
|
| 276 |
try:
|
| 277 |
ast.parse(llm_content)
|
|
@@ -283,4 +289,4 @@ except SyntaxError as e:
|
|
| 283 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 284 |
f.write(llm_content)
|
| 285 |
|
| 286 |
-
print("OK: Backend patched - 16 models,
|
|
|
|
| 211 |
f.write(content)
|
| 212 |
|
| 213 |
# === Step 2: Patch _resolve_llm_params for OpenRouter routing ===
|
| 214 |
+
# IMPORTANT: Keep the "openai/" prefix when sending to OpenRouter!
|
| 215 |
+
# OpenRouter accepts the full slug like "openai/openrouter/owl-alpha".
|
| 216 |
+
# LiteLLM needs "openai/" for provider detection.
|
| 217 |
with open(LLM_PARAMS_FILE) as f:
|
| 218 |
llm_content = f.read()
|
| 219 |
|
|
|
|
| 229 |
new_block = """ hf_model = normalized_model
|
| 230 |
api_key = _resolve_hf_router_token(session_hf_token)
|
| 231 |
|
| 232 |
+
# === PATCH: Route ALL openai/-prefixed models to OpenRouter ===
|
| 233 |
+
# Keep the "openai/" prefix — both LiteLLM and OpenRouter need it.
|
| 234 |
if normalized_model.startswith("openai/"):
|
|
|
|
| 235 |
return {
|
| 236 |
+
"model": normalized_model,
|
| 237 |
"api_base": "https://openrouter.ai/api/v1",
|
| 238 |
"api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
|
| 239 |
}
|
|
|
|
| 247 |
|
| 248 |
if old_block in llm_content:
|
| 249 |
llm_content = llm_content.replace(old_block, new_block)
|
| 250 |
+
print("OK: Patched _resolve_llm_params (catch-all openai/ -> OR, keep prefix)")
|
| 251 |
+
elif "normalized_model[len" in llm_content or "actual_model" in llm_content:
|
| 252 |
+
# Revert the strip approach — keep openai/ prefix
|
| 253 |
idx = llm_content.find("if normalized_model.startswith(\"openai/\"):")
|
| 254 |
if idx >= 0:
|
| 255 |
+
or_start = llm_content.find("# === PATCH:", idx)
|
| 256 |
+
if or_start < 0:
|
| 257 |
+
or_start = idx
|
| 258 |
+
or_end = llm_content.find("# === END PATCH", or_start)
|
| 259 |
+
if or_end < 0:
|
| 260 |
+
or_end = llm_content.find("params = {", or_start)
|
| 261 |
+
|
| 262 |
+
# Find the return block and fix it
|
| 263 |
+
ret_line = llm_content.find("return {", or_start, or_end)
|
| 264 |
+
if ret_line > 0:
|
| 265 |
+
model_line_start = llm_content.find('"model":', ret_line)
|
| 266 |
+
if model_line_start > 0:
|
| 267 |
+
line_start = llm_content.rfind('\n', 0, model_line_start) + 1
|
| 268 |
+
line_end = llm_content.find('\n', line_start)
|
| 269 |
+
old_model_line = llm_content[line_start:line_end]
|
| 270 |
+
new_model_line = ' "model": normalized_model,'
|
| 271 |
llm_content = llm_content.replace(old_model_line, new_model_line)
|
| 272 |
+
print("OK: Reverted to keep openai/ prefix for OpenRouter")
|
| 273 |
+
else:
|
| 274 |
+
print("OK: Could not find model line")
|
| 275 |
+
else:
|
| 276 |
+
print("OK: Could not find return block")
|
| 277 |
else:
|
| 278 |
+
print("OK: Already using correct routing")
|
| 279 |
+
else:
|
| 280 |
+
print("OK: Routing already correct")
|
| 281 |
|
| 282 |
try:
|
| 283 |
ast.parse(llm_content)
|
|
|
|
| 289 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 290 |
f.write(llm_content)
|
| 291 |
|
| 292 |
+
print("OK: Backend patched - 16 models, ALL openai/ -> OR (keep prefix), GPT-5.5→gpt-oss-120b, Riverflow→north-mini-code:free")
|