Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prompts): Skip rendering prompts if no context and forward as is #530

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}");
}
}
Loading