SDKs overview
Mistral AI provides programmatic access to its suite of generative AI models and embedding services primarily through a RESTful API. To facilitate integration and simplify development, Mistral AI offers an official Software Development Kit (SDK) for Python. This SDK encapsulates the HTTP requests and responses, providing a Pythonic interface for interacting with models such as Mistral Large, Mistral Small, and Mistral Tiny for chat completions, and Mistral Embed for generating text embeddings.
The SDK is designed to streamline common tasks, including sending prompts for text generation, managing conversation history, and processing model outputs. While the primary interface is the official Python SDK, the underlying API is accessible via standard HTTP requests, meaning any language capable of making web requests can interact with Mistral AI's services. The approach aligns with common industry practices for machine learning APIs, where a core REST API is complemented by language-specific SDKs to enhance developer experience.
Official SDKs by language
Mistral AI currently maintains one official SDK to interact with its hosted models.
| Language | Package Name | Install Command | Maturity |
|---|---|---|---|
| Python | mistralai |
pip install mistralai |
Stable |
This Python SDK provides classes and methods for authenticating with the Mistral AI API, sending requests to the chat completion endpoint, and generating embeddings. Developers using Python can leverage this SDK to abstract away the details of HTTP requests, error handling, and data serialization, allowing them to focus on application logic. For detailed usage, refer to the Mistral AI API reference.
Installation
To use the official Mistral AI Python SDK, you must first install it using pip, the Python package installer. Ensure you have a compatible Python version installed (typically Python 3.8+). If you need to install Python, consult the official Python downloads page for instructions.
Prerequisites
- Python 3.8 or higher.
- An internet connection to install the package.
- A Mistral AI API key for authentication, which can be obtained after signing up on the Mistral AI website.
Installation steps
-
Open your terminal or command prompt.
-
Install the
mistralaipackage:pip install mistralaiThis command downloads and installs the latest version of the official Mistral AI SDK along with its dependencies.
-
Verify the installation (optional):
You can verify that the package was installed correctly by trying to import it in a Python interpreter:
import mistralai print(mistralai.__version__)If no errors occur and a version number is displayed, the installation was successful.
Quickstart example
The following Python code demonstrates how to use the mistralai SDK to generate a chat completion and an embedding. Before running, replace YOUR_MISTRAL_API_KEY with your actual API key.
Setting up your API key securely
It is recommended to load your API key from an environment variable rather than hardcoding it into your script. This practice enhances security and portability. For example, you can set an environment variable named MISTRAL_API_KEY.
export MISTRAL_API_KEY="your-actual-api-key-here"
Example: Chat completion
This snippet demonstrates how to send a prompt to the Mistral Large model for a chat completion. The chat method is used to simulate a conversation.
import os
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
# Initialize the client with your API key from an environment variable
api_key = os.environ.get("MISTRAL_API_KEY")
if not api_key:
raise ValueError("MISTRAL_API_KEY environment variable not set.")
client = MistralClient(api_key=api_key)
messages = [
ChatMessage(role="user", content="What is the capital of France?")
]
try:
# Request a chat completion from Mistral Large
chat_response = client.chat(model="mistral-large-latest", messages=messages)
print("Chat Completion:")
print(chat_response.choices[0].message.content)
except Exception as e:
print(f"An error occurred during chat completion: {e}")
Example: Embedding generation
This snippet illustrates how to generate embeddings for a list of text inputs using the mistral-embed model. Embeddings are numerical representations of text that capture semantic meaning, useful for tasks like search, recommendation, and clustering.
import os
from mistralai.client import MistralClient
# Initialize the client with your API key
api_key = os.environ.get("MISTRAL_API_KEY")
if not api_key:
raise ValueError("MISTRAL_API_KEY environment variable not set.")
client = MistralClient(api_key=api_key)
input_texts = [
"Hello, world!",
"How are you today?",
"Mistral AI provides powerful LLMs."
]
try:
# Request embeddings from Mistral Embed
embeddings_response = client.embeddings(model="mistral-embed", input=input_texts)
print("\nEmbeddings:")
for i, embedding in enumerate(embeddings_response.data):
print(f"Embedding for '{input_texts[i]}': {embedding.embedding[:10]}...") # Print first 10 elements
except Exception as e:
print(f"An error occurred during embedding generation: {e}")
These examples provide a foundation for integrating Mistral AI's capabilities into Python applications. For more advanced features and error handling, consult the Mistral AI documentation.
Community libraries
While Mistral AI provides an official Python SDK, the developer community often creates additional libraries and integrations to extend functionality or provide support for other programming languages. These community-driven efforts can range from wrappers in languages without official SDKs to specialized tools that integrate Mistral AI models into existing frameworks.
Examples of common community contributions include:
- Client libraries in other languages: Developers might create unofficial client libraries for languages like JavaScript, Go, or Ruby, mirroring the functionality of the official Python SDK by making direct HTTP calls to the Mistral AI API.
- Integrations with AI frameworks: Libraries that integrate Mistral AI models into popular AI development frameworks, such as LangChain or LlamaIndex, allow developers to build more complex applications like retrieval-augmented generation (RAG) systems or autonomous agents. For instance, LangChain provides a Mistral AI LLM integration, allowing its users to interact with Mistral AI models within the LangChain ecosystem.
- CLI tools: Command-line interface tools built by the community can provide quick ways to test prompts or interact with Mistral AI models without writing full scripts.
- Fine-tuning utilities: While Mistral AI offers fine-tuning capabilities, community tools might streamline data preparation or model deployment processes.
Community libraries are typically open-source and can be found on platforms like GitHub. Developers should evaluate the maturity, maintenance, and security practices of any unofficial library before integrating it into production systems. Checking the project's documentation, issue tracker, and community activity can provide insights into its reliability and ongoing support.
For the most up-to-date and officially supported integration methods, refer to the official Mistral AI developer documentation.