7 Commits

Author SHA1 Message Date
Ivan Gabriele
0386b95b7b chore: Release mistralai-client version 0.3.0 2024-03-04 03:25:41 +01:00
Ivan Gabriele
c61f2278bb docs(changelog): update 2024-03-04 03:25:35 +01:00
Ivan Gabriele
4c8e330c95 ci(release): fix pre-release-replacements regex 2024-03-04 03:25:20 +01:00
Ivan Gabriele
64c7f2feb5 ci(release): fix type in pre-release-replacements prop 2024-03-04 03:21:13 +01:00
Ivan Gabriele
f44d951247 feat!: add client.embeddings() method
BREAKING CHANGE: Models are now enforced by `Model` & `EmbedModel` enums.
2024-03-04 03:16:59 +01:00
Ivan Gabriele
4e702aa48e refactor: rename ListModels* to ModelList* 2024-03-04 01:56:48 +01:00
Ivan Gabriele
809af31dd0 ci(release): fix changelog version replacement 2024-03-03 19:49:27 +01:00
13 changed files with 188 additions and 58 deletions

View File

@@ -1,16 +1,15 @@
## [](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.1.0...v) (2024-03-03)
## [0.3.0](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.2.0...v) (2024-03-04)
### ⚠ BREAKING CHANGES
* Chat completions must now be called directly from client.chat() without building a request in between.
* Models are now enforced by `Model` & `EmbedModel` enums.
### Features
* add client.list_models() method ([814b991](https://github.com/ivangabriele/mistralai-client-rs/commit/814b9918b3aca78bfd606b5b9bb470b70ea2a5c6))
* simplify chat completion call ([7de2b19](https://github.com/ivangabriele/mistralai-client-rs/commit/7de2b19b981f1d65fe5c566fcaf521e4f2a9ced1))
* add client.embeddings() method ([f44d951](https://github.com/ivangabriele/mistralai-client-rs/commit/f44d95124767c3a3f14c78c4be3d9c203fac49ad))
## [](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.1.0...v) (2024-03-03)
## [0.2.0](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.1.0...v) (2024-03-03)
### ⚠ BREAKING CHANGES

View File

@@ -2,7 +2,7 @@
name = "mistralai-client"
description = "Mistral AI API client library for Rust (unofficial)."
license = "Apache-2.0"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
rust-version = "1.76.0"

View File

@@ -26,7 +26,7 @@ Rust client for the Mistral AI API.
- [x] Chat without streaming
- [ ] Chat with streaming
- [ ] Embedding
- [x] Embedding
- [x] List models
- [ ] Function Calling
@@ -66,14 +66,14 @@ fn main() {
use mistralai_client::v1::{
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionRequestOptions},
client::Client,
constants::OPEN_MISTRAL_7B,
constants::Model,
};
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None);
let model = OPEN_MISTRAL_7B.to_string();
let model = Model::OpenMistral7b;
let messages = vec![ChatCompletionMessage {
role: ChatCompletionMessageRole::user,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
@@ -96,7 +96,25 @@ _In progress._
### Embeddings
_In progress._
```rs
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 model = EmbedModel::MistralEmbed;
let input = vec!["Embed this sentence.", "As well as this one."]
.iter()
.map(|s| s.to_string())
.collect();
let options = None;
let response = client.embeddings(model, input, options).unwrap();
println!("Embeddings: {:?}", response.data);
// => "Embeddings: [{...}, {...}]"
}
```
### List models

View File

@@ -1 +1,2 @@
allow-branch = ["main"]
pre-release-replacements = [{ file = "CHANGELOG.md", search = "## \\[\\]", replace = "## [{{version}}]" }]

View File

@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::v1::common;
use crate::v1::{common, constants};
#[derive(Debug)]
pub struct ChatCompletionParams {
@@ -29,7 +29,7 @@ impl Default for ChatCompletionParams {
#[derive(Debug, Serialize, Deserialize)]
pub struct ChatCompletionRequest {
pub messages: Vec<ChatCompletionMessage>,
pub model: String,
pub model: constants::Model,
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -51,7 +51,7 @@ pub struct ChatCompletionRequest {
}
impl ChatCompletionRequest {
pub fn new(
model: String,
model: constants::Model,
messages: Vec<ChatCompletionMessage>,
options: Option<ChatCompletionParams>,
) -> Self {
@@ -85,7 +85,7 @@ pub struct ChatCompletionResponse {
pub object: String,
/// Unix timestamp (in seconds).
pub created: u32,
pub model: String,
pub model: constants::Model,
pub choices: Vec<ChatCompletionChoice>,
pub usage: common::ResponseUsage,
}

View File

@@ -5,8 +5,9 @@ use crate::v1::{
chat_completion::{
ChatCompletionMessage, ChatCompletionParams, ChatCompletionRequest, ChatCompletionResponse,
},
constants::API_URL_BASE,
list_models::ListModelsResponse,
constants::{EmbedModel, Model, API_URL_BASE},
embedding::{EmbeddingRequest, EmbeddingRequestOptions, EmbeddingResponse},
model_list::ModelListResponse,
};
pub struct Client {
@@ -59,6 +60,8 @@ impl Client {
let result = request.send();
match result {
Ok(response) => {
print!("{:?}", response.as_str().unwrap());
if (200..=299).contains(&response.status_code) {
Ok(response)
} else {
@@ -88,7 +91,7 @@ impl Client {
let result = request.with_json(params).unwrap().send();
match result {
Ok(response) => {
// print!("{:?}", response.as_str().unwrap());
print!("{:?}", response.as_str().unwrap());
if (200..=299).contains(&response.status_code) {
Ok(response)
@@ -106,32 +109,9 @@ impl Client {
}
}
pub fn delete(&self, path: &str) -> Result<Response, APIError> {
let url = format!("{}{}", self.endpoint, path);
let request = self.build_request(minreq::post(url));
let result = request.send();
match result {
Ok(response) => {
if (200..=299).contains(&response.status_code) {
Ok(response)
} else {
Err(APIError {
message: format!(
"{}: {}",
response.status_code,
response.as_str().unwrap()
),
})
}
}
Err(error) => Err(self.new_error(error)),
}
}
pub fn chat(
&self,
model: String,
model: Model,
messages: Vec<ChatCompletionMessage>,
options: Option<ChatCompletionParams>,
) -> Result<ChatCompletionResponse, APIError> {
@@ -145,9 +125,25 @@ impl Client {
}
}
pub fn list_models(&self) -> Result<ListModelsResponse, APIError> {
pub fn embeddings(
&self,
model: EmbedModel,
input: Vec<String>,
options: Option<EmbeddingRequestOptions>,
) -> Result<EmbeddingResponse, APIError> {
let request = EmbeddingRequest::new(model, input, options);
let response = self.post("/embeddings", &request)?;
let result = response.json::<EmbeddingResponse>();
match result {
Ok(response) => Ok(response),
Err(error) => Err(self.new_error(error)),
}
}
pub fn list_models(&self) -> Result<ModelListResponse, APIError> {
let response = self.get("/models")?;
let result = response.json::<ListModelsResponse>();
let result = response.json::<ModelListResponse>();
match result {
Ok(response) => Ok(response),
Err(error) => Err(self.new_error(error)),

View File

@@ -1,7 +1,23 @@
use serde::{Deserialize, Serialize};
pub const API_URL_BASE: &str = "https://api.mistral.ai/v1";
pub const OPEN_MISTRAL_7B: &str = "open-mistral-7b";
pub const OPEN_MISTRAL_8X7B: &str = "open-mixtral-8x7b";
pub const MISTRAL_SMALL_LATEST: &str = "mistral-small-latest";
pub const MISTRAL_MEDIUM_LATEST: &str = "mistral-medium-latest";
pub const MISTRAL_LARGE_LATEST: &str = "mistral-large-latest";
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum Model {
#[serde(rename = "open-mistral-7b")]
OpenMistral7b,
#[serde(rename = "open-mistral-8x7b")]
OpenMistral8x7b,
#[serde(rename = "mistral-small-latest")]
MistralSmallLatest,
#[serde(rename = "mistral-medium-latest")]
MistralMediumLatest,
#[serde(rename = "mistral-large-latest")]
MistralLargeLatest,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum EmbedModel {
#[serde(rename = "mistral-embed")]
MistralEmbed,
}

60
src/v1/embedding.rs Normal file
View File

@@ -0,0 +1,60 @@
use serde::{Deserialize, Serialize};
use crate::v1::{common, constants};
#[derive(Debug)]
pub struct EmbeddingRequestOptions {
pub encoding_format: Option<EmbeddingRequestEncodingFormat>,
}
impl Default for EmbeddingRequestOptions {
fn default() -> Self {
Self {
encoding_format: None,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EmbeddingRequest {
pub model: constants::EmbedModel,
pub input: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub encoding_format: Option<EmbeddingRequestEncodingFormat>,
}
impl EmbeddingRequest {
pub fn new(
model: constants::EmbedModel,
input: Vec<String>,
options: Option<EmbeddingRequestOptions>,
) -> Self {
let EmbeddingRequestOptions { encoding_format } = options.unwrap_or_default();
Self {
model,
input,
encoding_format,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[allow(non_camel_case_types)]
pub enum EmbeddingRequestEncodingFormat {
float,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EmbeddingResponse {
pub id: String,
pub object: String,
pub model: constants::EmbedModel,
pub data: Vec<EmbeddingResponseDataItem>,
pub usage: common::ResponseUsage,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EmbeddingResponseDataItem {
pub index: u32,
pub embedding: Vec<f32>,
pub object: String,
}

View File

@@ -2,5 +2,6 @@ pub mod chat_completion;
pub mod client;
pub mod common;
pub mod constants;
pub mod embedding;
pub mod error;
pub mod list_models;
pub mod model_list;

View File

@@ -1,19 +1,19 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListModelsResponse {
pub struct ModelListResponse {
pub object: String,
pub data: Vec<ListModelsModel>,
pub data: Vec<ModelListData>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListModelsModel {
pub struct ModelListData {
pub id: String,
pub object: String,
/// Unix timestamp (in seconds).
pub created: u32,
pub owned_by: String,
pub permission: Vec<ListModelsModelPermission>,
pub permission: Vec<ModelListDataPermission>,
// TODO Check this prop (seen in API responses but undocumented).
// pub root: ???,
// TODO Check this prop (seen in API responses but undocumented).
@@ -21,7 +21,7 @@ pub struct ListModelsModel {
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListModelsModelPermission {
pub struct ModelListDataPermission {
pub id: String,
pub object: String,
/// Unix timestamp (in seconds).

View File

@@ -2,7 +2,7 @@ use jrest::expect;
use mistralai_client::v1::{
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionParams},
client::Client,
constants::OPEN_MISTRAL_7B,
constants::Model,
};
#[test]
@@ -14,7 +14,7 @@ fn test_chat_completion() {
let client = Client::new(None, None, None, None);
let model = OPEN_MISTRAL_7B.to_string();
let model = Model::OpenMistral7b;
let messages = vec![ChatCompletionMessage {
role: ChatCompletionMessageRole::user,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
@@ -27,7 +27,7 @@ fn test_chat_completion() {
let response = client.chat(model, messages, Some(options)).unwrap();
expect!(response.model).to_be("open-mistral-7b".to_string());
expect!(response.model).to_be(Model::OpenMistral7b);
expect!(response.object).to_be("chat.completion".to_string());
expect!(response.choices.len()).to_be(1);
expect!(response.choices[0].index).to_be(0);
@@ -36,5 +36,5 @@ fn test_chat_completion() {
.to_be("Tower. The Eiffel Tower is a famous landmark in Paris, France.".to_string());
expect!(response.usage.prompt_tokens).to_be_greater_than(0);
expect!(response.usage.completion_tokens).to_be_greater_than(0);
expect!(response.usage.total_tokens).to_be_greater_than(21);
expect!(response.usage.total_tokens).to_be_greater_than(0);
}

View File

@@ -0,0 +1,31 @@
use jrest::expect;
use mistralai_client::v1::{client::Client, constants::EmbedModel};
#[test]
fn test_embeddings() {
extern crate dotenv;
use dotenv::dotenv;
dotenv().ok();
let client: Client = Client::new(None, None, None, None);
let model = EmbedModel::MistralEmbed;
let input = vec!["Embed this sentence.", "As well as this one."]
.iter()
.map(|s| s.to_string())
.collect();
let options = None;
let response = client.embeddings(model, input, options).unwrap();
expect!(response.model).to_be(EmbedModel::MistralEmbed);
expect!(response.object).to_be("list".to_string());
expect!(response.data.len()).to_be(2);
expect!(response.data[0].index).to_be(0);
expect!(response.data[0].object.clone()).to_be("embedding".to_string());
expect!(response.data[0].embedding.len()).to_be_greater_than(0);
expect!(response.usage.prompt_tokens).to_be_greater_than(0);
expect!(response.usage.completion_tokens).to_be(0);
expect!(response.usage.total_tokens).to_be_greater_than(0);
}

View File

@@ -14,4 +14,12 @@ fn test_list_models() {
expect!(response.object).to_be("list".to_string());
expect!(response.data.len()).to_be_greater_than(0);
// let open_mistral_7b_data_item = response
// .data
// .iter()
// .find(|item| item.id == "open-mistral-7b")
// .unwrap();
// expect!(open_mistral_7b_data_item.id).to_be("open-mistral-7b".to_string());
}