Archived
oai: preserved_tokens on /v1/completions; find a text-tailed reasoning opener by its token
/v1/completions accepts llama-server's preserved_tokens: control tokens the completion's text keeps rather than drops. A client that renders its own template and parses its own reply (a tool-call sidecar) has no other way to see a reply format built of control tokens. Bounded like the stop strings. A thinking budget needs the reasoning block found by token. Gemma 4's opener <|channel>thought is one control token followed by text; it is now found by that first token (what a reply opening its own block writes first, and no text can spell), with the text after it counted as reasoning. An opener with a second control token in it is still refused, since its first token may open something other than the reasoning. The refusal message says why. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2381591823
commit
fb716830c2
1 file changed
+41
-5
+41
-5
@@ -245,7 +245,11 @@ const char* kAcceptedChat[] = {
|
|||||||
"functions", "function_call",
|
"functions", "function_call",
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* kAcceptedCompletion[] = { "prompt", "echo" };
|
/* `preserved_tokens` is llama-server's: control tokens a raw completion's text keeps. On chat the
|
||||||
|
* reply format decides them (the guarded entry below); a raw completion has no format, and a
|
||||||
|
* client that renders its own template and parses its own reply -- a tool-call sidecar -- has no
|
||||||
|
* other way to see a format built of control tokens. */
|
||||||
|
const char* kAcceptedCompletion[] = { "prompt", "echo", "preserved_tokens" };
|
||||||
|
|
||||||
const char* kAcceptedEmbedding[] = { "model", "input", "messages", "encoding_format",
|
const char* kAcceptedEmbedding[] = { "model", "input", "messages", "encoding_format",
|
||||||
"dimensions", "image_tokens", "prompt_name" };
|
"dimensions", "image_tokens", "prompt_name" };
|
||||||
@@ -1613,9 +1617,18 @@ static int parse_thinking_budget(const json& b, const OaiDeps& d, const OaiLimit
|
|||||||
if (!render.think_open.empty()) o = d.tok->encode(render.think_open, false, true);
|
if (!render.think_open.empty()) o = d.tok->encode(render.think_open, false, true);
|
||||||
n_close = c.size();
|
n_close = c.size();
|
||||||
n_open = o.size();
|
n_open = o.size();
|
||||||
if (c.size() == 1 && (render.think_starts_open || o.size() == 1)) {
|
/* AN OPENER THAT IS ONE CONTROL TOKEN AND THEN TEXT -- Gemma 4's `<|channel>thought` --
|
||||||
|
* is found by that first token: it is what a reply opening its own block writes first,
|
||||||
|
* and no text can be. The text after it counts as reasoning, as the markers do. An opener
|
||||||
|
* with a second control token in it (a channel and a message marker) is not read this
|
||||||
|
* way, since its first token may open something other than the reasoning. */
|
||||||
|
bool open_first = o.size() > 1 && d.tok->reply_bytes(o[0]).empty() &&
|
||||||
|
!d.tok->token_bytes(o[0]).empty();
|
||||||
|
for (size_t i = 1; open_first && i < o.size(); ++i)
|
||||||
|
if (d.tok->reply_bytes(o[i]).empty()) open_first = false;
|
||||||
|
if (c.size() == 1 && (render.think_starts_open || o.size() == 1 || open_first)) {
|
||||||
span.close = c[0];
|
span.close = c[0];
|
||||||
span.open = o.size() == 1 ? o[0] : -1;
|
span.open = (o.size() == 1 || open_first) ? o[0] : -1;
|
||||||
span.starts_open = render.think_starts_open;
|
span.starts_open = render.think_starts_open;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1654,8 +1667,8 @@ static int parse_thinking_budget(const json& b, const OaiDeps& d, const OaiLimit
|
|||||||
"as one token";
|
"as one token";
|
||||||
else
|
else
|
||||||
why = "this reply has to open its own reasoning block, and the opener `" +
|
why = "this reply has to open its own reasoning block, and the opener `" +
|
||||||
render.think_open + "` is " + std::to_string(n_open) + " tokens, so the block "
|
render.think_open + "` is " + std::to_string(n_open) + " tokens and does not "
|
||||||
"cannot be found by token";
|
"start with a lone control token, so the block cannot be found by token";
|
||||||
*err = err_unsupported(named, why);
|
*err = err_unsupported(named, why);
|
||||||
return RAD_E_UNSUPPORTED;
|
return RAD_E_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
@@ -2135,6 +2148,29 @@ int parse_completion_request(const std::string& body, const OaiDeps& d, const Oa
|
|||||||
|
|
||||||
if (!get_bool(b, "echo", &out->echo, err)) return RAD_E_INVAL;
|
if (!get_bool(b, "echo", &out->echo, err)) return RAD_E_INVAL;
|
||||||
|
|
||||||
|
/* The control tokens this completion's text keeps (llama-server's field): each string is
|
||||||
|
* tokenised by the decoder and every control token in it is rendered rather than dropped.
|
||||||
|
* Bounded as the stop strings are, since the decoder looks each one up per token. */
|
||||||
|
{
|
||||||
|
auto pt = b.find("preserved_tokens");
|
||||||
|
if (pt != b.end() && !pt->is_null()) {
|
||||||
|
if (!pt->is_array() || pt->size() > (size_t)lim.max_stops) {
|
||||||
|
*err = err_bad("preserved_tokens", "expected an array of at most " +
|
||||||
|
std::to_string(lim.max_stops) + " strings");
|
||||||
|
return RAD_E_INVAL;
|
||||||
|
}
|
||||||
|
for (const auto& s : *pt) {
|
||||||
|
if (!s.is_string() || s.get<std::string>().size() > (size_t)lim.max_stop_bytes) {
|
||||||
|
*err = err_bad("preserved_tokens", "expected strings of at most " +
|
||||||
|
std::to_string(lim.max_stop_bytes) +
|
||||||
|
" bytes");
|
||||||
|
return RAD_E_INVAL;
|
||||||
|
}
|
||||||
|
out->preserved_tokens.push_back(s.get<std::string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RAD_TRY(parse_logprobs(b, lim, Endpoint::Completion, out, err));
|
RAD_TRY(parse_logprobs(b, lim, Endpoint::Completion, out, err));
|
||||||
/* `echo` with logprobs asks for the PROMPT's logprobs too, which are not computed: a prompt
|
/* `echo` with logprobs asks for the PROMPT's logprobs too, which are not computed: a prompt
|
||||||
* position's distribution never leaves the card. */
|
* position's distribution never leaves the card. */
|
||||||
|
|||||||
Reference in new issue
Block a user