Spaces:
Running
Running
Upload patch_models.py
#12
by bep40 - opened
- patch_models.py +44 -42
patch_models.py
CHANGED
|
@@ -12,10 +12,12 @@ IDS_FILE = "/app/agent/core/model_ids.py"
|
|
| 12 |
with open(IDS_FILE) as f:
|
| 13 |
content = f.read()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Swap out old model definitions for new ones
|
| 16 |
-
content = content.replace(
|
| 17 |
-
"CLAUDE_OPUS_48_MODEL_ID", "__DELETED_CLAUDE"
|
| 18 |
-
)
|
| 19 |
content = content.replace(
|
| 20 |
'KIMI_K27_CODE_MODEL_ID = "moonshotai/Kimi-K2.7-Code:novita"',
|
| 21 |
'KIMI_K27_CODE_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro"',
|
|
@@ -33,37 +35,54 @@ content = content.replace(
|
|
| 33 |
'DEEPSEEK_V4_PRO_MODEL_ID = "nvidia/nemotron-3-super-120b-a12b:free"',
|
| 34 |
)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
TENCENT_HY3_FREE_MODEL_ID = "openai/tencent/hy3:free"
|
|
|
|
| 40 |
LLAMA_3_3_70B_FREE_MODEL_ID = "openai/meta-llama/llama-3.3-70b-instruct:free"
|
| 41 |
LLAMA_3_1_8B_MODEL_ID = "openai/meta-llama/llama-3.1-8b-instruct"
|
| 42 |
LAGUNA_M1_FREE_MODEL_ID = "openai/poolside/laguna-m.1:free"
|
| 43 |
LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"
|
| 44 |
NEX_N2_MINI_MODEL_ID = "openai/nex-agi/nex-n2-mini"
|
| 45 |
LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"""
|
| 49 |
-
if "GEMMA_4_31B_FREE_MODEL_ID" not in content:
|
| 50 |
-
content = content.replace(
|
| 51 |
-
"DEEPSEEK_V4_PRO_MODEL_ID",
|
| 52 |
-
new_ids + "\nDEEPSEEK_V4_PRO_MODEL_ID"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
# Update HOSTED_MODEL_IDS
|
| 56 |
-
hosted_start = content.find("HOSTED_MODEL_IDS = {")
|
| 57 |
-
if hosted_start >= 0:
|
| 58 |
-
hosted_end = content.find("}", hosted_start) + 1
|
| 59 |
-
hosted_block = content[hosted_start:hosted_end]
|
| 60 |
-
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 LAGUNA_S21_FREE_MODEL_ID,\n NEX_N2_MINI_MODEL_ID,\n LING_3_0_FLASH_FREE_MODEL_ID,\n DEEPSEEK_V4_FLASH_LATEST_MODEL_ID,\n}"
|
| 61 |
-
content = content.replace(hosted_block, new_hosted)
|
| 62 |
|
| 63 |
with open(IDS_FILE, "w") as f:
|
| 64 |
f.write(content)
|
| 65 |
print("OK: model_ids.py patched")
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
# === Step 2: Patch agent.py ===
|
| 68 |
with open(AGENT_FILE) as f:
|
| 69 |
content = f.read()
|
|
@@ -96,16 +115,14 @@ new_import = (
|
|
| 96 |
" GPT_55_MODEL_ID,\n"
|
| 97 |
" KIMI_K27_CODE_MODEL_ID,\n"
|
| 98 |
" MINIMAX_M3_MODEL_ID,\n"
|
| 99 |
-
" GEMMA_4_31B_FREE_MODEL_ID,\n"
|
| 100 |
" TENCENT_HY3_FREE_MODEL_ID,\n"
|
|
|
|
| 101 |
" LLAMA_3_3_70B_FREE_MODEL_ID,\n"
|
| 102 |
" LLAMA_3_1_8B_MODEL_ID,\n"
|
| 103 |
" LAGUNA_M1_FREE_MODEL_ID,\n"
|
| 104 |
" LAGUNA_S21_FREE_MODEL_ID,\n"
|
| 105 |
" NEX_N2_MINI_MODEL_ID,\n"
|
| 106 |
" LING_3_0_FLASH_FREE_MODEL_ID,\n"
|
| 107 |
-
" DEEPSEEK_V4_FLASH_LATEST_MODEL_ID,\n"
|
| 108 |
-
" NO_TOOLS_MODELS,\n"
|
| 109 |
" strip_huggingface_model_prefix,\n"
|
| 110 |
")"
|
| 111 |
)
|
|
@@ -127,12 +144,10 @@ new_func = (
|
|
| 127 |
' {"id": MINIMAX_M3_MODEL_ID, "label": "DeepSeek V4 Flash"},\n'
|
| 128 |
' {"id": DEFAULT_MODEL_ID, "label": "DeepSeek V4 Flash"},\n'
|
| 129 |
' {"id": DEEPSEEK_V4_PRO_MODEL_ID, "label": "Nemotron 3 Super 120B"},\n'
|
| 130 |
-
' {"id": DEEPSEEK_V4_FLASH_LATEST_MODEL_ID, "label": "DeepSeek V4 Flash latest"},\n'
|
| 131 |
" ]\n"
|
| 132 |
" return models"
|
| 133 |
)
|
| 134 |
|
| 135 |
-
# Find the old function by pattern
|
| 136 |
func_match = re.search(
|
| 137 |
r"def _available_models\(\)\s*->\s*list\[dict\[str,\s*Any\]\]:.*?return models",
|
| 138 |
content, re.DOTALL
|
|
@@ -141,7 +156,6 @@ if func_match:
|
|
| 141 |
content = content[:func_match.start()] + new_func + content[func_match.end():]
|
| 142 |
print("OK: Replaced _available_models()")
|
| 143 |
else:
|
| 144 |
-
# Fallback: try exact match
|
| 145 |
old_func = (
|
| 146 |
"def _available_models() -> list[dict[str, Any]]:\n"
|
| 147 |
" models = [\n"
|
|
@@ -158,12 +172,7 @@ else:
|
|
| 158 |
content = content.replace(old_func, new_func)
|
| 159 |
print("OK: String replace of _available_models()")
|
| 160 |
else:
|
| 161 |
-
print("
|
| 162 |
-
print("Content excerpt around 'def _available_models':")
|
| 163 |
-
idx = content.find("def _available_models")
|
| 164 |
-
if idx >= 0:
|
| 165 |
-
print(content[idx:idx+500])
|
| 166 |
-
import sys; sys.exit(1)
|
| 167 |
|
| 168 |
# Update title gen model
|
| 169 |
content = content.replace(
|
|
@@ -183,7 +192,7 @@ with open(AGENT_FILE, "w") as f:
|
|
| 183 |
f.write(content)
|
| 184 |
print("OK: agent.py patched")
|
| 185 |
|
| 186 |
-
# === Step 3: Patch _resolve_llm_params
|
| 187 |
with open(LLM_PARAMS_FILE) as f:
|
| 188 |
llm_content = f.read()
|
| 189 |
|
|
@@ -192,13 +201,6 @@ if "normalized_model.startswith" not in llm_content:
|
|
| 192 |
if api_key_find in llm_content:
|
| 193 |
line_end = llm_content.find("\n", llm_content.find(api_key_find) + len(api_key_find)) + 1
|
| 194 |
routing_insert = (
|
| 195 |
-
' # Route deepseek/-prefixed models to OpenRouter (no tool use)\n'
|
| 196 |
-
' if normalized_model.startswith("~deepseek/") or normalized_model.startswith("deepseek/"):\n'
|
| 197 |
-
' return {\n'
|
| 198 |
-
' "model": normalized_model,\n'
|
| 199 |
-
' "api_base": "https://openrouter.ai/api/v1",\n'
|
| 200 |
-
' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
|
| 201 |
-
' }\n\n'
|
| 202 |
' # Route openai/-prefixed models to OpenRouter\n'
|
| 203 |
' if normalized_model.startswith("openai/"):\n'
|
| 204 |
' return {\n'
|
|
|
|
| 12 |
with open(IDS_FILE) as f:
|
| 13 |
content = f.read()
|
| 14 |
|
| 15 |
+
# --- Remove any previous NO_TOOLS_MODELS addition first (clean slate) ---
|
| 16 |
+
if "NO_TOOLS_MODELS" in content:
|
| 17 |
+
content = re.sub(r'NO_TOOLS_MODELS\s*=\s*\{[^}]*\}\n?', '', content)
|
| 18 |
+
print("OK: Removed old NO_TOOLS_MODELS")
|
| 19 |
+
|
| 20 |
# Swap out old model definitions for new ones
|
|
|
|
|
|
|
|
|
|
| 21 |
content = content.replace(
|
| 22 |
'KIMI_K27_CODE_MODEL_ID = "moonshotai/Kimi-K2.7-Code:novita"',
|
| 23 |
'KIMI_K27_CODE_MODEL_ID = "deepseek-ai/DeepSeek-V4-Pro"',
|
|
|
|
| 35 |
'DEEPSEEK_V4_PRO_MODEL_ID = "nvidia/nemotron-3-super-120b-a12b:free"',
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# Replace all existing deepseek model IDs with 0731 version
|
| 39 |
+
content = content.replace(
|
| 40 |
+
'~deepseek/deepseek-v4-flash-latest',
|
| 41 |
+
'~deepseek/deepseek-v4-flash-0731'
|
| 42 |
+
)
|
| 43 |
+
content = content.replace(
|
| 44 |
+
'deepseek/deepseek-v4-flash-latest',
|
| 45 |
+
'~deepseek/deepseek-v4-flash-0731'
|
| 46 |
+
)
|
| 47 |
+
content = content.replace(
|
| 48 |
+
'deepseek/deepseek-v4-flash',
|
| 49 |
+
'deepseek/deepseek-v4-flash' # keep as-is
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Add missing model ID constants if not present
|
| 53 |
+
if "GEMMA_4_31B_FREE_MODEL_ID" not in content:
|
| 54 |
+
# Find where model constants end (after DEEPSEEK_V4_PRO_MODEL_ID)
|
| 55 |
+
pos = content.find('DEEPSEEK_V4_PRO_MODEL_ID')
|
| 56 |
+
if pos >= 0:
|
| 57 |
+
# Find the next line break after this constant definition
|
| 58 |
+
next_newline = content.find('\n', pos)
|
| 59 |
+
if next_newline >= 0:
|
| 60 |
+
before = content[:next_newline + 1]
|
| 61 |
+
after = content[next_newline + 1:]
|
| 62 |
+
new_constants = '''
|
| 63 |
TENCENT_HY3_FREE_MODEL_ID = "openai/tencent/hy3:free"
|
| 64 |
+
GEMMA_4_31B_FREE_MODEL_ID = "openai/google/gemma-4-31b-it:free"
|
| 65 |
LLAMA_3_3_70B_FREE_MODEL_ID = "openai/meta-llama/llama-3.3-70b-instruct:free"
|
| 66 |
LLAMA_3_1_8B_MODEL_ID = "openai/meta-llama/llama-3.1-8b-instruct"
|
| 67 |
LAGUNA_M1_FREE_MODEL_ID = "openai/poolside/laguna-m.1:free"
|
| 68 |
LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"
|
| 69 |
NEX_N2_MINI_MODEL_ID = "openai/nex-agi/nex-n2-mini"
|
| 70 |
LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"
|
| 71 |
+
'''
|
| 72 |
+
content = before + new_constants + after
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with open(IDS_FILE, "w") as f:
|
| 75 |
f.write(content)
|
| 76 |
print("OK: model_ids.py patched")
|
| 77 |
|
| 78 |
+
# Validate syntax
|
| 79 |
+
try:
|
| 80 |
+
ast.parse(content)
|
| 81 |
+
print("OK: model_ids.py syntax OK")
|
| 82 |
+
except SyntaxError as e:
|
| 83 |
+
print(f"FAIL: model_ids.py syntax error: {e}")
|
| 84 |
+
raise
|
| 85 |
+
|
| 86 |
# === Step 2: Patch agent.py ===
|
| 87 |
with open(AGENT_FILE) as f:
|
| 88 |
content = f.read()
|
|
|
|
| 115 |
" GPT_55_MODEL_ID,\n"
|
| 116 |
" KIMI_K27_CODE_MODEL_ID,\n"
|
| 117 |
" MINIMAX_M3_MODEL_ID,\n"
|
|
|
|
| 118 |
" TENCENT_HY3_FREE_MODEL_ID,\n"
|
| 119 |
+
" GEMMA_4_31B_FREE_MODEL_ID,\n"
|
| 120 |
" LLAMA_3_3_70B_FREE_MODEL_ID,\n"
|
| 121 |
" LLAMA_3_1_8B_MODEL_ID,\n"
|
| 122 |
" LAGUNA_M1_FREE_MODEL_ID,\n"
|
| 123 |
" LAGUNA_S21_FREE_MODEL_ID,\n"
|
| 124 |
" NEX_N2_MINI_MODEL_ID,\n"
|
| 125 |
" LING_3_0_FLASH_FREE_MODEL_ID,\n"
|
|
|
|
|
|
|
| 126 |
" strip_huggingface_model_prefix,\n"
|
| 127 |
")"
|
| 128 |
)
|
|
|
|
| 144 |
' {"id": MINIMAX_M3_MODEL_ID, "label": "DeepSeek V4 Flash"},\n'
|
| 145 |
' {"id": DEFAULT_MODEL_ID, "label": "DeepSeek V4 Flash"},\n'
|
| 146 |
' {"id": DEEPSEEK_V4_PRO_MODEL_ID, "label": "Nemotron 3 Super 120B"},\n'
|
|
|
|
| 147 |
" ]\n"
|
| 148 |
" return models"
|
| 149 |
)
|
| 150 |
|
|
|
|
| 151 |
func_match = re.search(
|
| 152 |
r"def _available_models\(\)\s*->\s*list\[dict\[str,\s*Any\]\]:.*?return models",
|
| 153 |
content, re.DOTALL
|
|
|
|
| 156 |
content = content[:func_match.start()] + new_func + content[func_match.end():]
|
| 157 |
print("OK: Replaced _available_models()")
|
| 158 |
else:
|
|
|
|
| 159 |
old_func = (
|
| 160 |
"def _available_models() -> list[dict[str, Any]]:\n"
|
| 161 |
" models = [\n"
|
|
|
|
| 172 |
content = content.replace(old_func, new_func)
|
| 173 |
print("OK: String replace of _available_models()")
|
| 174 |
else:
|
| 175 |
+
print("WARN: Cannot find _available_models(), skipping")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# Update title gen model
|
| 178 |
content = content.replace(
|
|
|
|
| 192 |
f.write(content)
|
| 193 |
print("OK: agent.py patched")
|
| 194 |
|
| 195 |
+
# === Step 3: Patch _resolve_llm_params ===
|
| 196 |
with open(LLM_PARAMS_FILE) as f:
|
| 197 |
llm_content = f.read()
|
| 198 |
|
|
|
|
| 201 |
if api_key_find in llm_content:
|
| 202 |
line_end = llm_content.find("\n", llm_content.find(api_key_find) + len(api_key_find)) + 1
|
| 203 |
routing_insert = (
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
' # Route openai/-prefixed models to OpenRouter\n'
|
| 205 |
' if normalized_model.startswith("openai/"):\n'
|
| 206 |
' return {\n'
|