feat!: add client.embeddings() method

BREAKING CHANGE: Models are now enforced by `Model` & `EmbedModel` enums.
This commit is contained in:
Ivan Gabriele
2024-03-04 03:14:23 +01:00
parent 4e702aa48e
commit f44d951247
9 changed files with 173 additions and 43 deletions

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,7 +5,8 @@ use crate::v1::{
chat_completion::{
ChatCompletionMessage, ChatCompletionParams, ChatCompletionRequest, ChatCompletionResponse,
},
constants::API_URL_BASE,
constants::{EmbedModel, Model, API_URL_BASE},
embedding::{EmbeddingRequest, EmbeddingRequestOptions, EmbeddingResponse},
model_list::ModelListResponse,
};
@@ -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,6 +125,22 @@ impl Client {
}
}
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::<ModelListResponse>();

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 model_list;