Archived
chat: tagged calls whose name the opener opens -- GLM-5's <tool_call>NAME<arg_key>
GLM-5 writes <tool_call>NAME<arg_key>K</arg_key><arg_value>V</arg_value>...</tool_call>: no prefix of the name's own and no suffix, the name ends at the first argument or at the call's close. The tagged family now takes an empty name_suffix when name_prefix opens the call (no call_open): the derivation from the template makes the per-call opener the prefix and its closer call_end, the grammar takes eps() for the suffix, and the streaming parser finds the name's end byte by byte (name_stop, name_plausible) and reads the closing tag as the first item of the body. arg_name_suffix and arg_value_prefix may both be set and are read back to back, so chat_format_markers keeps both tags. value_newlines comes from a probe render of the template rather than a constant (GLM and MiniCPM false, Qwen true). The ABI struct is unchanged; its comments say what an empty name_suffix means now. chatparse_test: GLM's template serves tools; the derived format equals the one a plugin would declare; every rendered reply in the corpus reads back as the message it rendered, split at every byte; and replies that stray from the grammar. Each fails without this change. The Gemma refusal names call_end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ac0bc9690
commit
e60810116c
6 files changed
+507
-22
No files matched your search
+52
-8
@@ -7,6 +7,7 @@
|
||||
* reply. */
|
||||
#include "llama/common/chat.h"
|
||||
#include "llama/common/chat-auto-parser.h"
|
||||
#include "llama/common/chat-auto-parser-helpers.h"
|
||||
#include "llama/common/json-schema-to-grammar.h"
|
||||
#include "shim/log.h"
|
||||
|
||||
@@ -120,6 +121,29 @@ int reasoning_anywhere_check(const ChatFormat& f, std::string* why) {
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Does the template write an argument's value on lines of its own? Then one newline at each end
|
||||
* is the tags' (ChatFormat::value_newlines). A template that writes it flush against its tags
|
||||
* (`<arg_value>VALUE</arg_value>`) leaves those newlines to the value; reading them as the tags'
|
||||
* would drop a written file's last line break. A render that fails counts as own lines. */
|
||||
bool value_on_own_lines(const common_chat_template& tmpl, const ChatFormat& c) {
|
||||
static const char* const kValue = "RADIANCE_VALUE_PROBE";
|
||||
ojson call = { { "id", "call_0" }, { "type", "function" } };
|
||||
call["function"] = { { "name", "probe" }, { "arguments", { { "arg", kValue } } } };
|
||||
ojson reply = { { "role", "assistant" }, { "content", "" } };
|
||||
reply["tool_calls"] = ojson::array({ call });
|
||||
ojson params = { { "type", "object" } };
|
||||
params["properties"]["arg"]["type"] = "string";
|
||||
ojson tool = { { "type", "function" } };
|
||||
tool["function"] = { { "name", "probe" }, { "description", "probe" } };
|
||||
tool["function"]["parameters"] = params;
|
||||
template_params tp;
|
||||
tp.messages = ojson::array({ ojson{ { "role", "user" }, { "content", "hi" } }, reply });
|
||||
tp.tools = ojson::array({ tool });
|
||||
const std::string r = autoparser::apply_template(tmpl, tp);
|
||||
const std::string flush = c.arg_name_suffix + c.arg_value_prefix + kValue + c.arg_value_suffix;
|
||||
return r.find(flush) == std::string::npos;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* ================================================================== validity */
|
||||
@@ -162,9 +186,10 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
|
||||
if (f.call_open.empty() && !f.call_close.empty())
|
||||
return no("call_close without call_open");
|
||||
const bool object = f.object_args();
|
||||
/* An object's own `{` may be what ends the name; anything else needs its suffix. */
|
||||
if (!object || !f.name_suffix.empty()) {
|
||||
if (f.name_suffix.empty() || trim_ws(f.name_suffix).empty())
|
||||
/* Without a suffix the name ends at what follows it: the object's `{`, or a tagged call's
|
||||
* first argument or close. */
|
||||
if (!f.name_suffix.empty()) {
|
||||
if (trim_ws(f.name_suffix).empty())
|
||||
return no("name_suffix must end the function name with something besides whitespace");
|
||||
if (is_ws((unsigned char)f.name_suffix[0]))
|
||||
return no("name_suffix " + show(f.name_suffix) + " may not begin with whitespace");
|
||||
@@ -176,9 +201,17 @@ int chat_format_check(const ChatFormat& f, std::string* why) {
|
||||
if (f.arg_name_suffix.empty() && f.arg_value_prefix.empty())
|
||||
return no("nothing ends an argument name: arg_name_suffix and arg_value_prefix are "
|
||||
"empty");
|
||||
if (!f.arg_name_suffix.empty() && !f.arg_value_prefix.empty())
|
||||
return no("arg_name_suffix and arg_value_prefix are both set; the streaming parser "
|
||||
"reads one delimiter between an argument's name and its value");
|
||||
if (f.name_suffix.empty()) {
|
||||
/* The name runs to the first arg_prefix or call_end. A wrapped format also reads
|
||||
* name_prefix alone as a call, told from prose only by its closed name tag, and
|
||||
* without a suffix there is none. */
|
||||
if (!f.call_open.empty())
|
||||
return no("name_suffix is empty, which a tagged call allows only when name_prefix "
|
||||
"opens it, and call_open is set");
|
||||
if (is_ws((unsigned char)f.arg_prefix[0]))
|
||||
return no("arg_prefix " + show(f.arg_prefix) + " may not begin with whitespace "
|
||||
"when it is what ends the function name");
|
||||
}
|
||||
if (f.string_value != ChatValue::Raw && f.string_value != ChatValue::Cdata)
|
||||
return no("string_value must be RAD_CHAT_VALUE_RAW or RAD_CHAT_VALUE_CDATA");
|
||||
if ((uint32_t)f.other_value > (uint32_t)ChatValue::Json)
|
||||
@@ -362,9 +395,18 @@ int chat_format_from_template(const std::string& jinja, const std::string& bos,
|
||||
c.arg_name_suffix = t.arguments.name_suffix;
|
||||
c.arg_value_prefix = t.arguments.value_prefix;
|
||||
c.arg_value_suffix = t.arguments.value_suffix;
|
||||
/* A name the per-call opener itself opens, with no tag of its own around it
|
||||
* (GLM's `<tool_call>NAME<arg_key>`): the opener is the name's prefix and the
|
||||
* per-call close the call's own, so the name ends at the first argument or close. */
|
||||
if (c.name_prefix.empty() && !c.call_open.empty() && c.call_end.empty()) {
|
||||
c.name_prefix = c.call_open;
|
||||
c.call_end = c.call_close;
|
||||
c.call_open.clear();
|
||||
c.call_close.clear();
|
||||
}
|
||||
c.string_value = ChatValue::Cdata;
|
||||
c.other_value = ChatValue::Json;
|
||||
c.value_newlines = true;
|
||||
c.value_newlines = value_on_own_lines(tmpl, c);
|
||||
/* The section or per-call opener both triggers the lazy grammar and ends an
|
||||
* unclosed reasoning block. A format with neither gets no grammar and no break,
|
||||
* since its calls open with the name prefix, which a model also writes in prose. */
|
||||
@@ -853,7 +895,9 @@ int chat_format_grammar(const ChatFormat& f, const ChatGrammarRequest& req, Chat
|
||||
if (!req_arms.empty()) args = seq({ any_arg, rep(seq({ space(), any_arg }), 0, -1) });
|
||||
else if (!opt_arms.empty()) args = seq({ eps(), rep(seq({ space(), any_arg }), 0, -1) });
|
||||
|
||||
G open = wrap(seq({ lit(f.name_prefix), wrap(lit(name)), lit(f.name_suffix) }));
|
||||
/* Without a suffix the name ends where its arguments or close begin. */
|
||||
G open = wrap(seq({ lit(f.name_prefix), wrap(lit(name)),
|
||||
f.name_suffix.empty() ? eps() : lit(f.name_suffix) }));
|
||||
G func = seq({ open, eps(), space(), args, space(), wrap(lit(f.call_end)) });
|
||||
b.add_rule(rule_name("tool-" + name), render(func));
|
||||
choices.push_back(rule_ref(rule_name("tool-" + name)));
|
||||
|
||||
+12
-5
@@ -22,12 +22,16 @@
|
||||
* Qwen3.6 / Qwen3.8 <tool_call>\n<function=NAME>\n<parameter=ARG>\nVALUE\n</parameter>\n
|
||||
* </function>\n</tool_call>
|
||||
* MiniCPM5 <function name="NAME"><param name="ARG">VALUE</param></function>
|
||||
* GLM-5 <tool_call>NAME<arg_key>ARG</arg_key><arg_value>VALUE</arg_value>
|
||||
* </tool_call>
|
||||
*
|
||||
* A call's name sits between a prefix and a suffix and the call ends with its own close; each
|
||||
* argument is a name between a prefix and a suffix, then a value ending at its own suffix; calls
|
||||
* may be wrapped one by one. Arguments are typed by the tool's JSON schema: a string-typed one is
|
||||
* raw text (optionally CDATA-wrapped), any other type is JSON, and text that does not parse as
|
||||
* JSON is taken raw, since models often write Python literals into a tag.
|
||||
* may be wrapped one by one. A name may also have no suffix of its own (GLM): it then ends where
|
||||
* the first argument or the call's close begins, and that tag is read as what it is. Arguments
|
||||
* are typed by the tool's JSON schema: a string-typed one is raw text (optionally CDATA-wrapped),
|
||||
* any other type is JSON, and text that does not parse as JSON is taken raw, since models often
|
||||
* write Python literals into a tag.
|
||||
*
|
||||
* Object family (ChatArgs::Object): a call is its name and one object holding the arguments.
|
||||
*
|
||||
@@ -107,8 +111,9 @@ struct ChatFormat {
|
||||
/* Around each call: "<tool_call>\n", "</tool_call>". When set, a call written without it
|
||||
* (name_prefix alone) is still read as a call (chatparse.h). */
|
||||
std::string call_open, call_close;
|
||||
/* "<function=", ">\n". In the object family name_suffix may be empty; the name then ends at
|
||||
* the object's `{`. */
|
||||
/* "<function=", ">\n". name_suffix may be empty: in the object family the name then ends at
|
||||
* the object's `{`; in the tagged family, where name_prefix must open the call (no
|
||||
* call_open), at the first arg_prefix or call_end, which stay part of the call's body. */
|
||||
std::string name_prefix, name_suffix;
|
||||
std::string call_end; /* "</function>\n"; may be empty for an object */
|
||||
|
||||
@@ -120,6 +125,8 @@ struct ChatFormat {
|
||||
bool object_bare_keys = false;
|
||||
|
||||
std::string arg_prefix; /* "<parameter=" */
|
||||
/* What ends a name and opens its value. Both may be set (GLM: "</arg_key>", "<arg_value>"):
|
||||
* they are then read as one delimiter, written back to back. */
|
||||
std::string arg_name_suffix; /* ">\n" (MiniCPM: empty, the value prefix ends it) */
|
||||
std::string arg_value_prefix; /* "" (MiniCPM: "\">") */
|
||||
std::string arg_value_suffix; /* "</parameter>\n" */
|
||||
|
||||
+94
-6
@@ -726,7 +726,8 @@ struct ReplyFormat::Impl {
|
||||
* second, since models often write a call without its wrapper; a wrapper written without
|
||||
* its trailing whitespace is read too. */
|
||||
std::vector<std::string> openers;
|
||||
std::string name_end; /* what ends an argument name */
|
||||
/* What ends an argument name: arg_name_suffix and arg_value_prefix, back to back. */
|
||||
std::string name_end;
|
||||
/* The same two, whitespace-trimmed. A loose call is one the grammar never opened, so it was
|
||||
* written unconstrained; its argument tags, like its name and close, are read with their
|
||||
* surrounding whitespace optional. */
|
||||
@@ -740,6 +741,9 @@ struct ReplyFormat::Impl {
|
||||
/* Object family. `object`: a call's arguments are one object. `ns_object`: the object's own
|
||||
* `{` ends the name (name_suffix empty), and ns_core is "{". */
|
||||
bool object = false, ns_object = false;
|
||||
/* Tagged family, name_suffix empty: a call's name ends where arg_prefix or end_core begins,
|
||||
* and that tag is the first of the call's body. */
|
||||
bool name_open = false;
|
||||
/* Where a key or value may begin, besides the object's punctuation: the dialect string's
|
||||
* opener, and the call's close and end (written there by a reply that leaves its object
|
||||
* unclosed). Matched anchored, by first byte; chat_format_check keeps them distinct. */
|
||||
@@ -788,8 +792,9 @@ int ReplyFormat::compile(const ChatFormat& f, std::shared_ptr<const ReplyFormat>
|
||||
split_ws(f.call_open, &m.co_core, &m.co_trail);
|
||||
if (f.call_open.empty()) m.openers = { f.name_prefix };
|
||||
else m.openers = { m.co_core, f.name_prefix };
|
||||
m.name_end = f.arg_name_suffix.empty() ? f.arg_value_prefix : f.arg_name_suffix;
|
||||
m.name_end = f.arg_name_suffix + f.arg_value_prefix;
|
||||
m.object = f.has_tools() && f.object_args();
|
||||
m.name_open = f.has_tools() && !m.object && f.name_suffix.empty();
|
||||
if (m.object && f.name_suffix.empty()) {
|
||||
m.ns_object = true;
|
||||
m.ns_core = "{";
|
||||
@@ -976,7 +981,15 @@ static int bind_tools(const ReplyFormat::Impl& F, const ojson& tools, ReplyParse
|
||||
t.params.emplace(pname, p);
|
||||
}
|
||||
t.arms.insert(t.arms.end(), optional.begin(), optional.end());
|
||||
if (t.name.find(F.ns_core) != std::string::npos ||
|
||||
if (F.name_open) {
|
||||
for (const std::string* d : { &F.f.arg_prefix, &F.end_core })
|
||||
if ((t.name + *d).find(*d) != t.name.size()) {
|
||||
*why = "tool '" + t.name + "' has a name running into '" + *d +
|
||||
"', which this model's tool-call format uses to end a function "
|
||||
"name";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
} else if (t.name.find(F.ns_core) != std::string::npos ||
|
||||
(!F.ns_object &&
|
||||
(t.name + F.f.name_suffix).find(F.f.name_suffix) != t.name.size())) {
|
||||
*why = "tool '" + t.name + "' has a name containing '" + F.ns_core +
|
||||
@@ -1053,7 +1066,8 @@ enum class St : uint8_t {
|
||||
OpenCoTail, /* the whitespace that ends call_open, after its core */
|
||||
OpenWs, /* after call_open: whitespace */
|
||||
OpenPrefix, /* name_prefix, after call_open */
|
||||
OpenName, /* the function name, to the core of name_suffix */
|
||||
OpenName, /* the function name, to the core of name_suffix (or, without one, to the
|
||||
first argument or the call's end) */
|
||||
OpenTail, /* the whitespace that ends name_suffix */
|
||||
OpenPeek, /* a loose call's name is read: whitespace, then an argument or the close */
|
||||
OpenPeekArg, /* ... and the first argument's name, which must close as a name */
|
||||
@@ -1330,6 +1344,75 @@ struct ReplyStream::Impl {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* A suffixless name (ReplyFormat::Impl::name_open) ends where arg_prefix or end_core begins.
|
||||
* Returns which one ended it (0, 1), with *len the name's length before it; or -1, with *len
|
||||
* the bytes that are the name's for certain, the rest possibly beginning either. */
|
||||
int name_stop(size_t* len) const {
|
||||
const std::string* const stops[] = { &F.f.arg_prefix, &F.end_core };
|
||||
size_t pend = 0;
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
const std::string& t = *stops[i];
|
||||
if (name.size() >= t.size() && name.compare(name.size() - t.size(), t.size(), t) == 0) {
|
||||
*len = name.size() - t.size();
|
||||
return i;
|
||||
}
|
||||
for (size_t k = std::min(t.size() - 1, name.size()); k > pend; --k)
|
||||
if (name.compare(name.size() - k, k, t, 0, k) == 0) { pend = k; break; }
|
||||
}
|
||||
*len = name.size() - pend;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Could these bytes, after any leading whitespace, still be a function name? One holds no
|
||||
* '<' and no whitespace before more text, unless a declared name does. Without a tag of its
|
||||
* own to close it, a name read from prose that mentions the opener would otherwise be held
|
||||
* up to kMaxLooseName bytes. */
|
||||
bool name_plausible(std::string_view n) const {
|
||||
size_t i = 0;
|
||||
while (i < n.size() && is_ws((unsigned char)n[i])) ++i;
|
||||
bool gap = false;
|
||||
for (; i < n.size(); ++i) {
|
||||
const unsigned char c = (unsigned char)n[i];
|
||||
if (c == '<') return declared_prefix(n);
|
||||
if (is_ws(c)) gap = true;
|
||||
else if (gap) return declared_prefix(n);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* OpenName for a suffixless name. The tag that ends it is read here, as the first of the
|
||||
* call's body: an argument's opener, or the call's end. Whitespace around the name is
|
||||
* dropped; before it, the grammar does not admit it. */
|
||||
const char* open_name_byte(const char* p) {
|
||||
size_t len = 0;
|
||||
const int stop = name_stop(&len);
|
||||
if (stop < 0) {
|
||||
if (len > kMaxLooseName || !name_plausible(std::string_view(name).substr(0, len)))
|
||||
fail_attempt();
|
||||
return p + 1;
|
||||
}
|
||||
size_t a = 0;
|
||||
while (a < len && is_ws((unsigned char)name[a])) ++a;
|
||||
const std::string nm(trim_trailing_ws(std::string_view(name).substr(a, len - a)));
|
||||
auto it = P.by_name.find(nm);
|
||||
tool = it == P.by_name.end() ? -1 : (int)it->second;
|
||||
if (tool < 0 && (nm.empty() || nm.find('<') != std::string::npos)) {
|
||||
fail_attempt();
|
||||
return p + 1;
|
||||
}
|
||||
name = nm;
|
||||
begin_call(a == 0);
|
||||
lpos = 0;
|
||||
lhold.clear();
|
||||
if (stop == 0) {
|
||||
start_arg();
|
||||
} else {
|
||||
finish_call_strictness();
|
||||
end_core_done();
|
||||
}
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
/* The call opener has matched at `att`; whether a call follows is decided below. */
|
||||
void begin_attempt(St from) {
|
||||
origin = from;
|
||||
@@ -1966,6 +2049,7 @@ struct ReplyStream::Impl {
|
||||
const char c = *p;
|
||||
att.push_back(c);
|
||||
name.push_back(c);
|
||||
if (F.name_open) return open_name_byte(p);
|
||||
const size_t k = F.ns_core.size();
|
||||
if (name.size() >= k && name.compare(name.size() - k, k, F.ns_core) == 0) {
|
||||
const std::string nm = name.substr(0, name.size() - k);
|
||||
@@ -2640,8 +2724,12 @@ struct ReplyStream::Impl {
|
||||
case St::OpenWs:
|
||||
case St::OpenPrefix:
|
||||
return true;
|
||||
case St::OpenName:
|
||||
return declared_prefix(name);
|
||||
case St::OpenName: {
|
||||
if (!F.name_open) return declared_prefix(name);
|
||||
size_t len = 0;
|
||||
name_stop(&len);
|
||||
return declared_prefix(std::string_view(name).substr(0, len));
|
||||
}
|
||||
case St::OpenTail:
|
||||
return tool >= 0;
|
||||
default:
|
||||
|
||||
Reference in new issue
Block a user