> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/mo-xing/tutorials/devstral-how-to-run-and-fine-tune.md).

# Devstral：如何运行与微调

运行并微调 Mistral Devstral 1.1，包括 Small-2507 和 2505。

**Devstral-Small-2507** （Devstral 1.1）是 Mistral 面向软件工程推出的新型代理式 LLM。它在工具调用、代码库探索以及为编码代理提供支持方面表现出色。Mistral AI 于 2025 年 5 月发布了最初的 2505 版本。

微调自 [**Mistral-Small-3.1**](https://huggingface.co/unsloth/Mistral-Small-3.1-24B-Instruct-2503-GGUF)，Devstral 支持 128k 上下文窗口。Devstral Small 1.1 的性能有所提升，在 [SWE-bench verified](https://openai.com/index/introducing-swe-bench-verified/)上取得了 53.6% 的分数，使其成为（截至 2025 年 7 月 10 日）该基准上的第一开源模型。

Unsloth Devstral 1.1 GGUF 包含额外的 <mark style="background-color:green;">**工具调用支持**</mark> 和 <mark style="background-color:green;">**聊天模板修复**</mark>。Devstral 1.1 仍然可以很好地与 OpenHands 配合使用，但现在对其他提示词和编码环境的泛化能力也更强了。

由于是纯文本模型，Devstral 的视觉编码器在微调之前已被移除。我们添加了 [*<mark style="background-color:green;">**可选的视觉支持**</mark>*](#possible-vision-support) 用于该模型。

{% hint style="success" %}
我们还在幕后与 Mistral 合作，帮助调试、测试并修正任何可能的错误和问题！请务必 **下载 Mistral 的官方下载或 Unsloth 的 GGUF 文件** / dynamic quants 以获得 **正确实现** （即正确的系统提示词、正确的聊天模板等）

请使用 `--jinja` ，以在 llama.cpp 中启用系统提示词！
{% endhint %}

所有 Devstral 上传都使用我们的 Unsloth [Dynamic 2.0](/docs/zh/ji-chu-zhi-shi/dynamic-3.0-ggufs.md) 方法论，在 5-shot MMLU 和 KL 散度基准上提供最佳性能。这意味着，你可以在几乎不损失准确率的情况下运行并微调量化的 Mistral LLM！

#### **Devstral - Unsloth 动态** 量化：

| Devstral 2507（新）                                                                                                       | Devstral 2505                                                                                               |
| ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| GGUF： [Devstral-Small-2507-GGUF](https://huggingface.co/unsloth/Devstral-Small-2507-GGUF)                              | [Devstral-Small-2505-GGUF](https://huggingface.co/unsloth/Devstral-Small-2505-GGUF)                         |
| 4-bit BnB： [Devstral-Small-2507-unsloth-bnb-4bit](https://huggingface.co/unsloth/Devstral-Small-2507-unsloth-bnb-4bit) | [Devstral-Small-2505-unsloth-bnb-4bit](https://huggingface.co/unsloth/Devstral-Small-2505-unsloth-bnb-4bit) |

## 🖥️ **运行 Devstral**

### :gear: 官方推荐设置

根据 Mistral AI 的说法，以下是推理的推荐设置：

* <mark style="background-color:blue;">**温度从 0.0 到 0.15**</mark>
* Min\_P 为 0.01（可选，但 0.01 效果不错，llama.cpp 默认值为 0.1）
* <mark style="background-color:orange;">**使用**</mark><mark style="background-color:orange;">**&#x20;**</mark><mark style="background-color:orange;">**`--jinja`**</mark><mark style="background-color:orange;">**&#x20;**</mark><mark style="background-color:orange;">**以启用系统提示词。**</mark>

**建议使用系统提示词**，并且它是 OpenHands 系统提示词的衍生版本。完整系统提示词如下 [这里](https://huggingface.co/unsloth/Devstral-Small-2505/blob/main/SYSTEM_PROMPT.txt).

```
你是 Devstral，一个由 Mistral AI 训练、使用 OpenHands 脚手架的有帮助的代理式模型。你可以与计算机交互来解决任务。

<ROLE>
你的主要职责是通过执行命令、修改代码并高效解决技术问题来帮助用户。你应该全面、系统，并把质量置于速度之上。
* 如果用户提出问题，比如 "为什么会发生 X"，不要试图修复问题。只需回答问题即可。
</ROLE>

.... 系统提示词继续 ....
```

{% hint style="success" %}
我们的动态上传文件带有 '`UD`' 前缀。没有该前缀的并非动态文件，但仍然使用我们的校准数据集。
{% endhint %}

## :llama: 教程：如何在 Ollama 中运行 Devstral

1. 安装 `ollama` 如果你还没有的话！

```bash
apt-get update
apt-get install pciutils -y
curl -fsSL https://ollama.com/install.sh | sh
```

2. 使用我们的动态量化运行模型。注意你可以调用 `ollama serve &`如果失败，可在另一个终端中执行！我们将所有建议参数（温度等）包含在 `params` 中，位于我们上传到 Hugging Face 的文件里！
3. 此外，Devstral 支持 128K 上下文长度，因此最好启用 [**KV 缓存量化**](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-set-the-quantization-type-for-the-kv-cache)。我们使用 8bit 量化，可节省 50% 的内存占用。你也可以尝试 `"q4_0"`

```bash
export OLLAMA_KV_CACHE_TYPE="q8_0"
ollama run hf.co/unsloth/Devstral-Small-2507-GGUF:UD-Q4_K_XL
```

## 📖 教程：如何在 llama.cpp 中运行 Devstral

1. 获取最新的 `llama.cpp` 在 [GitHub 上获取](https://github.com/ggml-org/llama.cpp)。你也可以按照下面的构建说明进行操作。将 `-DGGML_CUDA=ON` 改为 `-DGGML_CUDA=OFF` 如果你没有 GPU，或者只想使用 CPU 推理。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后按常规继续——Metal 支持默认开启。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \\
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-quantize llama-cli llama-gguf-split llama-mtmd-cli
cp llama.cpp/build/bin/llama-* llama.cpp
```

2. 如果你想使用 `llama.cpp` 如果你想直接加载模型，可以使用下面的命令：（:Q4\_K\_XL）是量化类型。你也可以通过 Hugging Face（见第 3 点）下载。这类似于 `ollama run`

```bash
./llama.cpp/llama-cli -hf unsloth/Devstral-Small-2507-GGUF:UD-Q4_K_XL --jinja
```

3. **或者** 通过（在安装后）下载模型 `pip install huggingface_hub hf_transfer` ）。你可以选择 Q4\_K\_M，或其他量化版本（例如 BF16 全精度）。

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/Devstral-Small-2507-GGUF",
    local_dir = "unsloth/Devstral-Small-2507-GGUF",
    allow_patterns = ["*Q4_K_XL*", "*mmproj-F16*"], # 用于 Q4_K_XL
)
```

4. 运行模型。
5. 编辑 `--threads -1` 用于最大 CPU 线程数， `--ctx-size 131072` 用于上下文长度（Devstral 支持 128K 上下文长度！）， `--n-gpu-layers 99` 用于 GPU 卸载的层数。如果你的 GPU 显存不足，请尝试调整。若你只使用 CPU 推理，也请移除它。我们还对 K 缓存使用 8bit 量化以减少内存占用。
6. 对于对话模式：

<pre class="language-bash"><code class="lang-bash">./llama.cpp/llama-cli \\
    --model unsloth/Devstral-Small-2507-GGUF/Devstral-Small-2507-UD-Q4_K_XL.gguf \\
    --threads -1 \\
    --ctx-size 131072 \\
    <a data-footnote-ref href="#user-content-fn-1">--cache-type-k q8_0</a> \\
    --n-gpu-layers 99 \
    --seed 3407 \
    --prio 2 \
    --temp 0.15 \
    --repeat-penalty 1.0 \\
    --min-p 0.01 \
    --top-k 64 \\
    --top-p 0.95 \\
    <a data-footnote-ref href="#user-content-fn-2">--jinja</a>
</code></pre>

7. 用于非对话模式以测试我们的 Flappy Bird 提示词：

<pre class="language-bash"><code class="lang-bash">./llama.cpp/llama-cli \\
    --model unsloth/Devstral-Small-2507-GGUF/Devstral-Small-2507-UD-Q4_K_XL.gguf \\
    --threads -1 \\
    --ctx-size 131072 \\
    <a data-footnote-ref href="#user-content-fn-1">--cache-type-k q8_0</a> \\
    --n-gpu-layers 99 \
    --seed 3407 \
    --prio 2 \
    --temp 0.15 \
    --repeat-penalty 1.0 \\
    --min-p 0.01 \
    --top-k 64 \\
    --top-p 0.95 \\
    -no-cnv \
    --prompt "[SYSTEM_PROMPT]你是 Devstral，一个由 Mistral AI 训练、使用 OpenHands 脚手架的有帮助的代理式模型。你可以与计算机交互来解决任务。\n\n&#x3C;ROLE>\n你的主要职责是通过执行命令、修改代码并高效解决技术问题来帮助用户。你应该全面、系统，并把质量置于速度之上。\n* 如果用户提出问题，比如 \"为什么会发生 X\"，不要试图修复问题。只需回答问题即可。\n&#x3C;/ROLE>\n\n&#x3C;EFFICIENCY>\n* 你执行的每个操作都有一定成本。只要可行，就将多个操作合并为一个操作，例如使用 sed 和 grep 一次编辑/查看多个文件，把多个 bash 命令合并成一个。\n* 在探索代码库时，使用 find、grep 和 git 等高效工具，并配合适当的过滤条件，以尽量减少不必要的操作。\n&#x3C;/EFFICIENCY>\n\n&#x3C;FILE_SYSTEM_GUIDELINES>\n* 当用户提供文件路径时，不要假设它相对于当前工作目录。先探索文件系统，定位该文件后再对其进行操作。\n* 如果被要求编辑文件，请直接编辑该文件，而不是用不同的文件名创建新文件。\n* 对于全局搜索替换操作，考虑使用 `sed`，而不是多次打开文件编辑器。\n&#x3C;/FILE_SYSTEM_GUIDELINES>\n\n&#x3C;CODE_QUALITY>\n* 编写整洁、高效的代码，尽量少写注释。避免注释中的冗余：不要重复代码本身就能轻易推断的信息。\n* 实现解决方案时，重点放在解决问题所需的最小改动上。\n* 在实施任何更改之前，先通过探索彻底理解代码库。\n* 如果你要向函数或文件中添加大量代码，适当时可以考虑将函数或文件拆分成更小的部分。\n&#x3C;/CODE_QUALITY>\n\n&#x3C;VERSION_CONTROL>\n* 配置 git 凭据时，默认使用 \"openhands\" 作为 user.name、\"openhands@all-hands.dev\" 作为 user.email，除非另有明确指示。\n* 使用 git 操作时要谨慎。除非明确要求，否则不要做可能危险的更改（例如推送到 main、删除仓库）。\n* 提交更改时，使用 `git status` 查看所有已修改文件，并暂存提交所需的全部文件。尽可能使用 `git commit -a`。\n* 不要提交通常不应进入版本控制的文件（例如 node_modules/、.env 文件、构建目录、缓存文件、大型二进制文件），除非用户明确要求。\n* 如果不确定某些文件是否应提交，请检查是否存在 .gitignore 文件，或者向用户寻求澄清。\n&#x3C;/VERSION_CONTROL>\n\n&#x3C;PULL_REQUESTS>\n* 创建拉取请求时，除非另有明确指示，否则每个会话/问题只创建一个。\n* 在处理现有 PR 时，用新提交更新它，而不是为同一问题创建额外的 PR。\n* 更新 PR 时，保留原有标题和目的，只在必要时更新描述。\n&#x3C;/PULL_REQUESTS>\n\n&#x3C;PROBLEM_SOLVING_WORKFLOW>\n1. 探索：在提出解决方案之前，彻底探索相关文件并理解上下文\n2. 分析：考虑多种方案，选择最有希望的一种\n3. 测试：\n   * 对于修复 bug：在实施修复之前创建测试来验证问题\n   * 对于新功能：在合适时考虑测试驱动开发\n   * 如果仓库缺少测试基础设施，而实现测试需要大量准备，请先与用户沟通，再投入时间搭建测试基础设施\n   * 如果环境尚未配置好无法运行测试，请先与用户沟通，再投入时间安装所有依赖\n4. 实施：进行聚焦且最小化的改动来解决问题\n5. 验证：如果环境已配置好可以运行测试，请全面测试你的实现，包括边界情况。如果环境尚未配置好无法运行测试，请先与用户沟通，再投入时间运行测试。\n&#x3C;/PROBLEM_SOLVING_WORKFLOW>\n\n&#x3C;SECURITY>\n* 仅在用户明确请求且符合预期的方式下使用 GITHUB_TOKEN 和其他凭据。\n* 除非用户另有要求或你的任务需要浏览，否则请使用 API 与 GitHub 或其他平台交互。\n&#x3C;/SECURITY>\n\n&#x3C;ENVIRONMENT_SETUP>\n* 当用户要求你运行某个应用时，如果该应用未安装，不要停下来。请直接安装该应用，然后再次运行命令。\n* 如果遇到缺失依赖：\n  1. 先在仓库中查找现有依赖文件（requirements.txt、pyproject.toml、package.json、Gemfile 等）\n  2. 如果存在依赖文件，就使用它们一次性安装所有依赖（例如 `pip install -r requirements.txt`、`npm install` 等）\n  3. 只有在未找到依赖文件，或只需要特定包时，才直接单独安装包\n* 同样地，如果必需工具缺失依赖，而用户请求的任务需要这些工具，请在可行时进行安装。\n&#x3C;/ENVIRONMENT_SETUP>\n\n&#x3C;TROUBLESHOOTING>\n* 如果你已经反复尝试解决问题，但测试仍然失败，或者用户报告问题仍未解决：\n  1. 退一步，反思 5-7 个不同的潜在问题来源\n  2. 评估每个可能原因的可能性\n  3. 有条不紊地处理最可能的原因，从概率最高的开始\n  4. 记录你的推理过程\n* 当你在执行用户计划时遇到任何重大问题，请不要试图直接绕过去。相反，请提出一个新计划，并在继续之前与用户确认。\n&#x3C;/TROUBLESHOOTING>[/SYSTEM_PROMPT][INST]使用 Python 创建一个 Flappy Bird 游戏。你必须包含以下内容：\n1. 你必须使用 pygame。\n2. 背景颜色应随机选择，并且是浅色。起始颜色为浅蓝色。\n3. 多次按下 SPACE 会加速小鸟。\n4. 小鸟的形状应随机选择为正方形、圆形或三角形。颜色应随机选择为深色。\n5. 在底部放置一些地面，颜色随机为深棕色或黄色。\n6. 在右上角显示分数。通过管道且未撞到时分数增加。\n7. 制作间距随机且留有足够空隙的管道。颜色随机为深绿色、浅棕色或深灰色。\n8. 当你失败时，显示最佳分数。文字应显示在屏幕内。按 q 或 Esc 将退出游戏。再次按 SPACE 即可重新开始。\n最终游戏应放在 Python 的 markdown 代码块中。请检查你的代码是否有错误[/INST]"
</code></pre>

{% hint style="danger" %}
记得删除 \<bos>，因为 Devstral 会自动添加 \<bos>！另外请使用 `--jinja` 以启用系统提示词！
{% endhint %}

## :eyes:实验性视觉支持

[Xuan-Son](https://x.com/ngxson) 来自 Hugging Face 的展示于他们的 [GGUF 仓库](https://huggingface.co/ngxson/Devstral-Small-Vision-2505-GGUF) 如何实际将 Mistral 3.1 Instruct 的视觉编码器“嫁接”到 Devstral 2507 上。我们还上传了我们的 mmproj 文件，这样你可以使用以下内容：

```bash
./llama.cpp/llama-mtmd-cli \\
    --model unsloth/Devstral-Small-2507-GGUF/Devstral-Small-2507-UD-Q4_K_XL.gguf \\
    --mmproj unsloth/Devstral-Small-2507-GGUF/mmproj-F16.gguf \\
    --threads -1 \\
    --ctx-size 131072 \\
    --cache-type-k q8_0 \\
    --n-gpu-layers 99 \
    --seed 3407 \
    --prio 2 \
    --temp 0.15
```

例如：

| 指令和输出代码                                                                                                       | 渲染后的代码                                                                                                        |
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| ![](https://cdn-uploads.huggingface.co/production/uploads/63ca214abedad7e2bf1d1517/HDic53ANsCoJbiWu2eE6K.png) | ![](https://cdn-uploads.huggingface.co/production/uploads/63ca214abedad7e2bf1d1517/onV1xfJIT8gzh81RkLn8J.png) |

## 🦥 使用 Unsloth 对 Devstral 进行微调

就像包括 Mistral Small 3.1 在内的标准 Mistral 模型一样，Unsloth 也支持 Devstral 微调。训练速度快 2 倍，显存占用减少 70%，并支持 8 倍更长的上下文长度。Devstral 在 24GB 显存的 L4 GPU 上也能轻松运行。

不幸的是，Devstral 的显存需求略高于 16GB 显存的限制，所以目前还不能在 Google Colab 上免费微调。不过，你 *可以* 使用我们的 [Kaggle 笔记本](https://www.kaggle.com/notebooks/welcome?src=https://github.com/unslothai/notebooks/blob/main/nb/Kaggle-Magistral_\(24B\)-Reasoning-Conversational.ipynb\&accelerator=nvidiaTeslaT4)可以使用它，它提供双 GPU 访问。只需把 notebook 里的 Magistral 模型名称改成 Devstral 模型。

如果你使用的是旧版本的 Unsloth 和/或是在本地进行微调，请安装最新版的 Unsloth：

```bash
pip install --upgrade --force-reinstall --no-cache-dir unsloth unsloth_zoo
```

[^1]: K 量化以减少内存使用。可以是 f16、q8\_0、q4\_0

[^2]: 必须使用 --jinja 来启用系统提示词


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/mo-xing/tutorials/devstral-how-to-run-and-fine-tune.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
