{"id":15673882,"url":"https://github.com/ivangabriele/mistralai-client-rs","last_synced_at":"2025-10-05T01:57:32.885Z","repository":{"id":225657007,"uuid":"766529714","full_name":"ivangabriele/mistralai-client-rs","owner":"ivangabriele","description":"Rust client for Mistral AI API.","archived":false,"fork":false,"pushed_at":"2025-03-01T02:22:30.000Z","size":149,"stargazers_count":22,"open_issues_count":11,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-02T16:06:39.364Z","etag":null,"topics":["ai","api","api-client","client","llm","mistral","mistralai","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivangabriele.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-03T14:21:09.000Z","updated_at":"2025-03-27T15:19:43.000Z","dependencies_parsed_at":"2025-01-16T19:13:55.039Z","dependency_job_id":"f352f014-46af-492f-902f-a809a669b5f6","html_url":"https://github.com/ivangabriele/mistralai-client-rs","commit_stats":{"total_commits":82,"total_committers":6,"mean_commits":"13.666666666666666","dds":0.1097560975609756,"last_synced_commit":"9ad6a1dc84f1c4befe07779ff40639b9ed20f45c"},"previous_names":["ivangabriele/mistralai-client-rs"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangabriele%2Fmistralai-client-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangabriele%2Fmistralai-client-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangabriele%2Fmistralai-client-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivangabriele%2Fmistralai-client-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivangabriele","download_url":"https://codeload.github.com/ivangabriele/mistralai-client-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248074942,"owners_count":21043496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","api","api-client","client","llm","mistral","mistralai","rust"],"created_at":"2024-10-03T15:42:43.855Z","updated_at":"2025-10-05T01:57:27.868Z","avatar_url":"https://github.com/ivangabriele.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mistral AI Rust Client\n\n[![Crates.io Package](https://img.shields.io/crates/v/mistralai-client?style=for-the-badge)](https://crates.io/crates/mistralai-client)\n[![Docs.rs Documentation](https://img.shields.io/docsrs/mistralai-client/latest?style=for-the-badge)](https://docs.rs/mistralai-client/latest/mistralai-client)\n[![Test Workflow Status](https://img.shields.io/github/actions/workflow/status/ivangabriele/mistralai-client-rs/test.yml?label=CI\u0026style=for-the-badge)](https://github.com/ivangabriele/mistralai-client-rs/actions?query=branch%3Amain+workflow%3ATest++)\n[![Code Coverage](https://img.shields.io/codecov/c/github/ivangabriele/mistralai-client-rs/main?label=Cov\u0026style=for-the-badge)](https://app.codecov.io/github/ivangabriele/mistralai-client-rs)\n\nRust client for the Mistral AI API.\n\n\u003e [!IMPORTANT]  \n\u003e While we are in v0, minor versions may introduce breaking changes.  \n\u003e Please, refer to the [CHANGELOG.md](./CHANGELOG.md) for more information.\n\n---\n\n- [Supported APIs](#supported-apis)\n- [Installation](#installation)\n  - [Mistral API Key](#mistral-api-key)\n    - [As an environment variable](#as-an-environment-variable)\n    - [As a client argument](#as-a-client-argument)\n- [Usage](#usage)\n  - [Chat](#chat)\n  - [Chat (async)](#chat-async)\n  - [Chat with streaming (async)](#chat-with-streaming-async)\n  - [Chat with Function Calling](#chat-with-function-calling)\n  - [Chat with Function Calling (async)](#chat-with-function-calling-async)\n  - [Embeddings](#embeddings)\n  - [Embeddings (async)](#embeddings-async)\n  - [List models](#list-models)\n  - [List models (async)](#list-models-async)\n- [Contributing](#contributing)\n\n---\n\n## Supported APIs\n\n- [x] Chat without streaming\n- [x] Chat without streaming (async)\n- [x] Chat with streaming\n- [x] Embedding\n- [x] Embedding (async)\n- [x] List models\n- [x] List models (async)\n- [x] Function Calling\n- [x] Function Calling (async)\n\n## Installation\n\nYou can install the library in your project using:\n\n```sh\ncargo add mistralai-client\n```\n\n### Mistral API Key\n\nYou can get your Mistral API Key there: \u003chttps://docs.mistral.ai/#api-access\u003e.\n\n#### As an environment variable\n\nJust set the `MISTRAL_API_KEY` environment variable.\n\n```rs\nuse mistralai_client::v1::client::Client;\n\nfn main() {\n    let client = Client::new(None, None, None, None);\n}\n```\n\n```sh\nMISTRAL_API_KEY=your_api_key cargo run\n```\n\n#### As a client argument\n\n```rs\nuse mistralai_client::v1::client::Client;\n\nfn main() {\n    let api_key = \"your_api_key\";\n\n    let client = Client::new(Some(api_key), None, None, None).unwrap();\n}\n```\n\n## Usage\n\n### Chat\n\n```rs\nuse mistralai_client::v1::{\n    chat::{ChatMessage, ChatMessageRole, ChatParams},\n    client::Client,\n    constants::Model,\n};\n\nfn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client = Client::new(None, None, None, None).unwrap();\n\n    let model = Model::OpenMistral7b;\n    let messages = vec![ChatMessage {\n        role: ChatMessageRole::User,\n        content: \"Just guess the next word: \\\"Eiffel ...\\\"?\".to_string(),\n        tool_calls: None,\n    }];\n    let options = ChatParams {\n        temperature: 0.0,\n        random_seed: Some(42),\n        ..Default::default()\n    };\n\n    let result = client.chat(model, messages, Some(options)).unwrap();\n    println!(\"Assistant: {}\", result.choices[0].message.content);\n    // =\u003e \"Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France.\"\n}\n```\n\n### Chat (async)\n\n```rs\nuse mistralai_client::v1::{\n    chat::{ChatMessage, ChatMessageRole, ChatParams},\n    client::Client,\n    constants::Model,\n};\n\n#[tokio::main]\nasync fn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client = Client::new(None, None, None, None).unwrap();\n\n    let model = Model::OpenMistral7b;\n    let messages = vec![ChatMessage {\n        role: ChatMessageRole::User,\n        content: \"Just guess the next word: \\\"Eiffel ...\\\"?\".to_string(),\n        tool_calls: None,\n    }];\n    let options = ChatParams {\n        temperature: 0.0,\n        random_seed: Some(42),\n        ..Default::default()\n    };\n\n    let result = client\n        .chat_async(model, messages, Some(options))\n        .await\n        .unwrap();\n    println!(\n        \"{:?}: {}\",\n        result.choices[0].message.role, result.choices[0].message.content\n    );\n    // =\u003e \"Assistant: Tower. The Eiffel Tower is a famous landmark in Paris, France.\"\n}\n```\n\n### Chat with streaming (async)\n\n```rs\nuse futures::stream::StreamExt;\nuse mistralai_client::v1::{\n    chat::{ChatMessage, ChatMessageRole, ChatParams},\n    client::Client,\n    constants::Model,\n};\nuse std::io::{self, Write};\n\n#[tokio::main]\nasync fn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client = Client::new(None, None, None, None).unwrap();\n\n    let model = Model::OpenMistral7b;\n    let messages = vec![ChatMessage {\n        role: ChatMessageRole::User,\n        content: \"Tell me a short happy story.\".to_string(),\n        tool_calls: None,\n    }];\n    let options = ChatParams {\n        temperature: 0.0,\n        random_seed: Some(42),\n        ..Default::default()\n    };\n\n    let stream_result = client\n        .chat_stream(model, messages, Some(options))\n        .await\n        .unwrap();\n    stream_result\n        .for_each(|chunk_result| async {\n            match chunk_result {\n                Ok(chunks) =\u003e chunks.iter().for_each(|chunk| {\n                    print!(\"{}\", chunk.choices[0].delta.content);\n                    io::stdout().flush().unwrap();\n                    // =\u003e \"Once upon a time, [...]\"\n                }),\n                Err(error) =\u003e {\n                    eprintln!(\"Error processing chunk: {:?}\", error)\n                }\n            }\n        })\n        .await;\n    print!(\"\\n\") // To persist the last chunk output.\n}\n```\n\n### Chat with Function Calling\n\n```rs\nuse mistralai_client::v1::{\n    chat::{ChatMessage, ChatMessageRole, ChatParams},\n    client::Client,\n    constants::Model,\n    tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},\n};\nuse serde::Deserialize;\nuse std::any::Any;\n\n#[derive(Debug, Deserialize)]\nstruct GetCityTemperatureArguments {\n    city: String,\n}\n\nstruct GetCityTemperatureFunction;\n#[async_trait::async_trait]\nimpl Function for GetCityTemperatureFunction {\n    async fn execute(\u0026self, arguments: String) -\u003e Box\u003cdyn Any + Send\u003e {\n        // Deserialize arguments, perform the logic, and return the result\n        let GetCityTemperatureArguments { city } = serde_json::from_str(\u0026arguments).unwrap();\n\n        let temperature = match city.as_str() {\n            \"Paris\" =\u003e \"20°C\",\n            _ =\u003e \"Unknown city\",\n        };\n\n        Box::new(temperature.to_string())\n    }\n}\n\nfn main() {\n    let tools = vec![Tool::new(\n        \"get_city_temperature\".to_string(),\n        \"Get the current temperature in a city.\".to_string(),\n        vec![ToolFunctionParameter::new(\n            \"city\".to_string(),\n            \"The name of the city.\".to_string(),\n            ToolFunctionParameterType::String,\n        )],\n    )];\n\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let mut client = Client::new(None, None, None, None).unwrap();\n    client.register_function(\n        \"get_city_temperature\".to_string(),\n        Box::new(GetCityTemperatureFunction),\n    );\n\n    let model = Model::MistralSmallLatest;\n    let messages = vec![ChatMessage {\n        role: ChatMessageRole::User,\n        content: \"What's the temperature in Paris?\".to_string(),\n        tool_calls: None,\n    }];\n    let options = ChatParams {\n        temperature: 0.0,\n        random_seed: Some(42),\n        tool_choice: Some(ToolChoice::Auto),\n        tools: Some(tools),\n        ..Default::default()\n    };\n\n    client.chat(model, messages, Some(options)).unwrap();\n    let temperature = client\n        .get_last_function_call_result()\n        .unwrap()\n        .downcast::\u003cString\u003e()\n        .unwrap();\n    println!(\"The temperature in Paris is: {}.\", temperature);\n    // =\u003e \"The temperature in Paris is: 20°C.\"\n}\n```\n\n### Chat with Function Calling (async)\n\n```rs\nuse mistralai_client::v1::{\n    chat::{ChatMessage, ChatMessageRole, ChatParams},\n    client::Client,\n    constants::Model,\n    tool::{Function, Tool, ToolChoice, ToolFunctionParameter, ToolFunctionParameterType},\n};\nuse serde::Deserialize;\nuse std::any::Any;\n\n#[derive(Debug, Deserialize)]\nstruct GetCityTemperatureArguments {\n    city: String,\n}\n\nstruct GetCityTemperatureFunction;\n#[async_trait::async_trait]\nimpl Function for GetCityTemperatureFunction {\n    async fn execute(\u0026self, arguments: String) -\u003e Box\u003cdyn Any + Send\u003e {\n        // Deserialize arguments, perform the logic, and return the result\n        let GetCityTemperatureArguments { city } = serde_json::from_str(\u0026arguments).unwrap();\n\n        let temperature = match city.as_str() {\n            \"Paris\" =\u003e \"20°C\",\n            _ =\u003e \"Unknown city\",\n        };\n\n        Box::new(temperature.to_string())\n    }\n}\n\n#[tokio::main]\nasync fn main() {\n    let tools = vec![Tool::new(\n        \"get_city_temperature\".to_string(),\n        \"Get the current temperature in a city.\".to_string(),\n        vec![ToolFunctionParameter::new(\n            \"city\".to_string(),\n            \"The name of the city.\".to_string(),\n            ToolFunctionParameterType::String,\n        )],\n    )];\n\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let mut client = Client::new(None, None, None, None).unwrap();\n    client.register_function(\n        \"get_city_temperature\".to_string(),\n        Box::new(GetCityTemperatureFunction),\n    );\n\n    let model = Model::MistralSmallLatest;\n    let messages = vec![ChatMessage {\n        role: ChatMessageRole::User,\n        content: \"What's the temperature in Paris?\".to_string(),\n        tool_calls: None,\n    }];\n    let options = ChatParams {\n        temperature: 0.0,\n        random_seed: Some(42),\n        tool_choice: Some(ToolChoice::Auto),\n        tools: Some(tools),\n        ..Default::default()\n    };\n\n    client\n        .chat_async(model, messages, Some(options))\n        .await\n        .unwrap();\n    let temperature = client\n        .get_last_function_call_result()\n        .unwrap()\n        .downcast::\u003cString\u003e()\n        .unwrap();\n    println!(\"The temperature in Paris is: {}.\", temperature);\n    // =\u003e \"The temperature in Paris is: 20°C.\"\n}\n```\n\n### Embeddings\n\n```rs\nuse mistralai_client::v1::{client::Client, constants::EmbedModel};\n\nfn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client: Client = Client::new(None, None, None, None).unwrap();\n\n    let model = EmbedModel::MistralEmbed;\n    let input = vec![\"Embed this sentence.\", \"As well as this one.\"]\n        .iter()\n        .map(|s| s.to_string())\n        .collect();\n    let options = None;\n\n    let response = client.embeddings(model, input, options).unwrap();\n    println!(\"First Embedding: {:?}\", response.data[0]);\n    // =\u003e \"First Embedding: {...}\"\n}\n```\n\n### Embeddings (async)\n\n```rs\nuse mistralai_client::v1::{client::Client, constants::EmbedModel};\n\n#[tokio::main]\nasync fn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client: Client = Client::new(None, None, None, None).unwrap();\n\n    let model = EmbedModel::MistralEmbed;\n    let input = vec![\"Embed this sentence.\", \"As well as this one.\"]\n        .iter()\n        .map(|s| s.to_string())\n        .collect();\n    let options = None;\n\n    let response = client\n        .embeddings_async(model, input, options)\n        .await\n        .unwrap();\n    println!(\"First Embedding: {:?}\", response.data[0]);\n    // =\u003e \"First Embedding: {...}\"\n}\n```\n\n### List models\n\n```rs\nuse mistralai_client::v1::client::Client;\n\nfn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client = Client::new(None, None, None, None).unwrap();\n\n    let result = client.list_models().unwrap();\n    println!(\"First Model ID: {:?}\", result.data[0].id);\n    // =\u003e \"First Model ID: open-mistral-7b\"\n}\n```\n\n### List models (async)\n\n```rs\nuse mistralai_client::v1::client::Client;\n\n#[tokio::main]\nasync fn main() {\n    // This example suppose you have set the `MISTRAL_API_KEY` environment variable.\n    let client = Client::new(None, None, None, None).unwrap();\n\n    let result = client.list_models_async().await.unwrap();\n    println!(\"First Model ID: {:?}\", result.data[0].id);\n    // =\u003e \"First Model ID: open-mistral-7b\"\n}\n```\n\n## Contributing\n\nPlease read [CONTRIBUTING.md](./CONTRIBUTING.md) for details on how to contribute to this library.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivangabriele%2Fmistralai-client-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivangabriele%2Fmistralai-client-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivangabriele%2Fmistralai-client-rs/lists"}