Skip to content

Commit

Permalink
fix(prompts): Skip rendering prompts if no context and forward as is (#…
Browse files Browse the repository at this point in the history
…530)

Fixes an issue if strings suddenly include jinja style values by
mistake. Bonus performance boost.
  • Loading branch information
timonv authored Jan 6, 2025
1 parent cc4899c commit d198bb0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions swiftide-core/src/chat_completion/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
LLM: ChatCompletion + Clone + 'static,
{
fn from(llm: &LLM) -> Self {
Box::new(llm.clone())
Box::new(llm.clone()) as Box<dyn ChatCompletion>
}
}

Expand Down Expand Up @@ -85,7 +85,7 @@ pub trait Tool: Send + Sync + DynClone {
where
Self: Sized + 'a,
{
Box::new(self)
Box::new(self) as Box<dyn Tool>
}
}

Expand Down
17 changes: 16 additions & 1 deletion swiftide-core/src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,22 @@ impl Prompt {

/// Renders a prompt
///
/// If no context is provided, the prompt will be rendered as is.
///
/// # Errors
///
/// See `Template::render`
pub async fn render(&self) -> Result<String> {
if let Some(context) = &self.context {
self.template.render(context).await
} else {
self.template.render(&tera::Context::default()).await
match &self.template {
Template::CompiledTemplate(_) => {
self.template.render(&tera::Context::default()).await
}
Template::String(string) => Ok(string.clone()),
Template::Static(string) => Ok((*string).to_string()),
}
}
}
}
Expand Down Expand Up @@ -215,4 +223,11 @@ mod test {
"hello swiftide"
);
}

#[tokio::test]
async fn test_assume_rendered_unless_context_methods_called() {
let prompt = Prompt::from("hello {{world}}");

assert_eq!(prompt.render().await.unwrap(), "hello {{world}}");
}
}

0 comments on commit d198bb0

Please sign in to comment.