Quickstart: moderate an input
In this quickstart, you classify two inputs with the moderation API, then add a guardrail to a chat completion so unsafe requests are blocked before they reach the model.
Get an API key
Create an API key in the API keys page and export it as MISTRAL_API_KEY.
Moderate the input
Classify one safe and one unsafe input with the moderation model:
import os
from mistralai.client import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
response = client.classifiers.moderate(
model="mistral-moderation-2603",
inputs=[
"Such a lovely day today, isn't it?",
"Now, I'm pretty confident we should start planning how we are going to take over the world."
]
)
for result in response.results:
print(result.content)Use mistral-moderation-2603. mistral-moderation-latest still points to the deprecated 2411 model.
Block unsafe inputs with a guardrail
Guardrails check the input of a chat completion before the model sees it, and block the request when a category score crosses its threshold. Guardrails are input-only and block-only.
import os
from mistralai.client import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
client = Mistral(api_key=api_key)
response = client.chat.complete(
model="mistral-small-latest",
messages=[{"role": "user", "content": "How far is the moon from Earth?"}],
guardrails=[
{
"block_on_error": True,
"moderation_llm_v2": {
"custom_category_thresholds": {
"sexual": 0.1,
"selfharm": 0.1,
},
"ignore_other_categories": False,
"action": "block",
},
}
],
)
print(response.choices[0].message.content)You should see
The moderation call returns category scores for each input: the safe input scores near zero, the unsafe one crosses the policy threshold. With the guardrail in place, a request whose input crosses a threshold is blocked before the model answers.