diff --git a/core/server/oai.cpp b/core/server/oai.cpp index 47e45a29..c0a3205e 100644 --- a/core/server/oai.cpp +++ b/core/server/oai.cpp @@ -2205,12 +2205,13 @@ std::string embeddings_body(const OaiRequest& r, const std::vector 0) m["max_model_len"] = max_model_len; json o; o["object"] = "list"; o["data"] = json::array({m}); diff --git a/core/server/oai.h b/core/server/oai.h index a5c52e7d..3aa5fa65 100644 --- a/core/server/oai.h +++ b/core/server/oai.h @@ -318,7 +318,13 @@ std::string text_completion_body(const OaiRequest& r, const std::vector>& vecs, const UsageOut& u); -std::string models_body(const std::string& model_id, int64_t created); +/* `max_model_len` is the context the server was started with (--max-model-len, else the + * container's trained length), the field vLLM's model card carries. Clients size their own + * context from it -- an agent that compresses its history at the window's edge, a UI that shows + * how much is left -- and without it they guess from the model's name, which is wrong whenever the + * deployment sets its own length. 0 leaves the field out rather than reporting a length the + * server does not know. */ +std::string models_body(const std::string& model_id, int64_t created, int64_t max_model_len = 0); /* The usage chunk of each endpoint's stream: an SSE event whose choices array is empty and whose * only payload is `usage`. Emitted when the request asked for stream_options.include_usage, ONCE diff --git a/core/server/server.cpp b/core/server/server.cpp index e125d120..1a56cf85 100644 --- a/core/server/server.cpp +++ b/core/server/server.cpp @@ -1154,7 +1154,7 @@ HttpResponse Server::handle_models(const HttpRequest& req) { return error_response(e, req.peer); } metrics_.observe_http(200); - return HttpResponse::json_body(200, models_body(opt_.model_id, started_at_)); + return HttpResponse::json_body(200, models_body(opt_.model_id, started_at_, opt_.max_ctx)); } HttpResponse Server::handle_metrics(const HttpRequest&) { diff --git a/docs/GUIDE.md b/docs/GUIDE.md index bca8acb4..727d84b4 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1132,7 +1132,7 @@ for a model that takes no images or video. | `POST /v1/chat/completions` | chat, with tools, reasoning and streaming | | `POST /v1/completions` | raw text completion | | `POST /v1/embeddings` | needs a model with an embedding head, else 501 | -| `GET /v1/models`, `/v1/models/:id` | what is loaded | +| `GET /v1/models`, `/v1/models/:id` | what is loaded, with `max_model_len` (the context it serves, as vLLM reports it) | | `GET /health`, `/ping`, `POST /ping` | liveness | | `GET /load` | current load | | `GET /version` | server version | diff --git a/tests/server_test.cpp b/tests/server_test.cpp index a78ba2f2..5846e69b 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -343,7 +343,7 @@ struct Fixture { FakeGrammar gram; std::unique_ptr srv; - explicit Fixture(bool with_tools = true) { + explicit Fixture(bool with_tools = true, int64_t max_ctx = 0) { Deps d; d.sched = &sched; d.tok = &tok; @@ -356,6 +356,7 @@ struct Fixture { o.model_id = "test-model"; o.default_max_tokens = 32; o.max_seqs = 4; + o.max_ctx = max_ctx; srv.reset(new Server(d, o)); } @@ -2521,6 +2522,18 @@ TEST(models_and_health_answer) { CHECK(json::parse(h.body)["status"].get() == "ok"); } +TEST(the_model_entry_carries_the_context_length_the_server_was_started_with) { + /* Both routes a client sizes its window from: the list, and the entry it fetches by id. */ + Fixture f(true, 262144); + for (const char* path : {"/v1/models", "/v1/models/test-model"}) { + HttpResponse r = f.get(path); + CHECK_EQ(r.status, 200); + CHECK_EQ(json::parse(r.body)["data"][0].value("max_model_len", (int64_t)0), (int64_t)262144); + } + Fixture unknown; + CHECK(!json::parse(unknown.get("/v1/models").body)["data"][0].contains("max_model_len")); +} + TEST(embeddings_without_an_embedder_is_501_not_an_empty_200) { Fixture f; HttpResponse r = f.post("/v1/embeddings", R"({"input":"hi"})"); diff --git a/tests/wire_test.cpp b/tests/wire_test.cpp index 50c8c085..b2cecf0d 100644 --- a/tests/wire_test.cpp +++ b/tests/wire_test.cpp @@ -414,6 +414,14 @@ TEST(the_model_list_is_a_list_of_one_with_the_fields_clients_read) { CHECK_EQ(o["data"][0].value("created", (int64_t)0), (int64_t)1700000000); /* `owned_by` is in the schema and generated clients deserialise it. */ CHECK(o["data"][0].contains("owned_by")); + /* No length known, none reported: a client falls back to its own default rather than + * trusting a made-up one. */ + CHECK(!o["data"][0].contains("max_model_len")); +} + +TEST(the_model_list_reports_the_context_the_server_runs_with) { + const json o = parsed(models_body("q38-flashnext", 1700000000, 262144)); + CHECK_EQ(o["data"][0].value("max_model_len", (int64_t)0), (int64_t)262144); } TEST(a_chat_body_carries_usage_and_one_choice_per_completion) {