Spaces:
Running
Running
Upload patch_models.py
Browse files- patch_models.py +206 -57
patch_models.py
CHANGED
|
@@ -1,66 +1,217 @@
|
|
| 1 |
-
"""Patch backend: add new
|
| 2 |
-
|
| 3 |
-
Routing rules for OpenRouter:
|
| 4 |
-
- openai/google/* → OpenRouter
|
| 5 |
-
- openai/deepseek/* → OpenRouter
|
| 6 |
-
- openai/nvidia/* → OpenRouter
|
| 7 |
-
- openai/poolside/* → OpenRouter
|
| 8 |
-
- openai/sourceful/* → OpenRouter
|
| 9 |
-
- openai/openrouter/* → OpenRouter
|
| 10 |
-
- Everything else → HF Inference Router
|
| 11 |
-
"""
|
| 12 |
import ast
|
| 13 |
import os
|
| 14 |
|
| 15 |
AGENT_FILE = "/app/backend/routes/agent.py"
|
| 16 |
LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
|
| 17 |
|
| 18 |
-
#
|
| 19 |
with open(AGENT_FILE) as f:
|
| 20 |
content = f.read()
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
else:
|
| 50 |
-
print("
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
ast.parse(content)
|
| 55 |
-
print("
|
| 56 |
except SyntaxError as e:
|
| 57 |
-
print(f"
|
| 58 |
raise
|
| 59 |
|
| 60 |
with open(AGENT_FILE, "w") as f:
|
| 61 |
f.write(content)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
with open(LLM_PARAMS_FILE) as f:
|
| 65 |
llm_content = f.read()
|
| 66 |
|
|
@@ -94,26 +245,24 @@ new_block = """ hf_model = normalized_model
|
|
| 94 |
|
| 95 |
if old_block in llm_content:
|
| 96 |
llm_content = llm_content.replace(old_block, new_block)
|
| 97 |
-
print("
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
-
|
| 100 |
-
if
|
| 101 |
-
print("
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
idx = llm_content.find("hf_model = normalized_model")
|
| 105 |
-
if idx >= 0:
|
| 106 |
-
print(f" Found at pos {idx}: {repr(llm_content[idx:idx+300])}")
|
| 107 |
-
raise SystemExit(1)
|
| 108 |
|
| 109 |
try:
|
| 110 |
ast.parse(llm_content)
|
| 111 |
-
print("
|
| 112 |
except SyntaxError as e:
|
| 113 |
-
print(f"
|
| 114 |
raise
|
| 115 |
|
| 116 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 117 |
f.write(llm_content)
|
| 118 |
|
| 119 |
-
print("
|
|
|
|
| 1 |
+
"""Patch backend: replace AVAILABLE_MODELS (remove old 5, add 16 new) + OpenRouter routing."""
|
| 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"
|
| 8 |
|
| 9 |
+
# === Step 1: Replace _available_models() to return ONLY new models ===
|
| 10 |
with open(AGENT_FILE) as f:
|
| 11 |
content = f.read()
|
| 12 |
|
| 13 |
+
# Replace the default model constants first
|
| 14 |
+
content = content.replace(
|
| 15 |
+
'DEFAULT_FREE_MODEL_ID = KIMI_K26_MODEL_ID',
|
| 16 |
+
'DEFAULT_FREE_MODEL_ID = "openai/openrouter/owl-alpha"'
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Replace _available_models() function body to return only new models
|
| 20 |
+
old_func = '''def _available_models() -> list[dict[str, Any]]:
|
| 21 |
+
models = [
|
| 22 |
+
{
|
| 23 |
+
"id": DEFAULT_OPUS_MODEL_ID,
|
| 24 |
+
"label": "Claude Opus 4.8",
|
| 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 = [
|
| 58 |
+
{
|
| 59 |
+
"id": "openai/openrouter/owl-alpha",
|
| 60 |
+
"label": "Owl Alpha",
|
| 61 |
+
"provider": "openrouter",
|
| 62 |
+
"tier": "free",
|
| 63 |
+
"recommended": True,
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"id": "deepseek-ai/DeepSeek-V4-Pro",
|
| 67 |
+
"label": "DeepSeek V4 Pro",
|
| 68 |
+
"provider": "huggingface",
|
| 69 |
+
"tier": "free",
|
| 70 |
+
"recommended": True,
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"id": "deepseek-ai/DeepSeek-V4-Flash",
|
| 74 |
+
"label": "DeepSeek V4 Flash",
|
| 75 |
+
"provider": "huggingface",
|
| 76 |
+
"tier": "free",
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"id": "openai/gpt-5.5:fal-ai",
|
| 80 |
+
"label": "GPT-5.5",
|
| 81 |
+
"provider": "huggingface",
|
| 82 |
+
"tier": "free",
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"id": "Qwen/Qwen3-Coder-Next",
|
| 86 |
+
"label": "Qwen3 Coder Next",
|
| 87 |
+
"provider": "huggingface",
|
| 88 |
+
"tier": "free",
|
| 89 |
+
"recommended": True,
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"id": "google/gemma-3-1b-it",
|
| 93 |
+
"label": "Gemma 3 1B",
|
| 94 |
+
"provider": "huggingface",
|
| 95 |
+
"tier": "free",
|
| 96 |
+
"recommended": True,
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"id": "openai/google/gemini-2.0-flash-001",
|
| 100 |
+
"label": "Gemini 2.0 Flash",
|
| 101 |
+
"provider": "openrouter",
|
| 102 |
+
"tier": "free",
|
| 103 |
+
},
|
| 104 |
+
{
|
| 105 |
+
"id": "openai/deepseek/deepseek-v4-flash:free",
|
| 106 |
+
"label": "DeepSeek V4 Flash (OR)",
|
| 107 |
+
"provider": "openrouter",
|
| 108 |
+
"tier": "free",
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"id": "openai/google/gemma-4-9b-it:free",
|
| 112 |
+
"label": "Gemma 4 9B",
|
| 113 |
+
"provider": "openrouter",
|
| 114 |
+
"tier": "free",
|
| 115 |
+
"recommended": True,
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"id": "openai/google/gemma-4-31b-it:free",
|
| 119 |
+
"label": "Gemma 4 31B",
|
| 120 |
+
"provider": "openrouter",
|
| 121 |
+
"tier": "free",
|
| 122 |
+
"recommended": True,
|
| 123 |
+
},
|
| 124 |
+
{
|
| 125 |
+
"id": "openai/sourceful/riverflow-v2.5-fast:free",
|
| 126 |
+
"label": "Riverflow V2.5 Fast",
|
| 127 |
+
"provider": "openrouter",
|
| 128 |
+
"tier": "free",
|
| 129 |
+
"recommended": True,
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
"id": "openai/sourceful/riverflow-v2.5-pro:free",
|
| 133 |
+
"label": "Riverflow V2.5 Pro",
|
| 134 |
+
"provider": "openrouter",
|
| 135 |
+
"tier": "free",
|
| 136 |
+
"recommended": True,
|
| 137 |
+
},
|
| 138 |
+
{
|
| 139 |
+
"id": "openai/nvidia/nemotron-3-super-120b-a12b:free",
|
| 140 |
+
"label": "Nemotron 3 Super 120B",
|
| 141 |
+
"provider": "openrouter",
|
| 142 |
+
"tier": "free",
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"id": "openai/nvidia/nemotron-3-ultra-550b-a55b:free",
|
| 146 |
+
"label": "Nemotron 3 Ultra 550B",
|
| 147 |
+
"provider": "openrouter",
|
| 148 |
+
"tier": "free",
|
| 149 |
+
"recommended": True,
|
| 150 |
+
},
|
| 151 |
+
{
|
| 152 |
+
"id": "openai/poolside/laguna-m.1:free",
|
| 153 |
+
"label": "Laguna M.1",
|
| 154 |
+
"provider": "openrouter",
|
| 155 |
+
"tier": "free",
|
| 156 |
+
"recommended": True,
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"id": "openai/poolside/laguna-xs.2:free",
|
| 160 |
+
"label": "Laguna XS.2",
|
| 161 |
+
"provider": "openrouter",
|
| 162 |
+
"tier": "free",
|
| 163 |
+
},
|
| 164 |
+
]
|
| 165 |
+
return models'''
|
| 166 |
+
|
| 167 |
+
if old_func in content:
|
| 168 |
+
content = content.replace(old_func, new_func)
|
| 169 |
+
print("OK: Replaced _available_models() with 16 new models")
|
| 170 |
else:
|
| 171 |
+
print("WARN: Could not find old _available_models(), trying extend approach...")
|
| 172 |
+
# Fallback: clear the list and extend
|
| 173 |
+
target = "AVAILABLE_MODELS = _available_models()\n"
|
| 174 |
+
if target in content:
|
| 175 |
+
content = content.replace(
|
| 176 |
+
target,
|
| 177 |
+
target + "AVAILABLE_MODELS.clear()\n" +
|
| 178 |
+
"AVAILABLE_MODELS.extend([\n"
|
| 179 |
+
' {"id": "openai/openrouter/owl-alpha", "label": "Owl Alpha", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 180 |
+
' {"id": "deepseek-ai/DeepSeek-V4-Pro", "label": "DeepSeek V4 Pro", "provider": "huggingface", "tier": "free", "recommended": True},\n'
|
| 181 |
+
' {"id": "deepseek-ai/DeepSeek-V4-Flash", "label": "DeepSeek V4 Flash", "provider": "huggingface", "tier": "free"},\n'
|
| 182 |
+
' {"id": "openai/gpt-5.5:fal-ai", "label": "GPT-5.5", "provider": "huggingface", "tier": "free"},\n'
|
| 183 |
+
' {"id": "Qwen/Qwen3-Coder-Next", "label": "Qwen3 Coder Next", "provider": "huggingface", "tier": "free", "recommended": True},\n'
|
| 184 |
+
' {"id": "google/gemma-3-1b-it", "label": "Gemma 3 1B", "provider": "huggingface", "tier": "free", "recommended": True},\n'
|
| 185 |
+
' {"id": "openai/google/gemini-2.0-flash-001", "label": "Gemini 2.0 Flash", "provider": "openrouter", "tier": "free"},\n'
|
| 186 |
+
' {"id": "openai/deepseek/deepseek-v4-flash:free", "label": "DeepSeek V4 Flash (OR)", "provider": "openrouter", "tier": "free"},\n'
|
| 187 |
+
' {"id": "openai/google/gemma-4-9b-it:free", "label": "Gemma 4 9B", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 188 |
+
' {"id": "openai/google/gemma-4-31b-it:free", "label": "Gemma 4 31B", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 189 |
+
' {"id": "openai/sourceful/riverflow-v2.5-fast:free", "label": "Riverflow V2.5 Fast", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 190 |
+
' {"id": "openai/sourceful/riverflow-v2.5-pro:free", "label": "Riverflow V2.5 Pro", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 191 |
+
' {"id": "openai/nvidia/nemotron-3-super-120b-a12b:free", "label": "Nemotron 3 Super 120B", "provider": "openrouter", "tier": "free"},\n'
|
| 192 |
+
' {"id": "openai/nvidia/nemotron-3-ultra-550b-a55b:free", "label": "Nemotron 3 Ultra 550B", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 193 |
+
' {"id": "openai/poolside/laguna-m.1:free", "label": "Laguna M.1", "provider": "openrouter", "tier": "free", "recommended": True},\n'
|
| 194 |
+
' {"id": "openai/poolside/laguna-xs.2:free", "label": "Laguna XS.2", "provider": "openrouter", "tier": "free"},\n'
|
| 195 |
+
"])\n"
|
| 196 |
+
'DEFAULT_FREE_MODEL_ID = "openai/openrouter/owl-alpha"\n'
|
| 197 |
+
)
|
| 198 |
+
print("OK: Used extend approach")
|
| 199 |
+
else:
|
| 200 |
+
print("FAIL: Cannot patch agent.py!")
|
| 201 |
+
import sys
|
| 202 |
+
sys.exit(1)
|
| 203 |
|
| 204 |
try:
|
| 205 |
ast.parse(content)
|
| 206 |
+
print("OK: agent.py syntax OK")
|
| 207 |
except SyntaxError as e:
|
| 208 |
+
print(f"FAIL: Syntax error in agent.py: {e}")
|
| 209 |
raise
|
| 210 |
|
| 211 |
with open(AGENT_FILE, "w") as f:
|
| 212 |
f.write(content)
|
| 213 |
|
| 214 |
+
# === Step 2: Patch _resolve_llm_params for OpenRouter routing ===
|
| 215 |
with open(LLM_PARAMS_FILE) as f:
|
| 216 |
llm_content = f.read()
|
| 217 |
|
|
|
|
| 245 |
|
| 246 |
if old_block in llm_content:
|
| 247 |
llm_content = llm_content.replace(old_block, new_block)
|
| 248 |
+
print("OK: Patched _resolve_llm_params")
|
| 249 |
+
elif "openai/openrouter/" in llm_content:
|
| 250 |
+
print("OK: Already patched")
|
| 251 |
else:
|
| 252 |
+
idx = llm_content.find("hf_model = normalized_model")
|
| 253 |
+
if idx >= 0:
|
| 254 |
+
print(f"WARN: Target block not found at idx {idx}")
|
| 255 |
+
import sys
|
| 256 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
try:
|
| 259 |
ast.parse(llm_content)
|
| 260 |
+
print("OK: llm_params.py syntax OK")
|
| 261 |
except SyntaxError as e:
|
| 262 |
+
print(f"FAIL: Syntax error: {e}")
|
| 263 |
raise
|
| 264 |
|
| 265 |
with open(LLM_PARAMS_FILE, "w") as f:
|
| 266 |
f.write(llm_content)
|
| 267 |
|
| 268 |
+
print("OK: Backend patched - 16 models, owl-alpha default, OpenRouter routing")
|