-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Mistral API in the Bedrock module
- Implemented MistralClient for interacting with the Mistral API in the Bedrock service - Added MistralRequestBuilder and MistralResponse models - Added examples for using the Mistral API - Updated the README with information about the Mistral API support - Bumped the version to 0.1.7
- Loading branch information
1 parent
49b0ed9
commit 92867a8
Showing
16 changed files
with
566 additions
and
305 deletions.
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
File renamed without changes.
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,6 @@ | ||
# Revision History | ||
|
||
## [2024-04-05] | ||
|
||
### 0.1.7 | ||
- Added support for the Mistral API in the Bedrock module. |
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,27 @@ | ||
use crate::bedrock::error::BedrockError; | ||
use serde_json::Error as SerdeJsonError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum MistralError { | ||
#[error("HTTP error: {0}")] | ||
Http(#[from] reqwest::Error), | ||
|
||
#[error("JSON error: {0}")] | ||
Json(#[from] SerdeJsonError), | ||
|
||
#[error("I/O error: {0}")] | ||
Io(#[from] std::io::Error), | ||
|
||
#[error("UTF-8 error: {0}")] | ||
Utf8(#[from] std::str::Utf8Error), | ||
|
||
#[error("Invalid response: {0}")] | ||
InvalidResponse(String), | ||
|
||
#[error("Unknown error: {0}")] | ||
Unknown(String), | ||
|
||
#[error("Bedrock error: {0}")] | ||
Bedrock(#[from] BedrockError), | ||
} |
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,125 @@ | ||
use crate::bedrock::bedrock_client::{BedrockClient, BedrockClientOptions}; | ||
use crate::bedrock::models::mistral::error::MistralError; | ||
use crate::bedrock::models::mistral::mistral_request_message::{MistralRequest, MistralResponse}; | ||
use futures::stream::Stream; | ||
use futures::TryStreamExt; | ||
|
||
pub type MistralOptions = BedrockClientOptions; | ||
|
||
pub struct MistralClient { | ||
client: BedrockClient, | ||
} | ||
|
||
impl MistralClient { | ||
/// Constructs a new `MistralClient`. | ||
pub async fn new(options: MistralOptions) -> Self { | ||
Self { | ||
client: BedrockClient::new(options).await, | ||
} | ||
} | ||
|
||
/// Generates a response from the Mistral model. | ||
pub async fn generate( | ||
&self, | ||
model_id: String, | ||
request: &MistralRequest, | ||
) -> Result<MistralResponse, MistralError> { | ||
let payload = serde_json::to_value(request).map_err(MistralError::Json)?; | ||
|
||
let response = self.client.generate_raw(model_id, payload).await?; | ||
|
||
let mistral_response = serde_json::from_value(response).map_err(MistralError::Json)?; | ||
Ok(mistral_response) | ||
} | ||
|
||
/// Generates a stream of responses from the Mistral model. | ||
pub async fn generate_with_stream( | ||
&self, | ||
model_id: String, | ||
request: &MistralRequest, | ||
) -> Result<impl Stream<Item = Result<MistralResponse, MistralError>>, MistralError> { | ||
let payload = serde_json::to_value(request).map_err(MistralError::Json)?; | ||
|
||
let response = self.client.generate_raw_stream(model_id, payload).await?; | ||
|
||
|
||
Ok(response | ||
.map_ok(|value| serde_json::from_value(value).map_err(MistralError::Json)) | ||
.map_err(|err| MistralError::Bedrock(err)) | ||
.and_then(futures::future::ready)) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::bedrock::{models::mistral::mistral_request_message::MistralRequestBuilder, ModelInfo}; | ||
use futures::stream::StreamExt; | ||
|
||
#[tokio::test] | ||
async fn test_generate() { | ||
let options = MistralOptions::new().profile_name("bedrock").region("us-west-2"); | ||
let client = MistralClient::new(options).await; | ||
|
||
let request = MistralRequestBuilder::new("<s>[INST] What is the capital of France ?[/INST]".to_string()) | ||
.max_tokens(200) | ||
.temperature(0.8) | ||
.build(); | ||
|
||
let model_name = ModelInfo::from_model_name(crate::bedrock::ModelName::MistralMixtral8X7BInstruct0x); | ||
|
||
let response = client.generate(model_name, &request).await; | ||
|
||
let response = match response { | ||
Ok(response) => response, | ||
Err(err) => panic!("Error: {:?}", err), | ||
}; | ||
|
||
println!("Response: {:?}", response.outputs[0].text.to_string()); | ||
|
||
assert!(!response.outputs.is_empty()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_generate_with_stream() { | ||
let options = MistralOptions::new().profile_name("bedrock").region("us-west-2"); | ||
let client = MistralClient::new(options).await; | ||
|
||
let request = MistralRequestBuilder::new("<s>[INST] What is the capital of France ?[/INST]".to_string()) | ||
.max_tokens(200) | ||
.temperature(0.8) | ||
.build(); | ||
|
||
let model_name = ModelInfo::from_model_name(crate::bedrock::ModelName::MistralMixtral8X7BInstruct0x); | ||
|
||
// display the request as a pretty-printed JSON string | ||
let display_request = serde_json::to_string_pretty(&request).unwrap(); | ||
println!("Request: {}", display_request); | ||
|
||
|
||
|
||
let mut stream = client | ||
.generate_with_stream("mistral.mistral-7b-instruct-v0:2".to_string(), &request) | ||
.await | ||
.unwrap(); | ||
|
||
let mut response_text = String::new(); | ||
while let Some(result) = stream.next().await { | ||
match result { | ||
Ok(response) => { | ||
println!("Response: {:?}", response.outputs[0].text.to_string()); | ||
response_text.push_str(&response.outputs[0].text); | ||
} | ||
Err(err) => { | ||
panic!("Error: {:?}", err); | ||
} | ||
} | ||
} | ||
|
||
assert!(!response_text.is_empty()); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.