72 lines
2.3 KiB
Rust
72 lines
2.3 KiB
Rust
//! The native Jinja renderer against the Python server's `ChatTemplate` (jinja2) on generated conversations.
|
|
//!
|
|
//! `tests/fixtures/offline/chat_template_oracle.json` comes from `scripts/offline/gen_goldens.py chat_template_oracle`:
|
|
//! 90 renders (every role incl. developer and tool, list content with image/video parts, assistant `tool_calls`
|
|
//! with mapping arguments, tools, `enable_thinking` / `reasoning_effort` / `preserve_thinking`, no generation
|
|
//! prompt) and 25 conversations the template refuses, with the exact `raise_exception` message.
|
|
//! `scripts/offline/diff_chat_template.py` runs the same generator live over thousands of cases.
|
|
use serde::Deserialize;
|
|
use leleka_control_plane::prompt::{ChatInput, Json, pinned};
|
|
|
|
#[derive(Deserialize)]
|
|
struct Case {
|
|
messages: Vec<Json>,
|
|
tools: Option<Json>,
|
|
add_generation_prompt: bool,
|
|
kwargs: Json,
|
|
rendered: Option<String>,
|
|
error: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Golden {
|
|
ok: Vec<Case>,
|
|
raised: Vec<Case>,
|
|
}
|
|
|
|
fn render(case: &Case) -> Result<String, String> {
|
|
let tools = match &case.tools {
|
|
Some(Json::Array(items)) => Some(items.as_slice()),
|
|
_ => None, // null, or a mapping the template ignores
|
|
};
|
|
let kwargs = match &case.kwargs {
|
|
Json::Object(fields) => fields
|
|
.iter()
|
|
.map(|(k, v)| (k.as_str(), v.clone()))
|
|
.collect(),
|
|
_ => Vec::new(),
|
|
};
|
|
pinned().render(&ChatInput {
|
|
messages: &case.messages,
|
|
tools,
|
|
add_generation_prompt: case.add_generation_prompt,
|
|
kwargs,
|
|
})
|
|
}
|
|
|
|
fn golden() -> Golden {
|
|
serde_json::from_str(include_str!("fixtures/offline/chat_template_oracle.json")).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn renders_are_byte_identical_to_jinja2() {
|
|
let golden = golden();
|
|
assert!(golden.ok.len() >= 90);
|
|
for (at, case) in golden.ok.iter().enumerate() {
|
|
assert_eq!(
|
|
render(case),
|
|
Ok(case.rendered.clone().unwrap()),
|
|
"case {at}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn refusals_carry_the_templates_own_message() {
|
|
let golden = golden();
|
|
assert!(golden.raised.len() >= 25);
|
|
for (at, case) in golden.raised.iter().enumerate() {
|
|
assert_eq!(render(case), Err(case.error.clone().unwrap()), "case {at}");
|
|
}
|
|
}
|