[GW] Real multi-turn chat: Inkling template + streaming REPL
chat_template renders the plain-text subset of chat_template.jinja (effort
header before first non-system message, role/content_text/end_message tags,
assistant turns closed with content_model_end_sampling, effort-0 model opener);
4 CPU tests. chat::chat_turn tokenizes the rendered prompt and drives the
Lean-proven serve_gen FSM, streaming decoded text; run_chat is an interactive
multi-turn REPL. CLI: vetta-infer chat <dir> [effort] [max_new].
MEASURED live (release, gfx1150): 3-turn Italian chat answers correctly and
keeps context ('La capitale della Francia e Parigi.' / '...del Giappone e
Tokyo.'). GPU test chat_turn_paris green. Workspace -D warnings green.
This commit is contained in:
1 parent
1df8342f96
commit
0a46c4dd7b
6 files changed
+288
No files matched your search
@@ -0,0 +1,132 @@
|
||||
//! Inkling chat template (plain-text subset of `chat_template.jinja`).
|
||||
//!
|
||||
//! Renders OpenAI-shaped messages to the prompt string the tokenizer encodes,
|
||||
//! mirroring the model's Jinja template for the no-tools / no-thinking path:
|
||||
//! a `Thinking effort level` system header emitted before the first non-system
|
||||
//! message, each message wrapped in its role token + `<|content_text|>` …
|
||||
//! `<|end_message|>`, assistant turns closed with `<|content_model_end_sampling|>`,
|
||||
//! and (effort 0) the `<|message_model|><|content_text|>` generation opener.
|
||||
//!
|
||||
//! Special-token ids (tokenizer.json added_tokens): message_user=200000,
|
||||
//! message_model=200001, message_system=200002, content_text=200004,
|
||||
//! content_model_end_sampling=200006 (EOS), end_message=200010.
|
||||
|
||||
/// One conversation turn. `role` is `user` / `assistant` / `system`.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ChatMessage {
|
||||
pub role: String,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
impl ChatMessage {
|
||||
pub fn new(role: &str, content: &str) -> Self {
|
||||
Self { role: role.to_string(), content: content.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
const MSG_USER: &str = "<|message_user|>";
|
||||
const MSG_MODEL: &str = "<|message_model|>";
|
||||
const MSG_SYSTEM: &str = "<|message_system|>";
|
||||
const CONTENT_TEXT: &str = "<|content_text|>";
|
||||
const END_MSG: &str = "<|end_message|>";
|
||||
const END_SAMPLING: &str = "<|content_model_end_sampling|>";
|
||||
|
||||
/// Map a reasoning-effort name to the template's numeric level (effort_map).
|
||||
fn effort_num(effort: &str) -> Result<&'static str, String> {
|
||||
match effort.trim() {
|
||||
"none" => Ok("0"),
|
||||
"minimal" => Ok("0.1"),
|
||||
"low" => Ok("0.2"),
|
||||
"medium" => Ok("0.7"),
|
||||
"high" => Ok("0.9"),
|
||||
"xhigh" | "max" => Ok("0.99"),
|
||||
other => Err(format!("unknown reasoning_effort: {other}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn role_token(role: &str) -> Result<&'static str, String> {
|
||||
match role {
|
||||
"user" => Ok(MSG_USER),
|
||||
"assistant" => Ok(MSG_MODEL),
|
||||
"system" => Ok(MSG_SYSTEM),
|
||||
other => Err(format!("unknown message role: {other}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Render the prompt for `history` plus the generation opener for the next
|
||||
/// assistant reply. `effort` selects the thinking level; `"none"` (0) makes the
|
||||
/// model answer directly via the `<|message_model|><|content_text|>` opener.
|
||||
pub fn render_prompt(history: &[ChatMessage], effort: &str) -> Result<String, String> {
|
||||
let num = effort_num(effort)?;
|
||||
let header = format!("{MSG_SYSTEM}{CONTENT_TEXT}Thinking effort level: {num}{END_MSG}");
|
||||
let mut s = String::new();
|
||||
let mut effort_emitted = false;
|
||||
for m in history {
|
||||
let rtok = role_token(&m.role)?;
|
||||
if !effort_emitted && m.role != "system" {
|
||||
s.push_str(&header);
|
||||
effort_emitted = true;
|
||||
}
|
||||
s.push_str(rtok);
|
||||
s.push_str(CONTENT_TEXT);
|
||||
s.push_str(&m.content);
|
||||
s.push_str(END_MSG);
|
||||
if m.role == "assistant" {
|
||||
s.push_str(END_SAMPLING);
|
||||
}
|
||||
}
|
||||
if !effort_emitted {
|
||||
s.push_str(&header);
|
||||
}
|
||||
// Generation opener (effort 0, no tools): the model opens content_text.
|
||||
s.push_str(MSG_MODEL);
|
||||
s.push_str(CONTENT_TEXT);
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn single_user_turn_exact() {
|
||||
let h = [ChatMessage::new("user", "Ciao")];
|
||||
let p = render_prompt(&h, "none").unwrap();
|
||||
assert_eq!(
|
||||
p,
|
||||
"<|message_system|><|content_text|>Thinking effort level: 0<|end_message|>\
|
||||
<|message_user|><|content_text|>Ciao<|end_message|>\
|
||||
<|message_model|><|content_text|>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_turn_history_closes_assistant_with_end_sampling() {
|
||||
let h = [
|
||||
ChatMessage::new("user", "Hi"),
|
||||
ChatMessage::new("assistant", "Hello!"),
|
||||
ChatMessage::new("user", "How are you?"),
|
||||
];
|
||||
let p = render_prompt(&h, "none").unwrap();
|
||||
assert!(p.contains("<|message_model|><|content_text|>Hello!<|end_message|><|content_model_end_sampling|>"));
|
||||
assert!(p.ends_with("<|message_user|><|content_text|>How are you?<|end_message|><|message_model|><|content_text|>"));
|
||||
// Exactly one effort header, before the first user message.
|
||||
assert_eq!(p.matches("Thinking effort level: 0").count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn system_message_precedes_effort_header() {
|
||||
let h = [ChatMessage::new("system", "Be brief."), ChatMessage::new("user", "Go")];
|
||||
let p = render_prompt(&h, "none").unwrap();
|
||||
let sys = p.find("<|message_system|><|content_text|>Be brief.").unwrap();
|
||||
let eff = p.find("Thinking effort level: 0").unwrap();
|
||||
assert!(sys < eff, "system message renders before the effort header");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effort_levels_map() {
|
||||
assert!(render_prompt(&[], "high").unwrap().contains("Thinking effort level: 0.9"));
|
||||
assert!(render_prompt(&[], "none").unwrap().contains("Thinking effort level: 0"));
|
||||
assert!(render_prompt(&[], "bogus").is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user