bep40 commited on
Commit
c8c52c4
·
verified ·
1 Parent(s): f91b3a9

Upload patch_models.py

Browse files
Files changed (1) hide show
  1. patch_models.py +34 -28
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: Strip the "openai/" prefix before sending to OpenRouter!
215
- # The "openai/" prefix is needed by LiteLLM for provider detection,
216
- # but OpenRouter expects bare model slugs like "openrouter/owl-alpha".
217
  with open(LLM_PARAMS_FILE) as f:
218
  llm_content = f.read()
219
 
@@ -229,12 +229,11 @@ old_block = """ hf_model = normalized_model
229
  new_block = """ hf_model = normalized_model
230
  api_key = _resolve_hf_router_token(session_hf_token)
231
 
232
- # === PATCH: Route openai/-prefixed models to OpenRouter ===
233
- # Strip the "openai/" prefix — OpenRouter expects bare slugs like "openrouter/owl-alpha"
234
  if normalized_model.startswith("openai/"):
235
- actual_model = normalized_model[len("openai/"):]
236
  return {
237
- "model": actual_model,
238
  "api_base": "https://openrouter.ai/api/v1",
239
  "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
240
  }
@@ -248,30 +247,37 @@ new_block = """ hf_model = normalized_model
248
 
249
  if old_block in llm_content:
250
  llm_content = llm_content.replace(old_block, new_block)
251
- print("OK: Patched _resolve_llm_params (strips openai/ prefix)")
252
- else:
253
- # Already patched — update the OR routing to strip the prefix
254
  idx = llm_content.find("if normalized_model.startswith(\"openai/\"):")
255
  if idx >= 0:
256
- # Find the return block
257
- return_start = llm_content.find('"model":', idx)
258
- if return_start >= 0:
259
- line_start = llm_content.rfind('\n', 0, return_start) + 1
260
- line_end = llm_content.find('\n', line_start)
261
- old_model_line = llm_content[line_start:line_end]
262
-
263
- # Check if it already strips prefix
264
- if "normalized_model[len" in old_model_line or "actual_model" in old_model_line:
265
- print("OK: Already strips openai/ prefix")
266
- else:
267
- # Replace the model line to strip prefix
268
- new_model_line = ' "model": normalized_model[len("openai/"):],'
 
 
 
269
  llm_content = llm_content.replace(old_model_line, new_model_line)
270
- print("OK: Fixed to strip openai/ prefix before sending to OpenRouter")
 
 
 
 
271
  else:
272
- print("FAIL: Could not find routing condition!")
273
- import sys
274
- sys.exit(1)
275
 
276
  try:
277
  ast.parse(llm_content)
@@ -283,4 +289,4 @@ except SyntaxError as e:
283
  with open(LLM_PARAMS_FILE, "w") as f:
284
  f.write(llm_content)
285
 
286
- print("OK: Backend patched - 16 models, OR strips openai/ prefix, GPT-5.5→gpt-oss-120b, Riverflow→north-mini-code:free")
 
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
 
 
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,
237
  "api_base": "https://openrouter.ai/api/v1",
238
  "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
239
  }
 
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: Already using correct routing")
279
+ else:
280
+ print("OK: Routing already correct")
281
 
282
  try:
283
  ast.parse(llm_content)
 
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 (keep prefix), GPT-5.5→gpt-oss-120b, Riverflow→north-mini-code:free")