Struct llm_chain::prompt::StringTemplate
source · pub struct StringTemplate(_);
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 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