Spaces:
Running
Running
Download patch_flint_free.py from bep40/ml-intern: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_flint_free.py
- Command line
-
hf download hf://spaces/bep40/ml-intern/patch_flint_free.py
-
curl -L -o patch_flint_free.py https://huggingface.co/spaces/bep40/ml-intern/resolve/main/patch_flint_free.py
10.5 kB
| """Patch backend + frontend: add recursive/flint-v1.0:free (OpenRouter). | |
| Uses the same `openai/` path convention as Laguna S 2.1:free: | |
| - backend constant: RECURSIVE_FLINT_V1_FREE_MODEL_ID = "openai/recursive/flint-v1.0:free" | |
| - registered in _available_models() backend list | |
| - frontend: new ModelOption in ChatInput.tsx DEFAULT_MODEL_OPTIONS | |
| IMPORTANT: recursive/flint-v1.0:free has `reasoning: { mandatory: true }` on | |
| OpenRouter. Without sending reasoning, the Recursive provider returns | |
| 400 "Inference request failed". We patch the `openai/` routing branch in | |
| llm_params.py to inject `reasoning: {"effort": "low"}` via extra_body so the | |
| provider accepts the request. | |
| Runs AFTER patch_models.py + patch_openrouter_fix.py (which adds the | |
| openai/ routing block with custom_llm_provider). Idempotent. | |
| """ | |
| import ast | |
| import os | |
| IDS_FILE = "/app/agent/core/model_ids.py" | |
| AGENT_FILE = "/app/backend/routes/agent.py" | |
| LLM_PARAMS_FILE = "/app/agent/core/llm_params.py" | |
| FRONTEND_FILE = "/source/frontend/src/components/Chat/ChatInput.tsx" | |
| # ---------- 1. model_ids.py ---------- | |
| with open(IDS_FILE) as f: | |
| c = f.read() | |
| new_constant = 'RECURSIVE_FLINT_V1_FREE_MODEL_ID = "openai/recursive/flint-v1.0:free"\n' | |
| if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in c: | |
| # Anchor: the NEMOTRON_3_5_SUPER_VL line injected by patch_xing_nemotron_vl.py. | |
| # Fall back to LAGUNA_S21 line from patch_models.py. | |
| anchors = [ | |
| 'NEMOTRON_3_5_SUPER_VL_FREE_MODEL_ID = "openai/nvidia/nemotron-3.5-super-vl:free"', | |
| 'LAGUNA_S21_FREE_MODEL_ID = "openai/poolside/laguna-s-2.1:free"', | |
| 'LING_3_0_FLASH_FREE_MODEL_ID = "openai/inclusionai/ling-3.0-flash:free"', | |
| ] | |
| inserted = False | |
| for anchor in anchors: | |
| if anchor in c: | |
| c = c.replace(anchor, anchor + "\n" + new_constant, 1) | |
| print(f"OK: added constant to model_ids.py after {anchor.split(' =')[0]}") | |
| inserted = True | |
| break | |
| if not inserted: | |
| print("FAIL: no anchor found in model_ids.py for constant insertion") | |
| raise SystemExit(1) | |
| with open(IDS_FILE, "w") as f: | |
| f.write(c) | |
| ast.parse(c) | |
| print("OK: model_ids.py written & syntax OK") | |
| else: | |
| print("OK: model_ids.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)") | |
| # ---------- 2. agent.py : imports ---------- | |
| with open(AGENT_FILE) as f: | |
| ac = f.read() | |
| if "RECURSIVE_FLINT_V1_FREE_MODEL_ID" not in ac: | |
| # Find the import block and add after the last imported constant | |
| imp_anchor = " SPACE_BUNNY_ALPHA_MODEL_ID,\n" | |
| if imp_anchor not in ac: | |
| imp_anchor = " LING_3_0_FLASH_FREE_MODEL_ID,\n" | |
| if imp_anchor in ac: | |
| ac = ac.replace( | |
| imp_anchor, | |
| imp_anchor + " RECURSIVE_FLINT_V1_FREE_MODEL_ID,\n", | |
| 1, | |
| ) | |
| print("OK: added RECURSIVE_FLINT_V1 import to agent.py") | |
| else: | |
| print("FAIL: import anchor not found in agent.py") | |
| raise SystemExit(1) | |
| # ---------- 3. agent.py : _available_models ---------- | |
| # Insert before the closing of _available_models | |
| models_anchor = ( | |
| ' {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n' | |
| ' ]\n' | |
| ' return models' | |
| ) | |
| new_models = ( | |
| ' {"id": "deepseek/deepseek-v4-flash-0731", "label": "DeepSeek V4 Flash 0731", "recommended": True},\n' | |
| ' {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n' | |
| ' ]\n' | |
| ' return models' | |
| ) | |
| if models_anchor in ac: | |
| ac = ac.replace(models_anchor, new_models, 1) | |
| print("OK: added recursive/flint-v1.0:free to _available_models()") | |
| else: | |
| # Fallback: try the SPACE_BUNNY line as anchor | |
| alt_anchor = ( | |
| ' {"id": SPACE_BUNNY_ALPHA_MODEL_ID, "label": "stealth/space-bunny-alpha", "recommended": True},\n' | |
| ) | |
| if alt_anchor in ac: | |
| ac = ac.replace( | |
| alt_anchor, | |
| alt_anchor + ' {"id": RECURSIVE_FLINT_V1_FREE_MODEL_ID, "label": "recursive/flint-v1.0:free", "recommended": True},\n', | |
| 1, | |
| ) | |
| print("OK: added recursive/flint-v1.0:free to _available_models() (fallback)") | |
| else: | |
| print("WARN: could not find _available_models anchor - model may already be present") | |
| with open(AGENT_FILE, "w") as f: | |
| f.write(ac) | |
| ast.parse(ac) | |
| print("OK: agent.py written & syntax OK") | |
| else: | |
| print("OK: agent.py already has RECURSIVE_FLINT_V1_FREE_MODEL_ID (idempotent)") | |
| # ---------- 4. llm_params.py : reasoning support for recursive/flint-v1.0 ---------- | |
| # Flint has reasoning: { mandatory: true } on OpenRouter. Without sending | |
| # reasoning, the Recursive provider returns 400 "Inference request failed". | |
| # We modify the openai/ routing block to include reasoning for flint models. | |
| with open(LLM_PARAMS_FILE) as f: | |
| lp = f.read() | |
| if "flint" not in lp.lower(): | |
| # The patched llm_params.py (after patch_models.py + patch_openrouter_fix.py) | |
| # has an openai/ routing block with custom_llm_provider: "openrouter". | |
| # We modify it to add reasoning via extra_body for flint models. | |
| # The block looks like: | |
| # if normalized_model.startswith("openai/"): | |
| # or_model = normalized_model[len("openai/"):] | |
| # return { | |
| # "model": or_model, | |
| # "api_base": "https://openrouter.ai/api/v1", | |
| # "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "", | |
| # "custom_llm_provider": "openrouter", | |
| # } | |
| # v2 (with custom_llm_provider) - the final state after all patches | |
| old_block_v2 = ( | |
| ' if normalized_model.startswith("openai/"):\n' | |
| ' or_model = normalized_model[len("openai/"):]\n' | |
| ' return {\n' | |
| ' "model": or_model,\n' | |
| ' "api_base": "https://openrouter.ai/api/v1",\n' | |
| ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n' | |
| ' "custom_llm_provider": "openrouter",\n' | |
| ' }\n' | |
| ) | |
| new_block_v2 = ( | |
| ' if normalized_model.startswith("openai/"):\n' | |
| ' or_model = normalized_model[len("openai/"):]\n' | |
| ' params = {\n' | |
| ' "model": or_model,\n' | |
| ' "api_base": "https://openrouter.ai/api/v1",\n' | |
| ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n' | |
| ' "custom_llm_provider": "openrouter",\n' | |
| ' }\n' | |
| ' # Flint requires reasoning (mandatory on OpenRouter)\n' | |
| ' if "flint" in or_model.lower():\n' | |
| ' params["extra_body"] = {"reasoning": {"effort": "low"}}\n' | |
| ' return params\n' | |
| ) | |
| # v1 (without custom_llm_provider) | |
| old_block_v1 = ( | |
| ' if normalized_model.startswith("openai/"):\n' | |
| ' or_model = normalized_model[len("openai/"):]\n' | |
| ' return {\n' | |
| ' "model": or_model,\n' | |
| ' "api_base": "https://openrouter.ai/api/v1",\n' | |
| ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n' | |
| ' }\n' | |
| ) | |
| new_block_v1 = ( | |
| ' if normalized_model.startswith("openai/"):\n' | |
| ' or_model = normalized_model[len("openai/"):]\n' | |
| ' params = {\n' | |
| ' "model": or_model,\n' | |
| ' "api_base": "https://openrouter.ai/api/v1",\n' | |
| ' "api_key": os.environ.get("OPENROUTER_API_KEY") or api_key or "",\n' | |
| ' }\n' | |
| ' # Flint requires reasoning (mandatory on OpenRouter)\n' | |
| ' if "flint" in or_model.lower():\n' | |
| ' params["extra_body"] = {"reasoning": {"effort": "low"}}\n' | |
| ' return params\n' | |
| ) | |
| if old_block_v2 in lp: | |
| lp = lp.replace(old_block_v2, new_block_v2) | |
| print("OK: patched llm_params.py openai/ block (v2 with custom_llm_provider) for flint reasoning") | |
| elif old_block_v1 in lp: | |
| lp = lp.replace(old_block_v1, new_block_v1) | |
| print("OK: patched llm_params.py openai/ block (v1) for flint reasoning") | |
| else: | |
| print("WARN: could not find openai/ routing block in llm_params.py") | |
| print(" - Searching for 'openai/' patterns...") | |
| for i, line in enumerate(lp.split('\n')): | |
| if 'startswith("openai/")' in line: | |
| print(f" Found at line {i+1}: {line.strip()}") | |
| lines = lp.split('\n') | |
| for j in range(max(0,i-2), min(len(lines), i+10)): | |
| print(f" {j+1}: {lines[j]}") | |
| print("---") | |
| print(" - Flint model will be registered but may fail with 400 'Inference request failed'") | |
| with open(LLM_PARAMS_FILE, "w") as f: | |
| f.write(lp) | |
| ast.parse(lp) | |
| print("OK: llm_params.py written & syntax OK") | |
| else: | |
| print("OK: llm_params.py already patched for flint (idempotent)") | |
| # ---------- 5. Frontend ChatInput.tsx ---------- | |
| if os.path.exists(FRONTEND_FILE): | |
| with open(FRONTEND_FILE, "r", encoding="utf-8") as f: | |
| fc = f.read() | |
| if "flint-v1.0" in fc: | |
| print("OK: recursive/flint-v1.0:free already present in ChatInput.tsx (idempotent)") | |
| else: | |
| # Anchor: the deepseek-v4-flash-latest entry at the end of DEFAULT_MODEL_OPTIONS. | |
| anchor = " {\n id: 'deepseek-v4-flash-latest',\n" | |
| new_entry = ( | |
| " {\n" | |
| " id: 'flint-v1.0-free',\n" | |
| " name: 'recursive/flint-v1.0:free',\n" | |
| " modelPath: 'openai/recursive/flint-v1.0:free',\n" | |
| " avatarUrl: 'https://huggingface.co/api/avatars/recursive',\n" | |
| " recommended: true,\n" | |
| " },\n" | |
| ) | |
| if anchor in fc: | |
| fc = fc.replace(anchor, new_entry + anchor, 1) | |
| with open(FRONTEND_FILE, "w", encoding="utf-8") as f: | |
| f.write(fc) | |
| print("OK: added recursive/flint-v1.0:free to ChatInput.tsx DEFAULT_MODEL_OPTIONS") | |
| else: | |
| print("WARN: deepseek anchor not found in ChatInput.tsx - frontend entry not added") | |
| else: | |
| print(f"WARN: {FRONTEND_FILE} not found (frontend patch skipped)") | |
| print("DONE: recursive/flint-v1.0:free added with Laguna S 2.1:free path convention + reasoning fix") | |