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

feat(prompts): add prompts as first class citizens #145

Merged
merged 14 commits into from
Jul 12, 2024
140 changes: 137 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions swiftide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ strum = "0.26.2"
strum_macros = "0.26.4"
num_cpus = "1.16.0"
pin-project-lite = "0.2"
tera = { version = "1", default-features = false }
lazy_static = { version = "1.5.0" }
uuid = { version = "1.10", features = ["v4"] }

# Integrations
async-openai = { version = "0.23.2", optional = true }
Expand Down
10 changes: 5 additions & 5 deletions swiftide/src/integrations/aws_bedrock/simple_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SimplePrompt;
use crate::{prompt::Prompt, SimplePrompt};
use anyhow::Result;
use async_trait::async_trait;
use aws_sdk_bedrockruntime::primitives::Blob;
Expand All @@ -8,10 +8,10 @@ use super::AwsBedrock;
#[async_trait]
impl SimplePrompt for AwsBedrock {
#[tracing::instrument(skip_all, err)]
async fn prompt(&self, prompt: &str) -> Result<String> {
async fn prompt(&self, prompt: &Prompt) -> Result<String> {
let blob = self
.model_family
.build_request_to_bytes(prompt, &self.model_config)
.build_request_to_bytes(prompt.render().await?, &self.model_config)
.map(Blob::new)?;

let response_bytes = self.client.prompt_u8(&self.model_id, blob).await?;
Expand Down Expand Up @@ -53,7 +53,7 @@ mod test {
.build()
.unwrap();

let response = bedrock.prompt("Hello").await.unwrap();
let response = bedrock.prompt(&"Hello".into()).await.unwrap();

timonv marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(response, "Hello, world!");
}
Expand Down Expand Up @@ -84,7 +84,7 @@ mod test {
.test_client(bedrock_mock)
.build()
.unwrap();
let response = bedrock.prompt("Hello").await.unwrap();
let response = bedrock.prompt(&"Hello".into()).await.unwrap();
assert_eq!(response, "Hello, world!");
}
}
6 changes: 3 additions & 3 deletions swiftide/src/integrations/openai/simple_prompt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module provides an implementation of the `SimplePrompt` trait for the `OpenAI` struct.
//! It defines an asynchronous function to interact with the `OpenAI` API, allowing prompt processing
//! and generating responses as part of the Swiftide system.
use crate::SimplePrompt;
use crate::{prompt::Prompt, SimplePrompt};
use async_openai::types::{ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs};
use async_trait::async_trait;

Expand All @@ -25,7 +25,7 @@ impl SimplePrompt for OpenAI {
/// - Returns an error if the request to the OpenAI API fails.
/// - Returns an error if the response does not contain the expected content.
#[tracing::instrument(skip_all, err)]
async fn prompt(&self, prompt: &str) -> Result<String> {
async fn prompt(&self, prompt: &Prompt) -> Result<String> {
// Retrieve the model from the default options, returning an error if not set.
let model = self
.default_options
Expand All @@ -37,7 +37,7 @@ impl SimplePrompt for OpenAI {
let request = CreateChatCompletionRequestArgs::default()
.model(model)
.messages(vec![ChatCompletionRequestUserMessageArgs::default()
.content(prompt)
.content(prompt.render().await?)
.build()?
.into()])
.build()?;
Expand Down
1 change: 1 addition & 0 deletions swiftide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod indexing;
pub mod integrations;
pub mod loaders;
pub mod persist;
pub mod prompt;
pub mod traits;
pub mod transformers;
pub mod type_aliases;
Expand Down
Loading
Loading