2024-03-09 11:28:50 +01:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
use mistralai_client::v1::{
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +00:00
|
|
|
chat::{ChatMessage, ChatParams},
|
2024-03-09 11:28:50 +01:00
|
|
|
client::Client,
|
|
|
|
|
constants::Model,
|
|
|
|
|
};
|
|
|
|
|
use std::io::{self, Write};
|
|
|
|
|
|
|
|
|
|
#[tokio::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();
|
|
|
|
|
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +00:00
|
|
|
let model = Model::mistral_small_latest();
|
|
|
|
|
let messages = vec![ChatMessage::new_user_message("Tell me a short happy story.")];
|
2024-03-09 11:28:50 +01:00
|
|
|
let options = ChatParams {
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +00:00
|
|
|
temperature: Some(0.0),
|
2024-03-09 11:28:50 +01:00
|
|
|
random_seed: Some(42),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let stream_result = client
|
|
|
|
|
.chat_stream(model, messages, Some(options))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
stream_result
|
|
|
|
|
.for_each(|chunk_result| async {
|
|
|
|
|
match chunk_result {
|
|
|
|
|
Ok(chunks) => chunks.iter().for_each(|chunk| {
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +00:00
|
|
|
if let Some(content) = &chunk.choices[0].delta.content {
|
|
|
|
|
print!("{}", content);
|
|
|
|
|
io::stdout().flush().unwrap();
|
|
|
|
|
}
|
2024-03-09 11:28:50 +01:00
|
|
|
}),
|
|
|
|
|
Err(error) => {
|
|
|
|
|
eprintln!("Error processing chunk: {:?}", error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.await;
|
Update to latest Mistral AI API (v1.0.0)
- Replace closed Model enum with flexible string-based Model type
with constructor methods for all current models (Mistral Large 3,
Small 4, Magistral, Codestral, Devstral, Pixtral, Voxtral, etc.)
- Add new API endpoints: FIM completions, Files, Fine-tuning, Batch
jobs, OCR, Audio transcription, Moderations/Classifications, and
Agent completions (sync + async for all)
- Add new chat fields: frequency_penalty, presence_penalty, stop,
n, parallel_tool_calls, reasoning_effort, min_tokens, json_schema
response format
- Add embedding fields: output_dimension, output_dtype
- Tool parameters now accept raw JSON Schema (serde_json::Value)
instead of limited enum types
- Add tool call IDs and Required tool choice variant
- Add DELETE HTTP method support and multipart file upload
- Bump thiserror to v2, add reqwest multipart feature
- Remove strum dependency (no longer needed)
- Update all tests and examples for new API
2026-03-20 17:16:26 +00:00
|
|
|
println!();
|
2024-03-09 11:28:50 +01:00
|
|
|
}
|