Setting engine.gptoss_gguf (or STRATA_GPTOSS_GGUF) selects the gpt-oss family and takes the tokenizer from that GGUF. Prompts: a Responses request (instructions, input items with reasoning, function calls and outputs, tools, tool_choice, parallel_tool_calls, text.format, reasoning effort) is rendered as the Harmony conversation by leleka-harmony; the same replay validation as the Qwen path, one call per assistant message with its output, no custom tools. Output: a stateful HarmonyDecoder turns the generated tokens into the reasoning / answer / <tool_call> markup that the existing code parses, so tool-name and argument validation, history and the streaming events are reused; gpt-oss always reasons, the end-of-generation markers <|return|> and <|call|> are the terminal tokens, and an open tool call is closed when the generation ends. Safety: the renderer can write its special markers as private-use sentinel characters (and replaces those characters in caller text); the codec turns exactly those into special tokens. Marker-looking text from a client, in messages, tool descriptions or tool output, stays ordinary text. Side effect, deliberate: serde_json preserve_order is now on workspace-wide (the renderer needs the client's property order), so tool-call argument objects keep the model's key order instead of alphabetical; one test expectation changed accordingly. 52 responses tests and the new family tests pass; the 13 other control-plane test failures on Windows predate this change. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
86 lines
2.7 KiB
Rust
86 lines
2.7 KiB
Rust
//! The Harmony chat format of gpt-oss: rendering a conversation to the prompt text the model expects,
|
|
//! and parsing the messages the model generates back into text, reasoning and tool calls.
|
|
//!
|
|
//! The renderer reproduces the chat template stored in the gpt-oss GGUF files (the same template
|
|
//! llama.cpp applies), including its whitespace; `tests/render_oracle.rs` compares it with llama.cpp's
|
|
//! `/apply-template` on a set of conversations. Text is not tokenized here: the special markers
|
|
//! (`<|start|>`, `<|end|>`, ...) are plain text in the rendered prompt, and the caller tokenizes with
|
|
//! special parsing. User-supplied text must be tokenized WITHOUT special parsing (see the tokenizer),
|
|
//! so it cannot forge markers.
|
|
|
|
mod parse;
|
|
mod render;
|
|
|
|
pub use parse::{Event, HarmonyParser, Piece, Special};
|
|
pub use render::{RenderOptions, SENTINELS, render};
|
|
|
|
use serde_json::Value;
|
|
use std::fmt;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum Role {
|
|
System,
|
|
Developer,
|
|
User,
|
|
Assistant,
|
|
Tool,
|
|
}
|
|
|
|
/// A function call made by the assistant.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct ToolCall {
|
|
pub name: String,
|
|
/// The arguments as a JSON value (normally an object).
|
|
pub arguments: Value,
|
|
/// Defaults to `json`.
|
|
pub content_type: Option<String>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Message {
|
|
pub role: Role,
|
|
/// Text of the message. For `Tool` messages: the tool output, rendered as a JSON string.
|
|
pub content: Option<String>,
|
|
/// The assistant's reasoning (analysis channel), rendered only for a tool-call message or a final
|
|
/// assistant message that is not followed by a generation prompt.
|
|
pub thinking: Option<String>,
|
|
/// Only the first call is rendered (the template supports one call per message).
|
|
pub tool_calls: Vec<ToolCall>,
|
|
}
|
|
|
|
impl Message {
|
|
pub fn new(role: Role, content: impl Into<String>) -> Self {
|
|
Self {
|
|
role,
|
|
content: Some(content.into()),
|
|
thinking: None,
|
|
tool_calls: Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A function the model may call.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Tool {
|
|
pub name: String,
|
|
pub description: Option<String>,
|
|
/// JSON Schema of the arguments.
|
|
pub parameters: Value,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub enum HarmonyError {
|
|
/// The conversation cannot be rendered (the template would raise an exception).
|
|
InvalidConversation(String),
|
|
}
|
|
|
|
impl fmt::Display for HarmonyError {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Self::InvalidConversation(what) => write!(f, "invalid conversation: {what}"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for HarmonyError {}
|