Refuse a request for a model that isn't loaded; show the slug in /models
The MLX proxy rewrote every request's "model" to the preloaded one, so asking for a model the server wasn't running returned another model's output wearing the requested name -- silently. The rewrite itself has to stay (mlx_lm would otherwise try to load a model matching the name), so validate first: a name that is neither the served name nor its repo id gets 404 model_not_found, naming what is loaded and how to switch. An unstated name still means "the loaded one", and the key is still checked before the model. opencode's /models hid the same trap twice over. Label models `<title> · <slug> (<where>)` so the list is searchable by the id used everywhere else -- titles alone had dropped the slug -- and mark the loaded one `(Local, loaded)`, refreshed by both switch paths (CLI and dashboard) through a new opencode.sync(), which only ever refreshes a block that is already there. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8c6615dbb4
commit
2c0818ca2b
5 changed files with 78 additions and 11 deletions
|
|
@ -3,8 +3,9 @@
|
|||
mlx_lm.server has no API-key support and loads whatever model a request names, so it
|
||||
listens on localhost only and this module runs a small proxy on the public port that:
|
||||
- checks the API key (like vLLM's --api-key),
|
||||
- rewrites the request's "model" to the loaded one and answers /v1/models with the
|
||||
served name, so clients see the same API as on vLLM,
|
||||
- rejects a request for a model that isn't the loaded one (404 model_not_found),
|
||||
then rewrites the request's "model" to the loaded one, and answers /v1/models with
|
||||
the served name, so clients see the same API as on vLLM,
|
||||
- counts in-flight requests and generated tokens for the dashboard (/llmhost/metrics).
|
||||
"""
|
||||
import hmac
|
||||
|
|
@ -115,7 +116,10 @@ def _uncached(usage):
|
|||
return max(n - int(cached), 0)
|
||||
|
||||
|
||||
def make_handler(inner_port, served_name, context, key, stats):
|
||||
def make_handler(inner_port, served_name, context, key, stats, aliases=()):
|
||||
# Names a client may call the loaded model: its llm-host name and its repo id.
|
||||
accepted = {served_name.lower(), *(a.lower() for a in aliases if a)}
|
||||
|
||||
class Proxy(BaseHTTPRequestHandler):
|
||||
protocol_version = "HTTP/1.1"
|
||||
|
||||
|
|
@ -159,6 +163,22 @@ def make_handler(inner_port, served_name, context, key, stats):
|
|||
{"id": served_name, "object": "model", "owned_by": "llm-host", "max_model_len": context}]})
|
||||
self._forward("GET", None)
|
||||
|
||||
def _model_ok(self, body):
|
||||
"""One model is loaded at a time, and mlx_lm answers with whatever it has
|
||||
regardless of the name asked for -- so an unrecognised name would come back
|
||||
as another model's output wearing it. Refuse it instead."""
|
||||
want = body.get("model")
|
||||
if not isinstance(want, str) or not want.strip():
|
||||
return True # no preference stated: the loaded one it is
|
||||
if want.strip().lower() in accepted:
|
||||
return True
|
||||
self._json(404, {"error": {
|
||||
"message": f"model '{want}' is not loaded; this server is serving "
|
||||
f"'{served_name}'. Load it with `llmhost switch {want}` "
|
||||
f"(one model is loaded at a time), or ask for '{served_name}'.",
|
||||
"type": "invalid_request_error", "code": "model_not_found", "param": "model"}})
|
||||
return False
|
||||
|
||||
def do_POST(self):
|
||||
if not self._authed():
|
||||
return
|
||||
|
|
@ -169,6 +189,10 @@ def make_handler(inner_port, served_name, context, key, stats):
|
|||
try:
|
||||
body = json.loads(raw or b"{}")
|
||||
if isinstance(body, dict):
|
||||
if self.path.startswith("/v1/") and not self._model_ok(body):
|
||||
return
|
||||
# Force the preloaded model: mlx_lm would otherwise try to load a
|
||||
# model matching the name it's given.
|
||||
body["model"] = "default_model"
|
||||
# Ask for the final usage chunk so prompt tokens can be counted, and
|
||||
# hide it again from clients that didn't ask for it.
|
||||
|
|
@ -314,7 +338,7 @@ def run_server(cfg, entry, choice, args):
|
|||
try:
|
||||
srv = ThreadingHTTPServer(("0.0.0.0", port),
|
||||
make_handler(inner, entry["name"], choice.get("est_context", entry["context"]),
|
||||
paths.api_key(), stats))
|
||||
paths.api_key(), stats, aliases=(choice.get("repo"),)))
|
||||
except OSError as e:
|
||||
print(f"llm-host: can't listen on port {port} ({e.strerror}). Another program is using it; "
|
||||
f"pick a free port with: llmhost setup --api-port <port>", flush=True)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue