use anyhow::Result;
use rlx_cli::{ChatMessage, auto_chat_template};
use rlx_qwen35::{decode_ids_auto, encode_prompt_auto};
use std::path::Path;
pub fn encode_chat_prompt_auto(
weights: &Path,
tokenizer: Option<&Path>,
system: Option<&str>,
user: &str,
use_chat_template: bool,
) -> Result<Vec<u32>> {
let text = if use_chat_template {
match auto_chat_template(weights) {
Ok(tmpl) => {
let mut msgs = Vec::new();
if let Some(s) = system {
msgs.push(ChatMessage::system(s.to_string()));
}
msgs.push(ChatMessage::user(user.to_string()));
tmpl.render(&msgs, true)?
}
Err(_) => user.to_string(),
}
} else {
user.to_string()
};
let mut ids = if use_chat_template && text.starts_with("<bos>") {
let body = text.strip_prefix("<bos>").unwrap_or(&text);
let mut v = encode_prompt_auto(weights, tokenizer, body)?;
if v.first() != Some(&2) {
v.insert(0, 2);
}
v
} else {
encode_prompt_auto(weights, tokenizer, &text)?
};
if ids.len() >= 2 && ids[0] == 107 && ids[1] == 2 {
ids.remove(0);
}
Ok(ids)
}
pub fn decode_token_auto(weights: &Path, tokenizer: Option<&Path>, tok: u32) -> Result<String> {
decode_ids_auto(weights, tokenizer, &[tok], false)
}