bep40 commited on
Commit
96eedae
·
verified ·
1 Parent(s): 38a63c8

Upload patch_models.py

Browse files
Files changed (1) hide show
  1. patch_models.py +25 -17
patch_models.py CHANGED
@@ -49,18 +49,22 @@ with open(AGENT_FILE, "w") as f:
49
  with open(LLM_PARAMS_FILE) as f:
50
  llm_content = f.read()
51
 
52
- marker = """ if local_model_provider(normalized_model) is not None:
53
- return _resolve_local_model_params(normalized_model, reasoning_effort, strict)
54
-
55
- hf_model = normalized_model"""
56
-
57
- replacement = """ if local_model_provider(normalized_model) is not None:
58
- return _resolve_local_model_params(normalized_model, reasoning_effort, strict)
 
 
 
 
 
59
 
60
  # === PATCH: Route OpenRouter-only models to openrouter.ai ===
61
- # Models with openai/<org>/<model> (3+ segments) are OpenRouter-only
62
- # and must NOT go through HF Router, which doesn't serve them.
63
- # openai/gpt-5.5:fal-ai (1 segment) stays on HF Router as before.
64
  _or_prefixes = ("openai/google/", "openai/deepseek/", "openai/nvidia/", "openai/poolside/")
65
  if normalized_model.startswith(_or_prefixes) or normalized_model == "openai/owl-alpha":
66
  return {
@@ -70,13 +74,17 @@ replacement = """ if local_model_provider(normalized_model) is not None:
70
  }
71
  # === END PATCH ===
72
 
73
- hf_model = normalized_model"""
 
 
 
 
74
 
75
- if marker in llm_content:
76
- llm_content = llm_content.replace(marker, replacement)
77
- print("✅ Patched _resolve_llm_params: OpenRouter routing added")
78
  else:
79
- print("⚠ Could not find marker in llm_params.py")
80
 
81
  try:
82
  ast.parse(llm_content)
@@ -90,5 +98,5 @@ with open(LLM_PARAMS_FILE, "w") as f:
90
 
91
  print("✅ Backend patched successfully")
92
  print(" - AVAILABLE_MODELS: 12 new models")
93
- print(" - _resolve_llm_params: openai/google/|deepseek/|nvidia/|poolside/ + owl-alpha OpenRouter")
94
- print(" - openai/gpt-5.5:fal-ai stays on HF Router (correct)")
 
49
  with open(LLM_PARAMS_FILE) as f:
50
  llm_content = f.read()
51
 
52
+ # The OpenRouter patch must go AFTER `api_key` is resolved.
53
+ # We replace the HF Router assignment block to add OR routing.
54
+ old_block = """ hf_model = normalized_model
55
+ api_key = _resolve_hf_router_token(session_hf_token)
56
+ params = {
57
+ "model": f"openai/{hf_model}",
58
+ "api_base": HF_ROUTER_BASE_URL,
59
+ "api_key": api_key,
60
+ }"""
61
+
62
+ new_block = """ hf_model = normalized_model
63
+ api_key = _resolve_hf_router_token(session_hf_token)
64
 
65
  # === PATCH: Route OpenRouter-only models to openrouter.ai ===
66
+ # Models with openai/<org>/<model> (3+ segments) are OpenRouter-only.
67
+ # openai/gpt-5.5:fal-ai (2 segments) stays on HF Router.
 
68
  _or_prefixes = ("openai/google/", "openai/deepseek/", "openai/nvidia/", "openai/poolside/")
69
  if normalized_model.startswith(_or_prefixes) or normalized_model == "openai/owl-alpha":
70
  return {
 
74
  }
75
  # === END PATCH ===
76
 
77
+ params = {
78
+ "model": f"openai/{hf_model}",
79
+ "api_base": HF_ROUTER_BASE_URL,
80
+ "api_key": api_key,
81
+ }"""
82
 
83
+ if old_block in llm_content:
84
+ llm_content = llm_content.replace(old_block, new_block)
85
+ print("✅ Patched _resolve_llm_params: OpenRouter routing added (after api_key)")
86
  else:
87
+ print("⚠ Could not find target block in llm_params.py")
88
 
89
  try:
90
  ast.parse(llm_content)
 
98
 
99
  print("✅ Backend patched successfully")
100
  print(" - AVAILABLE_MODELS: 12 new models")
101
+ print(" - _resolve_llm_params: OpenRouter check after api_key resolves")
102
+ print(" - No more UnboundLocalError on api_key")