Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loop_controls feature to minijinja to handle {% break %} #2998

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ngrok = { version = "0.13.1", features = ["axum"], optional = true }
init-tracing-opentelemetry = { version = "0.14.1", features = [
"opentelemetry-otlp",
] }
minijinja = { workspace = true }
minijinja = { workspace = true, features = ["loop_controls"] }
minijinja-contrib = { workspace = true }
futures-util = "0.3.30"
regex = "1.10.3"
Expand Down
66 changes: 66 additions & 0 deletions router/src/infer/chat_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,72 @@ mod tests {
);
}

#[test]
fn test_chat_template_loop_controls() {
// some chat templates as e.g. CohereForAI/c4ai-command-r7b-12-202 contain `break`
// statements in their chat templates, so the feature `loop_controls` has been included
// in `minijinja`
let env = Environment::new();

let source = r#"
{% set user_count = 0 %}
{% for message in messages %}
{% if message['role'] == 'user' %}
{{'### User:\n' + message['content']+'\n\n'}}
{% set user_count = user_count + 1 %}
{% if user_count >= 2 %}
{% break %}
{% endif %}
{% elif message['role'] == 'assistant' %}
{{'### Assistant:\n' + message['content']}}
{% endif %}
{% endfor %}
{% if add_generation_prompt %}
{{ '### Assistant:\n' }}
{% endif %}"#;

// trim all the whitespace
let source = source
.lines()
.map(|line| line.trim())
.collect::<Vec<&str>>()
.join("");

let tmpl = env.template_from_str(&source);

let chat_template_inputs = ChatTemplateInputs {
messages: vec![
TextMessage {
role: "user".to_string(),
content: "Hi!".to_string(),
},
TextMessage {
role: "assistant".to_string(),
content: "Hello how can I help?".to_string(),
},
TextMessage {
role: "user".to_string(),
content: "What is Deep Learning?".to_string(),
},
TextMessage {
role: "assistant".to_string(),
content: "magic!".to_string(),
},
],
bos_token: Some("[BOS]"),
eos_token: Some("[EOS]"),
add_generation_prompt: true,
..Default::default()
};

let result = tmpl.unwrap().render(chat_template_inputs).unwrap();

assert_eq!(
result,
"### User:\nHi!\n\n### Assistant:\nHello how can I help?### User:\nWhat is Deep Learning?\n\n### Assistant:\n"
);
}

#[test]
fn test_chat_template_invalid_with_raise() {
let mut env = Environment::new();
Expand Down
Loading