Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .genignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pyproject.toml
examples/*
/utils/*
src/mistral/extra/*
src/mistralai/extra/*
pylintrc
scripts/prepare_readme.py
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ repos:
hooks:
- id: ruff
args: [--fix]
files: ^(example/|src/mistralai/).*\.py$
files: ^(examples/|src/mistralai/|packages/(azure|gcp)/src/mistralai/).*\.py$
exclude: ^src/mistralai/(__init__|sdkhooks|types)\.py$
- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.401
hooks:
- id: pyright
files: ^(example/|src/mistralai/).*\.py$
files: ^(examples/|src/mistralai/|packages/(azure|gcp)/src/mistralai/).*\.py$
exclude: ^src/mistralai/(__init__|sdkhooks|types)\.py$
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.15.0
hooks:
- id: mypy
files: ^(example/|src/mistralai/).*\.py$
files: ^(examples/|src/mistralai/|packages/(azure|gcp)/src/mistralai/).*\.py$
exclude: ^src/mistralai/(__init__|sdkhooks|types)\.py$
26 changes: 13 additions & 13 deletions .speakeasy/workflow.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ speakeasyVersion: 1.685.0
sources:
mistral-azure-source:
sourceNamespace: mistral-openapi-azure
sourceRevisionDigest: sha256:d303e640ad565cc8a9801519b20dc7eab226efdfdab951c11256962d9e479f74
sourceBlobDigest: sha256:6e4c789de61b2c9c604bf581e0abbadae90e360491d95ec4247678f4f70cee87
sourceRevisionDigest: sha256:e32d21a6317d1bca6ab29f05603b96038e841752c2698aab47f434ea0d6530b7
sourceBlobDigest: sha256:2dad2b1b7a79de6917c363ce7e870d11efe31ac08e3bfe0258f72823fe1ad13e
tags:
- latest
mistral-google-cloud-source:
sourceNamespace: mistral-openapi-google-cloud
sourceRevisionDigest: sha256:351c4d392b8b2220c337a207e98ed5665ed27fd85de854871a70c4bc2b9c0784
sourceBlobDigest: sha256:d79b21f70efb93b0cd261d2044939a288beaf8707a7caae86aca5c4d5de3821b
sourceRevisionDigest: sha256:4d9938ab74c4d41d62cd24234c8b8109e286c4aeec093e21d369259a43173113
sourceBlobDigest: sha256:5a558d5ea7a936723c7a5540db5a1fba63d85d25b453372e1cf16395b30c98d3
tags:
- latest
mistral-openapi:
Expand All @@ -22,24 +22,24 @@ targets:
mistralai-azure-sdk:
source: mistral-azure-source
sourceNamespace: mistral-openapi-azure
sourceRevisionDigest: sha256:d303e640ad565cc8a9801519b20dc7eab226efdfdab951c11256962d9e479f74
sourceBlobDigest: sha256:6e4c789de61b2c9c604bf581e0abbadae90e360491d95ec4247678f4f70cee87
sourceRevisionDigest: sha256:e32d21a6317d1bca6ab29f05603b96038e841752c2698aab47f434ea0d6530b7
sourceBlobDigest: sha256:2dad2b1b7a79de6917c363ce7e870d11efe31ac08e3bfe0258f72823fe1ad13e
codeSamplesNamespace: mistral-openapi-azure-code-samples
codeSamplesRevisionDigest: sha256:0109302b87fa17b0103ef1e372fae76356811b3c552103e659bd5373d537d759
codeSamplesRevisionDigest: sha256:a34c3049c604d0bb67101d042e959f14098964fe784f98975a9201c84dbf44d0
mistralai-gcp-sdk:
source: mistral-google-cloud-source
sourceNamespace: mistral-openapi-google-cloud
sourceRevisionDigest: sha256:351c4d392b8b2220c337a207e98ed5665ed27fd85de854871a70c4bc2b9c0784
sourceBlobDigest: sha256:d79b21f70efb93b0cd261d2044939a288beaf8707a7caae86aca5c4d5de3821b
sourceRevisionDigest: sha256:4d9938ab74c4d41d62cd24234c8b8109e286c4aeec093e21d369259a43173113
sourceBlobDigest: sha256:5a558d5ea7a936723c7a5540db5a1fba63d85d25b453372e1cf16395b30c98d3
codeSamplesNamespace: mistral-openapi-google-cloud-code-samples
codeSamplesRevisionDigest: sha256:09bb7cbf291076170d228116db05d1c9606af541b301b6564609c4d76633258a
codeSamplesRevisionDigest: sha256:fa36e5999e79c32e8b2c1317cc0d6ed179912ced15194f02b5f80da22e45ae5f
mistralai-sdk:
source: mistral-openapi
sourceNamespace: mistral-openapi
sourceRevisionDigest: sha256:74d0de7750f6a1878b68c9da683eba7a447d7c367131d0cb8f5c3b1e05829624
sourceBlobDigest: sha256:41e8354c48993fc29be68959d835ea4f8e0cc1d4b4fbd527afcd970bc02c62a2
codeSamplesNamespace: mistral-openapi-code-samples
codeSamplesRevisionDigest: sha256:f37fb6188ad25957bef4cadaa03f454a4f9ab0c045db633a46d9cc89af145ba2
codeSamplesRevisionDigest: sha256:99fcae1bc81801e3825648a44f5ffa62a8f124e3186e5570be40414de164e7f2
workflow:
workflowVersion: 1.0.0
speakeasyVersion: 1.685.0
Expand All @@ -57,7 +57,7 @@ workflow:
mistralai-azure-sdk:
target: python
source: mistral-azure-source
output: ./packages/mistralai_azure
output: ./packages/azure
publish:
pypi:
token: $pypi_token
Expand All @@ -68,7 +68,7 @@ workflow:
mistralai-gcp-sdk:
target: python
source: mistral-google-cloud-source
output: ./packages/mistralai_gcp
output: ./packages/gcp
publish:
pypi:
token: $pypi_token
Expand Down
4 changes: 2 additions & 2 deletions .speakeasy/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets:
mistralai-azure-sdk:
target: python
source: mistral-azure-source
output: ./packages/mistralai_azure
output: ./packages/azure
publish:
pypi:
token: $pypi_token
Expand All @@ -25,7 +25,7 @@ targets:
mistralai-gcp-sdk:
target: python
source: mistral-google-cloud-source
output: ./packages/mistralai_gcp
output: ./packages/gcp
publish:
pypi:
token: $pypi_token
Expand Down
20 changes: 20 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ from mistralai.client.types import BaseModel
| `from mistralai.types import ...` | `from mistralai.client.types import ...` |
| `from mistralai.utils import ...` | `from mistralai.client.utils import ...` |

### Azure & GCP Import Changes

Azure and GCP SDKs now live under the `mistralai` namespace as separate distributions:

| v1 | v2 |
|---|---|
| `from mistralai_azure import MistralAzure` | `from mistralai.azure.client import MistralAzure` |
| `from mistralai_azure.models import ...` | `from mistralai.azure.client.models import ...` |
| `from mistralai_gcp import MistralGoogleCloud` | `from mistralai.gcp.client import MistralGCP` |
| `from mistralai_gcp.models import ...` | `from mistralai.gcp.client.models import ...` |

#### Installation Changes

| v1 | v2 |
|---|---|
| `pip install mistralai` | `pip install mistralai` (includes Azure and GCP) |
| `pip install mistralai[gcp]` (for GCP auth) | `pip install "mistralai[gcp]"` (for GCP auth dependencies) |

Azure and GCP are now standalone distributions that can be installed independently of the core SDK. The `mistralai[azure]` and `mistralai[gcp]` extras are syntactic sugar that pull in the respective distributions.

### What Stays the Same

- The `Mistral` client API is unchanged
Expand Down
98 changes: 52 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ It's also possible to write a standalone Python script without needing to set up
```python
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.9"
# requires-python = ">=3.10"
# dependencies = [
# "mistralai",
# ]
Expand Down Expand Up @@ -369,38 +369,41 @@ You can run the examples in the `examples/` directory using `uv run`.

**Prerequisites**

Before you begin, ensure you have `AZUREAI_ENDPOINT` and an `AZURE_API_KEY`. To obtain these, you will need to deploy Mistral on Azure AI.
Before you begin, ensure you have `AZURE_ENDPOINT` and an `AZURE_API_KEY`. To obtain these, you will need to deploy Mistral on Azure AI.
See [instructions for deploying Mistral on Azure AI here](https://docs.mistral.ai/deployment/cloud/azure/).

**Step 1: Install**

```bash
pip install mistralai
```

**Step 2: Example Usage**

Here's a basic example to get you started. You can also run [the example in the `examples` directory](/examples/azure).

```python
import asyncio
import os
from mistralai.azure.client import MistralAzure

from mistralai_azure import MistralAzure

# The SDK automatically injects api-version as a query parameter
client = MistralAzure(
azure_api_key=os.getenv("AZURE_API_KEY", ""),
azure_endpoint=os.getenv("AZURE_ENDPOINT", "")
api_key=os.environ["AZURE_API_KEY"],
server_url=os.environ["AZURE_ENDPOINT"],
api_version="2024-05-01-preview", # Optional, this is the default
)

async def main() -> None:
res = await client.chat.complete_async(
max_tokens= 100,
temperature= 0.5,
messages= [
{
"content": "Hello there!",
"role": "user"
}
]
)
print(res)

asyncio.run(main())
res = client.chat.complete(
model=os.environ["AZURE_MODEL"],
messages=[
{
"role": "user",
"content": "Hello there!",
}
],
)
print(res.choices[0].message.content)
```
The documentation for the Azure SDK is available [here](packages/mistralai_azure/README.md).

### Google Cloud

Expand All @@ -417,40 +420,43 @@ gcloud auth application-default login

**Step 1: Install**

Install the extras dependencies specific to Google Cloud:

```bash
pip install mistralai
# For GCP authentication support (required):
pip install "mistralai[gcp]"
```

**Step 2: Example Usage**

Here's a basic example to get you started.

```python
import asyncio
from mistralai_gcp import MistralGoogleCloud
Here's a basic example to get you started. You can also run [the example in the `examples` directory](/examples/gcp).

client = MistralGoogleCloud()
The SDK automatically:
- Detects credentials via `google.auth.default()`
- Auto-refreshes tokens when they expire
- Builds the Vertex AI URL from `project_id` and `region`

```python
import os
from mistralai.gcp.client import MistralGCP

async def main() -> None:
res = await client.chat.complete_async(
model= "mistral-small-2402",
messages= [
{
"content": "Hello there!",
"role": "user"
}
]
)
print(res)
# The SDK auto-detects credentials and builds the Vertex AI URL
client = MistralGCP(
project_id=os.environ.get("GCP_PROJECT_ID"), # Optional: auto-detected from credentials
region="us-central1", # Default: europe-west4
)

asyncio.run(main())
res = client.chat.complete(
model="mistral-small-2503",
messages=[
{
"role": "user",
"content": "Hello there!",
}
],
)
print(res.choices[0].message.content)
```

The documentation for the GCP SDK is available [here](packages/mistralai_gcp/README.md).


<!-- Start Available Resources and Operations [operations] -->
## Available Resources and Operations
Expand Down Expand Up @@ -669,8 +675,8 @@ with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:

res = mistral.models.list(,
RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))
res = mistral.models.list(
retries=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))

# Handle response
print(res)
Expand Down
4 changes: 4 additions & 0 deletions examples/azure/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AZURE_API_KEY=your-azure-api-key
AZURE_ENDPOINT=https://your-endpoint.services.ai.azure.com/models
AZURE_MODEL=your-deployment-name
AZURE_API_VERSION=2024-05-01-preview
15 changes: 0 additions & 15 deletions examples/azure/az_chat_no_streaming.py

This file was deleted.

19 changes: 13 additions & 6 deletions examples/azure/chat_no_streaming.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import os

from mistralai_azure import MistralAzure
from mistralai_azure.models import ChatCompletionRequestMessages, UserMessage
from mistralai.azure.client import MistralAzure
from mistralai.azure.client.models import ChatCompletionRequestMessage, UserMessage

AZURE_API_KEY = os.environ.get("AZURE_API_KEY", "")
AZURE_ENDPOINT = os.environ.get("AZURE_ENDPOINT", "")
AZURE_MODEL = os.environ.get("AZURE_MODEL", "mistral-small-2503")
AZURE_API_VERSION = os.environ.get("AZURE_API_VERSION", "2024-05-01-preview")

# The SDK automatically injects api-version as a query parameter
client = MistralAzure(
azure_api_key=os.environ["AZURE_API_KEY"],
azure_endpoint=os.environ["AZURE_ENDPOINT"],
api_key=AZURE_API_KEY,
server_url=AZURE_ENDPOINT,
api_version=AZURE_API_VERSION,
)

messages: list[ChatCompletionRequestMessages] = [
messages: list[ChatCompletionRequestMessage] = [
UserMessage(content="What is the capital of France?"),
]
res = client.chat.complete(messages=messages)
res = client.chat.complete(model=AZURE_MODEL, messages=messages)
print(res.choices[0].message.content)
3 changes: 3 additions & 0 deletions examples/gcp/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GCP_PROJECT_ID=your-gcp-project-id
GCP_REGION=us-central1
GCP_MODEL=mistral-small-2503
36 changes: 30 additions & 6 deletions examples/gcp/async_chat_no_streaming.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
#!/usr/bin/env python
"""
Example: Async chat completion with GCP Vertex AI.

The SDK automatically:
- Detects credentials via google.auth.default()
- Auto-refreshes tokens when they expire
- Builds the Vertex AI URL from project_id and region

Prerequisites:
gcloud auth application-default login

Usage:
GCP_PROJECT_ID=your-project GCP_REGION=us-central1 GCP_MODEL=mistral-small-2503 python async_chat_no_streaming.py
"""

import asyncio
import os

from mistralai_gcp import MistralGoogleCloud
from mistralai_gcp.models.usermessage import UserMessage
from mistralai.gcp.client import MistralGCP
from mistralai.gcp.client.models import UserMessage

# Configuration from environment variables
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID") # Optional: auto-detected from credentials
GCP_REGION = os.environ.get("GCP_REGION", "us-central1")
GCP_MODEL = os.environ.get("GCP_MODEL", "mistral-small-2503")

async def main():
model = "mistral-large-2407"

client = MistralGoogleCloud(project_id=os.environ["GCP_PROJECT_ID"])
async def main():
# The SDK automatically handles:
# - Credential detection via google.auth.default()
# - Token refresh when expired
# - Vertex AI URL construction
client = MistralGCP(
project_id=GCP_PROJECT_ID,
region=GCP_REGION,
)

chat_response = await client.chat.complete_async(
model=model,
model=GCP_MODEL,
messages=[UserMessage(content="What is the best French cheese?")],
)

Expand Down
Loading
Loading