bep40 commited on
Commit
e30a6a3
·
verified ·
1 Parent(s): 3ef74eb

Upload patch_flint_free.py

Browse files
Files changed (1) hide show
  1. patch_flint_free.py +60 -83
patch_flint_free.py CHANGED
@@ -8,12 +8,11 @@ Uses the same `openai/` path convention as Laguna S 2.1:free:
8
  IMPORTANT: recursive/flint-v1.0:free has `reasoning: { mandatory: true }` on
9
  OpenRouter. Without sending reasoning, the Recursive provider returns
10
  400 "Inference request failed". We patch the `openai/` routing branch in
11
- llm_params.py to inject `reasoning: {"effort": "low"}` into the params for
12
- flint models so the provider accepts the request.
13
 
14
- Runs AFTER patch_models.py / patch_new_models.py / patch_xing_nemotron_vl.py
15
- and patch_openrouter_fix.py (which adds the openai/ routing block).
16
- Idempotent.
17
  """
18
 
19
  import ast
@@ -36,6 +35,7 @@ if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in c:
36
  anchors = [
37
  'NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID = "openai/nvidia/nemotron-3.5-super-vl:free"',
38
  'LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"',
 
39
  ]
40
  inserted = False
41
  for anchor in anchors:
@@ -60,15 +60,14 @@ with open(AGENT_FILE) as f:
60
  ac = f.read()
61
 
62
  if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in ac:
63
- # patch_xing_nemotron_vl.py injected NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID into
64
- # the import block after LING_3_0_FLASH_FREE_MODEL_ID.
65
- imp_anchor = " LING_3_0_FLASH_FREE_MODEL_ID,\n"
 
66
  if imp_anchor in ac:
67
  ac = ac.replace(
68
  imp_anchor,
69
- imp_anchor
70
- + " NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID,\n"
71
- + " RECURSIVE_FLINT_V1_FREE_MODEL_ID,\n",
72
  1,
73
  )
74
  print("OK: added RECURSIVE_FLINT_V1 import to agent.py")
@@ -77,34 +76,35 @@ if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in ac:
77
  raise SystemExit(1)
78
 
79
  # ---------- 3. agent.py : _available_models ----------
80
- # Insert after the NEMOTRON_3_5_SUPER_VL entry in _available_models.
81
- line_anchor = (
82
- ' {"id": NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID, '
83
- '"label": "nvidia/nemotron-3.5-super-vl:free", "recommended": True},\n'
 
84
  )
85
- new_line = line_anchor + (
86
- ' {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, '
87
- '"label": "recursive/flint-v1.0:free", "recommended": True},\n'
 
 
88
  )
89
-
90
- if line_anchor in ac:
91
- ac = ac.replace(line_anchor, new_line, 1)
92
  print("OK: added recursive/flint-v1.0:free to _available_models()")
93
  else:
94
- print("WARN: NEMOTRON VL line anchor not matched, trying fallback...")
95
  alt_anchor = (
96
- ' {"id": LING_3_0_FLASH_FREE_MODEL_ID, '
97
- '"label": "inclusionai/ling-3.0-flash:free", "recommended": True},\n'
98
  )
99
  if alt_anchor in ac:
100
  ac = ac.replace(
101
  alt_anchor,
102
- alt_anchor + new_line,
103
  1,
104
  )
105
  print("OK: added recursive/flint-v1.0:free to _available_models() (fallback)")
106
  else:
107
- print("WARN: fallback anchor also not matched - model may already be present or patches changed")
108
 
109
  with open(AGENT_FILE, "w") as f:
110
  f.write(ac)
@@ -114,50 +114,29 @@ else:
114
  print("OK: agent.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)")
115
 
116
  # ---------- 4. llm_params.py : reasoning support for recursive/flint-v1.0 ----------
117
- # Flint has reasoning: { mandatory: true } on OpenRouter. Without sending reasoning,
118
- # the Recursive provider returns 400 "Inference request failed".
119
  # We modify the openai/ routing block to include reasoning for flint models.
120
- # The reasoning parameter is passed as a top-level LiteLLM kwarg so that when
121
- # using custom_llm_provider: "openrouter", LiteLLM includes it in the request
122
- # body sent to https://openrouter.ai/api/v1/chat/completions.
123
  with open(LLM_PARAMS_FILE) as f:
124
  lp = f.read()
125
 
126
  if "flint" not in lp.lower():
127
- # Version with comments (from patch_models.py comments)
128
- openai_block_v0 = (
129
- ' # Route openai/-prefixed models to OpenRouter\n'
130
- ' # FIX: Strip the "openai/" prefix - OpenRouter model IDs do not include it.\n'
131
- ' # e.g. "openai/deepseek/deepseek-v4-flash" -> "deepseek/deepseek-v4-flash"\n'
132
- ' # e.g. "openai/tencent/hy3:free" -> "tencent/hy3:free"\n'
133
- ' if normalized_model.startswith("openai/"):\n'
134
- ' or_model = normalized_model[len("openai/"):]\n'
135
- ' return {\n'
136
- ' "model": or_model,\n'
137
- ' "api_base": "https://openrouter.ai/api/v1",\n'
138
- ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
139
- ' }\n'
140
- )
141
- openai_block_v0_new = (
142
- ' # Route openai/-prefixed models to OpenRouter\n'
143
- ' # FIX: Strip the "openai/" prefix - OpenRouter model IDs do not include it.\n'
144
- ' # e.g. "openai/deepseek/deepseek-v4-flash" -> "deepseek/deepseek-v4-flash"\n'
145
- ' # e.g. "openai/tencent/hy3:free" -> "tencent/hy3:free"\n'
146
- ' if normalized_model.startswith("openai/"):\n'
147
- ' or_model = normalized_model[len("openai/"):]\n'
148
- ' params = {\n'
149
- ' "model": or_model,\n'
150
- ' "api_base": "https://openrouter.ai/api/v1",\n'
151
- ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
152
- ' }\n'
153
- ' # Flint requires reasoning to be explicitly enabled (mandatory on OpenRouter)\n'
154
- ' if "flint" in or_model.lower():\n'
155
- ' params["reasoning"] = {"effort": "low"}\n'
156
- ' return params\n'
157
- )
158
-
159
- # Version without comments (from patch_openrouter_fix.py, the final state)
160
- openai_block_v2 = (
161
  ' if normalized_model.startswith("openai/"):\n'
162
  ' or_model = normalized_model[len("openai/"):]\n'
163
  ' return {\n'
@@ -167,7 +146,8 @@ if "flint" not in lp.lower():
167
  ' "custom_llm_provider": "openrouter",\n'
168
  ' }\n'
169
  )
170
- openai_block_v2_new = (
 
171
  ' if normalized_model.startswith("openai/"):\n'
172
  ' or_model = normalized_model[len("openai/"):]\n'
173
  ' params = {\n'
@@ -176,14 +156,14 @@ if "flint" not in lp.lower():
176
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
177
  ' "custom_llm_provider": "openrouter",\n'
178
  ' }\n'
179
- ' # Flint requires reasoning to be explicitly enabled (mandatory on OpenRouter)\n'
180
  ' if "flint" in or_model.lower():\n'
181
- ' params["reasoning"] = {"effort": "low"}\n'
182
  ' return params\n'
183
  )
184
 
185
- # Plain version without custom_llm_provider (from patch_models.py, before openrouter_fix)
186
- openai_block_v1 = (
187
  ' if normalized_model.startswith("openai/"):\n'
188
  ' or_model = normalized_model[len("openai/"):]\n'
189
  ' return {\n'
@@ -192,7 +172,8 @@ if "flint" not in lp.lower():
192
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
193
  ' }\n'
194
  )
195
- openai_block_v1_new = (
 
196
  ' if normalized_model.startswith("openai/"):\n'
197
  ' or_model = normalized_model[len("openai/"):]\n'
198
  ' params = {\n'
@@ -200,20 +181,17 @@ if "flint" not in lp.lower():
200
  ' "api_base": "https://openrouter.ai/api/v1",\n'
201
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
202
  ' }\n'
203
- ' # Flint requires reasoning to be explicitly enabled (mandatory on OpenRouter)\n'
204
  ' if "flint" in or_model.lower():\n'
205
- ' params["reasoning"] = {"effort": "low"}\n'
206
  ' return params\n'
207
  )
208
 
209
- if openai_block_v0 in lp:
210
- lp = lp.replace(openai_block_v0, openai_block_v0_new)
211
- print("OK: patched llm_params.py openai/ block (v0 with comments) for flint reasoning")
212
- elif openai_block_v2 in lp:
213
- lp = lp.replace(openai_block_v2, openai_block_v2_new)
214
- print("OK: patched llm_params.py openai/ block (v2 custom_llm_provider) for flint reasoning")
215
- elif openai_block_v1 in lp:
216
- lp = lp.replace(openai_block_v1, openai_block_v1_new)
217
  print("OK: patched llm_params.py openai/ block (v1) for flint reasoning")
218
  else:
219
  print("WARN: could not find openai/ routing block in llm_params.py")
@@ -222,11 +200,10 @@ if "flint" not in lp.lower():
222
  if 'startswith("openai/")' in line:
223
  print(f" Found at line {i+1}: {line.strip()}")
224
  lines = lp.split('\n')
225
- for j in range(max(0,i-2), min(len(lines), i+8)):
226
  print(f" {j+1}: {lines[j]}")
227
  print("---")
228
  print(" - Flint model will be registered but may fail with 400 'Inference request failed'")
229
- print(" - The model has reasoning:{mandatory:true} on OpenRouter")
230
 
231
  with open(LLM_PARAMS_FILE, "w") as f:
232
  f.write(lp)
 
8
  IMPORTANT: recursive/flint-v1.0:free has `reasoning: { mandatory: true }` on
9
  OpenRouter. Without sending reasoning, the Recursive provider returns
10
  400 "Inference request failed". We patch the `openai/` routing branch in
11
+ llm_params.py to inject `reasoning: {"effort": "low"}` via extra_body so the
12
+ provider accepts the request.
13
 
14
+ Runs AFTER patch_models.py + patch_openrouter_fix.py (which adds the
15
+ openai/ routing block with custom_llm_provider). Idempotent.
 
16
  """
17
 
18
  import ast
 
35
  anchors = [
36
  'NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID = "openai/nvidia/nemotron-3.5-super-vl:free"',
37
  'LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"',
38
+ 'LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"',
39
  ]
40
  inserted = False
41
  for anchor in anchors:
 
60
  ac = f.read()
61
 
62
  if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in ac:
63
+ # Find the import block and add after the last imported constant
64
+ imp_anchor = " SPACE_BUNNY_ALPHA_MODEL_ID,\n"
65
+ if imp_anchor not in ac:
66
+ imp_anchor = " LING_3_0_FLASH_FREE_MODEL_ID,\n"
67
  if imp_anchor in ac:
68
  ac = ac.replace(
69
  imp_anchor,
70
+ imp_anchor + " RECURSIVE_FLINT_V1_FREE_MODEL_ID,\n",
 
 
71
  1,
72
  )
73
  print("OK: added RECURSIVE_FLINT_V1 import to agent.py")
 
76
  raise SystemExit(1)
77
 
78
  # ---------- 3. agent.py : _available_models ----------
79
+ # Insert before the closing of _available_models
80
+ models_anchor = (
81
+ ' {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n'
82
+ ' ]\n'
83
+ ' return models'
84
  )
85
+ new_models = (
86
+ ' {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n'
87
+ ' {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n'
88
+ ' ]\n'
89
+ ' return models'
90
  )
91
+ if models_anchor in ac:
92
+ ac = ac.replace(models_anchor, new_models, 1)
 
93
  print("OK: added recursive/flint-v1.0:free to _available_models()")
94
  else:
95
+ # Fallback: try the SPACE_BUNNY line as anchor
96
  alt_anchor = (
97
+ ' {"id": SPACE_BUNNY_ALPHA_MODEL_ID, "label": "stealth/space-bunny-alpha", "recommended": True},\n'
 
98
  )
99
  if alt_anchor in ac:
100
  ac = ac.replace(
101
  alt_anchor,
102
+ alt_anchor + ' {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n',
103
  1,
104
  )
105
  print("OK: added recursive/flint-v1.0:free to _available_models() (fallback)")
106
  else:
107
+ print("WARN: could not find _available_models anchor - model may already be present")
108
 
109
  with open(AGENT_FILE, "w") as f:
110
  f.write(ac)
 
114
  print("OK: agent.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)")
115
 
116
  # ---------- 4. llm_params.py : reasoning support for recursive/flint-v1.0 ----------
117
+ # Flint has reasoning: { mandatory: true } on OpenRouter. Without sending
118
+ # reasoning, the Recursive provider returns 400 "Inference request failed".
119
  # We modify the openai/ routing block to include reasoning for flint models.
 
 
 
120
  with open(LLM_PARAMS_FILE) as f:
121
  lp = f.read()
122
 
123
  if "flint" not in lp.lower():
124
+ # The patched llm_params.py (after patch_models.py + patch_openrouter_fix.py)
125
+ # has an openai/ routing block with custom_llm_provider: "openrouter".
126
+ # We modify it to add reasoning via extra_body for flint models.
127
+
128
+ # The block looks like:
129
+ # if normalized_model.startswith("openai/"):
130
+ # or_model = normalized_model[len("openai/"):]
131
+ # return {
132
+ # "model": or_model,
133
+ # "api_base": "https://openrouter.ai/api/v1",
134
+ # "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",
135
+ # "custom_llm_provider": "openrouter",
136
+ # }
137
+
138
+ # v2 (with custom_llm_provider) - the final state after all patches
139
+ old_block_v2 = (
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  ' if normalized_model.startswith("openai/"):\n'
141
  ' or_model = normalized_model[len("openai/"):]\n'
142
  ' return {\n'
 
146
  ' "custom_llm_provider": "openrouter",\n'
147
  ' }\n'
148
  )
149
+
150
+ new_block_v2 = (
151
  ' if normalized_model.startswith("openai/"):\n'
152
  ' or_model = normalized_model[len("openai/"):]\n'
153
  ' params = {\n'
 
156
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
157
  ' "custom_llm_provider": "openrouter",\n'
158
  ' }\n'
159
+ ' # Flint requires reasoning (mandatory on OpenRouter)\n'
160
  ' if "flint" in or_model.lower():\n'
161
+ ' params["extra_body"] = {"reasoning": {"effort": "low"}}\n'
162
  ' return params\n'
163
  )
164
 
165
+ # v1 (without custom_llm_provider)
166
+ old_block_v1 = (
167
  ' if normalized_model.startswith("openai/"):\n'
168
  ' or_model = normalized_model[len("openai/"):]\n'
169
  ' return {\n'
 
172
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
173
  ' }\n'
174
  )
175
+
176
+ new_block_v1 = (
177
  ' if normalized_model.startswith("openai/"):\n'
178
  ' or_model = normalized_model[len("openai/"):]\n'
179
  ' params = {\n'
 
181
  ' "api_base": "https://openrouter.ai/api/v1",\n'
182
  ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n'
183
  ' }\n'
184
+ ' # Flint requires reasoning (mandatory on OpenRouter)\n'
185
  ' if "flint" in or_model.lower():\n'
186
+ ' params["extra_body"] = {"reasoning": {"effort": "low"}}\n'
187
  ' return params\n'
188
  )
189
 
190
+ if old_block_v2 in lp:
191
+ lp = lp.replace(old_block_v2, new_block_v2)
192
+ print("OK: patched llm_params.py openai/ block (v2 with custom_llm_provider) for flint reasoning")
193
+ elif old_block_v1 in lp:
194
+ lp = lp.replace(old_block_v1, new_block_v1)
 
 
 
195
  print("OK: patched llm_params.py openai/ block (v1) for flint reasoning")
196
  else:
197
  print("WARN: could not find openai/ routing block in llm_params.py")
 
200
  if 'startswith("openai/")' in line:
201
  print(f" Found at line {i+1}: {line.strip()}")
202
  lines = lp.split('\n')
203
+ for j in range(max(0,i-2), min(len(lines), i+10)):
204
  print(f" {j+1}: {lines[j]}")
205
  print("---")
206
  print(" - Flint model will be registered but may fail with 400 'Inference request failed'")
 
207
 
208
  with open(LLM_PARAMS_FILE, "w") as f:
209
  f.write(lp)