docs: update README and examples for v1.0.0

- Rewrite README with all new endpoints and current model names
- Add available models table with constructors
- Add FIM and OCR examples
- Update all examples for string-based Model type
- Update streaming example for Option<String> delta content
- Use serde_json::json!() for tool schemas in examples
- Add .envrc to .gitignore
This commit is contained in:
2026-03-20 18:00:51 +00:00
parent ce96bcfeeb
commit 63f0edf574
11 changed files with 229 additions and 358 deletions

1
.gitignore vendored
View File

@@ -23,3 +23,4 @@ Cargo.lock
/cobertura.xml /cobertura.xml
.env .env
.envrc

416
README.md
View File

@@ -1,87 +1,43 @@
# Mistral AI Rust Client # Mistral AI Rust Client
[![Crates.io Package](https://img.shields.io/crates/v/mistralai-client?style=for-the-badge)](https://crates.io/crates/mistralai-client) Rust client for the [Mistral AI API](https://docs.mistral.ai/api/).
[![Docs.rs Documentation](https://img.shields.io/docsrs/mistralai-client/latest?style=for-the-badge)](https://docs.rs/mistralai-client/latest/mistralai-client)
[![Test Workflow Status](https://img.shields.io/github/actions/workflow/status/ivangabriele/mistralai-client-rs/test.yml?label=CI&style=for-the-badge)](https://github.com/ivangabriele/mistralai-client-rs/actions?query=branch%3Amain+workflow%3ATest++)
[![Code Coverage](https://img.shields.io/codecov/c/github/ivangabriele/mistralai-client-rs/main?label=Cov&style=for-the-badge)](https://app.codecov.io/github/ivangabriele/mistralai-client-rs)
Rust client for the Mistral AI API. > **Fork** of [ivangabriele/mistralai-client-rs](https://github.com/ivangabriele/mistralai-client-rs),
> updated to the latest Mistral API with all current endpoints and models.
> [!IMPORTANT]
> While we are in v0, minor versions may introduce breaking changes.
> Please, refer to the [CHANGELOG.md](./CHANGELOG.md) for more information.
---
- [Supported APIs](#supported-apis)
- [Installation](#installation)
- [Mistral API Key](#mistral-api-key)
- [As an environment variable](#as-an-environment-variable)
- [As a client argument](#as-a-client-argument)
- [Usage](#usage)
- [Chat](#chat)
- [Chat (async)](#chat-async)
- [Chat with streaming (async)](#chat-with-streaming-async)
- [Chat with Function Calling](#chat-with-function-calling)
- [Chat with Function Calling (async)](#chat-with-function-calling-async)
- [Embeddings](#embeddings)
- [Embeddings (async)](#embeddings-async)
- [List models](#list-models)
- [List models (async)](#list-models-async)
- [Contributing](#contributing)
---
## Supported APIs ## Supported APIs
- [x] Chat without streaming - [x] Chat completions (sync, async, streaming)
- [x] Chat without streaming (async) - [x] Function calling / tool use
- [x] Chat with streaming - [x] FIM (fill-in-the-middle) code completions
- [x] Embedding - [x] Embeddings (sync, async)
- [x] Embedding (async) - [x] Models (list, get, delete)
- [x] List models - [x] Files (upload, list, get, delete, download URL)
- [x] List models (async) - [x] Fine-tuning jobs (create, list, get, cancel, start)
- [x] Function Calling - [x] Batch jobs (create, list, get, cancel)
- [x] Function Calling (async) - [x] OCR (document text extraction)
- [x] Audio transcription
- [x] Moderations & classifications
- [x] Agent completions
## Installation ## Installation
You can install the library in your project using:
```sh ```sh
cargo add mistralai-client cargo add mistralai-client
``` ```
### Mistral API Key ### API Key
You can get your Mistral API Key there: <https://docs.mistral.ai/#api-access>. Get your key at <https://console.mistral.ai/api-keys>.
#### As an environment variable
Just set the `MISTRAL_API_KEY` environment variable.
```rs ```rs
use mistralai_client::v1::client::Client; use mistralai_client::v1::client::Client;
fn main() { // From MISTRAL_API_KEY environment variable:
let client = Client::new(None, None, None, None); let client = Client::new(None, None, None, None).unwrap();
}
```
```sh // Or pass directly:
MISTRAL_API_KEY=your_api_key cargo run let client = Client::new(Some("your_api_key".to_string()), None, None, None).unwrap();
```
#### As a client argument
```rs
use mistralai_client::v1::client::Client;
fn main() {
let api_key = "your_api_key";
let client = Client::new(Some(api_key), None, None, None).unwrap();
}
``` ```
## Usage ## Usage
@@ -90,30 +46,23 @@ fn main() {
```rs ```rs
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
fn main() { fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message("What is the Eiffel Tower?")];
role: ChatMessageRole::User,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.7),
random_seed: Some(42),
..Default::default() ..Default::default()
}; };
let result = client.chat(model, messages, Some(options)).unwrap(); let result = client.chat(model, messages, Some(options)).unwrap();
println!("Assistant: {}", result.choices[0].message.content); println!("{}", result.choices[0].message.content);
// => "Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France."
} }
``` ```
@@ -121,46 +70,29 @@ fn main() {
```rs ```rs
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message("What is the Eiffel Tower?")];
role: ChatMessageRole::User,
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
tool_calls: None,
}];
let options = ChatParams {
temperature: 0.0,
random_seed: Some(42),
..Default::default()
};
let result = client let result = client.chat_async(model, messages, None).await.unwrap();
.chat_async(model, messages, Some(options)) println!("{}", result.choices[0].message.content);
.await
.unwrap();
println!(
"{:?}: {}",
result.choices[0].message.role, result.choices[0].message.content
);
// => "Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France."
} }
``` ```
### Chat with streaming (async) ### Chat with streaming
```rs ```rs
use futures::stream::StreamExt; use futures::stream::StreamExt;
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
@@ -168,244 +100,111 @@ use std::io::{self, Write};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message("Tell me a short story.")];
role: ChatMessageRole::User,
content: "Tell me a short happy story.".to_string(),
tool_calls: None,
}];
let options = ChatParams {
temperature: 0.0,
random_seed: Some(42),
..Default::default()
};
let stream_result = client let stream = client.chat_stream(model, messages, None).await.unwrap();
.chat_stream(model, messages, Some(options)) stream
.await
.unwrap();
stream_result
.for_each(|chunk_result| async { .for_each(|chunk_result| async {
match chunk_result { match chunk_result {
Ok(chunks) => chunks.iter().for_each(|chunk| { Ok(chunks) => chunks.iter().for_each(|chunk| {
print!("{}", chunk.choices[0].delta.content); if let Some(content) = &chunk.choices[0].delta.content {
io::stdout().flush().unwrap(); print!("{}", content);
// => "Once upon a time, [...]" io::stdout().flush().unwrap();
}
}), }),
Err(error) => { Err(error) => eprintln!("Error: {:?}", error),
eprintln!("Error processing chunk: {:?}", error)
}
} }
}) })
.await; .await;
print!("\n") // To persist the last chunk output. println!();
} }
``` ```
### Chat with Function Calling ### Function calling
```rs ```rs
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType}, tool::{Function, Tool, ToolChoice},
}; };
use serde::Deserialize; use serde::Deserialize;
use std::any::Any; use std::any::Any;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct GetCityTemperatureArguments { struct GetWeatherArgs { city: String }
city: String,
}
struct GetCityTemperatureFunction; struct GetWeatherFunction;
#[async_trait::async_trait] #[async_trait::async_trait]
impl Function for GetCityTemperatureFunction { impl Function for GetWeatherFunction {
async fn execute(&self, arguments: String) -> Box<dyn Any + Send> { async fn execute(&self, arguments: String) -> Box<dyn Any + Send> {
// Deserialize arguments, perform the logic, and return the result let args: GetWeatherArgs = serde_json::from_str(&arguments).unwrap();
let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap(); Box::new(format!("20°C in {}", args.city))
let temperature = match city.as_str() {
"Paris" => "20°C",
_ => "Unknown city",
};
Box::new(temperature.to_string())
} }
} }
fn main() { fn main() {
let tools = vec![Tool::new( let tools = vec![Tool::new(
"get_city_temperature".to_string(), "get_weather".to_string(),
"Get the current temperature in a city.".to_string(), "Get the weather in a city.".to_string(),
vec![ToolFunctionParameter::new( serde_json::json!({
"city".to_string(), "type": "object",
"The name of the city.".to_string(), "properties": {
ToolFunctionParameterType::String, "city": { "type": "string", "description": "City name" }
)], },
"required": ["city"]
}),
)]; )];
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let mut client = Client::new(None, None, None, None).unwrap(); let mut client = Client::new(None, None, None, None).unwrap();
client.register_function( client.register_function("get_weather".to_string(), Box::new(GetWeatherFunction));
"get_city_temperature".to_string(),
Box::new(GetCityTemperatureFunction),
);
let model = Model::MistralSmallLatest; let messages = vec![ChatMessage::new_user_message("What's the weather in Paris?")];
let messages = vec![ChatMessage {
role: ChatMessageRole::User,
content: "What's the temperature in Paris?".to_string(),
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0,
random_seed: Some(42),
tool_choice: Some(ToolChoice::Auto), tool_choice: Some(ToolChoice::Auto),
tools: Some(tools), tools: Some(tools),
..Default::default() ..Default::default()
}; };
client.chat(model, messages, Some(options)).unwrap(); client.chat(Model::mistral_small_latest(), messages, Some(options)).unwrap();
let temperature = client let result = client.get_last_function_call_result().unwrap().downcast::<String>().unwrap();
.get_last_function_call_result() println!("{}", result);
.unwrap()
.downcast::<String>()
.unwrap();
println!("The temperature in Paris is: {}.", temperature);
// => "The temperature in Paris is: 20°C."
} }
``` ```
### Chat with Function Calling (async) ### FIM (code completion)
```rs ```rs
use mistralai_client::v1::{ use mistralai_client::v1::{client::Client, constants::Model, fim::FimParams};
chat::{ChatMessage, ChatMessageRole, ChatParams},
client::Client,
constants::Model,
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},
};
use serde::Deserialize;
use std::any::Any;
#[derive(Debug, Deserialize)] fn main() {
struct GetCityTemperatureArguments { let client = Client::new(None, None, None, None).unwrap();
city: String,
}
struct GetCityTemperatureFunction; let options = FimParams {
#[async_trait::async_trait] suffix: Some("\n return result".to_string()),
impl Function for GetCityTemperatureFunction {
async fn execute(&self, arguments: String) -> Box<dyn Any + Send> {
// Deserialize arguments, perform the logic, and return the result
let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap();
let temperature = match city.as_str() {
"Paris" => "20°C",
_ => "Unknown city",
};
Box::new(temperature.to_string())
}
}
#[tokio::main]
async fn main() {
let tools = vec![Tool::new(
"get_city_temperature".to_string(),
"Get the current temperature in a city.".to_string(),
vec![ToolFunctionParameter::new(
"city".to_string(),
"The name of the city.".to_string(),
ToolFunctionParameterType::String,
)],
)];
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let mut client = Client::new(None, None, None, None).unwrap();
client.register_function(
"get_city_temperature".to_string(),
Box::new(GetCityTemperatureFunction),
);
let model = Model::MistralSmallLatest;
let messages = vec![ChatMessage {
role: ChatMessageRole::User,
content: "What's the temperature in Paris?".to_string(),
tool_calls: None,
}];
let options = ChatParams {
temperature: 0.0,
random_seed: Some(42),
tool_choice: Some(ToolChoice::Auto),
tools: Some(tools),
..Default::default() ..Default::default()
}; };
client let result = client.fim(Model::codestral_latest(), "def fibonacci(".to_string(), Some(options)).unwrap();
.chat_async(model, messages, Some(options)) println!("{}", result.choices[0].message.content);
.await
.unwrap();
let temperature = client
.get_last_function_call_result()
.unwrap()
.downcast::<String>()
.unwrap();
println!("The temperature in Paris is: {}.", temperature);
// => "The temperature in Paris is: 20°C."
} }
``` ```
### Embeddings ### Embeddings
```rs ```rs
use mistralai_client::v1::{client::Client, constants::EmbedModel}; use mistralai_client::v1::{client::Client, constants::Model};
fn main() { fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. let client = Client::new(None, None, None, None).unwrap();
let client: Client = Client::new(None, None, None, None).unwrap();
let model = EmbedModel::MistralEmbed; let input = vec!["Hello world".to_string(), "Goodbye world".to_string()];
let input = vec!["Embed this sentence.", "As well as this one."] let response = client.embeddings(Model::mistral_embed(), input, None).unwrap();
.iter() println!("Dimensions: {}", response.data[0].embedding.len());
.map(|s| s.to_string())
.collect();
let options = None;
let response = client.embeddings(model, input, options).unwrap();
println!("First Embedding: {:?}", response.data[0]);
// => "First Embedding: {...}"
}
```
### Embeddings (async)
```rs
use mistralai_client::v1::{client::Client, constants::EmbedModel};
#[tokio::main]
async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
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."]
.iter()
.map(|s| s.to_string())
.collect();
let options = None;
let response = client
.embeddings_async(model, input, options)
.await
.unwrap();
println!("First Embedding: {:?}", response.data[0]);
// => "First Embedding: {...}"
} }
``` ```
@@ -415,31 +214,62 @@ async fn main() {
use mistralai_client::v1::client::Client; use mistralai_client::v1::client::Client;
fn main() { fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let result = client.list_models().unwrap(); let models = client.list_models().unwrap();
println!("First Model ID: {:?}", result.data[0].id); for model in &models.data {
// => "First Model ID: open-mistral-7b" println!("{}", model.id);
}
} }
``` ```
### List models (async) ### OCR
```rs ```rs
use mistralai_client::v1::client::Client; use mistralai_client::v1::{
client::Client,
constants::Model,
ocr::{OcrDocument, OcrRequest},
};
#[tokio::main] fn main() {
async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let result = client.list_models_async().await.unwrap(); let request = OcrRequest {
println!("First Model ID: {:?}", result.data[0].id); model: Model::mistral_ocr_latest(),
// => "First Model ID: open-mistral-7b" document: OcrDocument::from_url("https://example.com/document.pdf"),
pages: Some(vec![0]),
table_format: None,
include_image_base64: None,
image_limit: None,
};
let response = client.ocr(&request).unwrap();
println!("{}", response.pages[0].markdown);
} }
``` ```
## Contributing ## Available Models
Please read [CONTRIBUTING.md](./CONTRIBUTING.md) for details on how to contribute to this library. Use `Model::new("any-model-id")` for any model, or use the built-in constructors:
| Constructor | Model ID |
|---|---|
| `Model::mistral_large_latest()` | `mistral-large-latest` |
| `Model::mistral_medium_latest()` | `mistral-medium-latest` |
| `Model::mistral_small_latest()` | `mistral-small-latest` |
| `Model::mistral_small_4()` | `mistral-small-4-0-26-03` |
| `Model::codestral_latest()` | `codestral-latest` |
| `Model::magistral_medium_latest()` | `magistral-medium-latest` |
| `Model::magistral_small_latest()` | `magistral-small-latest` |
| `Model::mistral_embed()` | `mistral-embed` |
| `Model::mistral_ocr_latest()` | `mistral-ocr-latest` |
| `Model::mistral_moderation_latest()` | `mistral-moderation-26-03` |
| `Model::pixtral_large()` | `pixtral-large-2411` |
| `Model::voxtral_mini_transcribe()` | `voxtral-mini-transcribe-2-26-02` |
See `constants.rs` for the full list.
## License
Apache-2.0

View File

@@ -1,5 +1,5 @@
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
@@ -8,14 +8,12 @@ fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message(
role: ChatMessageRole::User, "Just guess the next word: \"Eiffel ...\"?",
content: "Just guess the next word: \"Eiffel ...\"?".to_string(), )];
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.0),
random_seed: Some(42), random_seed: Some(42),
..Default::default() ..Default::default()
}; };

View File

@@ -1,5 +1,5 @@
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
@@ -9,14 +9,12 @@ async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message(
role: ChatMessageRole::User, "Just guess the next word: \"Eiffel ...\"?",
content: "Just guess the next word: \"Eiffel ...\"?".to_string(), )];
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.0),
random_seed: Some(42), random_seed: Some(42),
..Default::default() ..Default::default()
}; };
@@ -29,5 +27,4 @@ async fn main() {
"{:?}: {}", "{:?}: {}",
result.choices[0].message.role, result.choices[0].message.content result.choices[0].message.role, result.choices[0].message.content
); );
// => "Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France."
} }

View File

@@ -1,8 +1,8 @@
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType}, tool::{Function, Tool, ToolChoice},
}; };
use serde::Deserialize; use serde::Deserialize;
use std::any::Any; use std::any::Any;
@@ -16,7 +16,6 @@ struct GetCityTemperatureFunction;
#[async_trait::async_trait] #[async_trait::async_trait]
impl Function for GetCityTemperatureFunction { impl Function for GetCityTemperatureFunction {
async fn execute(&self, arguments: String) -> Box<dyn Any + Send> { async fn execute(&self, arguments: String) -> Box<dyn Any + Send> {
// Deserialize arguments, perform the logic, and return the result
let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap(); let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap();
let temperature = match city.as_str() { let temperature = match city.as_str() {
@@ -32,11 +31,16 @@ fn main() {
let tools = vec![Tool::new( let tools = vec![Tool::new(
"get_city_temperature".to_string(), "get_city_temperature".to_string(),
"Get the current temperature in a city.".to_string(), "Get the current temperature in a city.".to_string(),
vec![ToolFunctionParameter::new( serde_json::json!({
"city".to_string(), "type": "object",
"The name of the city.".to_string(), "properties": {
ToolFunctionParameterType::String, "city": {
)], "type": "string",
"description": "The name of the city."
}
},
"required": ["city"]
}),
)]; )];
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
@@ -46,14 +50,12 @@ fn main() {
Box::new(GetCityTemperatureFunction), Box::new(GetCityTemperatureFunction),
); );
let model = Model::MistralSmallLatest; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message(
role: ChatMessageRole::User, "What's the temperature in Paris?",
content: "What's the temperature in Paris?".to_string(), )];
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.0),
random_seed: Some(42), random_seed: Some(42),
tool_choice: Some(ToolChoice::Auto), tool_choice: Some(ToolChoice::Auto),
tools: Some(tools), tools: Some(tools),

View File

@@ -1,8 +1,8 @@
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType}, tool::{Function, Tool, ToolChoice},
}; };
use serde::Deserialize; use serde::Deserialize;
use std::any::Any; use std::any::Any;
@@ -16,7 +16,6 @@ struct GetCityTemperatureFunction;
#[async_trait::async_trait] #[async_trait::async_trait]
impl Function for GetCityTemperatureFunction { impl Function for GetCityTemperatureFunction {
async fn execute(&self, arguments: String) -> Box<dyn Any + Send> { async fn execute(&self, arguments: String) -> Box<dyn Any + Send> {
// Deserialize arguments, perform the logic, and return the result
let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap(); let GetCityTemperatureArguments { city } = serde_json::from_str(&arguments).unwrap();
let temperature = match city.as_str() { let temperature = match city.as_str() {
@@ -33,11 +32,16 @@ async fn main() {
let tools = vec![Tool::new( let tools = vec![Tool::new(
"get_city_temperature".to_string(), "get_city_temperature".to_string(),
"Get the current temperature in a city.".to_string(), "Get the current temperature in a city.".to_string(),
vec![ToolFunctionParameter::new( serde_json::json!({
"city".to_string(), "type": "object",
"The name of the city.".to_string(), "properties": {
ToolFunctionParameterType::String, "city": {
)], "type": "string",
"description": "The name of the city."
}
},
"required": ["city"]
}),
)]; )];
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
@@ -47,14 +51,12 @@ async fn main() {
Box::new(GetCityTemperatureFunction), Box::new(GetCityTemperatureFunction),
); );
let model = Model::MistralSmallLatest; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message(
role: ChatMessageRole::User, "What's the temperature in Paris?",
content: "What's the temperature in Paris?".to_string(), )];
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.0),
random_seed: Some(42), random_seed: Some(42),
tool_choice: Some(ToolChoice::Auto), tool_choice: Some(ToolChoice::Auto),
tools: Some(tools), tools: Some(tools),

View File

@@ -1,6 +1,6 @@
use futures::stream::StreamExt; use futures::stream::StreamExt;
use mistralai_client::v1::{ use mistralai_client::v1::{
chat::{ChatMessage, ChatMessageRole, ChatParams}, chat::{ChatMessage, ChatParams},
client::Client, client::Client,
constants::Model, constants::Model,
}; };
@@ -11,14 +11,10 @@ async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap(); let client = Client::new(None, None, None, None).unwrap();
let model = Model::OpenMistral7b; let model = Model::mistral_small_latest();
let messages = vec![ChatMessage { let messages = vec![ChatMessage::new_user_message("Tell me a short happy story.")];
role: ChatMessageRole::User,
content: "Tell me a short happy story.".to_string(),
tool_calls: None,
}];
let options = ChatParams { let options = ChatParams {
temperature: 0.0, temperature: Some(0.0),
random_seed: Some(42), random_seed: Some(42),
..Default::default() ..Default::default()
}; };
@@ -31,9 +27,10 @@ async fn main() {
.for_each(|chunk_result| async { .for_each(|chunk_result| async {
match chunk_result { match chunk_result {
Ok(chunks) => chunks.iter().for_each(|chunk| { Ok(chunks) => chunks.iter().for_each(|chunk| {
print!("{}", chunk.choices[0].delta.content); if let Some(content) = &chunk.choices[0].delta.content {
io::stdout().flush().unwrap(); print!("{}", content);
// => "Once upon a time, [...]" io::stdout().flush().unwrap();
}
}), }),
Err(error) => { Err(error) => {
eprintln!("Error processing chunk: {:?}", error) eprintln!("Error processing chunk: {:?}", error)
@@ -41,5 +38,5 @@ async fn main() {
} }
}) })
.await; .await;
print!("\n") // To persist the last chunk output. println!();
} }

View File

@@ -1,10 +1,10 @@
use mistralai_client::v1::{client::Client, constants::EmbedModel}; use mistralai_client::v1::{client::Client, constants::Model};
fn main() { fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client: Client = Client::new(None, None, None, None).unwrap(); let client: Client = Client::new(None, None, None, None).unwrap();
let model = EmbedModel::MistralEmbed; let model = Model::mistral_embed();
let input = vec!["Embed this sentence.", "As well as this one."] let input = vec!["Embed this sentence.", "As well as this one."]
.iter() .iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@@ -13,5 +13,4 @@ fn main() {
let response = client.embeddings(model, input, options).unwrap(); let response = client.embeddings(model, input, options).unwrap();
println!("First Embedding: {:?}", response.data[0]); println!("First Embedding: {:?}", response.data[0]);
// => "First Embedding: {...}"
} }

View File

@@ -1,11 +1,11 @@
use mistralai_client::v1::{client::Client, constants::EmbedModel}; use mistralai_client::v1::{client::Client, constants::Model};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable. // This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client: Client = Client::new(None, None, None, None).unwrap(); let client: Client = Client::new(None, None, None, None).unwrap();
let model = EmbedModel::MistralEmbed; let model = Model::mistral_embed();
let input = vec!["Embed this sentence.", "As well as this one."] let input = vec!["Embed this sentence.", "As well as this one."]
.iter() .iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@@ -17,5 +17,4 @@ async fn main() {
.await .await
.unwrap(); .unwrap();
println!("First Embedding: {:?}", response.data[0]); println!("First Embedding: {:?}", response.data[0]);
// => "First Embedding: {...}"
} }

21
examples/fim.rs Normal file
View File

@@ -0,0 +1,21 @@
use mistralai_client::v1::{
client::Client,
constants::Model,
fim::FimParams,
};
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap();
let model = Model::codestral_latest();
let prompt = "def fibonacci(n):".to_string();
let options = FimParams {
suffix: Some("\n return result".to_string()),
temperature: Some(0.0),
..Default::default()
};
let response = client.fim(model, prompt, Some(options)).unwrap();
println!("Completion: {}", response.choices[0].message.content);
}

25
examples/ocr.rs Normal file
View File

@@ -0,0 +1,25 @@
use mistralai_client::v1::{
client::Client,
constants::Model,
ocr::{OcrDocument, OcrRequest},
};
fn main() {
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
let client = Client::new(None, None, None, None).unwrap();
let request = OcrRequest {
model: Model::mistral_ocr_latest(),
document: OcrDocument::from_url("https://arxiv.org/pdf/2201.04234"),
pages: Some(vec![0]),
table_format: None,
include_image_base64: None,
image_limit: None,
};
let response = client.ocr(&request).unwrap();
for page in &response.pages {
println!("--- Page {} ---", page.index);
println!("{}", &page.markdown[..200.min(page.markdown.len())]);
}
}