跳至主要內容

Mistral AI API

https://docs.mistral.ai/api/

API 金鑰

# env variable
os.environ['MISTRAL_API_KEY']

範例用法

from litellm import completion
import os

os.environ['MISTRAL_API_KEY'] = ""
response = completion(
model="mistral/mistral-tiny",
messages=[
{"role": "user", "content": "hello from litellm"}
],
)
print(response)

範例用法 - 串流

from litellm import completion
import os

os.environ['MISTRAL_API_KEY'] = ""
response = completion(
model="mistral/mistral-tiny",
messages=[
{"role": "user", "content": "hello from litellm"}
],
stream=True
)

for chunk in response:
print(chunk)

搭配 LiteLLM Proxy 使用

1. 在 config.yaml 中設定 Mistral 模型

model_list:
- model_name: mistral-small-latest
litellm_params:
model: mistral/mistral-small-latest
api_key: "os.environ/MISTRAL_API_KEY" # ensure you have `MISTRAL_API_KEY` in your .env

2. 啟動 Proxy

litellm --config config.yaml

3. 測試

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "mistral-small-latest",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}
'

支援的模型

資訊

此處列出的所有模型 https://docs.mistral.ai/platform/endpoints 都支援。我們持續維護模型清單、定價、token 視窗等資訊。請見此處

模型名稱函式呼叫推理支援
Mistral Smallcompletion(model="mistral/mistral-small-latest", messages)
Mistral Mediumcompletion(model="mistral/mistral-medium-latest", messages)
Mistral Large 2completion(model="mistral/mistral-large-2407", messages)
Mistral Large Latestcompletion(model="mistral/mistral-large-latest", messages)
Magistral Smallcompletion(model="mistral/magistral-small-2506", messages)
Magistral Mediumcompletion(model="mistral/magistral-medium-2506", messages)
Mistral 7Bcompletion(model="mistral/open-mistral-7b", messages)
Mixtral 8x7Bcompletion(model="mistral/open-mixtral-8x7b", messages)
Mixtral 8x22Bcompletion(model="mistral/open-mixtral-8x22b", messages)
Codestralcompletion(model="mistral/codestral-latest", messages)
Mistral NeMocompletion(model="mistral/open-mistral-nemo", messages)
Mistral NeMo 2407completion(model="mistral/open-mistral-nemo-2407", messages)
Codestral Mambacompletion(model="mistral/open-codestral-mamba", messages)
Codestral Mambacompletion(model="mistral/codestral-mamba-latest"", messages)

函式呼叫

from litellm import completion

# set env
os.environ["MISTRAL_API_KEY"] = "your-api-key"

tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]

response = completion(
model="mistral/mistral-large-latest",
messages=messages,
tools=tools,
tool_choice="auto",
)
# Add any assertions, here to check response args
print(response)
assert isinstance(response.choices[0].message.tool_calls[0].function.name, str)
assert isinstance(
response.choices[0].message.tool_calls[0].function.arguments, str
)

推理

Mistral 不直接支援推理,而是建議在其 magistral 模型中使用特定的 system prompt。設定 reasoning_effort 參數後,LiteLLM 會將 system prompt 前置到請求中。

如果已提供現有的 system message,LiteLLM 會將兩者以 system messages 清單形式傳送(您可以透過啟用 litellm._turn_on_debug() 來驗證)。

支援的模型

模型名稱函式呼叫
Magistral Smallcompletion(model="mistral/magistral-small-2506", messages)
Magistral Mediumcompletion(model="mistral/magistral-medium-2506", messages)

使用 Reasoning Effort

reasoning_effort 參數可控制模型在推理上投入的努力程度。搭配 magistral 模型使用時。

from litellm import completion
import os

os.environ['MISTRAL_API_KEY'] = "your-api-key"

response = completion(
model="mistral/magistral-medium-2506",
messages=[
{"role": "user", "content": "What is 15 multiplied by 7?"}
],
reasoning_effort="medium" # Options: "low", "medium", "high"
)

print(response)

System Message 範例

如果您已經有 system message,LiteLLM 會將推理指示前置:

response = completion(
model="mistral/magistral-medium-2506",
messages=[
{"role": "system", "content": "You are a helpful math tutor."},
{"role": "user", "content": "Explain how to solve quadratic equations."}
],
reasoning_effort="high"
)

# The system message becomes:
# "When solving problems, think step-by-step in <think> tags before providing your final answer...
#
# You are a helpful math tutor."

搭配 LiteLLM Proxy 使用

您也可以透過 LiteLLM proxy 使用推理功能:

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "magistral-medium-2506",
"messages": [
{
"role": "user",
"content": "What is the square root of 144? Show your reasoning."
}
],
"reasoning_effort": "medium"
}'

重要注意事項

  • 模型相容性:推理參數僅適用於 magistral 模型
  • 回溯相容性:非 magistral 模型會忽略推理參數並正常運作

音訊轉錄

透過 litellm.transcription() 使用 Mistral 的 Voxtral 模型進行音訊轉錄。

SDK 用法

from litellm import transcription
import os

os.environ["MISTRAL_API_KEY"] = ""

audio_file = open("path/to/audio.wav", "rb")

response = transcription(
model="mistral/voxtral-mini-latest",
file=audio_file,
)

print(response.text)

含可選參數

response = transcription(
model="mistral/voxtral-mini-latest",
file=audio_file,
language="en",
temperature=0.0,
response_format="json",
)

Mistral 專屬參數

Mistral 除了相容 OpenAI 的參數之外,還支援其他額外參數:

參數類型說明
diarizebool啟用說話者分離
response = transcription(
model="mistral/voxtral-mini-latest",
file=audio_file,
diarize=True,
)

搭配 LiteLLM Proxy 使用

model_list:
- model_name: voxtral
litellm_params:
model: mistral/voxtral-mini-latest
api_key: os.environ/MISTRAL_API_KEY
model_info:
mode: audio_transcription
litellm --config /path/to/config.yaml
curl --location 'http://0.0.0.0:4000/v1/audio/transcriptions' \
--header 'Authorization: Bearer sk-1234' \
--form 'file=@"audio.wav"' \
--form 'model="voxtral"'

範例用法 - 嵌入

from litellm import embedding
import os

os.environ['MISTRAL_API_KEY'] = ""
response = embedding(
model="mistral/mistral-embed",
input=["good morning from litellm"],
)
print(response)

支援的模型

此處列出的所有模型 https://docs.mistral.ai/platform/endpoints 都支援

模型名稱函式呼叫
Mistral Embeddingsembedding(model="mistral/mistral-embed", input)