Spaces:
Running
Running
File size: 6,977 Bytes
dc7c214 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | """Patch llm_params.py: add a DEDICATED BeatAPI provider branch.
BEATAPI vs OpenRouter -- why this branch is separate and must stay separate
=========================================================================
Every other model in this app except the HF Router / local-ollama ones is an
*OpenRouter* model. They are all tagged with an `openai/` prefix, and
_resolve_llm_params has a single catch-all that:
1. strips the `openai/` prefix,
2. points api_base at https://openrouter.ai/api/v1,
3. authenticates with OPENROUTER_API_KEY.
BeatAPI (https://api.beatapi.io) is a *different provider* that happens to
expose an OpenAI-compatible surface. It must therefore NOT reuse that branch:
provider OpenRouter BeatAPI
----------- ----------------------------- ------------------------------
api_base https://openrouter.ai/api/v1 https://api.beatapi.io/v1
key env OPENROUTER_API_KEY BEATAPI_API_KEY
model id vendor/model space-bunny-alpha (bare, no vendor prefix)
effort unsupported (omitted) low/medium/high/xhigh/max via extra_body
BeatAPI additionally:
- does not understand the `openai/` prefix at all, so sending
"openai/space-bunny-alpha" or "openai/stealth/space-bunny-alpha" 404s;
- uses `space-bunny-alpha` as a *flat* model id, so we must strip our own
`beatapi/` routing prefix before the request leaves the app;
- rate-limits the free tier (1 req/min before first top-up, 10/min after,
1000/day) and returns HTTP 429 with Retry-After.
Therefore BeatAPI models use the `beatapi/` prefix -- a namespace nothing else
uses -- so this branch can never be shadowed by the `openai/` catch-all, and
the OpenRouter branch can never accidentally strip a BeatAPI model id.
If BEATAPI_API_KEY is unset we raise a clear, actionable error instead of
silently sending an empty key (which BeatAPI answers with a bare 401
missing_api_key).
Runs AFTER patch_models.py and patch_openrouter_fix.py. Idempotent.
"""
import ast
FILE = "/app/agent/core/llm_params.py"
BEATAPI_BASE_URL = "https://api.beatapi.io/v1"
BEATAPI_MODEL_PREFIX = "beatapi/"
with open(FILE) as f:
c = f.read()
if "BeatApiKeyMissingError" in c:
print("OK: llm_params.py already contains BeatAPI routing (idempotent)")
raise SystemExit(0)
if "\nimport os\n" not in c:
print("FAIL: expected `import os` in llm_params.py")
raise SystemExit(1)
# --- Insert the BeatAPI block before _resolve_llm_params ---
anchor = "def _resolve_llm_params(\n"
if anchor not in c:
print("FAIL: could not find _resolve_llm_params definition")
raise SystemExit(1)
beatapi_block = f'''BEATAPI_BASE_URL = "{BEATAPI_BASE_URL}"
BEATAPI_MODEL_PREFIX = "{BEATAPI_MODEL_PREFIX}"
# BeatAPI accepts low/medium/high/xhigh/max (HF Router only low/medium/high).
_BEATAPI_EFFORTS = {{"low", "medium", "high", "xhigh", "max"}}
class BeatApiKeyMissingError(ValueError):
"""BEATAPI_API_KEY is not configured, so BeatAPI cannot be called.
Raised before any network call so the UI shows an actionable message
("set BEATAPI_API_KEY") instead of an opaque 401 missing_api_key.
"""
def _beatapi_base_model(model_name: str) -> str:
"""Return the wire model id for a BeatAPI request.
``beatapi/space-bunny-alpha`` -> ``space-bunny-alpha``
BeatAPI model ids are flat; they must never carry our routing prefix.
"""
return model_name[len(BEATAPI_MODEL_PREFIX):].strip()
def _resolve_beatapi_params(
model_name: str,
reasoning_effort: str | None = None,
strict: bool = False,
) -> dict:
"""Build LiteLLM kwargs for a BeatAPI (``beatapi/``-prefixed) model.
Mirrors the OpenRouter branch but targets api.beatapi.io, uses
BEATAPI_API_KEY, forces LiteLLM's OpenAI-compatible client, and forwards
BeatAPI's own reasoning_effort levels (it accepts xhigh and max, which the
HF Router does not).
"""
base_model = _beatapi_base_model(model_name)
if not base_model:
raise ValueError(f"Missing BeatAPI model id: {{model_name!r}}")
api_key = os.environ.get("BEATAPI_API_KEY", "").strip()
if not api_key:
raise BeatApiKeyMissingError(
"BEATAPI_API_KEY is not set. BeatAPI models (beatapi/...) require a "
"BeatAPI key from https://beatapi.io (Dashboard -> API Keys); "
"set it as a Space secret named BEATAPI_API_KEY."
)
params: dict = {{
"model": f"openai/{{base_model}}",
"api_base": BEATAPI_BASE_URL,
"api_key": api_key,
# Force LiteLLM's OpenAI-compatible client. Without this, LiteLLM would
# try to resolve a provider from the model string and raise
# "LLM Provider NOT provided" (same class of bug as the OpenRouter fix).
"custom_llm_provider": "openai",
}}
if reasoning_effort:
level = reasoning_effort
if level not in _BEATAPI_EFFORTS:
if strict:
raise UnsupportedEffortError(
f"BeatAPI doesn't accept effort={{level!r}}"
)
else:
# BeatAPI reads reasoning_effort from the request body directly.
params["extra_body"] = {{"reasoning_effort": level}}
return params
'''
c = c.replace(anchor, beatapi_block + anchor, 1)
# --- Add the dispatch inside _resolve_llm_params, BEFORE the openai/ catch-all ---
# The dispatch must sit after the local-model checks and before the OpenRouter
# branch so `beatapi/` can never be swallowed by `openai/`.
dispatch_anchor = " hf_model = normalized_model\n"
if dispatch_anchor not in c:
print("FAIL: could not find hf_model assignment in _resolve_llm_params")
raise SystemExit(1)
dispatch = """ # --- BeatAPI provider (beatapi/ prefix) -------------------------------
# NOTE: distinct from the OpenRouter branch below on api_base, API key,
# model-id shape and reasoning_effort levels. Keep this check above the
# openai/ catch-all so the two providers can never collide.
if normalized_model.startswith(BEATAPI_MODEL_PREFIX):
return _resolve_beatapi_params(
normalized_model, reasoning_effort, strict
)
"""
c = c.replace(dispatch_anchor, dispatch + dispatch_anchor, 1)
with open(FILE, "w") as f:
f.write(c)
try:
ast.parse(c)
print("OK: llm_params.py patched & syntax valid")
except SyntaxError as e:
print(f"FAIL: llm_params.py syntax error: {e}")
raise
# Sanity: the BeatAPI branch must appear before the openai/ catch-all.
beat_i = c.find("startswith(BEATAPI_MODEL_PREFIX)")
openai_i = c.find('startswith("openai/")')
if beat_i != -1 and openai_i != -1:
if beat_i < openai_i:
print("OK: BeatAPI dispatch precedes the openai/ catch-all")
else:
print("FAIL: BeatAPI dispatch is AFTER the openai/ catch-all")
raise SystemExit(1)
print("DONE: BeatAPI routing branch added to llm_params.py")
|