只需4个命令行,就能在Mac上运行Mistral-7B-Instruct大模型
Second_State
2023年11月16日 19:04

Mistral-7b-Instruct-v0.1 模型[1] 是 Mistral AI 发布的一款 7B 指令调优 LLM。它是一个真正的开源模型,采用 Apache 2.0 许可证。它的上下文长度为 8,000 个token,并且与 13B llama2 模型的性能相当。它非常适合生成散文、总结文档和编写代码。

在这篇文章中,我们将介绍:

  • 如何在你自己的设备上运行 mistral-7b-instruct-v0.1

  • 如何为 mistral-7b-instruct-v0.1 创建一个与 OpenAI 兼容的 API 服务

我们将使用 Rust + Wasm 技术栈来开发和部署这个模型的应用程序。不需要安装复杂的 Python 包或 C++ 工具链!了解我们选择这个技术栈的原因[2]。

在自己的设备上运行模型

步骤 1:通过以下命令行安装 WasmEdge[3]。

curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugin wasi_nn-ggml

步骤 2:下载 模型 GGUF 文件[4]。由于模型大小为几 GB,所以可能需要很长时间。

curl -LO https://huggingface.co/second-state/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q5_K_M.gguf

步骤 3:下载用于聊天应用的跨平台可移植 Wasm 文件。该应用程序允许你在命令行与模型对话。该应用的 Rust 源代码在这里[5]。

curl -LO https://github.com/second-state/llama-utils/raw/main/chat/llama-chat.wasm

就是这样。可以通过输入以下命令在终端与模型对话。

wasmedge --dir .:. --nn-preload default:GGML:AUTO:mistral-7b-instruct-v0.1.Q5_K_M.gguf llama-chat.wasm -p mistral-instruct-v0.1

可移植 Wasm 应用程序会自动利用我设备上的硬件加速器(例如 GPU)。

在我的 Mac M1 32G 内存设备上,它的速度约为每秒 20.71 个 token。

[USER]:

what's the capital of France

[ASSITANT]:

It's Paris.

[USER]:

创建与 OpenAI 兼容的 API 服务

与 OpenAI 兼容的 Web API ,能够让模型与大量 LLM 工具和代理框架(如 flows.network、LangChain 和 LlamaIndex)一起工作。

首先,先下载一个 API 服务器应用。这也是一个跨平台可移植 Wasm 应用,可以在许多 CPU 和 GPU 设备上运行。

curl -LO https://github.com/second-state/llama-utils/raw/main/api-server/llama-api-server.wasm

然后,使用以下命令行启动模型的 API 服务器。

wasmedge --dir .:. --nn-preload default: GGML:AUTO:mistral-7b-instruct-v0.1.Q5_K_M.gguf llama-api-server.wasm -p mistral-instruct-v0.1

从另一个终端,可以使用 curl 与 API 服务器进行交互。

curl -X POST http://0.0.0.0:8080/v1/chat/completions -H 'accept:application/json' -H 'Content-Type: application/json' -d '{"messages":[{"role":"system", "content":"You are a helpful AI assistant"}, {"role":"user", "content":" Who is Robert Oppenheimer?"}], "model":"mistral-7b-instruct-v0.1"}'

就这样。WasmEdge 是运行 LLM 应用程序最简单、最快、最安全的方式[6]。快来试试看吧!

参考资料

[1]

Mistral-7b-Instruct-v0.1 模型: https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1

[2]

了解我们选择这个技术栈的原因: https://www.secondstate.io/articles/fast-llm-inference/

[3]

WasmEdge: https://github.com/WasmEdge/WasmEdge

[4]

模型 GGUF 文件: https://huggingface.co/second-state/Mistral-7B-Instruct-v0.1-GGUF/blob/main/mistral-7b-instruct-v0.1.Q5_K_M.gguf

[5]

这里: https://github.com/second-state/llama-utils/tree/main/chat

[6]

运行 LLM 应用程序最简单、最快、最安全的方式: https://www.secondstate.io/articles/fast-llm-inference/