use std::{
collections::BTreeMap,
path::Path,
};
use bunsen::{
errors::{
BunsenError,
BunsenResult,
sys_at,
},
kits::support::model_config::ChatTemplateSource,
};
use minijinja::Environment;
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct ChatMessage {
pub role: String,
pub content: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
}
impl ChatMessage {
pub fn new(
role: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
role: role.into(),
content: content.into(),
reasoning_content: None,
}
}
pub fn system(content: impl Into<String>) -> Self {
Self::new("system", content)
}
pub fn user(content: impl Into<String>) -> Self {
Self::new("user", content)
}
pub fn assistant(content: impl Into<String>) -> Self {
Self::new("assistant", content)
}
pub fn with_reasoning(
mut self,
reasoning: impl Into<String>,
) -> Self {
self.reasoning_content = Some(reasoning.into());
self
}
}
const NAME: &str = "chat";
#[derive(Debug)]
pub struct ChatTemplate {
env: Environment<'static>,
special_tokens: BTreeMap<String, String>,
}
fn special_tokens(config: &serde_json::Value) -> BTreeMap<String, String> {
["bos_token", "eos_token", "pad_token", "unk_token"]
.into_iter()
.filter_map(|key| {
let value = config.get(key)?;
let content = value
.as_str()
.or_else(|| value.get("content").and_then(serde_json::Value::as_str))?;
Some((key.to_string(), content.to_string()))
})
.collect()
}
impl ChatTemplate {
pub fn from_file(tokenizer_config_json: &Path) -> BunsenResult<Self> {
let json = std::fs::read_to_string(tokenizer_config_json)
.map_err(sys_at("read", tokenizer_config_json))?;
Self::from_tokenizer_config(&json).map_err(|e| e.context(tokenizer_config_json.display()))
}
pub fn from_jinja_file(
template: &Path,
tokenizer_config_json: &Path,
) -> BunsenResult<Self> {
let source = std::fs::read_to_string(template).map_err(sys_at("read", template))?;
let json = std::fs::read_to_string(tokenizer_config_json)
.map_err(sys_at("read", tokenizer_config_json))?;
let value = Self::parse_config(&json)?;
Ok(Self::new(&source)
.map_err(|e| e.context(template.display()))?
.with_special_tokens(special_tokens(&value)))
}
pub fn from_source<'a>(
source: &ChatTemplateSource,
file: impl Fn(&str) -> Option<&'a Path>,
tokenizer_config_json: &Path,
) -> BunsenResult<Self> {
match source {
ChatTemplateSource::File { file: key } => {
let path = file(key).ok_or_else(|| {
BunsenError::invalid_resource(format!(
"a chat template in {key:?}, which the checkpoint does not have"
))
})?;
if path == tokenizer_config_json {
Self::from_file(path)
} else {
Self::from_jinja_file(path, tokenizer_config_json)
}
}
ChatTemplateSource::Inline(source) => {
let json = std::fs::read_to_string(tokenizer_config_json)
.map_err(sys_at("read", tokenizer_config_json))?;
Ok(Self::new(source)?
.with_special_tokens(special_tokens(&Self::parse_config(&json)?)))
}
}
}
fn parse_config(json: &str) -> BunsenResult<serde_json::Value> {
serde_json::from_str(json)
.map_err(|e| BunsenError::invalid_resource("a tokenizer_config.json").with_cause(e))
}
pub fn from_tokenizer_config(json: &str) -> BunsenResult<Self> {
let value = Self::parse_config(json)?;
let source = value
.get("chat_template")
.and_then(serde_json::Value::as_str)
.ok_or_else(|| {
BunsenError::invalid_resource("a tokenizer_config.json without a chat_template")
})?;
Ok(Self::new(source)?.with_special_tokens(special_tokens(&value)))
}
pub fn with_special_tokens(
self,
special_tokens: BTreeMap<String, String>,
) -> Self {
Self {
special_tokens,
..self
}
}
pub fn new(source: &str) -> BunsenResult<Self> {
let mut env = Environment::new();
env.set_trim_blocks(true);
env.set_lstrip_blocks(true);
env.set_unknown_method_callback(minijinja_contrib::pycompat::unknown_method_callback);
env.add_function(
"raise_exception",
|message: String| -> Result<String, minijinja::Error> {
Err(minijinja::Error::new(
minijinja::ErrorKind::InvalidOperation,
message,
))
},
);
env.add_template_owned(NAME, source.to_string())
.map_err(|e| BunsenError::invalid_resource("a chat template").with_cause(e))?;
Ok(Self {
env,
special_tokens: BTreeMap::new(),
})
}
pub fn render(
&self,
messages: &[ChatMessage],
add_generation_prompt: bool,
enable_thinking: Option<bool>,
) -> BunsenResult<String> {
let template = self.env.get_template(NAME).expect("added at construction");
let base = minijinja::context! {
messages,
add_generation_prompt,
..minijinja::Value::from_serialize(&self.special_tokens)
};
let context = match enable_thinking {
Some(enable_thinking) => minijinja::context! { enable_thinking, ..base },
None => base,
};
template
.render(context)
.map_err(|e| BunsenError::invalid_resource("a chat template's render").with_cause(e))
}
}
#[cfg(test)]
mod tests {
use super::*;
const TOY: &str = r#"{%- for message in messages %}
{%- if message.role == "assistant" and '</think>' in message.content %}
{{- '<|im_start|>assistant\n' + message.content.split('</think>')[-1].lstrip('\n') + '<|im_end|>\n' }}
{%- else %}
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- endif %}
{%- endif %}"#;
#[test]
fn test_render() {
let template = ChatTemplate::new(TOY).unwrap();
let messages = [
ChatMessage::user("hi"),
ChatMessage::assistant("<think>\nhm\n</think>\n\nhello"),
ChatMessage::user("again"),
];
let text = template.render(&messages, true, None).unwrap();
assert_eq!(
text,
"<|im_start|>user\nhi<|im_end|>\n<|im_start|>assistant\nhello<|im_end|>\n\
<|im_start|>user\nagain<|im_end|>\n<|im_start|>assistant\n"
);
let off = template.render(&messages[..1], true, Some(false)).unwrap();
assert!(off.ends_with("<|im_start|>assistant\n<think>\n\n</think>\n\n"));
let on = template.render(&messages[..1], true, Some(true)).unwrap();
assert!(on.ends_with("<|im_start|>assistant\n"));
}
#[test]
fn test_from_tokenizer_config() {
let json = serde_json::json!({ "chat_template": TOY }).to_string();
ChatTemplate::from_tokenizer_config(&json).unwrap();
assert!(ChatTemplate::from_tokenizer_config("{}").is_err());
}
const GEMMA2: &str = "{{ bos_token }}{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '<start_of_turn>' + role + '\n' + message['content'] | trim + '<end_of_turn>\n' }}{% endfor %}{% if add_generation_prompt %}{{'<start_of_turn>model\n'}}{% endif %}";
#[test]
fn test_special_tokens_and_raise_exception() {
let json = serde_json::json!({
"chat_template": GEMMA2,
"bos_token": "<bos>",
"eos_token": {"content": "<eos>", "special": true},
})
.to_string();
let template = ChatTemplate::from_tokenizer_config(&json).unwrap();
let text = template
.render(
&[ChatMessage::user(" hi "), ChatMessage::assistant("hello")],
true,
None,
)
.unwrap();
assert_eq!(
text,
"<bos><start_of_turn>user\nhi<end_of_turn>\n<start_of_turn>model\nhello<end_of_turn>\n\
<start_of_turn>model\n"
);
let err = template
.render(&[ChatMessage::system("be brief")], true, None)
.unwrap_err();
assert!(
format!("{err:?}").contains("System role not supported"),
"{err:?}"
);
}
}