Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7016cffb05 | ||
|
|
43cf87529e | ||
|
|
7627a336cc | ||
|
|
4983f69cd5 | ||
|
|
814b9918b3 | ||
|
|
7de2b19b98 | ||
|
|
8cb2c3cd0c |
29
CHANGELOG.md
Normal file
29
CHANGELOG.md
Normal file
@@ -0,0 +1,29 @@
|
||||
## [](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.1.0...v) (2024-03-03)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* Chat completions must now be called directly from client.chat() without building a request in between.
|
||||
|
||||
### 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))
|
||||
|
||||
## [](https://github.com/ivangabriele/mistralai-client-rs/compare/v0.1.0...v) (2024-03-03)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* Chat completions must now be called directly from client.chat() without building a request in between.
|
||||
|
||||
### 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))
|
||||
|
||||
## [0.1.0](https://github.com/ivangabriele/mistralai-client-rs/compare/7d3b438d16e9936591b6454525968c5c2cdfd6ad...v0.1.0) (2024-03-03)
|
||||
|
||||
### Features
|
||||
|
||||
- add chat completion without streaming ([7d3b438](https://github.com/ivangabriele/mistralai-client-rs/commit/7d3b438d16e9936591b6454525968c5c2cdfd6ad))
|
||||
@@ -2,7 +2,7 @@
|
||||
name = "mistralai-client"
|
||||
description = "Mistral AI API client library for Rust (unofficial)."
|
||||
license = "Apache-2.0"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
|
||||
edition = "2021"
|
||||
rust-version = "1.76.0"
|
||||
|
||||
20
Makefile
20
Makefile
@@ -1,6 +1,26 @@
|
||||
.PHONY: test
|
||||
|
||||
define RELEASE_TEMPLATE
|
||||
conventional-changelog -p conventionalcommits -i CHANGELOG.md -s
|
||||
git add .
|
||||
git commit -m "docs(changelog): update"
|
||||
git push origin HEAD
|
||||
cargo release $(1) --execute
|
||||
git push origin HEAD --tags
|
||||
endef
|
||||
|
||||
test:
|
||||
cargo test --no-fail-fast
|
||||
test-cover:
|
||||
cargo tarpaulin --frozen --no-fail-fast --out Xml --skip-clean
|
||||
test-watch:
|
||||
cargo watch -x "test -- --nocapture"
|
||||
|
||||
release-patch:
|
||||
$(call RELEASE_TEMPLATE,patch)
|
||||
|
||||
release-minor:
|
||||
$(call RELEASE_TEMPLATE,minor)
|
||||
|
||||
release-major:
|
||||
$(call RELEASE_TEMPLATE,major)
|
||||
|
||||
25
README.md
25
README.md
@@ -27,7 +27,7 @@ Rust client for the Mistral AI API.
|
||||
- [x] Chat without streaming
|
||||
- [ ] Chat with streaming
|
||||
- [ ] Embedding
|
||||
- [ ] List models
|
||||
- [x] List models
|
||||
- [ ] Function Calling
|
||||
|
||||
## Installation
|
||||
@@ -63,11 +63,8 @@ fn main() {
|
||||
### Chat without streaming
|
||||
|
||||
```rs
|
||||
use mistralai::v1::{
|
||||
chat_completion::{
|
||||
ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionRequest,
|
||||
ChatCompletionRequestOptions,
|
||||
},
|
||||
use mistralai_client::v1::{
|
||||
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionRequestOptions},
|
||||
client::Client,
|
||||
constants::OPEN_MISTRAL_7B,
|
||||
};
|
||||
@@ -87,8 +84,7 @@ fn main() {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let chat_completion_request = ChatCompletionRequest::new(model, messages, Some(options));
|
||||
let result = client.chat(chat_completion_request).unwrap();
|
||||
let result = client.chat(model, messages, Some(options)).unwrap();
|
||||
println!("Assistant: {}", result.choices[0].message.content);
|
||||
// => "Assistant: Tower. [...]"
|
||||
}
|
||||
@@ -104,4 +100,15 @@ _In progress._
|
||||
|
||||
### List models
|
||||
|
||||
_In progress._
|
||||
```rs
|
||||
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 result = client.list_models(model, messages, Some(options)).unwrap();
|
||||
println!("First Model ID: {:?}", result.data[0].id);
|
||||
// => "First Model ID: open-mistral-7b"
|
||||
}
|
||||
```
|
||||
|
||||
1
release.toml
Normal file
1
release.toml
Normal file
@@ -0,0 +1 @@
|
||||
allow-branch = ["main"]
|
||||
@@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::v1::common;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChatCompletionRequestOptions {
|
||||
pub struct ChatCompletionParams {
|
||||
pub tools: Option<String>,
|
||||
pub temperature: Option<f32>,
|
||||
pub max_tokens: Option<u32>,
|
||||
@@ -12,7 +12,7 @@ pub struct ChatCompletionRequestOptions {
|
||||
pub stream: Option<bool>,
|
||||
pub safe_prompt: Option<bool>,
|
||||
}
|
||||
impl Default for ChatCompletionRequestOptions {
|
||||
impl Default for ChatCompletionParams {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tools: None,
|
||||
@@ -44,18 +44,18 @@ pub struct ChatCompletionRequest {
|
||||
pub stream: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub safe_prompt: Option<bool>,
|
||||
// TODO Check that prop (seen in official Python client but not in API doc).
|
||||
// TODO Check this prop (seen in official Python client but not in API doc).
|
||||
// pub tool_choice: Option<String>,
|
||||
// TODO Check that prop (seen in official Python client but not in API doc).
|
||||
// TODO Check this prop (seen in official Python client but not in API doc).
|
||||
// pub response_format: Option<String>,
|
||||
}
|
||||
impl ChatCompletionRequest {
|
||||
pub fn new(
|
||||
model: String,
|
||||
messages: Vec<ChatCompletionMessage>,
|
||||
options: Option<ChatCompletionRequestOptions>,
|
||||
options: Option<ChatCompletionParams>,
|
||||
) -> Self {
|
||||
let ChatCompletionRequestOptions {
|
||||
let ChatCompletionParams {
|
||||
tools,
|
||||
temperature,
|
||||
max_tokens,
|
||||
@@ -95,7 +95,7 @@ pub struct ChatCompletionChoice {
|
||||
pub index: u32,
|
||||
pub message: ChatCompletionMessage,
|
||||
pub finish_reason: String,
|
||||
// TODO Check that prop (seen in API responses but undocumented).
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub logprobs: ???
|
||||
}
|
||||
|
||||
|
||||
117
src/v1/client.rs
117
src/v1/client.rs
@@ -2,8 +2,11 @@ use crate::v1::error::APIError;
|
||||
use minreq::Response;
|
||||
|
||||
use crate::v1::{
|
||||
chat_completion::{ChatCompletionRequest, ChatCompletionResponse},
|
||||
chat_completion::{
|
||||
ChatCompletionMessage, ChatCompletionParams, ChatCompletionRequest, ChatCompletionResponse,
|
||||
},
|
||||
constants::API_URL_BASE,
|
||||
list_models::ListModelsResponse,
|
||||
};
|
||||
|
||||
pub struct Client {
|
||||
@@ -36,7 +39,7 @@ impl Client {
|
||||
pub fn build_request(&self, request: minreq::Request) -> minreq::Request {
|
||||
let authorization = format!("Bearer {}", self.api_key);
|
||||
let user_agent = format!(
|
||||
"ivangabriele/mistral-client-rs/{}",
|
||||
"ivangabriele/mistralai-client-rs/{}",
|
||||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
|
||||
@@ -51,20 +54,24 @@ impl Client {
|
||||
|
||||
pub fn get(&self, path: &str) -> Result<Response, APIError> {
|
||||
let url = format!("{}{}", self.endpoint, path);
|
||||
let request = self.build_request(minreq::post(url));
|
||||
let request = self.build_request(minreq::get(url));
|
||||
|
||||
let result = request.send();
|
||||
match result {
|
||||
Ok(res) => {
|
||||
if (200..=299).contains(&res.status_code) {
|
||||
Ok(res)
|
||||
Ok(response) => {
|
||||
if (200..=299).contains(&response.status_code) {
|
||||
Ok(response)
|
||||
} else {
|
||||
Err(APIError {
|
||||
message: format!("{}: {}", res.status_code, res.as_str().unwrap()),
|
||||
message: format!(
|
||||
"{}: {}",
|
||||
response.status_code,
|
||||
response.as_str().unwrap()
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(e) => Err(self.new_error(e)),
|
||||
Err(error) => Err(self.new_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,18 +87,22 @@ impl Client {
|
||||
|
||||
let result = request.with_json(params).unwrap().send();
|
||||
match result {
|
||||
Ok(res) => {
|
||||
print!("{:?}", res.as_str().unwrap());
|
||||
Ok(response) => {
|
||||
// print!("{:?}", response.as_str().unwrap());
|
||||
|
||||
if (200..=299).contains(&res.status_code) {
|
||||
Ok(res)
|
||||
if (200..=299).contains(&response.status_code) {
|
||||
Ok(response)
|
||||
} else {
|
||||
Err(APIError {
|
||||
message: format!("{}: {}", res.status_code, res.as_str().unwrap()),
|
||||
message: format!(
|
||||
"{}: {}",
|
||||
response.status_code,
|
||||
response.as_str().unwrap()
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(e) => Err(self.new_error(e)),
|
||||
Err(error) => Err(self.new_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,43 +112,45 @@ impl Client {
|
||||
|
||||
let result = request.send();
|
||||
match result {
|
||||
Ok(res) => {
|
||||
if (200..=299).contains(&res.status_code) {
|
||||
Ok(res)
|
||||
Ok(response) => {
|
||||
if (200..=299).contains(&response.status_code) {
|
||||
Ok(response)
|
||||
} else {
|
||||
Err(APIError {
|
||||
message: format!("{}: {}", res.status_code, res.as_str().unwrap()),
|
||||
message: format!(
|
||||
"{}: {}",
|
||||
response.status_code,
|
||||
response.as_str().unwrap()
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(e) => Err(self.new_error(e)),
|
||||
Err(error) => Err(self.new_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn completion(&self, req: CompletionRequest) -> Result<CompletionResponse, APIError> {
|
||||
// let res = self.post("/completions", &req)?;
|
||||
// let r = res.json::<CompletionResponse>();
|
||||
// match r {
|
||||
// Ok(r) => Ok(r),
|
||||
// Err(e) => Err(self.new_error(e)),
|
||||
// }
|
||||
// }
|
||||
pub fn chat(
|
||||
&self,
|
||||
model: String,
|
||||
messages: Vec<ChatCompletionMessage>,
|
||||
options: Option<ChatCompletionParams>,
|
||||
) -> Result<ChatCompletionResponse, APIError> {
|
||||
let request = ChatCompletionRequest::new(model, messages, options);
|
||||
|
||||
// pub fn embedding(&self, req: EmbeddingRequest) -> Result<EmbeddingResponse, APIError> {
|
||||
// let res = self.post("/embeddings", &req)?;
|
||||
// let r = res.json::<EmbeddingResponse>();
|
||||
// match r {
|
||||
// Ok(r) => Ok(r),
|
||||
// Err(e) => Err(self.new_error(e)),
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn chat(&self, request: ChatCompletionRequest) -> Result<ChatCompletionResponse, APIError> {
|
||||
let response = self.post("/chat/completions", &request)?;
|
||||
let result = response.json::<ChatCompletionResponse>();
|
||||
match result {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => Err(self.new_error(e)),
|
||||
Ok(response) => Ok(response),
|
||||
Err(error) => Err(self.new_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_models(&self) -> Result<ListModelsResponse, APIError> {
|
||||
let response = self.get("/models")?;
|
||||
let result = response.json::<ListModelsResponse>();
|
||||
match result {
|
||||
Ok(response) => Ok(response),
|
||||
Err(error) => Err(self.new_error(error)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,30 +159,4 @@ impl Client {
|
||||
message: err.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
// fn query_params(
|
||||
// limit: Option<i64>,
|
||||
// order: Option<String>,
|
||||
// after: Option<String>,
|
||||
// before: Option<String>,
|
||||
// mut url: String,
|
||||
// ) -> String {
|
||||
// let mut params = vec![];
|
||||
// if let Some(limit) = limit {
|
||||
// params.push(format!("limit={}", limit));
|
||||
// }
|
||||
// if let Some(order) = order {
|
||||
// params.push(format!("order={}", order));
|
||||
// }
|
||||
// if let Some(after) = after {
|
||||
// params.push(format!("after={}", after));
|
||||
// }
|
||||
// if let Some(before) = before {
|
||||
// params.push(format!("before={}", before));
|
||||
// }
|
||||
// if !params.is_empty() {
|
||||
// url = format!("{}?{}", url, params.join("&"));
|
||||
// }
|
||||
// url
|
||||
// }
|
||||
}
|
||||
|
||||
39
src/v1/list_models.rs
Normal file
39
src/v1/list_models.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ListModelsResponse {
|
||||
pub object: String,
|
||||
pub data: Vec<ListModelsModel>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ListModelsModel {
|
||||
pub id: String,
|
||||
pub object: String,
|
||||
/// Unix timestamp (in seconds).
|
||||
pub created: u32,
|
||||
pub owned_by: String,
|
||||
pub permission: Vec<ListModelsModelPermission>,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub root: ???,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub parent: ???,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct ListModelsModelPermission {
|
||||
pub id: String,
|
||||
pub object: String,
|
||||
/// Unix timestamp (in seconds).
|
||||
pub created: u32,
|
||||
pub allow_create_engine: bool,
|
||||
pub allow_sampling: bool,
|
||||
pub allow_logprobs: bool,
|
||||
pub allow_search_indices: bool,
|
||||
pub allow_view: bool,
|
||||
pub allow_fine_tuning: bool,
|
||||
pub organization: String,
|
||||
pub is_blocking: bool,
|
||||
// TODO Check this prop (seen in API responses but undocumented).
|
||||
// pub group: ???,
|
||||
}
|
||||
@@ -3,3 +3,4 @@ pub mod client;
|
||||
pub mod common;
|
||||
pub mod constants;
|
||||
pub mod error;
|
||||
pub mod list_models;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use jrest::expect;
|
||||
use mistralai_client::v1::{
|
||||
chat_completion::{
|
||||
ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionRequest,
|
||||
ChatCompletionRequestOptions,
|
||||
},
|
||||
chat_completion::{ChatCompletionMessage, ChatCompletionMessageRole, ChatCompletionParams},
|
||||
client::Client,
|
||||
constants::OPEN_MISTRAL_7B,
|
||||
};
|
||||
@@ -22,32 +19,22 @@ fn test_chat_completion() {
|
||||
role: ChatCompletionMessageRole::user,
|
||||
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
|
||||
}];
|
||||
let options = ChatCompletionRequestOptions {
|
||||
let options = ChatCompletionParams {
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let chat_completion_request = ChatCompletionRequest::new(model, messages, Some(options));
|
||||
let result = client.chat(chat_completion_request);
|
||||
let response = client.chat(model, messages, Some(options)).unwrap();
|
||||
|
||||
match result {
|
||||
Ok(res) => {
|
||||
expect!(res.model).to_be("open-mistral-7b".to_string());
|
||||
expect!(res.object).to_be("chat.completion".to_string());
|
||||
expect!(res.choices.len()).to_be(1);
|
||||
expect!(res.choices[0].index).to_be(0);
|
||||
expect!(res.choices[0].message.role.clone())
|
||||
.to_be(ChatCompletionMessageRole::assistant);
|
||||
expect!(res.choices[0].message.content.clone()).to_be(
|
||||
"Tower. The Eiffel Tower is a famous landmark in Paris, France.".to_string(),
|
||||
);
|
||||
expect!(res.usage.prompt_tokens).to_be_greater_than(0);
|
||||
expect!(res.usage.completion_tokens).to_be_greater_than(0);
|
||||
expect!(res.usage.total_tokens).to_be_greater_than(21);
|
||||
}
|
||||
Err(err) => {
|
||||
panic!("Error: {}", err);
|
||||
}
|
||||
}
|
||||
expect!(response.model).to_be("open-mistral-7b".to_string());
|
||||
expect!(response.object).to_be("chat.completion".to_string());
|
||||
expect!(response.choices.len()).to_be(1);
|
||||
expect!(response.choices[0].index).to_be(0);
|
||||
expect!(response.choices[0].message.role.clone()).to_be(ChatCompletionMessageRole::assistant);
|
||||
expect!(response.choices[0].message.content.clone())
|
||||
.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);
|
||||
}
|
||||
|
||||
17
tests/v1_list_models_test.rs
Normal file
17
tests/v1_list_models_test.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use jrest::expect;
|
||||
use mistralai_client::v1::client::Client;
|
||||
|
||||
#[test]
|
||||
fn test_list_models() {
|
||||
extern crate dotenv;
|
||||
|
||||
use dotenv::dotenv;
|
||||
dotenv().ok();
|
||||
|
||||
let client = Client::new(None, None, None, None);
|
||||
|
||||
let response = client.list_models().unwrap();
|
||||
|
||||
expect!(response.object).to_be("list".to_string());
|
||||
expect!(response.data.len()).to_be_greater_than(0);
|
||||
}
|
||||
Reference in New Issue
Block a user