Spaces:
Running
Running
Fix patch_models.py for current upstream (regex-based, handles new model_ids structure)
Browse files- patch_models.py +32 -99
patch_models.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
"""Patch backend: replace AVAILABLE_MODELS (remove old
|
| 2 |
|
| 3 |
import ast
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
AGENT_FILE = "/app/backend/routes/agent.py"
|
| 7 |
LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
|
|
@@ -10,48 +11,22 @@ LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
|
|
| 10 |
with open(AGENT_FILE) as f:
|
| 11 |
content = f.read()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
content = content.replace(
|
| 15 |
-
|
| 16 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"provider": "huggingface",
|
| 26 |
-
"recommended": True,
|
| 27 |
-
},
|
| 28 |
-
{
|
| 29 |
-
"id": DEFAULT_GPT_MODEL_ID,
|
| 30 |
-
"label": "GPT-5.5",
|
| 31 |
-
"provider": "huggingface",
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"id": DEFAULT_FREE_MODEL_ID,
|
| 35 |
-
"label": "Kimi K2.6",
|
| 36 |
-
"provider": "huggingface",
|
| 37 |
-
},
|
| 38 |
-
{
|
| 39 |
-
"id": MINIMAX_M27_MODEL_ID,
|
| 40 |
-
"label": "MiniMax M2.7",
|
| 41 |
-
"provider": "huggingface",
|
| 42 |
-
},
|
| 43 |
-
{
|
| 44 |
-
"id": GLM_51_MODEL_ID,
|
| 45 |
-
"label": "GLM 5.1",
|
| 46 |
-
"provider": "huggingface",
|
| 47 |
-
},
|
| 48 |
-
{
|
| 49 |
-
"id": DEEPSEEK_V4_PRO_MODEL_ID,
|
| 50 |
-
"label": "DeepSeek V4 Pro",
|
| 51 |
-
"provider": "huggingface",
|
| 52 |
-
},
|
| 53 |
-
]
|
| 54 |
-
return models'''
|
| 55 |
|
| 56 |
new_func = '''def _available_models() -> list[dict[str, Any]]:
|
| 57 |
models = [
|
|
@@ -164,11 +139,12 @@ new_func = '''def _available_models() -> list[dict[str, Any]]:
|
|
| 164 |
]
|
| 165 |
return models'''
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
else:
|
| 171 |
-
print("WARN: Could not find
|
| 172 |
target = "AVAILABLE_MODELS = _available_models()\n"
|
| 173 |
if target in content:
|
| 174 |
content = content.replace(
|
|
@@ -211,26 +187,15 @@ 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 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 |
|
| 220 |
-
#
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
"api_key": api_key,
|
| 227 |
-
}"""
|
| 228 |
-
|
| 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,
|
|
@@ -239,45 +204,13 @@ new_block = """ hf_model = normalized_model
|
|
| 239 |
}
|
| 240 |
# === END PATCH ===
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
"
|
| 245 |
-
"api_key": api_key,
|
| 246 |
-
}"""
|
| 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:
|
| 279 |
else:
|
| 280 |
-
print("OK:
|
| 281 |
|
| 282 |
try:
|
| 283 |
ast.parse(llm_content)
|
|
@@ -289,4 +222,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
|
|
|
|
| 1 |
+
"""Patch backend: replace AVAILABLE_MODELS (remove old 6, add 16 new) + OpenRouter routing."""
|
| 2 |
|
| 3 |
import ast
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
AGENT_FILE = "/app/backend/routes/agent.py"
|
| 8 |
LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
|
|
|
|
| 11 |
with open(AGENT_FILE) as f:
|
| 12 |
content = f.read()
|
| 13 |
|
| 14 |
+
# Make DEFAULT_MODEL_ID point to our default
|
| 15 |
content = content.replace(
|
| 16 |
+
"DEFAULT_MODEL_ID = GLM_52_MODEL_ID",
|
| 17 |
+
'DEFAULT_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro"'
|
| 18 |
+
)
|
| 19 |
+
content = content.replace(
|
| 20 |
+
"DEFAULT_GPT_MODEL_ID = GPT_55_MODEL_ID",
|
| 21 |
+
'DEFAULT_GPT_MODEL_ID = "openai/gpt-oss-120b"'
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Find and replace the _available_models() function using regex
|
| 25 |
+
func_pattern = re.compile(
|
| 26 |
+
r'def _available_models\(\) -> list\[dict\[str, Any\]\]:\s*\n'
|
| 27 |
+
r'\s+models\s*=\s*\[.*?\]\s*\n\s+return models',
|
| 28 |
+
re.DOTALL
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
new_func = '''def _available_models() -> list[dict[str, Any]]:
|
| 32 |
models = [
|
|
|
|
| 139 |
]
|
| 140 |
return models'''
|
| 141 |
|
| 142 |
+
func_match = func_pattern.search(content)
|
| 143 |
+
if func_match:
|
| 144 |
+
content = content[:func_match.start()] + new_func + content[func_match.end():]
|
| 145 |
+
print("OK: Replaced _available_models() with 16 new models via regex")
|
| 146 |
else:
|
| 147 |
+
print("WARN: Could not find _available_models() via regex, trying extend approach...")
|
| 148 |
target = "AVAILABLE_MODELS = _available_models()\n"
|
| 149 |
if target in content:
|
| 150 |
content = content.replace(
|
|
|
|
| 187 |
f.write(content)
|
| 188 |
|
| 189 |
# === Step 2: Patch _resolve_llm_params for OpenRouter routing ===
|
|
|
|
|
|
|
|
|
|
| 190 |
with open(LLM_PARAMS_FILE) as f:
|
| 191 |
llm_content = f.read()
|
| 192 |
|
| 193 |
+
# Insert the OpenRouter routing before the params block
|
| 194 |
+
api_key_match = re.search(r'api_key\s*=\s*_resolve_hf_router_token\(session_hf_token\)', llm_content)
|
| 195 |
+
if api_key_match:
|
| 196 |
+
line_end = llm_content.find('\n', api_key_match.end()) + 1
|
| 197 |
+
if line_end > 0:
|
| 198 |
+
routing_insert = ''' # === PATCH: Route ALL openai/-prefixed models to OpenRouter ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
if normalized_model.startswith("openai/"):
|
| 200 |
return {
|
| 201 |
"model": normalized_model,
|
|
|
|
| 204 |
}
|
| 205 |
# === END PATCH ===
|
| 206 |
|
| 207 |
+
'''
|
| 208 |
+
llm_content = llm_content[:line_end] + routing_insert + llm_content[line_end:]
|
| 209 |
+
print("OK: Patched _resolve_llm_params (catch-all openai/ -> OR)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
else:
|
| 211 |
+
print("OK: Could not find end of api_key line")
|
| 212 |
else:
|
| 213 |
+
print("OK: api_key line not found, may already be patched")
|
| 214 |
|
| 215 |
try:
|
| 216 |
ast.parse(llm_content)
|
|
|
|
| 222 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 223 |
f.write(llm_content)
|
| 224 |
|
| 225 |
+
print("OK: Backend patched - 16 models, ALL openai/ -> OR")
|