Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Gemma.Protocol.ixx File Reference

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< GemmaToolCallMila::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|>.

Variables

constexpr std::string_view Mila::Dnn::Gemma::kBos = "<bos>"
constexpr std::string_view Mila::Dnn::Gemma::kChannelClose = "<channel|>"
constexpr std::string_view Mila::Dnn::Gemma::kChannelOpen = "<|channel>"
constexpr std::string_view Mila::Dnn::Gemma::kEos = "<eos>"
constexpr std::string_view Mila::Dnn::Gemma::kOutputKeys [] = { "output", "result", "content", "stdout", "text" }
constexpr std::string_view Mila::Dnn::Gemma::kPad = "<pad>"
constexpr std::string_view Mila::Dnn::Gemma::kStringDelimiter = "<|\"|>"
constexpr std::string_view Mila::Dnn::Gemma::kThink = "<|think|>"
constexpr std::string_view Mila::Dnn::Gemma::kToolCallClose = "<tool_call|>"
constexpr std::string_view Mila::Dnn::Gemma::kToolCallOpen = "<|tool_call>"
constexpr std::string_view Mila::Dnn::Gemma::kToolClose = "<tool|>"
constexpr std::string_view Mila::Dnn::Gemma::kToolOpen = "<|tool>"
constexpr std::string_view Mila::Dnn::Gemma::kToolResponseClose = "<tool_response|>"
constexpr std::string_view Mila::Dnn::Gemma::kToolResponseOpen = "<|tool_response>"
constexpr std::string_view Mila::Dnn::Gemma::kTurnClose = "<turn|>"
constexpr std::string_view Mila::Dnn::Gemma::kTurnOpen = "<|turn>"

Detailed Description

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).

Function Documentation

◆ formatToolCall()

std::string Mila::Dnn::Gemma::formatToolCall ( std::string_view name,
std::string_view arguments_json )
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.

◆ formatToolResponse()

std::string Mila::Dnn::Gemma::formatToolResponse ( std::string_view name,
std::string_view result_json )
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.

◆ parseToolCall()

std::optional< GemmaToolCall > Mila::Dnn::Gemma::parseToolCall ( std::string_view text)
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).