pub struct StringTemplate(/* private fields */);
Expand description
A template for a prompt. This is a string that can be formatted with a set of parameters.
§Examples
Using the default key
use llm_chain::prompt::StringTemplate;
use llm_chain::Parameters;
let template: StringTemplate = "Hello {{ text }}!".into();
let parameters: Parameters = "World".into();
assert_eq!(template.format(¶meters).unwrap(), "Hello World!");
Using a custom key
use llm_chain::prompt::StringTemplate;
use llm_chain::Parameters;
let template: StringTemplate = "Hello {{ name }}!".into();
let parameters: Parameters = vec![("name", "World")].into();
assert_eq!(template.format(¶meters).unwrap(), "Hello World!");
§Tera
use llm_chain::prompt::StringTemplate;
use llm_chain::Parameters;
let template: StringTemplate = StringTemplate::tera("Hello {{name}}!");
let parameters: Parameters = vec![("name", "World")].into();
assert_eq!(template.format(¶meters).unwrap(), "Hello World!");
Implementations§
Source§impl StringTemplate
impl StringTemplate
Sourcepub fn format(
&self,
parameters: &Parameters,
) -> Result<String, StringTemplateError>
pub fn format( &self, parameters: &Parameters, ) -> Result<String, StringTemplateError>
Format the template with the given parameters.
Sourcepub fn static_string<K: Into<String>>(template: K) -> StringTemplate
pub fn static_string<K: Into<String>>(template: K) -> StringTemplate
Creates a non-dynmamic prompt template, useful for untrusted inputs.
Sourcepub fn tera<K: Into<String>>(template: K) -> StringTemplate
pub fn tera<K: Into<String>>(template: K) -> StringTemplate
Creates a prompt template that uses the Tera templating engine.
This is only available if the tera
feature is enabled, which it is by default.
§Examples
use llm_chain::prompt::StringTemplate;
use llm_chain::Parameters;
let template = StringTemplate::tera("Hello {{name}}!");
let parameters: Parameters = vec![("name", "World")].into();
assert_eq!(template.format(¶meters).unwrap(), "Hello World!");
Sourcepub fn from_file<K: AsRef<Path>>(path: K) -> Result<StringTemplate, Error>
pub fn from_file<K: AsRef<Path>>(path: K) -> Result<StringTemplate, Error>
Creates a prompt template from a file. The file should be a text file containing the template as a tera template.
§Examples
use llm_chain::prompt::StringTemplate;
let template = StringTemplate::from_file("template.txt").unwrap();
Sourcepub fn combine(parts: Vec<StringTemplate>) -> StringTemplate
pub fn combine(parts: Vec<StringTemplate>) -> StringTemplate
Combines two prompt templates into one. This is useful for creating a prompt template from multiple sources.
§Examples
use llm_chain::prompt::StringTemplate;
use llm_chain::Parameters;
let template1 = StringTemplate::tera("Hello {{name}}");
let template2 = StringTemplate::tera("!");
let template3 = StringTemplate::combine(vec![template1, template2]);
let parameters: Parameters = vec![("name", "World")].into();
assert_eq!(template3.format(¶meters).unwrap(), "Hello World!");
Trait Implementations§
Source§impl Clone for StringTemplate
impl Clone for StringTemplate
Source§fn clone(&self) -> StringTemplate
fn clone(&self) -> StringTemplate
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for StringTemplate
impl Debug for StringTemplate
Source§impl<'de> Deserialize<'de> for StringTemplate
impl<'de> Deserialize<'de> for StringTemplate
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for StringTemplate
impl Display for StringTemplate
Source§impl From<&str> for StringTemplate
impl From<&str> for StringTemplate
Auto Trait Implementations§
impl Freeze for StringTemplate
impl RefUnwindSafe for StringTemplate
impl Send for StringTemplate
impl Sync for StringTemplate
impl Unpin for StringTemplate
impl UnwindSafe for StringTemplate
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more