模型简介
模型特点
模型能力
使用案例
🚀 Devstral - 增强版代码大模型
Devstral是一款专为软件工程任务打造的大语言模型,由Mistral AI和All Hands AI合作开发。它在处理代码相关任务时表现出色,能有效探索代码库、编辑多个文件,为软件工程代理提供强大支持。该模型在SWE-bench基准测试中成绩优异,是开源模型中的佼佼者。
🚀 快速开始
免费微调
你可以使用我们的Google Colab笔记本 免费微调Mistral v0.3 (7B)。
了解更多
- 阅读我们关于Devstral支持的博客:docs.unsloth.ai/basics/devstral
- 查看我们的其他笔记本:文档
✨ 主要特性
- 智能编码:专为智能编码任务设计,是软件工程代理的理想选择。
- 轻量级:仅240亿参数,体积小巧,可在单张RTX 4090或32GB RAM的Mac上运行,适合本地部署和设备端使用。
- 开源许可:采用Apache 2.0许可证,允许商业和非商业用途的使用和修改。
- 长上下文窗口:拥有高达128k的上下文窗口。
- 分词器:使用Tekken分词器,词汇量达131k。
📊 基准测试结果
SWE-Bench
Devstral在SWE-Bench Verified测试中得分46.8%,比之前的开源最优模型高出6%。
模型 | 脚手架 | SWE-Bench Verified (%) |
---|---|---|
Devstral | OpenHands Scaffold | 46.8 |
GPT-4.1-mini | OpenAI Scaffold | 23.6 |
Claude 3.5 Haiku | Anthropic Scaffold | 40.6 |
SWE-smith-LM 32B | SWE-agent Scaffold | 40.2 |
在相同的测试脚手架(OpenHands,由All Hands AI提供)下,Devstral的表现远超Deepseek-V3-0324和Qwen3 232B-A22B等更大的模型。
💻 使用示例
基础用法
我们推荐使用OpenHands脚手架与Devstral交互。你可以通过API或本地推理的方式使用该模型。
API使用
- 按照说明创建Mistral账户并获取API密钥。
- 运行以下命令启动OpenHands Docker容器:
export MISTRAL_API_KEY=<MY_KEY>
docker pull docker.all-hands.dev/all-hands-ai/runtime:0.39-nikolaik
mkdir -p ~/.openhands-state && echo '{"language":"en","agent":"CodeActAgent","max_iterations":null,"security_analyzer":null,"confirmation_mode":false,"llm_model":"mistral/devstral-small-2505","llm_api_key":"'$MISTRAL_API_KEY'","remote_runtime_resource_factor":null,"github_token":null,"enable_default_condenser":true}' > ~/.openhands-state/settings.json
docker run -it --rm --pull=always \
-e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.39-nikolaik \
-e LOG_ALL_EVENTS=true \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ~/.openhands-state:/.openhands-state \
-p 3000:3000 \
--add-host host.docker.internal:host-gateway \
--name openhands-app \
docker.all-hands.dev/all-hands-ai/openhands:0.39
本地推理
你也可以在本地运行该模型,可使用LMStudio或其他以下列出的库。
启动Openhands
docker pull docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik
docker run -it --rm --pull=always \
-e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik \
-e LOG_ALL_EVENTS=true \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ~/.openhands-state:/.openhands-state \
-p 3000:3000 \
--add-host host.docker.internal:host-gateway \
--name openhands-app \
docker.all-hands.dev/all-hands-ai/openhands:0.38
服务器将在http://0.0.0.0:3000启动,在浏览器中打开该地址,你将看到一个“AI Provider Configuration”选项卡。现在你可以通过点击左侧栏的加号与代理开始新的对话。
高级用法
OpenHands(推荐)
启动服务器部署Devstral-Small-2505
确保你已经按照上述说明启动了一个兼容OpenAI的服务器,如vLLM或Ollama。然后,你可以使用OpenHands与Devstral-Small-2505
进行交互。
在本教程中,我们启动一个vLLM服务器:
vllm serve mistralai/Devstral-Small-2505 --tokenizer_mode mistral --config_format mistral --load_format mistral --tool-call-parser mistral --enable-auto-tool-choice --tensor-parallel-size 2
服务器地址应采用以下格式:http://<your-server-url>:8000/v1
启动OpenHands
你可以按照此处的说明安装OpenHands。
最简单的启动方式是使用Docker镜像:
docker pull docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik
docker run -it --rm --pull=always \
-e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik \
-e LOG_ALL_EVENTS=true \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ~/.openhands-state:/.openhands-state \
-p 3000:3000 \
--add-host host.docker.internal:host-gateway \
--name openhands-app \
docker.all-hands.dev/all-hands-ai/openhands:0.38
然后,你可以在http://localhost:3000
访问OpenHands UI。
连接到服务器
访问OpenHands UI时,系统会提示你连接到服务器。你可以使用高级模式连接到之前启动的服务器。
填写以下字段:
- 自定义模型:
openai/mistralai/Devstral-Small-2505
- 基础URL:
http://<your-server-url>:8000/v1
- API密钥:
token
(或启动服务器时使用的任何其他令牌)
使用由Devstral驱动的OpenHands
现在你可以通过开始新对话在OpenHands中使用Devstral Small。让我们来构建一个待办事项列表应用。
待办事项列表应用
- 让我们使用以下提示让Devstral生成应用:
Build a To-Do list app with the following requirements:
- Built using FastAPI and React.
- Make it a one page app that:
- Allows to add a task.
- Allows to delete a task.
- Allows to mark a task as done.
- Displays the list of tasks.
- Store the tasks in a SQLite database.
- 查看结果 你应该会看到代理构建应用,并能够查看它生成的代码。
如果它没有自动完成,你可以让Devstral部署应用或手动部署,然后访问前端URL以查看应用。
- 迭代优化 现在你已经有了第一个结果,你可以通过要求代理改进它来进行迭代。例如,在生成的应用中,我们可以点击任务将其标记为已完成,但添加一个复选框会改善用户体验。你还可以要求它添加编辑任务的功能,或添加按状态过滤任务的功能。
享受使用Devstral Small和OpenHands进行开发的乐趣!
LMStudio(推荐用于量化模型)
从Hugging Face下载权重:
pip install -U "huggingface_hub[cli]"
huggingface-cli download \
"mistralai/Devstral-Small-2505_gguf" \
--include "devstralQ4_K_M.gguf" \
--local-dir "mistralai/Devstral-Small-2505_gguf/"
你可以使用LMStudio在本地提供模型服务:
- 下载并安装LM Studio
- 安装
lms cli ~/.lmstudio/bin/lms bootstrap
- 在bash终端中,在下载模型检查点的目录(例如
mistralai/Devstral-Small-2505_gguf
)中运行lms import devstralQ4_K_M.ggu
- 打开LMStudio应用程序,点击终端图标进入开发者选项卡。点击“选择要加载的模型”并选择Devstral Q4 K M。切换状态按钮以启动模型,在设置中切换“在本地网络上服务”为开启状态。
- 在右侧选项卡中,你将看到一个API标识符(应该是devstralq4_k_m)和一个API地址。记录此地址,我们将在下一步中使用。
启动Openhands:
docker pull docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik
docker run -it --rm --pull=always \
-e SANDBOX_RUNTIME_CONTAINER_IMAGE=docker.all-hands.dev/all-hands-ai/runtime:0.38-nikolaik \
-e LOG_ALL_EVENTS=true \
-v /var/run/docker.sock:/var/run/docker.sock \
-v ~/.openhands-state:/.openhands-state \
-p 3000:3000 \
--add-host host.docker.internal:host-gateway \
--name openhands-app \
docker.all-hands.dev/all-hands-ai/openhands:0.38
点击第二行的“查看高级设置”。在新选项卡中,将高级模式切换为开启状态。将自定义模型设置为mistral/devstralq4_k_m,将基础URL设置为我们在LM Studio的最后一步中获得的API地址。将API密钥设置为dummy。点击“保存更改”。
vLLM(推荐)
我们推荐使用vLLM库来实现生产就绪的推理管道。
安装
确保你安装了vLLM >= 0.8.5
:
pip install vllm --upgrade
这样做应该会自动安装mistral_common >= 1.5.4
。
检查安装情况:
python -c "import mistral_common; print(mistral_common.__version__)"
你还可以使用现成的Docker镜像或在Docker Hub上的镜像。
服务器部署
我们建议在服务器/客户端设置中使用Devstral。
- 启动服务器:
vllm serve mistralai/Devstral-Small-2505 --tokenizer_mode mistral --config_format mistral --load_format mistral --tool-call-parser mistral --enable-auto-tool-choice --tensor-parallel-size 2
- 你可以使用以下简单的Python代码片段来测试客户端:
import requests
import json
from huggingface_hub import hf_hub_download
url = "http://<your-server-url>:8000/v1/chat/completions"
headers = {"Content-Type": "application/json", "Authorization": "Bearer token"}
model = "mistralai/Devstral-Small-2505"
def load_system_prompt(repo_id: str, filename: str) -> str:
file_path = hf_hub_download(repo_id=repo_id, filename=filename)
with open(file_path, "r") as file:
system_prompt = file.read()
return system_prompt
SYSTEM_PROMPT = load_system_prompt(model, "SYSTEM_PROMPT.txt")
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Write a function that computes fibonacci in Python.",
},
],
},
]
data = {"model": model, "messages": messages, "temperature": 0.15}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json()["choices"][0]["message"]["content"])
输出
当然!斐波那契数列是一个数字序列,其中每个数字是前两个数字的和,通常从0和1开始。以下是一个简单的Python函数来计算斐波那契数列:
迭代方法
这种方法使用循环迭代计算斐波那契数。
def fibonacci(n):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
a, b = 0, 1
for _ in range(2, n):
a, b = b, a + b
return b
# 示例用法:
print(fibonacci(10)) # 输出: 34
递归方法
这种方法使用递归计算斐波那契数。请注意,对于较大的n
,由于重复计算,这种方法效率较低。
def fibonacci_recursive(n):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)
# 示例用法:
print(fibonacci_recursive(10)) # 输出: 34
记忆化方法
这种方法使用记忆化来存储先前计算的斐波那契数,比简单的递归方法更高效。
def fibonacci_memo(n, memo={}):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
elif n in memo:
return memo[n]
memo[n] = fibonacci_memo(n - 1, memo) + fibonacci_memo(n - 2, memo)
return memo[n]
# 示例用法:
print(fibonacci_memo(10)) # 输出: 34
动态规划方法
这种方法使用数组存储直到n
的斐波那契数。
def fibonacci_dp(n):
if n <= 0:
return "Input should be a positive integer."
elif n == 1:
return 0
elif n == 2:
return 1
fib = [0, 1] + [0] * (n - 2)
for i in range(2, n):
fib[i] = fib[i - 1] + fib[i - 2]
return fib[n - 1]
# 示例用法:
print(fibonacci_dp(10)) # 输出: 34
你可以根据需要选择任何一种方法。对于较大的n
值,迭代和动态规划方法通常更高效。
Mistral-inference
我们推荐使用mistral-inference快速试用Devstral。
安装
确保安装了mistral_inference >= 1.6.0
:
pip install mistral_inference --upgrade
下载
from huggingface_hub import snapshot_download
from pathlib import Path
mistral_models_path = Path.home().joinpath('mistral_models', 'Devstral')
mistral_models_path.mkdir(parents=True, exist_ok=True)
snapshot_download(repo_id="mistralai/Devstral-Small-2505", allow_patterns=["params.json", "consolidated.safetensors", "tekken.json"], local_dir=mistral_models_path)
Python运行 你可以使用以下命令运行模型:
mistral-chat $HOME/mistral_models/Devstral --instruct --max_tokens 300
如果你输入提示“Write me a unique and efficient function that computes fibonacci in Python”,模型应该会生成类似以下的内容:
输出
当然!计算斐波那契数的一种常见且高效的方法是使用记忆化来存储先前计算的值。这避免了冗余计算并显著提高了性能。以下是一个使用记忆化高效计算斐波那契数的Python函数:
def fibonacci(n, memo=None):
if memo is None:
memo = {}
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
return memo[n]
# 示例用法:
n = 10
print(f"Fibonacci number at position {n} is {fibonacci(n)}")
解释:
- 基本情况:如果
n
为0或1,函数返回n
,因为斐波那契数列从0和1开始。 - 记忆化:函数使用字典
memo
存储先前计算的斐波那契数的结果。 - 递归情况:对于其他
n
值,函数通过递归计算fibonacci(n - 1)
和fibonacci(n - 2)
的结果并求和来计算斐波那契数。
Ollama
你可以使用Ollama CLI运行Devstral:
ollama run devstral
Transformers
为了充分利用我们的模型与transformers库,确保安装了mistral-common >= 1.5.5
以使用我们的分词器:
pip install mistral-common --upgrade
然后加载我们的分词器和模型并生成文本:
import torch
from mistral_common.protocol.instruct.messages import (
SystemMessage, UserMessage
)
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.tokens.tokenizers.tekken import SpecialTokenPolicy
from huggingface_hub import hf_hub_download
from transformers import AutoModelForCausalLM
def load_system_prompt(repo_id: str, filename: str) -> str:
file_path = hf_hub_download(repo_id=repo_id, filename=filename)
with open(file_path, "r") as file:
system_prompt = file.read()
return system_prompt
model_id = "mistralai/Devstral-Small-2505"
tekken_file = hf_hub_download(repo_id=model_id, filename="tekken.json")
SYSTEM_PROMPT = load_system_prompt(model_id, "SYSTEM_PROMPT.txt")
tokenizer = MistralTokenizer.from_file(tekken_file)
model = AutoModelForCausalLM.from_pretrained(model_id)
tokenized = tokenizer.encode_chat_completion(
ChatCompletionRequest(
messages=[
SystemMessage(content=SYSTEM_PROMPT),
UserMessage(content="Write me a function that computes fibonacci in Python."),
],
)
)
output = model.generate(
input_ids=torch.tensor([tokenized.tokens]),
max_new_tokens=1000,
)[0]
decoded_output = tokenizer.decode(output[len(tokenized.tokens):])
print(decoded_output)
📄 许可证
本项目采用Apache 2.0许可证。
⚠️ 重要提示
你应该在
llama.cpp
中使用--jinja
来启用系统提示。



