Files
leleka/crates/harmony/src/render.rs
T

59 lines
2.4 KiB
Rust

//! Prompt rendering. Mirrors the gpt-oss chat template (Jinja) statement by statement; the odd
//! whitespace in nested object types is the template's, kept on purpose.
use crate::{HarmonyError, Message, Role, Tool, ToolCall};
use serde_json::{Map, Value};
#[derive(Clone, Debug)]
pub struct RenderOptions {
/// `YYYY-MM-DD`, written into the system message.
pub current_date: String,
/// EXPERIMENTAL: leave the "Current date:" line out of the system message (the model is trained with it; the
/// blank line that follows it stays). For clients that offer the date as a tool, and for stable prompt prefixes.
pub omit_date: bool,
/// `low`, `medium` or `high`.
pub reasoning_effort: String,
/// Replaces "You are ChatGPT, a large language model trained by OpenAI." when set.
pub model_identity: Option<String>,
/// Append `<|start|>assistant` so the model continues as the assistant.
pub add_generation_prompt: bool,
/// Write the special markers as private-use sentinel characters ([`SENTINELS`]) and neutralize those
/// characters in caller text, so a tokenizer can map exactly the template's markers to special tokens.
pub sentinel_markers: bool,
}
// ---- tool definitions ----
/// Indent the template writes before the type of a property inside an object type.
const NESTED_INDENT: &str = " ";
/// Indent before a oneOf variant's default comment (the template's, on its expression line).
const ONE_OF_DEFAULT_INDENT: &str = " ";
// ---- the conversation ----
/// The private-use characters that stand for the format's special tokens in sentinel mode, in the order
/// start, end, message, channel, call, return, constrain.
pub const SENTINELS: [(char, &str); 7] = [
('\u{E000}', "<|start|>"),
('\u{E001}', "<|end|>"),
('\u{E002}', "<|message|>"),
('\u{E003}', "<|channel|>"),
('\u{E004}', "<|call|>"),
('\u{E005}', "<|return|>"),
('\u{E006}', "<|constrain|>"),
];
/// The prompt under construction. Template text goes in through [`Out::lit`], text that came from the
/// caller through [`Out::text`]. In sentinel mode the template's special markers are written as their
/// [`SENTINELS`] and any sentinel character in caller text is replaced, so only the template can
/// produce a marker when the result is tokenized.
struct Out {
s: String,
sentinel: bool,
}
mod render_options;
pub use render_options::*;
mod render_namespace;
pub use render_namespace::*;