1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
use crate::schemas::{messages::Message, prompt::PromptValue};
use super::{FormatPrompter, PromptArgs, PromptError, PromptFromatter};
#[derive(Clone)]
pub enum TemplateFormat {
FString,
Jinja2,
}
#[derive(Clone)]
pub struct PromptTemplate {
template: String,
variables: Vec<String>,
format: TemplateFormat,
}
impl PromptTemplate {
pub fn new(template: String, variables: Vec<String>, format: TemplateFormat) -> Self {
Self {
template,
variables,
format,
}
}
}
//PromptTemplate will be default transformed to an Human Input when used as FromatPrompter
impl FormatPrompter for PromptTemplate {
fn format_prompt(&self, input_variables: PromptArgs) -> Result<PromptValue, PromptError> {
let messages = vec![Message::new_human_message(self.format(input_variables)?)];
Ok(PromptValue::from_messages(messages))
}
fn get_input_variables(&self) -> Vec<String> {
self.variables.clone()
}
}
impl PromptFromatter for PromptTemplate {
fn template(&self) -> String {
self.template.clone()
}
fn variables(&self) -> Vec<String> {
self.variables.clone()
}
fn format(&self, input_variables: PromptArgs) -> Result<String, PromptError> {
let mut prompt = self.template();
// check if all variables are in the input variables
for key in self.variables() {
if !input_variables.contains_key(key.as_str()) {
return Err(PromptError::MissingVariable(key));
}
}
for (key, value) in input_variables {
let key = match self.format {
TemplateFormat::FString => format!("{{{}}}", key),
TemplateFormat::Jinja2 => format!("{{{{{}}}}}", key),
};
let value_str = match &value {
serde_json::Value::String(s) => s.clone(),
_ => value.to_string(),
};
prompt = prompt.replace(&key, &value_str);
}
log::debug!("Formatted prompt: {}", prompt);
Ok(prompt)
}
}
/// `prompt_args!` is a utility macro used for creating a `std::collections::HashMap<String, serde_json::Value>`.
/// This HashMap can then be passed as arguments to a function or method.
///
/// # Usage
/// In this macro, the keys are `&str` and values are arbitrary types that get serialized into `serde_json::Value`:
/// ```rust,ignore
/// prompt_args! {
/// "input" => "Who is the writer of 20,000 Leagues Under the Sea, and what is my name?",
/// "history" => vec![
/// Message::new_human_message("My name is: Luis"),
/// Message::new_ai_message("Hi Luis"),
/// ],
/// }
/// ```
///
/// # Arguments
/// * `key` - A `&str` that will be used as the key in the resulting HashMap.<br>
/// * `value` - An arbitrary type that will be serialized into `serde_json::Value` and associated with the corresponding key.
///
/// The precise keys and values are dependent on your specific use case. In this example, "input" and "history" are keys,
/// and
#[macro_export]
macro_rules! prompt_args {
( $($key:expr => $value:expr),* $(,)? ) => {
{
#[allow(unused_mut)]
let mut args = std::collections::HashMap::<String, serde_json::Value>::new();
$(
// Convert the value to serde_json::Value before inserting
args.insert($key.to_string(), serde_json::json!($value));
)*
args
}
};
}
/// `template_fstring` is a utility macro that creates a new `PromptTemplate` with FString as the template format.
///
/// # Usage
/// The macro is called with a template string and a list of variables that exist in the template. For example:
/// ```rust,ignore
/// template_fstring!(
/// "Hello {name}",
/// "name"
/// )
/// ```
/// This returns a `PromptTemplate` object that contains the string "Hello {name}" as the template and ["name"] as the variables, with TemplateFormat set to FString.
#[macro_export]
macro_rules! template_fstring {
($template:expr, $($var:expr),* $(,)?) => {
$crate::prompt::PromptTemplate::new(
$template.to_string(),
vec![$($var.to_string()),*],
$crate::prompt::TemplateFormat::FString,
)
};
}
/// `template_jinja2` is a utility macro that creates a new `PromptTemplate` with Jinja2 as the template format.
///
/// # Usage
/// The macro is called with a template string and a list of variables that exist in the template. For example:
/// ```rust,ignore
/// template_jinja2!(
/// "Hello {{ name }}",
/// "name"
/// )
/// ```
/// This returns a `PromptTemplate` object that contains the string "Hello {{ name }}" as the template and ["name"] as the variables, with TemplateFormat set to Jinja2.
#[macro_export]
macro_rules! template_jinja2 {
($template:expr, $($var:expr),* $(,)?) => {
$crate::prompt::PromptTemplate::new(
$template.to_string(),
vec![$($var.to_string()),*],
$crate::prompt::TemplateFormat::Jinja2,
)
};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prompt_args;
#[test]
fn should_format_jinja2_template() {
let template = PromptTemplate::new(
"Hello {{name}}!".to_string(),
vec!["name".to_string()],
TemplateFormat::Jinja2,
);
let input_variables = prompt_args! {};
let result = template.format(input_variables);
assert!(result.is_err());
let input_variables = prompt_args! {
"name" => "world",
};
let result = template.format(input_variables);
println!("{:?}", result);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Hello world!");
}
#[test]
fn should_format_fstring_template() {
let template = PromptTemplate::new(
"Hello {name}!".to_string(),
vec!["name".to_string()],
TemplateFormat::FString,
);
let input_variables = prompt_args! {};
let result = template.format(input_variables);
assert!(result.is_err());
let input_variables = prompt_args! {
"name" => "world",
};
let result = template.format(input_variables);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "Hello world!");
}
#[test]
fn should_prompt_macro_work() {
let args = prompt_args! {};
assert!(args.is_empty());
let args = prompt_args! {
"name" => "world",
};
assert_eq!(args.len(), 1);
assert_eq!(args.get("name").unwrap(), &"world");
let args = prompt_args! {
"name" => "world",
"age" => "18",
};
assert_eq!(args.len(), 2);
assert_eq!(args.get("name").unwrap(), &"world");
assert_eq!(args.get("age").unwrap(), &"18");
}
#[test]
fn test_chat_template_macros() {
// Creating an FString chat template
let fstring_template = template_fstring!(
"FString Chat: {user} says {message} {test}",
"user",
"message",
"test"
);
// Creating a Jinja2 chat template
let jinja2_template =
template_jinja2!("Jinja2 Chat: {{user}} says {{message}}", "user", "message");
// Define input variables for the templates
let input_variables_fstring = prompt_args! {
"user" => "Alice",
"message" => "Hello, Bob!",
"test"=>"test2"
};
let input_variables_jinja2 = prompt_args! {
"user" => "Bob",
"message" => "Hi, Alice!",
};
// Format the FString chat template
let formatted_fstring = fstring_template.format(input_variables_fstring).unwrap();
assert_eq!(
formatted_fstring,
"FString Chat: Alice says Hello, Bob! test2"
);
// Format the Jinja2 chat template
let formatted_jinja2 = jinja2_template.format(input_variables_jinja2).unwrap();
assert_eq!(formatted_jinja2, "Jinja2 Chat: Bob says Hi, Alice!");
}
}