Archived
chat: a call written as its name and one object; reasoning that opens again later
The reply format described one call syntax, the tagged one: each argument in its own tags,
typed by the tool's schema. A call written as its name followed by ONE object holding the
arguments could not be described -- Gemma 4's `<|tool_call>call:NAME{key:<|"|>text<|"|>}<tool_call|>`
is one, and its tool requests were refused; JSON behind a name tag (`[TOOL_CALLS]NAME[ARGS]{...}`)
or inside a function tag is the same shape.
RadChatFormat grows at its end (no ABI bump; an older plugin reads the new fields as zero):
- args_form RAD_CHAT_ARGS_OBJECT: the arguments are one object after the name. name_suffix may
be empty, and the name then ends at the object's `{`; call_end may be empty, and the object's
own `}` ends the call. The arg_* fields are unused and must be empty.
- object_string_open / object_string_close: a string delimiter of the object's dialect besides
JSON's `"`, between which the text is verbatim (Gemma 4: <|"|> twice).
- object_bare_keys: the dialect writes keys bare, `key:value`.
- reasoning_anywhere: a reasoning block may open again later in the reply (after the answer,
between calls) and is read as reasoning there too; a close with no block open is dropped.
The streaming parser reads the object as it arrives and writes it as JSON as it reads: brackets
and separators as they come, a key once its `:` shows it is one, a string's bytes as they arrive
(the dialect's verbatim, JSON's with its escapes checked: an unknown escape is text, a lone
surrogate is U+FFFD), and a bare value as the tagged family writes a typed one -- held while it
could be a number or literal, else a string. Trailing and missing separators, a bracket of the
wrong kind, a key without value and an object cut off by the call's close are repaired locally
and reported as leaving the grammar; a closed call's arguments are JSON whatever was written.
The streaming contract holds: deltas are never revised, holds stay bounded.
The lazy grammar writes the object in its dialect (strings between the delimiters, bare keys,
JSON numbers and literals) and leaves the values to the model, as llama.cpp's own Gemma 4
grammar does: the schema converter speaks JSON, and the dialect is not JSON.
chat_format_check refuses what the parser could not read unambiguously, by name: tagged fields in
an object format, a dialect string opener that begins like a bare word, a call close that holds
object punctuation or cannot be told apart from the string opener, a reasoning marker that
overlaps a call opener in content.
chatparse_test: JSON behind a name tag and behind a function tag (strict, lenient, streamed every
way, grammar admits and refuses), reasoning_anywhere on and off, and the refusals.
This commit is contained in:
1 parent
b9229091a9
commit
dbd4e164b3
6 files changed
+1007
-53
No files matched your search
+38
-6
@@ -32,10 +32,21 @@
|
||||
* the raw text (optionally CDATA-wrapped), any other type is JSON, and text that does not parse as
|
||||
* JSON is taken raw -- a model writing into an XML tag writes Python as often as JSON.
|
||||
*
|
||||
* The analyser also recognises formats outside this family (a JSON object per call, JSON
|
||||
* arguments inside a tag, call ids). For those chat_format_from_template keeps the reasoning split
|
||||
* and names why tool calls are refused, and a model that needs one gets it by adding the shape to
|
||||
* the parser, not by a fallback. */
|
||||
* THE OBJECT FAMILY writes a call as its name and one object holding the arguments
|
||||
* (ChatArgs::Object):
|
||||
*
|
||||
* Gemma 4 <|tool_call>call:NAME{key:<|"|>text<|"|>,n:3}<tool_call|>
|
||||
*
|
||||
* The object is JSON or a dialect of it -- strings between two declared delimiters with their
|
||||
* text verbatim, keys written bare -- and the arguments a client receives are that object in
|
||||
* JSON. Here the values carry their own types, so no schema is consulted to read them; a bare
|
||||
* word that is no JSON literal or number is read as a string, as a typed argument that is not
|
||||
* JSON is in the tagged family.
|
||||
*
|
||||
* The analyser also recognises formats outside these families (a JSON object per call with its
|
||||
* name inside it, call ids). For those chat_format_from_template keeps the reasoning split and
|
||||
* names why tool calls are refused, and a model that needs one gets it by adding the shape to the
|
||||
* parser, not by a fallback. */
|
||||
#pragma once
|
||||
#include "rad_internal.h"
|
||||
|
||||
@@ -68,6 +79,12 @@ enum class ChatThink : uint32_t {
|
||||
Never = 2, /* the model opens its own block, if any */
|
||||
};
|
||||
|
||||
/* How a call's arguments are written. The numbering is the ABI's (RAD_CHAT_ARGS_*). */
|
||||
enum class ChatArgs : uint32_t {
|
||||
Tagged = 0, /* each argument in its own tags: arg_prefix NAME .. VALUE arg_value_suffix */
|
||||
Object = 1, /* one object after the name: `{` key `:` value, ... `}` */
|
||||
};
|
||||
|
||||
struct ChatFormat {
|
||||
std::string name; /* for logs: "plugin:<arch>" or "template" */
|
||||
|
||||
@@ -83,6 +100,10 @@ struct ChatFormat {
|
||||
* Must name the call opener (`call_open`) when non-empty. */
|
||||
std::vector<std::string> reasoning_breaks;
|
||||
ChatThink reasoning_in_prompt = ChatThink::FromPrompt;
|
||||
/* A block may also open later in the reply -- after the answer, between calls -- and is read
|
||||
* as reasoning there; a close with no block open is dropped. Gemma 4 writes an empty block
|
||||
* after its answer now and then, and a lone close. Needs `reasoning_open`. */
|
||||
bool reasoning_anywhere = false;
|
||||
|
||||
/* ------------------------------------------------------------------ tool calls
|
||||
* `name_prefix` is what opens a call (after `call_open`, when there is one). Empty means the
|
||||
@@ -94,8 +115,18 @@ 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, leniently (chatparse.h). */
|
||||
std::string call_open, call_close;
|
||||
std::string name_prefix, name_suffix; /* "<function=", ">\n" */
|
||||
std::string call_end; /* "</function>\n" */
|
||||
/* "<function=", ">\n". In the object family name_suffix may be empty: the name then ends
|
||||
* where the object begins, at its `{`. */
|
||||
std::string name_prefix, name_suffix;
|
||||
std::string call_end; /* "</function>\n"; may be empty for an object */
|
||||
|
||||
/* How the arguments are written. Object: the arg_* fields below are empty, and these three
|
||||
* describe the object's dialect -- a string delimiter besides JSON's `"`, between which the
|
||||
* text is verbatim ("<|\"|>" twice for Gemma 4; empty for JSON strings only), and whether a
|
||||
* key may be written bare. */
|
||||
ChatArgs args_form = ChatArgs::Tagged;
|
||||
std::string object_string_open, object_string_close;
|
||||
bool object_bare_keys = false;
|
||||
|
||||
std::string arg_prefix; /* "<parameter=" */
|
||||
std::string arg_name_suffix; /* ">\n" (MiniCPM: empty, the value prefix ends it) */
|
||||
@@ -115,6 +146,7 @@ struct ChatFormat {
|
||||
|
||||
bool has_reasoning() const { return !reasoning_close.empty(); }
|
||||
bool has_tools() const { return !name_prefix.empty(); }
|
||||
bool object_args() const { return args_form == ChatArgs::Object; }
|
||||
};
|
||||
|
||||
/* Can the streaming parser run this format? RAD_OK, or RAD_E_UNSUPPORTED with *why naming the
|
||||
|
||||
Reference in new issue
Block a user