Spaces:
Running
Running
Upload patch_models.py
Browse files- patch_models.py +23 -25
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: Keep the "openai/" prefix
|
| 215 |
-
#
|
| 216 |
-
#
|
| 217 |
with open(LLM_PARAMS_FILE) as f:
|
| 218 |
llm_content = f.read()
|
| 219 |
|
|
@@ -230,10 +230,13 @@ 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
|
|
|
|
|
|
|
| 234 |
if normalized_model.startswith("openai/"):
|
|
|
|
| 235 |
return {
|
| 236 |
-
"model":
|
| 237 |
"api_base": "https://openrouter.ai/api/v1",
|
| 238 |
"api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
|
| 239 |
}
|
|
@@ -247,37 +250,32 @@ new_block = """ hf_model = normalized_model
|
|
| 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,
|
| 251 |
elif "normalized_model[len" in llm_content or "actual_model" in llm_content:
|
| 252 |
-
#
|
| 253 |
idx = llm_content.find("if normalized_model.startswith(\"openai/\"):")
|
| 254 |
if idx >= 0:
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 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 |
-
|
|
|
|
| 271 |
llm_content = llm_content.replace(old_model_line, new_model_line)
|
| 272 |
-
print("OK:
|
| 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:
|
| 279 |
else:
|
| 280 |
-
print("OK: Routing
|
| 281 |
|
| 282 |
try:
|
| 283 |
ast.parse(llm_content)
|
|
@@ -289,4 +287,4 @@ except SyntaxError as e:
|
|
| 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 (
|
|
|
|
| 211 |
f.write(content)
|
| 212 |
|
| 213 |
# === Step 2: Patch _resolve_llm_params for OpenRouter routing ===
|
| 214 |
+
# IMPORTANT: Keep the "openai/" prefix for LiteLLM provider detection,
|
| 215 |
+
# but STRIP it from the model field when sending to OpenRouter API.
|
| 216 |
+
# OpenRouter expects model slugs like "openrouter/owl-alpha" NOT "openai/openrouter/owl-alpha"
|
| 217 |
with open(LLM_PARAMS_FILE) as f:
|
| 218 |
llm_content = f.read()
|
| 219 |
|
|
|
|
| 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 for LiteLLM provider routing,
|
| 234 |
+
# but strip it before sending to OpenRouter API.
|
| 235 |
+
# OpenRouter expects model slugs WITHOUT "openai/" prefix.
|
| 236 |
if normalized_model.startswith("openai/"):
|
| 237 |
+
or_model = normalized_model[len("openai/"):] # Strip openai/ prefix
|
| 238 |
return {
|
| 239 |
+
"model": or_model,
|
| 240 |
"api_base": "https://openrouter.ai/api/v1",
|
| 241 |
"api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
|
| 242 |
}
|
|
|
|
| 250 |
|
| 251 |
if old_block in llm_content:
|
| 252 |
llm_content = llm_content.replace(old_block, new_block)
|
| 253 |
+
print("OK: Patched _resolve_llm_params (catch-all openai/ -> OR, strip openai/ prefix)")
|
| 254 |
elif "normalized_model[len" in llm_content or "actual_model" in llm_content:
|
| 255 |
+
# Find and fix any existing OpenRouter routing block
|
| 256 |
idx = llm_content.find("if normalized_model.startswith(\"openai/\"):")
|
| 257 |
if idx >= 0:
|
| 258 |
+
# Find the return block
|
| 259 |
+
ret_idx = llm_content.find("return {", idx)
|
| 260 |
+
if ret_idx > 0:
|
| 261 |
+
# Find the "model" line in the return block
|
| 262 |
+
model_line_start = llm_content.find('"model":', ret_idx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
if model_line_start > 0:
|
| 264 |
line_start = llm_content.rfind('\n', 0, model_line_start) + 1
|
| 265 |
line_end = llm_content.find('\n', line_start)
|
| 266 |
old_model_line = llm_content[line_start:line_end]
|
| 267 |
+
# Replace with stripped version
|
| 268 |
+
new_model_line = ' "model": normalized_model[len("openai/"):], # Strip openai/ prefix for OpenRouter'
|
| 269 |
llm_content = llm_content.replace(old_model_line, new_model_line)
|
| 270 |
+
print("OK: Fixed model line to strip openai/ prefix")
|
| 271 |
else:
|
| 272 |
+
print("OK: Could not find model line in OpenRouter block")
|
| 273 |
else:
|
| 274 |
+
print("OK: Could not find return block in OpenRouter routing")
|
| 275 |
else:
|
| 276 |
+
print("OK: No existing OpenRouter block found")
|
| 277 |
else:
|
| 278 |
+
print("OK: Routing block not found in expected form")
|
| 279 |
|
| 280 |
try:
|
| 281 |
ast.parse(llm_content)
|
|
|
|
| 287 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 288 |
f.write(llm_content)
|
| 289 |
|
| 290 |
+
print("OK: Backend patched - 16 models, ALL openai/ -> OR (strip openai/ prefix), Owl Alpha default")
|