Archived
radiance 1.0.4
- oh-my-pi works unmodified on every Qwen model. Its requests carry a top-level
preserve_thinking, which was refused as an unknown field; it now reaches the chat template the
way enable_thinking does. Its task tool's `model` parameter is the `not` of every JSON type,
which the 1.0.1 fix did not cover: any `not` that excludes every value is now an argument no
call writes, under every chat template and Flash-Next's declared format.
- Command-line defaults and bounds for what a request leaves out. A flag left unset changes
nothing; /server_info reports the values under request_defaults.
- Sampling: --typical-p, --presence-penalty, --frequency-penalty, --repetition-penalty,
--repeat-last-n, --dry-multiplier, --dry-base, --dry-allowed-length, --dry-penalty-last-n,
--dry-sequence-breakers, --xtc-probability, --xtc-threshold.
- Requests: --default-max-tokens N|auto, --max-tokens-cap, --max-n, --max-queued-requests,
--max-stop-strings, --max-stop-bytes.
- Chat template: --chat-template-kwargs JSON|@FILE sets template variables a request's own
chat_template_kwargs override; --reasoning-format auto|none.
- HTTP: --http-threads, --read-timeout, --write-timeout, --keep-alive-timeout, --max-body-mib,
--retry-after, --no-cors.
- Media: --image-min-pixels, --image-max-pixels, --video-min-pixels, --video-max-pixels,
--video-fps, --video-min-frames, --video-max-frames, --video-max-frame-tokens,
--max-source-pixels.
- Under --reasoning-effort none or minimal, a request that sets enable_thinking true now thinks
instead of having the deployment default override it.
- --temp, --top-p, --min-p and --top-k refuse a value that is not a number instead of reading
it as 0.
This commit is contained in:
1 parent
d008fdb32b
commit
09fb41735b
23 files changed
+2478
-104
No files matched your search
+43
-3
@@ -155,6 +155,7 @@ HttpResponse Server::handle_tokenize(const HttpRequest& req) {
|
||||
|
||||
ChatRenderOptions opt;
|
||||
opt.add_generation_prompt = get_bool_or(b, "add_generation_prompt", true);
|
||||
apply_template_defaults(b, lim_, &opt);
|
||||
auto kw = b.find("chat_template_kwargs");
|
||||
if (kw != b.end() && kw->is_object())
|
||||
for (auto it = kw->begin(); it != kw->end(); ++it)
|
||||
@@ -273,8 +274,13 @@ HttpResponse Server::handle_server_info(const HttpRequest&) {
|
||||
srv["max_model_len"] = opt_.max_ctx;
|
||||
srv["max_num_seqs"] = opt_.max_seqs;
|
||||
srv["queue_cap"] = queue_depth_;
|
||||
srv["default_max_tokens"] = opt_.default_max_tokens;
|
||||
/* null is `--default-max-tokens auto`: what the context leaves after the prompt. */
|
||||
srv["default_max_tokens"] = opt_.default_max_tokens > 0 ? json(opt_.default_max_tokens)
|
||||
: json(nullptr);
|
||||
srv["max_tokens_cap"] = opt_.max_tokens_cap > 0 ? json(opt_.max_tokens_cap) : json(nullptr);
|
||||
srv["max_n"] = opt_.max_n;
|
||||
srv["max_stop_strings"] = opt_.max_stops;
|
||||
srv["max_stop_bytes"] = opt_.max_stop_bytes;
|
||||
srv["uptime_s"] = unix_now() - started_at_;
|
||||
srv["in_flight"] = in_flight_.load(std::memory_order_relaxed);
|
||||
|
||||
@@ -289,9 +295,43 @@ HttpResponse Server::handle_server_info(const HttpRequest&) {
|
||||
caps["audio"] = deps_.mm && deps_.mm->accepts(MediaKind::Audio);
|
||||
caps["logprobs"] = opt_.supports_logprobs;
|
||||
|
||||
/* WHAT A REQUEST THAT LEAVES A FIELD OUT GETS, under the request's own spelling of each
|
||||
* field, so a client can read off exactly what leaving one out means on this deployment. */
|
||||
const SamplingParams& sp = opt_.default_sampling;
|
||||
json defs;
|
||||
defs["temperature"] = sp.temp;
|
||||
defs["top_k"] = sp.top_k;
|
||||
defs["top_p"] = sp.top_p;
|
||||
defs["min_p"] = sp.min_p;
|
||||
defs["typical_p"] = sp.typical_p;
|
||||
defs["presence_penalty"] = sp.pres_penalty;
|
||||
defs["frequency_penalty"] = sp.freq_penalty;
|
||||
defs["repetition_penalty"] = sp.rep_penalty;
|
||||
defs["repeat_last_n"] = sp.penalty_last_n;
|
||||
defs["dry_multiplier"] = sp.dry_multiplier;
|
||||
defs["dry_base"] = sp.dry_base;
|
||||
defs["dry_allowed_length"] = sp.dry_allowed_length;
|
||||
defs["dry_penalty_last_n"] = sp.dry_penalty_last_n;
|
||||
defs["dry_sequence_breakers"] = opt_.default_dry_breakers_set
|
||||
? json(sp.dry_seq_breakers)
|
||||
: json(std::vector<std::string>{ "\n", ":", "\"", "*" });
|
||||
defs["xtc_probability"] = sp.xtc_probability;
|
||||
defs["xtc_threshold"] = sp.xtc_threshold;
|
||||
defs["max_tokens"] = srv["default_max_tokens"];
|
||||
defs["reasoning_effort"] = opt_.default_reasoning_effort.empty()
|
||||
? json(nullptr) : json(opt_.default_reasoning_effort);
|
||||
json kw = json::object();
|
||||
for (const auto& [k, v] : opt_.default_template_kwargs) {
|
||||
json parsed = json::parse(v, nullptr, false);
|
||||
kw[k] = parsed.is_discarded() ? json(v) : std::move(parsed);
|
||||
}
|
||||
defs["chat_template_kwargs"] = std::move(kw);
|
||||
defs["reasoning_format"] = opt_.reasoning_format;
|
||||
|
||||
json o;
|
||||
o["server"] = std::move(srv);
|
||||
o["capabilities"] = std::move(caps);
|
||||
o["server"] = std::move(srv);
|
||||
o["request_defaults"] = std::move(defs);
|
||||
o["capabilities"] = std::move(caps);
|
||||
o["endpoints"] = router_.routes();
|
||||
|
||||
/* Spliced rather than re-parsed field by field: the engine owns what it says about itself and
|
||||
|
||||
+160
-38
@@ -9,6 +9,7 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
|
||||
namespace rad {
|
||||
@@ -201,7 +202,7 @@ const char* kAcceptedCommon[] = {
|
||||
const char* kAcceptedChat[] = {
|
||||
"messages", "tools", "tool_choice", "response_format", "top_logprobs",
|
||||
"parallel_tool_calls", "add_generation_prompt", "max_completion_tokens",
|
||||
"chat_template_kwargs", "reasoning_effort", "enable_thinking",
|
||||
"chat_template_kwargs", "reasoning_effort", "enable_thinking", "preserve_thinking",
|
||||
};
|
||||
|
||||
const char* kAcceptedCompletion[] = { "prompt", "echo" };
|
||||
@@ -311,15 +312,6 @@ bool get_str(const json& b, const char* k, std::string* out, ApiError* err) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool range(double v, double lo, double hi, const char* k, ApiError* err) {
|
||||
if (v < lo || v > hi) {
|
||||
char b[160];
|
||||
snprintf(b, sizeof b, "must be in [%g, %g], got %g", lo, hi, v);
|
||||
*err = err_bad(k, b);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* The body of every generation endpoint: one JSON object, parsed into a bounded tree. */
|
||||
int parse_request_object(const std::string& body, const OaiLimits& lim, json* b, ApiError* err) {
|
||||
@@ -349,10 +341,68 @@ int parse_request_object(const std::string& body, const OaiLimits& lim, json* b,
|
||||
|
||||
/* ================================================================== sampling params */
|
||||
|
||||
namespace {
|
||||
|
||||
/* One row per bounded sampler field. `lo_open` excludes the lower bound; `hi` of infinity is no
|
||||
* upper bound. `why` is the refusal when the range alone does not say it well, and null for a
|
||||
* closed interval, whose refusal names the interval and the value. */
|
||||
struct SamplerRange {
|
||||
const char* field;
|
||||
double lo, hi;
|
||||
bool lo_open;
|
||||
const char* why;
|
||||
};
|
||||
|
||||
constexpr double kInf = std::numeric_limits<double>::infinity();
|
||||
|
||||
const SamplerRange kSamplerRanges[] = {
|
||||
{ "temperature", 0.0, 2.0, false, nullptr },
|
||||
{ "top_p", 0.0, 1.0, true, "must be in (0, 1]" },
|
||||
{ "top_k", 0.0, kInf, false, "must be >= 0 (0 disables it)" },
|
||||
{ "min_p", 0.0, 1.0, false, nullptr },
|
||||
{ "typical_p", 0.0, 1.0, true, "must be in (0, 1]" },
|
||||
{ "presence_penalty", -2.0, 2.0, false, nullptr },
|
||||
{ "frequency_penalty", -2.0, 2.0, false, nullptr },
|
||||
{ "repetition_penalty", 0.0, kInf, true, "must be > 0" },
|
||||
{ "repeat_last_n", -1.0, kInf, false, "must be >= -1" },
|
||||
{ "xtc_probability", 0.0, 1.0, false, nullptr },
|
||||
{ "xtc_threshold", 0.0, 1.0, false, nullptr },
|
||||
{ "dry_base", 1.0, kInf, false, "must be >= 1" },
|
||||
{ "dry_penalty_last_n", -1.0, kInf, false, "must be >= -1" },
|
||||
};
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* Written as "inside the range" and negated, so a NaN -- which a flag can parse and JSON cannot
|
||||
* carry -- fails every row rather than passing them all. */
|
||||
bool sampler_value_ok(const char* field, double v, std::string* why) {
|
||||
for (const SamplerRange& r : kSamplerRanges) {
|
||||
if (std::strcmp(r.field, field)) continue;
|
||||
const bool in = (r.lo_open ? v > r.lo : v >= r.lo) && v <= r.hi;
|
||||
if (in) return true;
|
||||
if (why) {
|
||||
if (r.why) *why = r.why;
|
||||
else {
|
||||
char b[160];
|
||||
snprintf(b, sizeof b, "must be in [%g, %g], got %g", r.lo, r.hi, v);
|
||||
*why = b;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, ApiError* err) {
|
||||
double d;
|
||||
int64_t i64;
|
||||
bool bl;
|
||||
auto ok = [err](const char* k, double v) {
|
||||
std::string why;
|
||||
if (sampler_value_ok(k, v, &why)) return true;
|
||||
*err = err_bad(k, why);
|
||||
return false;
|
||||
};
|
||||
|
||||
/* THE DEPLOYMENT'S DEFAULTS ARE THE SEED, and every field below reads its current value
|
||||
* before looking for one in the body -- so a field the request names overrides, and a field
|
||||
@@ -363,57 +413,57 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
|
||||
d = r->sp.temp;
|
||||
if (!get_num(b, "temperature", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, 0.0, 2.0, "temperature", err)) return RAD_E_INVAL;
|
||||
if (!ok("temperature", d)) return RAD_E_INVAL;
|
||||
r->sp.temp = (float)d;
|
||||
|
||||
d = r->sp.top_p;
|
||||
if (!get_num(b, "top_p", &d, err)) return RAD_E_INVAL;
|
||||
if (d <= 0.0 || d > 1.0) { *err = err_bad("top_p", "must be in (0, 1]"); return RAD_E_INVAL; }
|
||||
if (!ok("top_p", d)) return RAD_E_INVAL;
|
||||
r->sp.top_p = (float)d;
|
||||
|
||||
i64 = r->sp.top_k;
|
||||
if (!get_int(b, "top_k", &i64, err)) return RAD_E_INVAL;
|
||||
if (i64 < 0) { *err = err_bad("top_k", "must be >= 0 (0 disables it)"); return RAD_E_INVAL; }
|
||||
if (!ok("top_k", (double)i64)) return RAD_E_INVAL;
|
||||
r->sp.top_k = (int)i64;
|
||||
|
||||
d = r->sp.min_p;
|
||||
if (!get_num(b, "min_p", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, 0.0, 1.0, "min_p", err)) return RAD_E_INVAL;
|
||||
if (!ok("min_p", d)) return RAD_E_INVAL;
|
||||
r->sp.min_p = (float)d;
|
||||
|
||||
d = r->sp.typical_p;
|
||||
if (!get_num(b, "typical_p", &d, err)) return RAD_E_INVAL;
|
||||
if (d <= 0.0 || d > 1.0) { *err = err_bad("typical_p", "must be in (0, 1]"); return RAD_E_INVAL; }
|
||||
if (!ok("typical_p", d)) return RAD_E_INVAL;
|
||||
r->sp.typical_p = (float)d;
|
||||
|
||||
d = r->sp.pres_penalty;
|
||||
if (!get_num(b, "presence_penalty", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, -2.0, 2.0, "presence_penalty", err)) return RAD_E_INVAL;
|
||||
if (!ok("presence_penalty", d)) return RAD_E_INVAL;
|
||||
r->sp.pres_penalty = (float)d;
|
||||
|
||||
d = r->sp.freq_penalty;
|
||||
if (!get_num(b, "frequency_penalty", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, -2.0, 2.0, "frequency_penalty", err)) return RAD_E_INVAL;
|
||||
if (!ok("frequency_penalty", d)) return RAD_E_INVAL;
|
||||
r->sp.freq_penalty = (float)d;
|
||||
|
||||
d = r->sp.rep_penalty;
|
||||
if (!get_num(b, "repetition_penalty", &d, err)) return RAD_E_INVAL;
|
||||
if (d <= 0.0) { *err = err_bad("repetition_penalty", "must be > 0"); return RAD_E_INVAL; }
|
||||
if (!ok("repetition_penalty", d)) return RAD_E_INVAL;
|
||||
r->sp.rep_penalty = (float)d;
|
||||
|
||||
i64 = r->sp.penalty_last_n;
|
||||
if (!get_int(b, "repeat_last_n", &i64, err)) return RAD_E_INVAL;
|
||||
if (i64 < -1) { *err = err_bad("repeat_last_n", "must be >= -1"); return RAD_E_INVAL; }
|
||||
if (!ok("repeat_last_n", (double)i64)) return RAD_E_INVAL;
|
||||
r->sp.penalty_last_n = (int)i64;
|
||||
|
||||
d = r->sp.xtc_probability;
|
||||
if (!get_num(b, "xtc_probability", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, 0.0, 1.0, "xtc_probability", err)) return RAD_E_INVAL;
|
||||
if (!ok("xtc_probability", d)) return RAD_E_INVAL;
|
||||
r->sp.xtc_probability = (float)d;
|
||||
|
||||
d = r->sp.xtc_threshold;
|
||||
if (!get_num(b, "xtc_threshold", &d, err)) return RAD_E_INVAL;
|
||||
if (!range(d, 0.0, 1.0, "xtc_threshold", err)) return RAD_E_INVAL;
|
||||
if (!ok("xtc_threshold", d)) return RAD_E_INVAL;
|
||||
r->sp.xtc_threshold = (float)d;
|
||||
|
||||
d = r->sp.dry_multiplier;
|
||||
@@ -421,14 +471,14 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
r->sp.dry_multiplier = (float)d;
|
||||
d = r->sp.dry_base;
|
||||
if (!get_num(b, "dry_base", &d, err)) return RAD_E_INVAL;
|
||||
if (d < 1.0) { *err = err_bad("dry_base", "must be >= 1"); return RAD_E_INVAL; }
|
||||
if (!ok("dry_base", d)) return RAD_E_INVAL;
|
||||
r->sp.dry_base = (float)d;
|
||||
i64 = r->sp.dry_allowed_length;
|
||||
if (!get_int(b, "dry_allowed_length", &i64, err)) return RAD_E_INVAL;
|
||||
r->sp.dry_allowed_length = (int)i64;
|
||||
i64 = r->sp.dry_penalty_last_n;
|
||||
if (!get_int(b, "dry_penalty_last_n", &i64, err)) return RAD_E_INVAL;
|
||||
if (i64 < -1) { *err = err_bad("dry_penalty_last_n", "must be >= -1"); return RAD_E_INVAL; }
|
||||
if (!ok("dry_penalty_last_n", (double)i64)) return RAD_E_INVAL;
|
||||
r->sp.dry_penalty_last_n = (int)i64;
|
||||
|
||||
/* ABSENT MEANS llama.cpp's FOUR, not none. A client that turns DRY on and names no breakers
|
||||
@@ -437,7 +487,8 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
* with DRY off: the sampler resolves breakers only for a request that runs DRY. */
|
||||
auto dsb = b.find("dry_sequence_breakers");
|
||||
if (dsb == b.end() || dsb->is_null()) {
|
||||
if (r->sp.dry_seq_breakers.empty()) r->sp.dry_seq_breakers = { "\n", ":", "\"", "*" };
|
||||
if (r->sp.dry_seq_breakers.empty() && !lim.default_dry_breakers_set)
|
||||
r->sp.dry_seq_breakers = { "\n", ":", "\"", "*" };
|
||||
} else {
|
||||
r->sp.dry_seq_breakers.clear();
|
||||
if (!dsb->is_array()) {
|
||||
@@ -500,14 +551,15 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
* text a choice produces -- by the decoder and again by the server's own scan -- so what one
|
||||
* request costs per token is its number of stop strings times the longest of them. OpenAI
|
||||
* allows four. The bounds here are far past anything a client sends and exist so that no
|
||||
* request can buy that scan without limit. */
|
||||
constexpr size_t kMaxStops = 64, kMaxStopBytes = 4096;
|
||||
* request can buy that scan without limit. The deployment may move them (--max-stop-strings,
|
||||
* --max-stop-bytes). */
|
||||
const size_t max_stops = (size_t)lim.max_stops, max_stop_bytes = (size_t)lim.max_stop_bytes;
|
||||
auto st = b.find("stop");
|
||||
if (st != b.end() && !st->is_null()) {
|
||||
if (st->is_string()) r->stop.push_back(st->get<std::string>());
|
||||
else if (st->is_array()) {
|
||||
if (st->size() > kMaxStops) {
|
||||
*err = err_bad("stop", "at most " + std::to_string(kMaxStops) +
|
||||
if (st->size() > max_stops) {
|
||||
*err = err_bad("stop", "at most " + std::to_string(max_stops) +
|
||||
" stop strings are accepted, got " +
|
||||
std::to_string(st->size()));
|
||||
return RAD_E_INVAL;
|
||||
@@ -524,9 +576,9 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
for (const auto& s : r->stop) {
|
||||
if (s.size() > kMaxStopBytes) {
|
||||
if (s.size() > max_stop_bytes) {
|
||||
*err = err_bad("stop", "a stop string may be at most " +
|
||||
std::to_string(kMaxStopBytes) + " bytes, got one of " +
|
||||
std::to_string(max_stop_bytes) + " bytes, got one of " +
|
||||
std::to_string(s.size()));
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
@@ -563,15 +615,54 @@ static int parse_sampling(const json& b, const OaiLimits& lim, OaiRequest* r, Ap
|
||||
* caller who sends the kwarg and nothing else has it silently overwritten with the default true,
|
||||
* and sees the model think anyway -- a 200 that did not honour the request, which is the one
|
||||
* outcome this file's header forbids. Lifting the kwarg into the field is what makes it real.
|
||||
*
|
||||
* THE DEPLOYMENT'S DEFAULTS GO IN FIRST AND THE REQUEST OVERWRITES THEM, key by key -- except that
|
||||
* thinking is one setting with three spellings, and a default in one spelling must not survive a
|
||||
* request that used another. A request that says anything about thinking (either enable_thinking,
|
||||
* or a reasoning effort) drops the deployment's enable_thinking; one that names an effort drops
|
||||
* the deployment's effort too. Otherwise `--chat-template-kwargs '{"enable_thinking":false}'`
|
||||
* would turn off thinking for a caller who sent `reasoning_effort: "high"`, and
|
||||
* `--reasoning-effort none` would do the same to one who sent `enable_thinking: true`.
|
||||
*/
|
||||
RequestThinking request_thinking(const json& b) {
|
||||
auto kw = b.find("chat_template_kwargs");
|
||||
const bool obj = kw != b.end() && kw->is_object();
|
||||
auto top = b.find("enable_thinking");
|
||||
/* An empty reasoning_effort string reads as unset, so it names no effort either. */
|
||||
auto re = b.find("reasoning_effort");
|
||||
RequestThinking t;
|
||||
t.effort = (obj && kw->contains("reasoning_effort")) ||
|
||||
(re != b.end() && re->is_string() && !re->get<std::string>().empty());
|
||||
t.thinking = t.effort || (top != b.end() && !top->is_null()) ||
|
||||
(obj && kw->contains("enable_thinking"));
|
||||
return t;
|
||||
}
|
||||
|
||||
void apply_template_defaults(const json& b, const OaiLimits& lim, ChatRenderOptions* opt) {
|
||||
const RequestThinking says = request_thinking(b);
|
||||
for (const auto& [k, v] : lim.default_template_kwargs) {
|
||||
if (k == "enable_thinking") {
|
||||
if (says.thinking) continue;
|
||||
opt->enable_thinking = v == "true";
|
||||
}
|
||||
if (k == "reasoning_effort" && says.effort) continue;
|
||||
opt->template_kwargs[k] = v;
|
||||
}
|
||||
opt->reasoning_format = lim.reasoning_format;
|
||||
}
|
||||
|
||||
static int parse_thinking(const json& b, const OaiLimits& lim, ChatRenderOptions* opt,
|
||||
ApiError* err) {
|
||||
auto kw = b.find("chat_template_kwargs");
|
||||
if (kw != b.end() && !kw->is_null()) {
|
||||
if (!kw->is_object()) {
|
||||
*err = err_bad("chat_template_kwargs", "expected an object");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
const bool has_kw = kw != b.end() && !kw->is_null();
|
||||
if (has_kw && !kw->is_object()) {
|
||||
*err = err_bad("chat_template_kwargs", "expected an object");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
const RequestThinking says = request_thinking(b);
|
||||
apply_template_defaults(b, lim, opt);
|
||||
|
||||
if (has_kw) {
|
||||
for (auto it = kw->begin(); it != kw->end(); ++it) {
|
||||
/* dump(), not get<string>(): the template context wants JSON, so a string value has
|
||||
* to arrive still quoted. See ChatRenderOptions. */
|
||||
@@ -590,6 +681,21 @@ static int parse_thinking(const json& b, const OaiLimits& lim, ChatRenderOptions
|
||||
if (!get_bool(b, "enable_thinking", &think, err)) return RAD_E_INVAL;
|
||||
opt->enable_thinking = think;
|
||||
|
||||
/* `preserve_thinking` AT THE TOP LEVEL is the template variable of that name, the same
|
||||
* shorthand `enable_thinking` is: llama.cpp maps the field into the template, and clients
|
||||
* written against it (oh-my-pi, for every Qwen model) send it there as well as in
|
||||
* chat_template_kwargs. The Qwen3.6 and later templates read it to keep earlier assistant
|
||||
* turns' reasoning in the prompt; a template that does not read it ignores it, as it ignores
|
||||
* any variable it does not name. Like enable_thinking, the top-level spelling wins. */
|
||||
auto pt = b.find("preserve_thinking");
|
||||
if (pt != b.end() && !pt->is_null()) {
|
||||
if (!pt->is_boolean()) {
|
||||
*err = err_bad("preserve_thinking", "expected a boolean");
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
opt->template_kwargs["preserve_thinking"] = pt->dump();
|
||||
}
|
||||
|
||||
/* REASONING EFFORT IS THE TEMPLATE'S VOCABULARY, NOT OURS, so there is no allow-list here.
|
||||
* One would be wrong in both directions: OpenAI's ladder is none/minimal/low/medium/high,
|
||||
* while a model's template may accept a different set -- Qwen3.8's takes xhigh/medium/low and
|
||||
@@ -607,8 +713,14 @@ static int parse_thinking(const json& b, const OaiLimits& lim, ChatRenderOptions
|
||||
std::string effort;
|
||||
if (!get_str(b, "reasoning_effort", &effort, err)) return RAD_E_INVAL;
|
||||
/* The deployment's default, where the request did not say. Last, so every explicit spelling
|
||||
* above it wins; emplace, so a chat_template_kwargs entry that already set the key stands. */
|
||||
if (effort.empty()) effort = lim.default_reasoning_effort;
|
||||
* above it wins; emplace, so a chat_template_kwargs entry that already set the key stands.
|
||||
* "none" and "minimal" are an enable_thinking default, so any word about thinking drops them;
|
||||
* a level is an effort default, dropped by an effort. */
|
||||
if (effort.empty()) {
|
||||
const std::string& def = lim.default_reasoning_effort;
|
||||
const bool off = def == "none" || def == "minimal";
|
||||
if (off ? !says.thinking : !says.effort) effort = def;
|
||||
}
|
||||
if (!effort.empty()) {
|
||||
if (effort == "none" || effort == "minimal") opt->enable_thinking = false;
|
||||
else opt->template_kwargs.emplace("reasoning_effort", json(effort).dump());
|
||||
@@ -1005,8 +1117,16 @@ static int tokenize_with_media(const std::string& rendered, const OaiDeps& d,
|
||||
* anyway. What the caller gets back still says so: the completion ends with finish_reason
|
||||
* "length" and `usage` reports the tokens it actually produced.
|
||||
*/
|
||||
/* THE DEPLOYMENT'S CAP IS A THIRD BOUND OF THE SAME KIND, clamped for the same reason: a request
|
||||
* asking for more than the operator allows is still a request worth serving up to the cap. And a
|
||||
* max_tokens of 0 is the `--default-max-tokens auto` default, which is "what the context leaves" --
|
||||
* resolved here, against this prompt, so that the scheduler always sees a finite bound. */
|
||||
static int clamp_max_tokens(size_t n_prompt, const OaiLimits& lim, const char* field,
|
||||
OaiRequest* out, ApiError* err) {
|
||||
if (lim.max_tokens_cap > 0 &&
|
||||
(out->max_tokens <= 0 || (int64_t)out->max_tokens > lim.max_tokens_cap))
|
||||
out->max_tokens = (int32_t)lim.max_tokens_cap;
|
||||
|
||||
if (lim.max_ctx <= 0) return RAD_OK; /* no bound declared, nothing to clamp against */
|
||||
|
||||
const int64_t room = lim.max_ctx - (int64_t)n_prompt;
|
||||
@@ -1021,7 +1141,9 @@ static int clamp_max_tokens(size_t n_prompt, const OaiLimits& lim, const char* f
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
|
||||
if ((int64_t)out->max_tokens > room) {
|
||||
if (out->max_tokens <= 0) {
|
||||
out->max_tokens = (int32_t)std::min<int64_t>(room, INT32_MAX);
|
||||
} else if ((int64_t)out->max_tokens > room) {
|
||||
/* Debug rather than Warn: on a long-context deployment whose clients send a constant
|
||||
* max_tokens this fires on every request that is working correctly, and a line printed
|
||||
* per request is how a server's log stops being read at all. */
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "sink.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -107,8 +108,15 @@ struct OaiRequest {
|
||||
struct OaiLimits {
|
||||
std::string model_id = "radiance";
|
||||
int64_t max_ctx = 0; /* 0 = unknown, no length check */
|
||||
/* What a request that names no max_tokens may generate. 0 is "whatever the context leaves",
|
||||
* resolved against the prompt in clamp_max_tokens. */
|
||||
int64_t default_max_tokens = 512;
|
||||
/* The most any request may generate; a larger max_tokens is clamped, not refused. 0 = none. */
|
||||
int64_t max_tokens_cap = 0;
|
||||
int max_n = 8;
|
||||
/* How many stop strings a request may send, and how long each may be. */
|
||||
int64_t max_stops = 64;
|
||||
int64_t max_stop_bytes = 4096;
|
||||
bool allow_image = false;
|
||||
bool allow_video = false;
|
||||
bool allow_audio = false;
|
||||
@@ -137,8 +145,40 @@ struct OaiLimits {
|
||||
* after this is copied. A request that names a field always wins, exactly as with
|
||||
* default_reasoning_effort. */
|
||||
SamplingParams default_sampling;
|
||||
/* SamplingParams cannot tell an empty breaker list that was stated from one that was not, and
|
||||
* the two mean different things: unstated is llama-server's four, stated-empty is none. */
|
||||
bool default_dry_breakers_set = false;
|
||||
|
||||
/* THE DEPLOYMENT'S CHAT TEMPLATE VARIABLES, key -> JSON text, applied under the request's own
|
||||
* chat_template_kwargs so a key the request names is the request's. The thinking keys follow
|
||||
* parse_thinking's rule: a request that says anything about thinking replaces them whole. */
|
||||
std::map<std::string, std::string> default_template_kwargs;
|
||||
/* "auto" returns reasoning as reasoning_content; "none" leaves it inline in content. */
|
||||
std::string reasoning_format = "auto";
|
||||
};
|
||||
|
||||
/* WHAT A REQUEST SAYS ABOUT THINKING, in any of its three spellings: `effort` when it names a
|
||||
* reasoning effort (top-level or in chat_template_kwargs, non-empty), `thinking` when it says
|
||||
* anything at all -- an effort, or enable_thinking in either place. */
|
||||
struct RequestThinking {
|
||||
bool thinking = false;
|
||||
bool effort = false;
|
||||
};
|
||||
RequestThinking request_thinking(const json& b);
|
||||
|
||||
/* The deployment's template variables and reply format, onto `opt`, before the request's own
|
||||
* chat_template_kwargs are written over them. A request that says anything about thinking keeps
|
||||
* the deployment's enable_thinking out; one that names an effort keeps its reasoning_effort out.
|
||||
* The chat endpoint and /tokenize both render through this, so a prompt counted by one is the
|
||||
* prompt served by the other. */
|
||||
void apply_template_defaults(const json& b, const OaiLimits& lim, ChatRenderOptions* opt);
|
||||
|
||||
/* True when `v` is a legal value of the sampler field a request spells `field`; otherwise `why`
|
||||
* says what the legal set is, in the words a 400 carries. The same table checks a request and the
|
||||
* command-line flag that sets the field's default, so a default the command line accepts is one
|
||||
* every request omitting the field can be served with. A field with no bounds takes any value. */
|
||||
bool sampler_value_ok(const char* field, double v, std::string* why);
|
||||
|
||||
struct OaiDeps {
|
||||
ITokenizer* tok = nullptr;
|
||||
IChatTemplate* chat = nullptr;
|
||||
|
||||
@@ -47,13 +47,19 @@ Server::Server(const Deps& d, const ServerOptions& o)
|
||||
lim_.model_id = opt_.model_id;
|
||||
lim_.max_ctx = opt_.max_ctx;
|
||||
lim_.default_max_tokens = opt_.default_max_tokens;
|
||||
lim_.max_tokens_cap = opt_.max_tokens_cap;
|
||||
lim_.max_n = opt_.max_n;
|
||||
lim_.max_stops = opt_.max_stops;
|
||||
lim_.max_stop_bytes = opt_.max_stop_bytes;
|
||||
lim_.allow_image = opt_.allow_image;
|
||||
lim_.allow_video = opt_.allow_video;
|
||||
lim_.allow_audio = opt_.allow_audio;
|
||||
lim_.supports_logprobs = opt_.supports_logprobs;
|
||||
lim_.default_reasoning_effort = opt_.default_reasoning_effort;
|
||||
lim_.default_sampling = opt_.default_sampling;
|
||||
lim_.default_dry_breakers_set = opt_.default_dry_breakers_set;
|
||||
lim_.default_template_kwargs = opt_.default_template_kwargs;
|
||||
lim_.reasoning_format = opt_.reasoning_format;
|
||||
|
||||
odeps_.tok = deps_.tok;
|
||||
odeps_.chat = deps_.chat;
|
||||
@@ -158,9 +164,35 @@ int Server::run() {
|
||||
t.max_queued = kSpareWorkers;
|
||||
t.api_key = opt_.api_key;
|
||||
t.cors = opt_.cors;
|
||||
t.read_timeout_s = opt_.read_timeout_s;
|
||||
t.write_timeout_s = opt_.write_timeout_s;
|
||||
t.keep_alive_timeout_s = opt_.keep_alive_timeout_s;
|
||||
t.max_body_bytes = opt_.max_body_bytes;
|
||||
return transport_.serve(router_, t);
|
||||
}
|
||||
|
||||
void Server::check_template_defaults(std::string* kwargs_why, std::string* effort_why) const {
|
||||
kwargs_why->clear();
|
||||
effort_why->clear();
|
||||
if (!deps_.chat) return;
|
||||
const std::string body = R"({"messages":[{"role":"user","content":"Hello"}],"max_tokens":1})";
|
||||
auto render = [&](const OaiLimits& l, std::string* why) {
|
||||
OaiRequest r;
|
||||
ApiError e;
|
||||
if (parse_chat_request(body, odeps_, l, &r, &e) >= 0) return true;
|
||||
if (why) *why = e.message;
|
||||
return false;
|
||||
};
|
||||
OaiLimits l = lim_;
|
||||
l.default_template_kwargs.clear();
|
||||
l.default_reasoning_effort.clear();
|
||||
if (!render(l, nullptr)) return;
|
||||
l.default_template_kwargs = lim_.default_template_kwargs;
|
||||
if (!l.default_template_kwargs.empty() && !render(l, kwargs_why)) return;
|
||||
l.default_reasoning_effort = lim_.default_reasoning_effort;
|
||||
if (!l.default_reasoning_effort.empty()) render(l, effort_why);
|
||||
}
|
||||
|
||||
void Server::stop() {
|
||||
stopping_.store(true);
|
||||
transport_.stop();
|
||||
|
||||
+22
-1
@@ -66,8 +66,11 @@ struct ServerOptions {
|
||||
int retry_after_s = 1;
|
||||
|
||||
int64_t max_ctx = 0;
|
||||
int64_t default_max_tokens = 512;
|
||||
int64_t default_max_tokens = 512; /* 0: what the context leaves (OaiLimits) */
|
||||
int64_t max_tokens_cap = 0; /* 0: none */
|
||||
int max_n = 8;
|
||||
int64_t max_stops = 64;
|
||||
int64_t max_stop_bytes = 4096;
|
||||
|
||||
bool supports_logprobs = false;
|
||||
/* The deployment default for `reasoning_effort`, empty for the template's own. See
|
||||
@@ -76,6 +79,10 @@ struct ServerOptions {
|
||||
/* What a request that sends no sampler fields is served with. See
|
||||
* OaiLimits::default_sampling; only the sampler knobs are read from it. */
|
||||
SamplingParams default_sampling;
|
||||
bool default_dry_breakers_set = false;
|
||||
/* The deployment's chat template variables and reply format. See OaiLimits. */
|
||||
std::map<std::string, std::string> default_template_kwargs;
|
||||
std::string reasoning_format = "auto";
|
||||
bool allow_image = false;
|
||||
bool allow_video = false;
|
||||
bool allow_audio = false;
|
||||
@@ -104,6 +111,12 @@ struct ServerOptions {
|
||||
|
||||
std::string api_key;
|
||||
bool cors = true;
|
||||
|
||||
/* The transport's timeouts and body bound. See TransportOptions. */
|
||||
int read_timeout_s = 30;
|
||||
int write_timeout_s = 600;
|
||||
int keep_alive_timeout_s = 5;
|
||||
size_t max_body_bytes = 512ull << 20;
|
||||
};
|
||||
|
||||
class Server {
|
||||
@@ -120,6 +133,14 @@ public:
|
||||
const Router& router() const { return router_; }
|
||||
|
||||
int run(); /* binds, serves, blocks. RAD_OK or negative. */
|
||||
|
||||
/* THE DEPLOYMENT'S TEMPLATE DEFAULTS, RENDERED ONCE BEFORE A CALLER DEPENDS ON THEM. A value
|
||||
* the chat template refuses is a 400 on every request that leaves it to the default, so a
|
||||
* one-message chat is parsed exactly as a request would be: first with no template defaults,
|
||||
* then with the --chat-template-kwargs ones, then with the --reasoning-effort one as well.
|
||||
* A render that fails at the first step says nothing about the defaults and is not reported.
|
||||
* `kwargs_why` / `effort_why` are left empty, or hold the template's refusal at that step. */
|
||||
void check_template_defaults(std::string* kwargs_why, std::string* effort_why) const;
|
||||
void stop();
|
||||
int port() const { return transport_.port(); } /* 0 until run() is accepting */
|
||||
|
||||
|
||||
Reference in new issue
Block a user