|
Mila
Deep Neural Network Library
|
Canonical Gemma 4 native token grammar: parse/format for the model's registered turn / channel / tool-call / tool-response vocabulary. More...
#include <string>#include <string_view>#include <optional>#include <vector>#include <cstdint>#include <cstdlib>#include <memory>import nlohmann.json;Classes | |
| struct | Mila::Dnn::Gemma::GemmaToolCall |
| A tool call parsed out of the model's native <|tool_call> emission. More... | |
Namespaces | |
| namespace | Mila |
| Mila main API namespace. | |
Functions | |
| std::string | Mila::Dnn::Gemma::formatToolCall (std::string_view name, std::string_view arguments_json) |
| Render an assistant tool call back into Gemma's native call grammar. | |
| std::string | Mila::Dnn::Gemma::formatToolResponse (std::string_view name, std::string_view result_json) |
| Render a client-executed tool result into Gemma's <|tool_response> grammar. | |
| std::optional< GemmaToolCall > | Mila::Dnn::Gemma::parseToolCall (std::string_view text) |
| Parse the most recent native Gemma tool call out of accumulated model output, once generation has stopped right after <tool_call|>. | |
Canonical Gemma 4 native token grammar: parse/format for the model's registered turn / channel / tool-call / tool-response vocabulary.
The Gemma 4 grammar is a property of the model, not of any single adaptor. This runtime module is the one source of truth for it; the Chat harness and (via a future pybind or a parity test) the Python inference server consume it rather than each carrying a private copy that drifts. It was seeded from the union of the two prior implementations – the Python gemma_protocol.py, which carried the spec-verified behaviors (the <|"|> string delimiter, tool-response output-field distillation, failed-tool error surfacing), and the C++ GemmaToolCallParser it replaces. See GemmaChatProtocol.md.
String-level parse/format only. Token-level splice into the live KV cache is the decided direction but is post-release (see MilaProductFamily.md).
|
export |
Render an assistant tool call back into Gemma's native call grammar.
Non-object / unparseable arguments render as an empty body rather than throwing, so a malformed history entry degrades to a bare call.
|
export |
Render a client-executed tool result into Gemma's <|tool_response> grammar.
When the result is a JSON envelope only its primary output field is surfaced (the first non-empty of output/result/content/stdout/text); metadata siblings are dropped. A failed tool ({"content": "", "error": "..."}) has no usable output field, so its error is surfaced explicitly – without it the model sees an empty result and blind-retries. A non-JSON result is passed through under the value: key, which is what the canonical template emits for a non-mapping response.
|
export |
Parse the most recent native Gemma tool call out of accumulated model output, once generation has stopped right after <tool_call|>.
Returns nullopt when no <|tool_call> ... call:name{...} block is present or the block is malformed (so the caller surfaces the text as-is rather than looping on a call it cannot dispatch).