介绍用 LLM CLI 工具在终端直接运行 Mistral 等开源模型。对想本地部署模型替代商业 API 的开发者实用。
Mistral AI 目前是最令人兴奋的 AI 研究实验室。他们已经在 Apache 2 许可证下发布了两个极其强大的较小型大语言模型,并有第三个更大的模型可通过他们的 API 获得。
我一直在使用我的 LLM 命令行工具尝试他们的模型。以下是我目前了解到的情况。
12 月 8 日周五,Mistral AI 发推了一个神秘的磁力链接(BitTorrent)。这是他们第二次这样做,第一次是在 9 月 26 日时发布他们优秀的 Mistral 7B 模型,也是作为磁力链接发布。
新版本是一个 87GB 的文件,包含 Mixtral 8x7B——根据他们三天后发布的文章,这是一个"具有开放权重的高质量稀疏专家混合模型(SMoE)"。
Mixtral 是一个非常令人印象深刻的模型。GPT-4 长期以来一直被传言使用混合专家架构,而 Mixtral 是我见过的第一个真正令人信服的开放许可实现。它已经展现出令人印象深刻的基准分数。
这个 llama.cpp 的 PR 为新模型增加了支持。llama-cpp-python 随后不久更新以采用了那个补丁。
这意味着...你现在可以使用我的 llm-llama-cpp 插件在 Mac(以及其他平台上,虽然我还没有亲自测试过)上运行 Mixtral。
以下是操作方法:
安装 LLM:
pipx install llm
安装插件:
llm install llm-llama-cpp
安装 llama-cpp-python——这需要手动完成,因为最佳方法因平台而异。对于 Apple Silicon Mac,我建议运行:
CMAKE_ARGS="-DLLAMA_METAL=on" FORCE_CMAKE=1 llm install llama-cpp-python
更多细节请见 llm-llama-cpp README
下载 Mixtral 8X7B Instruct v0.1 的 GGUF 文件。TheBloke 有 8 个不同的选项——我选择了 36GB 的 mixtral-8x7b-instruct-v0.1.Q6_K.gguf:
curl -LO 'https://huggingface.co/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/resolve/main/mixtral-8x7b-instruct-v0.1.Q6_K.gguf?download=true'
像这样运行模型:
llm -m gguf \
-o path mixtral-8x7b-instruct-v0.1.Q6_K.gguf \
'[INST] Write a Python function that downloads a file from a URL[/INST]'
这里 -m gguf 表示使用由 llm-llama-cpp 插件安装的 gguf 模型——这是一个需要你传递 path 选项的模型——用 -o path filename 指定你想要运行的模型。
[INST] 部分是 Instruct 模型期望的 prompt 格式。我需要更新 llm-llama-cpp 插件以更容易地自动构造正确的 prompt 格式,见这个 issue。
这是我对那个 prompt 得到的回复:
Sure, here's an example Python function that downloads a file from a URL using the requests library:
import requests
def download_file(url, save_path):
"""
Download a file from a URL and save it to a local path.
:param url: The URL of the file to download.
:param save_path: The local path to save the file.
"""
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
print(f"File downloaded successfully at {save_path}")
else:
print(f"Failed to download file from {url}. Status code: {response.status_code}")
To use this function, simply call it with the URL of the file you want to download and the local path where you want to save it. For example:
download_file('https://example.com/myfile.txt', '/path/to/save/myfile.txt')
Note that this function uses the stream=True parameter in the requests.get() method to download the file in chunks, which can help prevent memory issues when downloading large files. It also checks the HTTP status code of the response before saving the file, so you can handle any errors that might occur during the download.
这是一个非常扎实的回复!
更小的 Mistral 7B 模型在 9 月发布。从那时起,它已成为该规模最有能力的模型系列——这个规模非常适合在个人设备上运行。
我现在甚至在我的 iPhone 上运行 Mistral 7B,感谢几天前对 MLC Chat iOS 应用的更新。
在 Mac 上使用 LLM 运行这个模型及其变体有一些不同的选择——可能在其他平台上也有,不过我还没有在 Linux 或 Windows 上测试过这些选项:
使用 llm-llama-cpp:为聊天调优版本下载其中一个 Mistral-7B-Instruct GGUF 文件,或为基础 Mistral 下载其中一个,然后按照上面列出的步骤操作
使用 llm-gpt4all。这是最容易安装的插件:
llm install llm-gpt4all
模型将在你第一次尝试使用它时下载:
llm -m mistral-7b-instruct-v0 'Introduce yourself'
使用 llm-mlc。按照 README 中的说明安装它,然后:
# Download the model:
llm mlc download-model https://huggingface.co/mlc-ai/mlc-chat-Mistral-7B-Instruct-v0.2-q3f16_1
# Run it like this:
llm -m mlc-chat-Mistral-7B-Instruct-v0.2-q3f16_1 'Introduce yourself'
这些选项都有效,但我还没有花时间比较它们在输出质量或性能方面的差异。
Mistral 最近还宣布了 La plateforme,他们的早期访问 API 用于调用他们模型的托管版本。
他们新的 API 将 Mistral 7B 模型重命名为"Mistral-tiny",新的 Mixtral 模型重命名为"Mistral-small"......并还提供了一个叫 Mistral-medium 的东西:
我们最高质量的端点目前服务于一个原型模型,根据标准基准,它目前属于最顶级的可用模型之列。它掌握英语/法语/意大利语/德语/西班牙语和代码,在 MT-Bench 上获得 8.6 的分数。
我获得了他们 API 的访问权,并用它构建了一个新的插件 llm-mistral。以下是使用方法:
安装它:
llm install llm-mistral
设置你的 Mistral API 密钥:
llm keys set mistral
# <paste key here>
像这样运行模型:
llm -m mistral-tiny 'Say hi'
# Or mistral-small or mistral-medium
cat mycode.py | llm -m mistral-medium -s 'Explain this code'
这是他们比较 Mistral Small 和 Medium 与 GPT-3.5 的表格:
这些可能经过了精选,但请注意 Small 在几乎每个指标上都击败了 GPT-3.5,而 Medium 在所有指标上都以更大的优势击败了它。
这是 MT Bench 排行榜,包括 GPT-4 和 Claude 2.1 的分数:
Medium 的 8.61 分把它放在 GPT-3.5 和 GPT-4 之间的中间位置。
基准分数不能替代花时间与模型相处以感受它在广泛任务中的表现如何,但这些分数极其有前景。GPT-4 可能不会长期保持最佳模型的桂冠。
由于 Mistral 7B 和 Mixtral 8x7B 都在 Apache 2 许可证下可用,其他 LLM 托管提供商在定价方面已经形成了某种"逐底竞争"。
这种趋势让我有点紧张,因为它实际上抑制了 Mistral 和其他希望提供自己托管版本的提供商未来开放模型发布的动力。
LLM 已经为多个这样的提供商准备了插件。我迄今为止尝试过的三个是 Replicate、Anyscale Endpoints 和 OpenRouter。
对于使用 llm-replicate 的 Replicate:
llm install llm-replicate
llm keys set replicate
# <paste API key here>
llm replicate add mistralai/mistral-7b-v0.1
然后像这样运行 prompt:
llm -m replicate-mistralai-mistral-7b-v0.1 '3 reasons to get a pet weasel:'
这个例子是非指令调优模型,所以 prompt 需要以一种使模型能够完成它的方式来形成。
对于使用 llm-anyscale-endpoints 的 Anyscale Endpoints:
llm install llm-anyscale-endpoints
llm keys set anyscale-endpoints
# <paste API key here>
现在你可以运行 7B 和 Mixtral 8x7B 模型:
llm -m mistralai/Mixtral-8x7B-Instruct-v0.1 \
'3 reasons to get a pet weasel'
llm -m mistralai/Mistral-7B-Instruct-v0.1 \
'3 reasons to get a pet weasel'
对于使用 llm-openrouter 的 OpenRouter:
llm install llm-openrouter
llm keys set openrouter
# <paste API key here>
然后像这样运行模型:
llm -m openrouter/mistralai/mistral-7b-instruct \
'2 reasons to get a pet dragon'
llm -m openrouter/mistralai/mixtral-8x7b-instruct \
'2 reasons to get a pet dragon'
OpenRouter 目前以 $0.00/1M 输入 token 的价格通过他们的 API 提供 Mistral 和 Mixtral——是免费的!显然这不可持续,所以不要依赖这个继续下去,但这确实使他们成为与这些模型进行一些初始实验的绝佳平台。
我最近写过关于 Llamafile 的文章,这是一个迷人的选择,用于运行 LLM,其中 LLM 可以被打包到一个可执行文件中,该文件包含在多个平台上运行它所需的一切。
Justine Tunney 在几天前为 Mixtral 发布了 llamafiles。
mixtral-8x7b-instruct-v0.1.Q5_K_M-server.llamafile 一个运行 OpenAI 兼容的 API 端点,LLM 可以与之通信。
以下是使用方法:
下载 llamafile:
curl -LO https://huggingface.co/jartine/Mixtral-8x7B-v0.1.llamafile/resolve/main/mixtral-8x7b-instruct-v0.1.Q5_K_M-server.llamafile
开始运行它:
./mixtral-8x7b-instruct-v0.1.Q5_K_M-server.llamafile
你可能需要先 chmod 755 mixtral-8x7b-instruct-v0.1.Q5_K_M-server.llamafile,但我发现我不需要。
通过将以下内容添加到 ~/Library/Application Support/io.datasette.llm/extra-openai-models.yaml 的文件来配置 LLM 了解该端点:
- model_id: llamafile
model_name: llamafile
api_base: "http://127.0.0.1:8080/v1"
这注册了一个叫 llamafile 的模型,你现在可以像这样调用它:
llm -m llamafile 'Say hello to the world'
设置那个 llamafile 别名意味着你将能对在那个默认 8080 端口上运行的任何 llamafile 模型使用相同的 CLI 调用。
相同的方法应该对任何提供模仿 OpenAI API 的端点的其他模型托管选项都有效。
当我给 LLM 添加插件支持时,这正是我的想法:我希望尽可能容易地为新模型添加支持,无论是本地的还是远程托管的。
LLM 插件目录现在列出了总共 19 个插件。
如果你想构建自己的插件——对于本地托管的模型或通过远程 API 公开的模型——插件作者教程(加上审查来自现有插件的代码)应该能提供你需要的一切。
你也欢迎加入我们在 #llm Discord 频道中讨论你的项目计划。
OpenAI 对 Hugging Face 的意外网络攻击是发生的科幻小说 - 2026 年 7 月 22 日
与 Claude Code 团队的 Cat 和 Thariq 的炉边谈话 - 2026 年 7 月 21 日
Kimi K3,以及我们仍然可以从鹈鹕基准学到的东西 - 2026 年 7 月 16 日
这是《在终端中运行 Mistral 模型的多个选项使用 LLM》,由 Simon Willison 发布于 2023 年 12 月 18 日。
LLM 在个人设备上系列的一部分
谷歌泄露文件:《我们没有护城河,OpenAI 也没有》- 2023 年 5 月 4 日,下午 4:05
使用 LLM 和 Homebrew 在你的 Mac 上运行 Llama 2 - 2023 年 8 月 1 日,下午 6:56
llamafile 是在你自己的计算机上运行 LLM 的新最佳方式 - 2023 年 11 月 29 日,晚上 8:54
在终端中运行 Mistral 模型的多个选项使用 LLM - 2023 年 12 月 18 日,下午 6:18
使用 LLM 从终端访问 Llama 3 的选项 - 2024 年 4 月 22 日,下午 1:38
在 Mac 上使用 mistral.rs 运行 Llama 3.2 Vision 和 Phi-3.5 Vision - 2024 年 10 月 19 日,下午 4:14
Qwen2.5-Coder-32B 是一个能很好地编码的 LLM,在我的 Mac 上运行 - 2024 年 11 月 12 日,晚上 11:37
下一个:帮助减轻 prompt 注入的建议:限制爆炸半径
上一个:AI 信任危机
以每月 $10 的价格赞助我,获得该月最重要 LLM 发展的精选电子邮件摘要。
付我钱,少给你发送!