Интеграция Mistral AI API: Mistral Large, Small, Codestral
Mistral AI — европейский провайдер LLM. Ключевые преимущества: соответствие GDPR (данные обрабатываются в ЕС), конкурентная стоимость, open-source модели для локального деплоя. Codestral — специализированная модель для кода с поддержкой Fill-in-the-Middle (FIM).
Интеграция через официальный SDK
from mistralai import Mistral
client = Mistral(api_key="MISTRAL_API_KEY")
# Базовый вызов
response = client.chat.complete(
model="mistral-large-latest",
messages=[{"role": "user", "content": "Привет"}],
temperature=0.1,
)
print(response.choices[0].message.content)
# Async
import asyncio
async def async_chat(prompt: str) -> str:
response = await client.chat.complete_async(
model="mistral-small-latest",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
# Streaming
with client.chat.stream(
model="mistral-large-latest",
messages=[{"role": "user", "content": "Длинный ответ"}],
) as stream:
for event in stream:
print(event.data.choices[0].delta.content or "", end="")
Function Calling
tools = [{
"type": "function",
"function": {
"name": "search_db",
"description": "Search company database",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"limit": {"type": "integer", "default": 10},
},
"required": ["query"]
}
}
}]
response = client.chat.complete(
model="mistral-large-latest",
messages=[{"role": "user", "content": "Найди информацию о клиенте Иванов"}],
tools=tools,
tool_choice="auto",
)
Codestral для кода (FIM)
# Fill-in-the-Middle — заполнение кода между prefix и suffix
fim_response = client.fim.complete(
model="codestral-latest",
prompt="def calculate_discount(price: float,",
suffix=") -> float:\n return discounted_price",
temperature=0,
max_tokens=256,
)
print(fim_response.choices[0].message.content)
# Генерация кода
code_response = client.chat.complete(
model="codestral-latest",
messages=[{"role": "user", "content": "Напиши Python функцию для парсинга CSV с обработкой ошибок"}],
)
Эмбеддинги
response = client.embeddings.create(
model="mistral-embed",
inputs=["Первый документ", "Второй документ"],
)
embeddings = [item.embedding for item in response.data]
Стоимость Mistral (2025)
| Модель | Input (1M) | Output (1M) |
|---|---|---|
| Mistral Large | $2 | $6 |
| Mistral Small | $0.20 | $0.60 |
| Codestral | $0.20 | $0.60 |
| Mistral Embed | $0.10 | — |
Локальный деплой через Ollama
ollama pull mistral:7b
ollama pull codestral:22b
# Mistral через Ollama — OpenAI-совместимый API
from openai import OpenAI
local_client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
response = local_client.chat.completions.create(
model="mistral:7b",
messages=[{"role": "user", "content": "Привет"}],
)
Сроки
- Базовая интеграция: 0.5–1 день
- Codestral для IDE-ассистента: 2–3 дня
- Локальный деплой + оптимизация: 1 неделя







