Skip to content

Commit

Permalink
tools for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeadie committed Feb 4, 2025
1 parent dc08077 commit a17f391
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mistralrs-core/src/pipeline/chat_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use anyhow::Result;
use either::Either;
use indexmap::IndexMap;
use itertools::Itertools;
use minijinja::{context, value::Kwargs, Environment, Error, ErrorKind, Value};
use minijinja::{context, value::Kwargs, Environment, Error, ErrorKind, Template, Value};
use serde::{Deserialize, Serialize};
use tokenizers::Tokenizer;
use tracing::info;
use tracing::{info, warn};

use crate::{MessageContent, Tool};

Expand Down Expand Up @@ -264,8 +264,10 @@ pub fn apply_chat_template_to(
};

env.add_template("chat_template", &template)?;

env.add_function("raise_exception", raise_exception);
env.add_filter("tojson", tojson);

let tmpl = env.get_template("chat_template").unwrap();

let date = chrono::Utc::now();
Expand All @@ -281,6 +283,9 @@ pub fn apply_chat_template_to(
date_string => date_string,
})?)
} else {
if !tools_are_supported(&tmpl) {
warn!("tools were provided to a 'chat_template' that does not support them. Tools will be ignored...");
}
Ok(tmpl.render(context! {
messages => new_messages,
add_generation_prompt => add_generation_prompt,
Expand All @@ -292,3 +297,7 @@ pub fn apply_chat_template_to(
})?)
}
}

fn tools_are_supported(t: &Template) -> bool {
t.undeclared_variables(true).contains("tools")
}

0 comments on commit a17f391

Please sign in to comment.