feat!: wrap Client::new() return in a Result

BREAKING CHANGE: `Client::new()` now returns a `Result`.
This commit is contained in:
Ivan Gabriele
2024-03-04 04:43:16 +01:00
parent 1deab88251
commit 33876183e4
7 changed files with 25 additions and 20 deletions

View File

@@ -54,7 +54,7 @@ use mistralai_client::v1::client::Client;
fn main() {
let api_key = "your_api_key";
let client = Client::new(Some(api_key), None, None, None);
let client = Client::new(Some(api_key), None, None, None).unwrap();
}
```
@@ -71,7 +71,7 @@ use mistralai_client::v1::{
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None);
let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b;
let messages = vec![ChatCompletionMessage {
@@ -101,7 +101,7 @@ use mistralai_client::v1::{client::Client, constants::EmbedModel};
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client: Client = Client::new(None, None, None, None);
let client: Client = Client::new(None, None, None, None).unwrap();
let model = EmbedModel::MistralEmbed;
let input = vec!["Embed this sentence.", "As well as this one."]
@@ -123,7 +123,7 @@ use mistralai_client::v1::client::Client;
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None);
let client = Client::new(None, None, None, None).unwrap();
let result = client.list_models().unwrap();
println!("First Model ID: {:?}", result.data[0].id);

View File

@@ -24,21 +24,21 @@ impl Client {
endpoint: Option<String>,
max_retries: Option<u32>,
timeout: Option<u32>,
) -> Self {
let api_key = api_key.unwrap_or(
std::env::var("MISTRAL_API_KEY")
.unwrap_or_else(|_| panic!("{}", ClientError::ApiKeyError)),
);
) -> Result<Self, ClientError> {
let api_key = api_key.unwrap_or(match std::env::var("MISTRAL_API_KEY") {
Ok(api_key_from_env) => api_key_from_env,
Err(_) => return Err(ClientError::ApiKeyError),
});
let endpoint = endpoint.unwrap_or(API_URL_BASE.to_string());
let max_retries = max_retries.unwrap_or(5);
let timeout = timeout.unwrap_or(120);
Self {
Ok(Self {
api_key,
endpoint,
max_retries,
timeout,
}
})
}
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {

View File

@@ -12,7 +12,7 @@ impl fmt::Display for ApiError {
}
impl Error for ApiError {}
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum ClientError {
#[error("You must either set the `MISTRAL_API_KEY` environment variable or specify it in `Client::new(api_key, ...).")]
ApiKeyError,

View File

@@ -7,7 +7,7 @@ use mistralai_client::v1::{
#[test]
fn test_client_chat() {
let client = Client::new(None, None, None, None);
let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b;
let messages = vec![ChatCompletionMessage {

View File

@@ -3,7 +3,7 @@ use mistralai_client::v1::{client::Client, constants::EmbedModel};
#[test]
fn test_client_embeddings() {
let client: Client = Client::new(None, None, None, None);
let client: Client = Client::new(None, None, None, None).unwrap();
let model = EmbedModel::MistralEmbed;
let input = vec!["Embed this sentence.", "As well as this one."]

View File

@@ -3,7 +3,7 @@ use mistralai_client::v1::client::Client;
#[test]
fn test_client_list_models() {
let client = Client::new(None, None, None, None);
let client = Client::new(None, None, None, None).unwrap();
let response = client.list_models().unwrap();

View File

@@ -1,5 +1,5 @@
use jrest::expect;
use mistralai_client::v1::client::Client;
use mistralai_client::v1::{client::Client, error::ClientError};
#[test]
fn test_client_new_with_none_params() {
@@ -7,7 +7,7 @@ fn test_client_new_with_none_params() {
std::env::remove_var("MISTRAL_API_KEY");
std::env::set_var("MISTRAL_API_KEY", "test_api_key_from_env");
let client = Client::new(None, None, None, None);
let client = Client::new(None, None, None, None).unwrap();
expect!(client.api_key).to_be("test_api_key_from_env".to_string());
expect!(client.endpoint).to_be("https://api.mistral.ai/v1".to_string());
@@ -38,7 +38,8 @@ fn test_client_new_with_all_params() {
endpoint.clone(),
max_retries.clone(),
timeout.clone(),
);
)
.unwrap();
expect!(client.api_key).to_be(api_key.unwrap());
expect!(client.endpoint).to_be(endpoint.unwrap());
@@ -54,12 +55,16 @@ fn test_client_new_with_all_params() {
}
#[test]
#[should_panic]
fn test_client_new_with_missing_api_key() {
let maybe_original_mistral_api_key = std::env::var("MISTRAL_API_KEY").ok();
std::env::remove_var("MISTRAL_API_KEY");
let _client = Client::new(None, None, None, None);
let call = || Client::new(None, None, None, None);
match call() {
Ok(_) => panic!("Expected `ClientError::ApiKeyError` but got Ok.`"),
Err(error) => assert_eq!(error, ClientError::ApiKeyError),
}
match maybe_original_mistral_api_key {
Some(original_mistral_api_key) => {