-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add Bedrock Cloud Model provider #21092
Draft
5herlocked
wants to merge
13
commits into
zed-industries:main
Choose a base branch
from
5herlocked:feature/bedrock-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
96fde1b
Added Bedrock UI elements, credential storage and a few other things
5herlocked 62f8ce3
Compiles with the appropriate UI fields + saves credentials
5herlocked b146a6d
Starting the transpose to the Bedrock SDK
5herlocked 1240bad
Added Bedrock Error enum that transparently exposes SDK Errors as Bed…
5herlocked c723521
Had Q Dev rewrite most of the model enums based on the AWS Bedrock li…
5herlocked 4fcc8ec
Fixed the names in impl Model
5herlocked 2932c19
removed a whole host of unsupported types
5herlocked fe42049
Merge branch 'main' into feature/bedrock-provider
5herlocked 3ff200a
seemingly migrated to the new way of managing language models
5herlocked 05d7e2a
Removed bedrock from the cloudmodel
5herlocked 5c4a533
Fixed a whole host of mismatched types, Arc<dyn HttpClient> will be t…
5herlocked 97eb677
Helper functions
5herlocked 5db57d5
Trying some additional things
5herlocked File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "bedrock" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
license = "AGPL-3.0-or-later" | ||
|
||
[features] | ||
default = [] | ||
schemars = ["dep:schemars"] | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
path = "src/bedrock.rs" | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
chrono.workspace = true | ||
futures.workspace = true | ||
http_client.workspace = true | ||
schemars = { workspace = true, optional = true } | ||
serde.workspace = true | ||
serde_json.workspace = true | ||
strum.workspace = true | ||
thiserror.workspace = true | ||
util.workspace = true | ||
aws-sdk-bedrockruntime = { workspace = true, features = ["behavior-version-latest"]} | ||
aws-config = {workspace = true, features = ["behavior-version-latest"]} | ||
|
||
[dev-dependencies] | ||
tokio.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
mod models; | ||
|
||
use anyhow::{anyhow, Context, Result}; | ||
use aws_sdk_bedrockruntime::config::SharedHttpClient; | ||
use futures::stream; | ||
use futures::StreamExt; | ||
use http_client::HttpClient; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
use thiserror::Error; | ||
|
||
use aws_sdk_bedrockruntime as bedrock; | ||
pub use aws_sdk_bedrockruntime as bedrock_client; | ||
pub use bedrock::operation::converse_stream::ConverseStreamInput as BedrockStreamingRequest; | ||
pub use bedrock::types::ContentBlock as BedrockRequestContent; | ||
pub use bedrock::types::ConversationRole as BedrockRole; | ||
use bedrock::types::ConverseOutput as Response; | ||
pub use bedrock::types::ConverseStreamOutput as BedrockStreamingResponse; | ||
pub use bedrock::types::Message as BedrockMessage; | ||
pub use bedrock::types::ResponseStream as BedrockResponseStream; | ||
use futures::stream::BoxStream; | ||
use strum::Display; | ||
//TODO: Re-export the Bedrock stuff | ||
// https://doc.rust-lang.org/rustdoc/write-documentation/re-exports.html | ||
|
||
pub use models::*; | ||
|
||
pub async fn complete( | ||
client: &bedrock::Client, | ||
request: Request, | ||
) -> Result<Response, BedrockError> { | ||
let mut response = bedrock::Client::converse(client) | ||
.model_id(request.model.clone()) | ||
.set_messages(request.messages.into()) | ||
.send() | ||
.await | ||
.context("Failed to send request to Bedrock"); | ||
|
||
match response { | ||
Ok(output) => Ok(output.output.unwrap()), | ||
Err(err) => Err(BedrockError::Other(err)), | ||
} | ||
} | ||
|
||
pub async fn stream_completion( | ||
client: &bedrock::Client, | ||
request: Request, | ||
) -> Result<BoxStream<'static, Result<Option<BedrockStreamingResponse>, BedrockError>>, BedrockError> { | ||
let response = bedrock::Client::converse_stream(client) | ||
.model_id(request.model) | ||
.set_messages(request.messages.into()) | ||
.send() | ||
.await; | ||
|
||
match response { | ||
Ok(mut output) => { | ||
let stream = stream::unfold(output.stream, |mut stream| async move { | ||
match stream.recv().await { | ||
Ok(Some(output)) => Some((Ok(Some(output)), stream)), | ||
Ok(None) => Some((Ok(None), stream)), | ||
Err(e) => Some(( | ||
Err(BedrockError::ClientError(anyhow!( | ||
"Failed to receive response from Bedrock" | ||
))), | ||
stream, | ||
)), | ||
} | ||
}) | ||
.boxed(); | ||
|
||
Ok(stream) | ||
} | ||
Err(e) => Err(BedrockError::ClientError(anyhow!(e))), | ||
} | ||
} | ||
|
||
//TODO: A LOT of these types need to re-export the Bedrock types instead of making custom ones | ||
|
||
#[derive(Debug)] | ||
pub struct Request { | ||
pub model: String, | ||
pub max_tokens: u32, | ||
pub messages: Vec<BedrockMessage>, | ||
// #[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub system: Option<String>, | ||
// #[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub metadata: Option<Metadata>, | ||
// #[serde(default, skip_serializing_if = "Vec::is_empty")] | ||
pub stop_sequences: Vec<String>, | ||
// #[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub temperature: Option<f32>, | ||
// #[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub top_k: Option<u32>, | ||
// #[serde(default, skip_serializing_if = "Option::is_none")] | ||
pub top_p: Option<f32>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Metadata { | ||
pub user_id: Option<String>, | ||
} | ||
|
||
#[derive(Error, Debug, Display)] | ||
pub enum BedrockError { | ||
ClientError(anyhow::Error), | ||
ExtensionError(anyhow::Error), | ||
Other(anyhow::Error), | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see on https://docs.rs/aws-sdk-bedrockruntime/latest/aws_sdk_bedrockruntime/ that this requires tokio. Does this setup get around that to ensure it uses gpui's executors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be honest, didn't think about that -- but, I don't believe so.
All the uses of tokio in the Bedrock SDK are opaque, so I don't know if they can be gotten around to use the gpui executors.
Probably to get around i'd have to implement SigV4 + use the raw HTTP client although it defeats the point of the SDK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a similar vein I've had to export just the serde types and then construct the API calls inside of Zed. At the very least I wish that Zed's
HttpClient
trait was exported so that it would be easier to build libraries that target GPUI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think this could be possible with: the SDK config builder
Based on the source code it seems to accept an http_client as part of the config constructor.
Will see if i can implement it to accept the GPUI HttpClient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust will only be happy with that if they're the same
HttpClient
trait between GPUI/Zed and the SDK config builder.