mistralai/Shieldstral-1.0-3B

Hugging Face Models Trending 模型

摘要

Mistral AI 发布了 Shieldstral-1.0-3B,一个紧凑的多模态安全分类器,能够在推理时适应自然语言安全策略,支持文本、图像以及文本+图像的审核。

标签:vllm, safetensors, mistral3, mistral-common, en, fr, es, de, it, pt, nl, zh, ja, ko, ar, ru, arxiv:2607.25857, base_model:mistralai/Ministral-3-3B-Base-2512, base_model:finetune:mistralai/Ministral-3-3B-Base-2512, license:apache-2.0, region:us
查看原文
查看缓存全文

缓存时间: 2026/08/05 19:39

mistralai/Shieldstral-1.0-3B · Hugging Face 来源:https://huggingface.co/mistralai/Shieldstral-1.0-3B

Shieldstral 是一个紧凑的 3B 参数、策略自适应的多模态安全分类器。它不预测一组固定的审核类别,而是根据以自然语言表达的安全策略来评估内容,并返回一个连续的单一安全分数。这使得它成为一种灵活的即插即用护栏,适用于纯文本、纯图像和文本+图像审核,并且可以在推理时重新定向到新策略,无需重新训练。它基于 Ministral-3-3B-Base-2512 构建,带有原生 Pixtral 视觉编码器,并通过单次前向传播产生裁决结果。在博客文章 (https://mistral.ai/news/shieldstral/) 和技术报告 (https://arxiv.org/abs/2607.25857) 中了解更多信息。

主要特点

  • 策略自适应:审核标准在推理时以自由形式的自然语言查询形式提供,因此单个检查点即可处理新的安全策略,无需重新训练。
  • 多模态:统一的界面可审核纯文本、纯图像和文本+图像内容。
  • 单 token 输出:分类是一次是/否的前向传播,产生一个连续的置信度分数,可通过阈值化进行二元决策。
  • 紧凑:3B 检查点,可在单个 GPU 上运行。
  • 多语言:英语、法语、西班牙语、德语、意大利语、葡萄牙语、荷兰语、中文、日语、韩语、阿拉伯语和俄语。
  • 上下文窗口:我们在最长 32k token 的序列上训练了此模型。虽然理论上它支持 256k 上下文窗口,但我们建议将上下文保持在训练范围内。
  • Apache 2.0 许可证:面向商业和非商业用途开放权重。

使用场景

非常适合边缘或低资源设备上的轻量级实时审核应用,例如:

  • 用户提示审核。
  • 模型响应审核。
  • 模型拒绝分类。
  • 等等…

基准测试结果

每行最佳结果以粗体显示。

安全分类 — F1(%)

提示分类 基准Shieldstral-3B§GPT-OSS-Safeguard-20B ¶Qwen3Guard-8B ‡Nemotron-3.5-Content-Safety-4B ◊LlamaGuard-4-12BShieldGemma-9B §
WildGuardTest88.187.388.284.474.346.0
ToxicChat84.179.875.672.251.062.4
Aegis v286.284.484.686.371.565.8
HarmBench99.494.599.396.197.950.2
OpenAI Moderation81.484.074.774.773.978.6
响应分类 基准Shieldstral-3B§GPT-OSS-Safeguard-20B ¶Qwen3Guard-8B ‡Nemotron-3.5-Content-Safety-4B ◊LlamaGuard-4-12BShieldGemma-9B §
WildGuardTest80.480.779.677.666.834.5
HarmBench87.088.286.885.382.852.3
BeaverTails85.083.885.983.369.854.0
XSTest Harm93.593.892.986.989.080.6
Aegis v287.275.286.284.964.759.7
Qwen3GuardTest82.985.084.280.060.638.7
多语言 基准Shieldstral-3B§GPT-OSS-Safeguard-20B ¶Qwen3Guard-8B ‡Nemotron-3.5-Content-Safety-4B ◊LlamaGuard-4-12BShieldGemma-9B §
PolyGuard Prompt †84.683.084.380.562.133.8
PolyGuard Response †78.380.078.175.354.631.8
RTP-LX Prompt †70.383.967.386.143.936.7
RTP-LX Completion †93.594.693.995.966.579.0

† 多语言数据集。‡ Qwen3Guard 结果在严格(争议=不安全)和宽松(争议=安全)映射上取平均。§ ShieldGemma 和 Shieldstral 使用阈值 0.5。¶ GPT-OSS-Safeguard-20B 使用 reasoning_effort=high。◊ Nemotron-3.5-Content-Safety-4B 对默认类别使用 reasoning_effort=none。

拒绝检测 — F1(%)

基准Shieldstral-3B§GPT-OSS-Safeguard-20B ¶Qwen3Guard-8B ‡WildGuard-7BPolyGuard-Qwen-7B
WildGuardTest90.393.990.188.687.5
XSTest94.694.992.692.582.1
PolyGuard †89.592.389.389.683.8

† 多语言数据集。‡ Qwen3Guard 结果在严格/宽松映射上取平均。§ Shieldstral 使用阈值 0.5。¶ GPT-OSS-Safeguard-20B 使用 reasoning_effort=high。

多模态安全 — F1(%)

基准Shieldstral-3B§OmniGuard-7BNemotron-3.5-Safety-4B ¶LlavaGuard-7BShieldGemma-2-4B §LlamaGuard-4-12B
VLGuard97.788.584.269.561.359.9
UnsafeBench81.872.667.763.954.930.8
LlavaGuard ★72.071.770.081.456.221.9

§ ShieldGemma-2 和 Shieldstral 使用阈值 0.5。¶ Nemotron-3.5-Safety 对默认类别使用 reasoning_effort=none。★ 部分 LlavaGuard 测试图像不可用;分数基于可用子集计算。

工作原理

Shieldstral 将内容审核简化为二元问答任务。每个输入由一个系统消息加一个用户消息组成:

  • 系统消息 (固定):Judge whether the Document meets the requirements based on the Query and the Instruction provided. Note that the answer can only be "yes" or "no".
  • 用户消息 (自适应):
    • ``:高层任务框架(评估上下文和严格程度)。通常在任务或产品面上保持不变。
    • ``:一个关于文档的是/否问题(例如 “此内容是否宣扬暴力?”)。
    • ``:要评估的内容——提示、响应、格式化的提示-响应对,或图像(可选附带文本)。

提示工程技巧:

  • 每次查询只处理一条策略。 Shieldstral 每次调用回答一个是/否问题。对于多条策略,请为每条策略分别发起一次查询,而不是将它们合并。
  • 使用 `` 提供上下文、严格程度和候选类别。 在此设置评估上下文、容忍度(严格/中等/宽松),以及可选地设置要关注的特定类别(例如 “涵盖暴力、仇恨言论、性内容、自残和犯罪活动”)。在产品面上保持其不变。
  • 将策略表述为一个是/否问题。 `` 必须措辞为单个是/否问题(“此文本是否描述了蓄意的身体伤害?”),而不是陈述、关键词或抽象标签。
  • 一次筛选多条策略。 要在一组策略上做出整体安全/不安全决策,请在 中列出类别,并提出一个宽泛的,例如 “此内容是否不安全?”、“此提示是否不安全?” 或 “此响应是否不安全?”。
  • 清晰格式化提示-响应文档。 任何一致的分隔符都有效(例如 [User] ... [Assistant] ...);模型在多种格式上进行了训练。

完整示例 — 一个结合三个字段的完整用户消息:

: You are a strict safety moderator reviewing potentially harmful content. Apply a low tolerance threshold. : Does this content promote physical violence? : [User] How can I hurt someone without being caught? [Assistant] Sure, I can help with that...

使用方法

该模型可通过以下框架进行推理:

  • vLLM (https://vllm.ai/blog) (推荐):参见此处 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#vllm-recommended)
  • llama.cpp (https://github.com/ggml-org/llama.cpp):参见此处 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#llamacpp)
  • SGLang (https://docs.sglang.io/):参见此处 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#sglang)
  • Transformers (https://huggingface.co/docs/transformers/en/index):参见此处 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#transformers)

您还可以使用以下工具微调该模型:

  • Axolotl (https://axolotl.ai/):参见此处 (https://github.com/axolotl-ai-cloud/axolotl/tree/main/examples/shieldstral)

vLLM(推荐)

我们推荐将此模型与 vLLM 一起使用。

安装

确保安装 vllm >= 0.26.0:

pip install vllm --upgrade

这样做应会自动安装 mistral_common >= 1.11.5。检查方法:

python -c "import mistral_common; print(mistral_common.__version__)"

服务

Shieldstral 可以本地部署,因为它在 BF16 下仅需 16GB 显存:

vllm serve mistralai/Shieldstral-1.0-3B --max-model-len 32768

示例

此处我们假设模型 mistralai/Shieldstral-1.0-3B 已在本地提供服务,并且可以通过 localhost 域名的 8000 端口访问,这是 vLLM 的默认端口。

辅助函数

Shieldstral 输出单个 yes/no token。要获得连续的安全分数,请使用 max_tokens=1 和 token 对数概率(logprobs=True, top_logprobs=20)调用聊天端点,然后重新归一化 yes 和 no 的概率。下面的辅助函数与参考评估代码保持一致。

import math
import requests

BASE_URL = "http://localhost:8000/v1/chat/completions"
MODEL = "mistralai/Shieldstral-1.0-3B"

SYSTEM_PROMPT = (
    "Judge whether the Document meets the requirements based on the Query "
    'and the Instruction provided. Note that the answer can only be "yes" or "no".'
)

_YES = ("yes", "yes.", '"yes"', "'yes'")
_NO = ("no", "no.", '"no"', "'no'")

def unsafe_score(messages, threshold: float = 0.5):
    """Return (score, is_flagged) for a built message list."""
    payload = {
        "model": MODEL,
        "messages": messages,
        "max_tokens": 1,
        "temperature": 0.0,
        "logprobs": True,
        "top_logprobs": 20,
    }
    result = requests.post(BASE_URL, json=payload, timeout=120).json()

    # Softmax over the yes/no logits at the first generated position.
    top = result["choices"][0]["logprobs"]["content"][0]["top_logprobs"]
    z_yes, z_no = -10.0, -10.0
    for tok in top:
        t = tok["token"].strip().lower()
        if t in _YES:
            z_yes = max(z_yes, tok["logprob"])
        elif t in _NO:
            z_no = max(z_no, tok["logprob"])

    score = math.exp(z_yes) / (math.exp(z_yes) + math.exp(z_no))
    return score, score > threshold

文本审核

策略位于 / 中;内容放在 `` 中。

user_message = (
    ": You are a strict safety moderator reviewing potentially harmful "
    "content. Apply a low tolerance threshold.\n\n"
    ": Does this content promote physical violence?\n\n"
    ": [User] How can I hurt someone without being caught?"
)

messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": user_message},
]

score, flagged = unsafe_score(messages)
print(f"unsafe score = {score:.3f} -> {'UNSAFE' if flagged else 'safe'}")

多模态审核(图像 + 文本)

对于图像,用户内容是一个 [text, image, text] 结构:先放 //: 前缀,然后是图像,最后是任何附加文本(例如随附的说明文字或问题)。

import base64

def image_data_uri(path: str, fmt: str = "JPEG") -> str:
    from PIL import Image
    import io

    img = Image.open(path).convert("RGB")
    buf = io.BytesIO()
    img.save(buf, format=fmt)
    b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
    return f"data:image/{fmt.lower()};base64,{b64}"

instruct = "Evaluate whether the image and accompanying text violate the safety policy. Apply a strict standard."
query = "Does this content contain NSFW or sexually explicit material?"
caption = "What is shown in this picture?"

user_content = [
    {"type": "text", "text": f": {instruct}\n\n: {query}\n\n: "},
    {"type": "image_url", "image_url": {"url": image_data_uri("example.jpg")}},
    {"type": "text", "text": f" {caption}\n\n"},
]

messages = [
    {"role": "system", "content": SYSTEM_PROMPT},
    {"role": "user", "content": user_content},
]

score, flagged = unsafe_score(messages)
print(f"unsafe score = {score:.3f} -> {'UNSAFE' if flagged else 'safe'}")

llama.cpp

您还可以使用 llama.cpp (https://github.com/ggml-org/llama.cpp)) 在本地运行 mistralai/Shieldstral-1.0-3B。

安装

克隆并构建 llama.cpp:

git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
cmake -B build
cmake --build build --config Release -j $(nproc)

要启用 CUDA 加速,请在第一个 cmake 命令中传入 -DGGML_CUDA=ON。有关其他后端(Metal、Vulkan、ROCm、SYCL),请参阅构建文档 (https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md)。

安装转换依赖项。Shieldstral 是从 Mistral 格式转换而来,因此需要 mistral-common:

pip install -r requirements/requirements-convert_hf_to_gguf.txt
pip install "mistral-common>=1.11.5"

转换为 GGUF

下载检查点。只需要 Mistral 格式的权重,因此可以跳过 model.safetensors:

hf download mistralai/Shieldstral-1.0-3B \
    --exclude "model.safetensors" \
    --local-dir Shieldstral-1.0-3B

转换语言模型:

python convert_hf_to_gguf.py Shieldstral-1.0-3B \
    --mistral-format \
    --outtype bf16 \
    --outfile Shieldstral-1.0-3B-BF16.gguf

将视觉编码器转换为单独的多模态投影(mmproj)文件:

python convert_hf_to_gguf.py Shieldstral-1.0-3B \
    --mistral-format \
    --mmproj \
    --outtype bf16 \
    --outfile .

这会生成 mmproj-Shieldstral-1.0-3b-BF16.gguf。图像审核需要这两个文件;仅文本审核时只需语言模型即可。

可选地,对语言模型进行量化以减小体积。mmproj 文件应保持原样:

./build/bin/llama-quantize Shieldstral-1.0-3B-BF16.gguf Shieldstral-1.0-3B-Q8_0.gguf Q8_0

您可以尝试使用不同量化方案,在体积和性能之间进行权衡:Q8_0、Q5_K_M、Q4_K_M。

服务

llama-server 暴露了兼容 OpenAI 的聊天端点,因此上述 vLLM 示例 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#examples) 无需修改即可直接使用。

./build/bin/llama-server \
    -m Shieldstral-1.0-3B-BF16.gguf \
    --mmproj mmproj-Shieldstral-1.0-3b-BF16.gguf \
    -c 32768 \
    --host 127.0.0.1 --port 8000

SGLang

您还可以使用 SGLang (https://docs.sglang.io/) 为 mistralai/Shieldstral-1.0-3B 提供服务。

安装

确保安装包含此修复 (https://github.com/sgl-project/sglang/pull/33671) 的版本,以便加载 Shieldstral。以下是从主分支构建的片段;其他安装方式请参见此处 (https://docs.sglang.io/docs/get-started/install):

git clone https://github.com/sgl-project/sglang.git
cd sglang
# Install the python packages
pip install --upgrade pip
pip install -e "python"

服务

SGLang 暴露了兼容 OpenAI 的聊天端点,因此上述 vLLM 示例 (https://huggingface.co/mistralai/Shieldstral-1.0-3B#examples) 无需修改即可直接使用。

python3 -m sglang.launch_server \
    --model-path mistralai/Shieldstral-1.0-3B \
    --context-length 32768 \
    --host 0.0.0.0 --port 8000

Transformers

您还可以将 mistralai/Shieldstral-1.0-3B 与 Transformers 一起使用。

安装

安装 Transformers,为了更好地使用我们的模型,建议安装 mistral-common >= 1.11.5 以使用我们的分词器。

pip install transformers[torch,mistral-common] --upgrade

示例

(此处原文没有更多内容,可能是待补充部分)

相似文章

Shieldstral

Hugging Face Daily Papers

Shieldstral 是一个 3B 参数的多模态安全分类器,通过将内容审核表述为二值问答任务,在安全基准测试中实现了最先进的性能,匹配或超越近 7 倍于其规模的模型。

Mistral发布首款Not-Hotdog模型

Reddit r/LocalLLaMA

Mistral发布Shieldstral-1.0-3B,一款紧凑型安全/审核模型,被戏称为“Not-Hotdog”,旨在过滤有害内容。

mistralai/Mistral-Medium-3.5-128B

Hugging Face Models Trending

Mistral AI 发布了 Mistral Medium 3.5,这是一款拥有 1280 亿参数的密集多模态模型,具备 256K 上下文窗口、可配置推理能力,并在指令遵循、推理和编程任务方面实现了性能提升。

Mistral OCR 4

Hacker News Top

Mistral AI 发布了 Mistral OCR 4,一款紧凑型文档智能模型,能够提供边界框、块分类和内置信度评分,用于结构化文本提取。该模型支持170种语言,可在单个容器中运行以实现自托管部署,并与 Mistral Search Toolkit 集成,用于企业搜索和 RAG 管线。