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:
@@ -1,5 +1,5 @@
|
||||
use mistralai_client::v1::{
|
||||
chat::{ChatMessage, ChatMessageRole, ChatParams},
|
||||
chat::{ChatMessage, ChatParams},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
};
|
||||
@@ -8,14 +8,12 @@ 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::OpenMistral7b;
|
||||
let messages = vec![ChatMessage {
|
||||
role: ChatMessageRole::User,
|
||||
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
|
||||
tool_calls: None,
|
||||
}];
|
||||
let model = Model::mistral_small_latest();
|
||||
let messages = vec![ChatMessage::new_user_message(
|
||||
"Just guess the next word: \"Eiffel ...\"?",
|
||||
)];
|
||||
let options = ChatParams {
|
||||
temperature: 0.0,
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use mistralai_client::v1::{
|
||||
chat::{ChatMessage, ChatMessageRole, ChatParams},
|
||||
chat::{ChatMessage, ChatParams},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
};
|
||||
@@ -9,14 +9,12 @@ 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 model = Model::OpenMistral7b;
|
||||
let messages = vec![ChatMessage {
|
||||
role: ChatMessageRole::User,
|
||||
content: "Just guess the next word: \"Eiffel ...\"?".to_string(),
|
||||
tool_calls: None,
|
||||
}];
|
||||
let model = Model::mistral_small_latest();
|
||||
let messages = vec![ChatMessage::new_user_message(
|
||||
"Just guess the next word: \"Eiffel ...\"?",
|
||||
)];
|
||||
let options = ChatParams {
|
||||
temperature: 0.0,
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
..Default::default()
|
||||
};
|
||||
@@ -29,5 +27,4 @@ async fn main() {
|
||||
"{:?}: {}",
|
||||
result.choices[0].message.role, result.choices[0].message.content
|
||||
);
|
||||
// => "Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France."
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use mistralai_client::v1::{
|
||||
chat::{ChatMessage, ChatMessageRole, ChatParams},
|
||||
chat::{ChatMessage, ChatParams},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},
|
||||
tool::{Function, Tool, ToolChoice},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::any::Any;
|
||||
@@ -16,7 +16,6 @@ struct GetCityTemperatureFunction;
|
||||
#[async_trait::async_trait]
|
||||
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() {
|
||||
@@ -32,11 +31,16 @@ 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,
|
||||
)],
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string",
|
||||
"description": "The name of the city."
|
||||
}
|
||||
},
|
||||
"required": ["city"]
|
||||
}),
|
||||
)];
|
||||
|
||||
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
||||
@@ -46,14 +50,12 @@ fn main() {
|
||||
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 model = Model::mistral_small_latest();
|
||||
let messages = vec![ChatMessage::new_user_message(
|
||||
"What's the temperature in Paris?",
|
||||
)];
|
||||
let options = ChatParams {
|
||||
temperature: 0.0,
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
tool_choice: Some(ToolChoice::Auto),
|
||||
tools: Some(tools),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use mistralai_client::v1::{
|
||||
chat::{ChatMessage, ChatMessageRole, ChatParams},
|
||||
chat::{ChatMessage, ChatParams},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},
|
||||
tool::{Function, Tool, ToolChoice},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use std::any::Any;
|
||||
@@ -16,7 +16,6 @@ struct GetCityTemperatureFunction;
|
||||
#[async_trait::async_trait]
|
||||
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() {
|
||||
@@ -33,11 +32,16 @@ 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,
|
||||
)],
|
||||
serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string",
|
||||
"description": "The name of the city."
|
||||
}
|
||||
},
|
||||
"required": ["city"]
|
||||
}),
|
||||
)];
|
||||
|
||||
// This example suppose you have set the `MISTRAL_API_KEY` environment variable.
|
||||
@@ -47,14 +51,12 @@ async fn main() {
|
||||
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 model = Model::mistral_small_latest();
|
||||
let messages = vec![ChatMessage::new_user_message(
|
||||
"What's the temperature in Paris?",
|
||||
)];
|
||||
let options = ChatParams {
|
||||
temperature: 0.0,
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
tool_choice: Some(ToolChoice::Auto),
|
||||
tools: Some(tools),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use futures::stream::StreamExt;
|
||||
use mistralai_client::v1::{
|
||||
chat::{ChatMessage, ChatMessageRole, ChatParams},
|
||||
chat::{ChatMessage, ChatParams},
|
||||
client::Client,
|
||||
constants::Model,
|
||||
};
|
||||
@@ -11,14 +11,10 @@ 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 model = Model::OpenMistral7b;
|
||||
let messages = vec![ChatMessage {
|
||||
role: ChatMessageRole::User,
|
||||
content: "Tell me a short happy story.".to_string(),
|
||||
tool_calls: None,
|
||||
}];
|
||||
let model = Model::mistral_small_latest();
|
||||
let messages = vec![ChatMessage::new_user_message("Tell me a short happy story.")];
|
||||
let options = ChatParams {
|
||||
temperature: 0.0,
|
||||
temperature: Some(0.0),
|
||||
random_seed: Some(42),
|
||||
..Default::default()
|
||||
};
|
||||
@@ -31,9 +27,10 @@ async fn main() {
|
||||
.for_each(|chunk_result| async {
|
||||
match chunk_result {
|
||||
Ok(chunks) => chunks.iter().for_each(|chunk| {
|
||||
print!("{}", chunk.choices[0].delta.content);
|
||||
io::stdout().flush().unwrap();
|
||||
// => "Once upon a time, [...]"
|
||||
if let Some(content) = &chunk.choices[0].delta.content {
|
||||
print!("{}", content);
|
||||
io::stdout().flush().unwrap();
|
||||
}
|
||||
}),
|
||||
Err(error) => {
|
||||
eprintln!("Error processing chunk: {:?}", error)
|
||||
@@ -41,5 +38,5 @@ async fn main() {
|
||||
}
|
||||
})
|
||||
.await;
|
||||
print!("\n") // To persist the last chunk output.
|
||||
println!();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use mistralai_client::v1::{client::Client, constants::EmbedModel};
|
||||
use mistralai_client::v1::{client::Client, constants::Model};
|
||||
|
||||
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 model = Model::mistral_embed();
|
||||
let input = vec!["Embed this sentence.", "As well as this one."]
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
@@ -13,5 +13,4 @@ fn main() {
|
||||
|
||||
let response = client.embeddings(model, input, options).unwrap();
|
||||
println!("First Embedding: {:?}", response.data[0]);
|
||||
// => "First Embedding: {...}"
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use mistralai_client::v1::{client::Client, constants::EmbedModel};
|
||||
use mistralai_client::v1::{client::Client, constants::Model};
|
||||
|
||||
#[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 model = Model::mistral_embed();
|
||||
let input = vec!["Embed this sentence.", "As well as this one."]
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
@@ -17,5 +17,4 @@ async fn main() {
|
||||
.await
|
||||
.unwrap();
|
||||
println!("First Embedding: {:?}", response.data[0]);
|
||||
// => "First Embedding: {...}"
|
||||
}
|
||||
|
||||
21
examples/fim.rs
Normal file
21
examples/fim.rs
Normal 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
25
examples/ocr.rs
Normal 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())]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user