Spaces:
Paused
Paused
Upload patch_models.py
Browse files- patch_models.py +90 -15
patch_models.py
CHANGED
|
@@ -1,43 +1,120 @@
|
|
| 1 |
-
"""Patch backend: replace
|
| 2 |
|
| 3 |
import ast
|
| 4 |
import os
|
|
|
|
| 5 |
|
|
|
|
| 6 |
LLM_PARAMS_FILE = "/app/agent/core/llm_params.py"
|
| 7 |
|
| 8 |
-
# === Step 1: Patch model_ids.py
|
| 9 |
IDS_FILE = "/app/agent/core/model_ids.py"
|
| 10 |
with open(IDS_FILE, "r") as f:
|
| 11 |
content = f.read()
|
| 12 |
|
| 13 |
old_models_block = 'CLAUDE_OPUS_48_MODEL_ID = "anthropic/claude-opus-4.8:fal-ai"\nGPT_55_MODEL_ID = "openai/gpt-5.5:fal-ai"\nKIMI_K27_CODE_MODEL_ID = "moonshotai/Kimi-K2.7-Code:novita"\nMINIMAX_M3_MODEL_ID = "MiniMaxAI/MiniMax-M3:novita"\nGLM_52_MODEL_ID = "zai-org/GLM-5.2:novita"\nDEEPSEEK_V4_PRO_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro:novita"'
|
| 14 |
|
| 15 |
-
new_models_block = 'GPT_55_MODEL_ID = "openai/gpt-5.5:fal-ai"\nKIMI_K27_CODE_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro"\nMINIMAX_M3_MODEL_ID = "deepseek-ai/DeepSeek-V4-Flash"\nGLM_52_MODEL_ID = "openai/deepseek/deepseek-v4-flash"\nDEEPSEEK_V4_PRO_MODEL_ID = "nvidia/nemotron-3-super-120b-a12b:free"\n\n# === PATCH:
|
| 16 |
|
| 17 |
content = content.replace(old_models_block, new_models_block)
|
| 18 |
|
| 19 |
old_hosted = "HOSTED_MODEL_IDS = {\n CLAUDE_OPUS_48_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GLM_52_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n}"
|
| 20 |
-
new_hosted = "HOSTED_MODEL_IDS = {\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GLM_52_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n GEMMA_4_31B_FREE_MODEL_ID,\n TENCENT_HY3_FREE_MODEL_ID,\n LLAMA_3_3_70B_FREE_MODEL_ID,\n LLAMA_3_1_8B_MODEL_ID,\n}"
|
| 21 |
content = content.replace(old_hosted, new_hosted)
|
| 22 |
|
| 23 |
with open(IDS_FILE, "w") as f:
|
| 24 |
f.write(content)
|
| 25 |
print("OK: model_ids.py patched")
|
| 26 |
|
| 27 |
-
# === Step 2: Patch agent.py —
|
| 28 |
-
AGENT_FILE
|
| 29 |
-
with open(AGENT_FILE, "r") as f:
|
| 30 |
content = f.read()
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Update imports
|
| 33 |
old_import = "from agent.core.model_ids import (\n CLAUDE_OPUS_48_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n GLM_52_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n strip_huggingface_model_prefix,\n)"
|
| 34 |
-
new_import = "from agent.core.model_ids import (\n DEEPSEEK_V4_PRO_MODEL_ID,\n GLM_52_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GEMMA_4_31B_FREE_MODEL_ID,\n TENCENT_HY3_FREE_MODEL_ID,\n LLAMA_3_3_70B_FREE_MODEL_ID,\n LLAMA_3_1_8B_MODEL_ID,\n strip_huggingface_model_prefix,\n)"
|
| 35 |
content = content.replace(old_import, new_import)
|
| 36 |
|
| 37 |
-
# Replace _available_models()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Update title generation
|
| 43 |
old_title = '"openai/gpt-oss-120b:cerebras",'
|
|
@@ -56,11 +133,9 @@ with open(AGENT_FILE, "w") as f:
|
|
| 56 |
print("OK: agent.py patched")
|
| 57 |
|
| 58 |
# === Step 3: Patch _resolve_llm_params for OpenRouter routing ===
|
| 59 |
-
# Route ALL openai/-prefixed models to OpenRouter
|
| 60 |
with open(LLM_PARAMS_FILE) as f:
|
| 61 |
llm_content = f.read()
|
| 62 |
|
| 63 |
-
# Find where normalized_model is used and insert routing
|
| 64 |
if "normalized_model.startswith" not in llm_content:
|
| 65 |
api_key_find = "api_key = _resolve_hf_router_token(session_hf_token)"
|
| 66 |
if api_key_find in llm_content:
|
|
@@ -78,7 +153,7 @@ if "normalized_model.startswith" not in llm_content:
|
|
| 78 |
llm_content = llm_content[:line_end] + routing_insert + llm_content[line_end:]
|
| 79 |
print("OK: Patched _resolve_llm_params (catch-all openai/ -> OR)")
|
| 80 |
else:
|
| 81 |
-
print("WARN: Could not find api_key line
|
| 82 |
else:
|
| 83 |
print("OK: llm_params.py already patched")
|
| 84 |
|
|
|
|
| 1 |
+
"""Patch backend: replace AVAILABLE_MODELS with OpenRouter models + 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"
|
| 9 |
|
| 10 |
+
# === Step 1: Patch model_ids.py ===
|
| 11 |
IDS_FILE = "/app/agent/core/model_ids.py"
|
| 12 |
with open(IDS_FILE, "r") as f:
|
| 13 |
content = f.read()
|
| 14 |
|
| 15 |
old_models_block = 'CLAUDE_OPUS_48_MODEL_ID = "anthropic/claude-opus-4.8:fal-ai"\nGPT_55_MODEL_ID = "openai/gpt-5.5:fal-ai"\nKIMI_K27_CODE_MODEL_ID = "moonshotai/Kimi-K2.7-Code:novita"\nMINIMAX_M3_MODEL_ID = "MiniMaxAI/MiniMax-M3:novita"\nGLM_52_MODEL_ID = "zai-org/GLM-5.2:novita"\nDEEPSEEK_V4_PRO_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro:novita"'
|
| 16 |
|
| 17 |
+
new_models_block = 'GPT_55_MODEL_ID = "openai/gpt-5.5:fal-ai"\nKIMI_K27_CODE_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro"\nMINIMAX_M3_MODEL_ID = "deepseek-ai/DeepSeek-V4-Flash"\nGLM_52_MODEL_ID = "openai/deepseek/deepseek-v4-flash"\nDEEPSEEK_V4_PRO_MODEL_ID = "nvidia/nemotron-3-super-120b-a12b:free"\n\n# === PATCH: OpenRouter models ===\nGEMMA_4_31B_FREE_MODEL_ID = "openai/google/gemma-4-31b-it:free"\nTENCENT_HY3_FREE_MODEL_ID = "openai/tencent/hy3:free"\nLLAMA_3_3_70B_FREE_MODEL_ID = "openai/meta-llama/llama-3.3-70b-instruct:free"\nLLAMA_3_1_8B_MODEL_ID = "openai/meta-llama/llama-3.1-8b-instruct"\nLAGUNA_M1_FREE_MODEL_ID = "openai/poolside/laguna-m.1:free"'
|
| 18 |
|
| 19 |
content = content.replace(old_models_block, new_models_block)
|
| 20 |
|
| 21 |
old_hosted = "HOSTED_MODEL_IDS = {\n CLAUDE_OPUS_48_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GLM_52_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n}"
|
| 22 |
+
new_hosted = "HOSTED_MODEL_IDS = {\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GLM_52_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n GEMMA_4_31B_FREE_MODEL_ID,\n TENCENT_HY3_FREE_MODEL_ID,\n LLAMA_3_3_70B_FREE_MODEL_ID,\n LLAMA_3_1_8B_MODEL_ID,\n LAGUNA_M1_FREE_MODEL_ID,\n}"
|
| 23 |
content = content.replace(old_hosted, new_hosted)
|
| 24 |
|
| 25 |
with open(IDS_FILE, "w") as f:
|
| 26 |
f.write(content)
|
| 27 |
print("OK: model_ids.py patched")
|
| 28 |
|
| 29 |
+
# === Step 2: Patch agent.py — replace _available_models() via regex ===
|
| 30 |
+
with open(AGENT_FILE) as f:
|
|
|
|
| 31 |
content = f.read()
|
| 32 |
|
| 33 |
+
# Make DEFAULT_MODEL_ID and DEFAULT_GPT_MODEL_ID point to our models
|
| 34 |
+
content = content.replace(
|
| 35 |
+
"DEFAULT_MODEL_ID = GLM_52_MODEL_ID",
|
| 36 |
+
'DEFAULT_MODEL_ID = "openai/tencent/hy3:free"'
|
| 37 |
+
)
|
| 38 |
+
content = content.replace(
|
| 39 |
+
"DEFAULT_GPT_MODEL_ID = GPT_55_MODEL_ID",
|
| 40 |
+
'DEFAULT_GPT_MODEL_ID = "openai/gpt-oss-120b"'
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
# Update imports
|
| 44 |
old_import = "from agent.core.model_ids import (\n CLAUDE_OPUS_48_MODEL_ID,\n DEEPSEEK_V4_PRO_MODEL_ID,\n GLM_52_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n strip_huggingface_model_prefix,\n)"
|
| 45 |
+
new_import = "from agent.core.model_ids import (\n DEEPSEEK_V4_PRO_MODEL_ID,\n GLM_52_MODEL_ID,\n GPT_55_MODEL_ID,\n KIMI_K27_CODE_MODEL_ID,\n MINIMAX_M3_MODEL_ID,\n GEMMA_4_31B_FREE_MODEL_ID,\n TENCENT_HY3_FREE_MODEL_ID,\n LLAMA_3_3_70B_FREE_MODEL_ID,\n LLAMA_3_1_8B_MODEL_ID,\n LAGUNA_M1_FREE_MODEL_ID,\n strip_huggingface_model_prefix,\n)"
|
| 46 |
content = content.replace(old_import, new_import)
|
| 47 |
|
| 48 |
+
# Replace _available_models() using regex (giống control space)
|
| 49 |
+
func_pattern = re.compile(
|
| 50 |
+
r'def _available_models\(\) -> list\[dict\[str, Any\]\]:\s*\n'
|
| 51 |
+
r'\s+models\s*=\s*\[.*?\]\s*\n\s+return models',
|
| 52 |
+
re.DOTALL
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
new_func = '''def _available_models() -> list[dict[str, Any]]:
|
| 56 |
+
models = [
|
| 57 |
+
{
|
| 58 |
+
"id": TENCENT_HY3_FREE_MODEL_ID,
|
| 59 |
+
"label": "Tencent HY3:free",
|
| 60 |
+
"recommended": True,
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"id": GEMMA_4_31B_FREE_MODEL_ID,
|
| 64 |
+
"label": "Gemma 4 31B:free",
|
| 65 |
+
"recommended": True,
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"id": LLAMA_3_3_70B_FREE_MODEL_ID,
|
| 69 |
+
"label": "Llama 3.3 70B:free",
|
| 70 |
+
"recommended": True,
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"id": LAGUNA_M1_FREE_MODEL_ID,
|
| 74 |
+
"label": "Laguna M.1:free",
|
| 75 |
+
"recommended": True,
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"id": LLAMA_3_1_8B_MODEL_ID,
|
| 79 |
+
"label": "Llama 3.1 8B",
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"id": DEFAULT_GPT_MODEL_ID,
|
| 83 |
+
"label": "GPT-5.5",
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
"id": KIMI_K27_CODE_MODEL_ID,
|
| 87 |
+
"label": "DeepSeek V4 Pro",
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
"id": MINIMAX_M3_MODEL_ID,
|
| 91 |
+
"label": "DeepSeek V4 Flash",
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"id": DEFAULT_MODEL_ID,
|
| 95 |
+
"label": "DeepSeek V4 Flash",
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"id": DEEPSEEK_V4_PRO_MODEL_ID,
|
| 99 |
+
"label": "Nemotron 3 Super 120B",
|
| 100 |
+
},
|
| 101 |
+
]
|
| 102 |
+
return models'''
|
| 103 |
+
|
| 104 |
+
func_match = func_pattern.search(content)
|
| 105 |
+
if func_match:
|
| 106 |
+
content = content[:func_match.start()] + new_func + content[func_match.end():]
|
| 107 |
+
print("OK: Replaced _available_models() via regex")
|
| 108 |
+
else:
|
| 109 |
+
print("WARN: Regex failed, trying string replace...")
|
| 110 |
+
old_available = 'def _available_models() -> list[dict[str, Any]]:\n models = [\n {\n "id": CLAUDE_OPUS_48_MODEL_ID,\n "label": "Claude Opus 4.8",\n },\n {\n "id": DEFAULT_GPT_MODEL_ID,\n "label": "GPT-5.5",\n },\n {\n "id": KIMI_K27_CODE_MODEL_ID,\n "label": "Kimi K2.7 Code",\n },\n {\n "id": MINIMAX_M3_MODEL_ID,\n "label": "MiniMax M3",\n },\n {\n "id": DEFAULT_MODEL_ID,\n "label": "GLM 5.2",\n "recommended": True,\n },\n {\n "id": DEEPSEEK_V4_PRO_MODEL_ID,\n "label": "DeepSeek V4 Pro",\n },\n ]\n return models'
|
| 111 |
+
if old_available in content:
|
| 112 |
+
content = content.replace(old_available, new_func)
|
| 113 |
+
print("OK: String replace worked")
|
| 114 |
+
else:
|
| 115 |
+
print("FAIL: Cannot find _available_models()!")
|
| 116 |
+
import sys
|
| 117 |
+
sys.exit(1)
|
| 118 |
|
| 119 |
# Update title generation
|
| 120 |
old_title = '"openai/gpt-oss-120b:cerebras",'
|
|
|
|
| 133 |
print("OK: agent.py patched")
|
| 134 |
|
| 135 |
# === Step 3: Patch _resolve_llm_params for OpenRouter routing ===
|
|
|
|
| 136 |
with open(LLM_PARAMS_FILE) as f:
|
| 137 |
llm_content = f.read()
|
| 138 |
|
|
|
|
| 139 |
if "normalized_model.startswith" not in llm_content:
|
| 140 |
api_key_find = "api_key = _resolve_hf_router_token(session_hf_token)"
|
| 141 |
if api_key_find in llm_content:
|
|
|
|
| 153 |
llm_content = llm_content[:line_end] + routing_insert + llm_content[line_end:]
|
| 154 |
print("OK: Patched _resolve_llm_params (catch-all openai/ -> OR)")
|
| 155 |
else:
|
| 156 |
+
print("WARN: Could not find api_key line")
|
| 157 |
else:
|
| 158 |
print("OK: llm_params.py already patched")
|
| 159 |
|