diff --git a/swiftide-agents/src/system_prompt.rs b/swiftide-agents/src/system_prompt.rs index 738f5ccf..905b8d29 100644 --- a/swiftide-agents/src/system_prompt.rs +++ b/swiftide-agents/src/system_prompt.rs @@ -28,7 +28,7 @@ pub struct SystemPrompt { /// The template to use for the system prompt #[builder(default = default_prompt_template())] - template: Template, + template: Template<'static>, } impl SystemPrompt { @@ -76,7 +76,7 @@ impl SystemPromptBuilder { } } -fn default_prompt_template() -> Template { +fn default_prompt_template() -> Template<'static> { include_str!("system_prompt_template.md").into() } diff --git a/swiftide-core/src/prompt.rs b/swiftide-core/src/prompt.rs index 324b99c9..5370ea34 100644 --- a/swiftide-core/src/prompt.rs +++ b/swiftide-core/src/prompt.rs @@ -31,18 +31,16 @@ //! assert_eq!(prompt.render().await.unwrap(), "hello swiftide"); //! # } //! ``` -use anyhow::{Context as _, Result}; -use lazy_static::lazy_static; -use tera::Tera; -use tokio::sync::RwLock; -use uuid::Uuid; + +use anyhow::Result; use crate::{node::Node, template::Template}; /// A Prompt can be used with large language models to prompt. #[derive(Clone, Debug)] pub struct Prompt { - template: Template, + // Should always have an owned template + template: Template<'static>, context: Option, } @@ -50,7 +48,7 @@ pub struct Prompt { since = "0.16.0", note = "Use `Template` instead; they serve a more general purpose" )] -pub type PromptTemplate = Template; +pub type PromptTemplate<'inner> = Template<'inner>; impl Prompt { /// Adds an `ingestion::Node` to the context of the Prompt @@ -95,7 +93,7 @@ impl Prompt { impl From<&'static str> for Prompt { fn from(prompt: &'static str) -> Self { Prompt { - template: Template::Static(prompt), + template: Template::OneOff(prompt.into()).to_owned(), context: None, } } @@ -104,14 +102,14 @@ impl From<&'static str> for Prompt { impl From for Prompt { fn from(prompt: String) -> Self { Prompt { - template: Template::String(prompt), + template: Template::OneOff(prompt.into()).to_owned(), context: None, } } } -impl From<&Template> for Prompt { - fn from(template: &Template) -> Self { +impl From<&Template<'static>> for Prompt { + fn from(template: &Template<'static>) -> Self { Prompt { template: template.clone(), context: None, @@ -119,8 +117,19 @@ impl From<&Template> for Prompt { } } +impl From> for Prompt { + fn from(template: Template<'static>) -> Self { + Prompt { + template, + context: None, + } + } +} + #[cfg(test)] mod test { + use tera::Tera; + use super::*; #[tokio::test] @@ -160,9 +169,8 @@ mod test { Template::extend(&custom_tera).await.unwrap(); - let prompt = Template::from_compiled_template_name("hello") - .to_prompt() - .with_context_value("world", "swiftide"); + let template = Template::from_compiled_template_name("hello"); + let prompt = template.to_prompt().with_context_value("world", "swiftide"); assert_eq!(prompt.render().await.unwrap(), "hello swiftide"); } diff --git a/swiftide-core/src/template.rs b/swiftide-core/src/template.rs index 05d61e55..bbeac6a1 100644 --- a/swiftide-core/src/template.rs +++ b/swiftide-core/src/template.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use anyhow::{Context as _, Result}; use tokio::sync::RwLock; @@ -25,20 +27,24 @@ lazy_static! { } /// A `Template` defines a template for a prompt #[derive(Clone, Debug)] -pub enum Template { - CompiledTemplate(String), - String(String), - Static(&'static str), +pub enum Template<'inner> { + /// A reference to a compiled template stored in the template repository + /// These can also be created on the fly with `Template::try_compiled_from_str`, + /// or retrieved at runtime with `Template::from_compiled_template_name` + CompiledTemplate(Cow<'inner, str>), + + /// A one-off template that is not stored in the repository + OneOff(Cow<'inner, str>), } -impl Template { +impl<'inner> Template<'inner> { /// Creates a reference to a template already stored in the repository - pub fn from_compiled_template_name(name: impl Into) -> Template { + pub fn from_compiled_template_name(name: impl Into>) -> Template<'inner> { Template::CompiledTemplate(name.into()) } - pub fn from_string(template: impl Into) -> Template { - Template::String(template.into()) + pub fn from_string(template: impl Into>) -> Template<'inner> { + Template::OneOff(template.into()) } /// Extends the prompt repository with a custom [`tera::Tera`] instance. @@ -69,13 +75,13 @@ impl Template { /// Errors if the template fails to compile pub async fn try_compiled_from_str( template: impl AsRef + Send + 'static, - ) -> Result