本地快速部署谷歌开放模型Gemma教程(基于WasmEdge)
本地快速部署谷歌开放模型Gemma教程(基于WasmEdge)
- 一、介绍 Gemma
- 二、部署 Gemma
- 2.1 部署工具
- 2.1 部署步骤
- 三、构建超轻量级 AI 代理
- 四、总结
一、介绍 Gemma
Gemma是一系列轻量级、最先进的开放式模型,采用与创建Gemini模型相同的研究和技术而构建。可以直接运行在本地的电脑上,无GPU也可以运行,只用CPU即可,只不过速度慢点。
二、部署 Gemma
2.1 部署工具
使用 Linux 环境 + WasmEdge 一个工具部署Gemma,WasmEdge 用来运行模型。
WasmEdge:https://github.com/wasmedge/wasmedge
🤩 WasmEdge 是在您自己的设备上运行 LLM 的最简单、最快的方法。🤩
WasmEdge 是一个轻量级、高性能且可扩展的 WebAssembly 运行时。它是当今最快的 Wasm 虚拟机。WasmEdge 是CNCF主办的官方沙箱项目。其用例包括现代 Web 应用程序架构(同构和 Jamstack 应用程序)、边缘云上的微服务、无服务器 SaaS API、嵌入式功能、智能合约和智能设备。
2.1 部署步骤
- 安装具有 LLM 支持的 WasmEdge
可以从一行命令开始安装 WasmEdge 运行时,并提供 LLM 支持。
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugin wasi_nn-ggml
或者可以按照此处的安装指南手动下载并复制 WasmEdge 安装文件。
- 在 Wasm 中下载 LLM 聊天应用程序
接下来,获取超小型 2MB 跨平台二进制文件 - LLM 聊天应用程序。它证明了效率,不需要其他依赖项并提供跨各种环境的无缝操作。这个小 Wasm 文件是从 Rust 编译而来的。要构建您自己的,请查看llama-utils存储库。
curl -LO https://github.com/LlamaEdge/LlamaEdge/releases/latest/download/llama-chat.wasm
3.下载Llama2 7b聊天模型
curl -LO https://huggingface.co/wasmedge/llama2/blob/main/llama-2-7b-chat-q5_k_m.gguf
模型下载汇总:https://github.com/LlamaEdge/LlamaEdge/blob/main/models.md
WasmEdge 还支持 Llama2、CodeLlama、Codeshell、Mistrial、MiscialLite、TinyLlama、Baichuan、BELLE、Alpaca、Vicuna、OpenChat、Starcoder、OpenBuddy 等等!
- 在 CLI 上与 Llama2 7b 模型聊天
现在您已完成所有设置,您可以开始使用命令行与 Llama2 7b 聊天支持的 LLM 聊天。
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-q5_k_m.gguf llama-chat.wasm
之后,您可以向 Llama2 7b chat 询问有关 WebAssembly 的任何问题。
[USER]:
Who is Robert Oppenheimer[ASSISTANT]:
Robert Oppenheimer was an American theoretical physicist and director of the Manhattan Project, which developed the atomic bomb during World War II. He is widely regarded as one of the most important physicists of the 20th century and is known for his contributions to the development of quantum mechanics and the theory of the atomic nucleus. Oppenheimer was also a prominent figure in the post-war nuclear weapons debate and was a strong advocate for international cooperation on nuclear weapons control.
您可以使用同一llama-chat.wasm
文件来运行其他 LLM,例如 OpenChat、CodeLlama、Mistral 等。
三、构建超轻量级 AI 代理
- 创建兼容OpenAI的API服务
当您使用领域知识或自托管 LLama2 模型微调模型时,仅使用 CLI 运行模型是不够的。接下来,我们为开源模型设置兼容 OpenAI 的 API 服务,然后我们可以将微调后的模型集成到其他工作流程中。
假设您已经安装了带有 ggml 插件的 WasmEdge 并下载了您需要的模型。
首先,通过终端下载Wasm文件来构建API服务器。
curl -LO https://github.com/LlamaEdge/LlamaEdge/releases/latest/download/llama-api-server.wasm
接下来,使用模型运行 API 服务器。
wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-wasm-q5_k_m.gguf llama-api-server.wasm -p llama-2-chat -s 0.0.0.0:8080
看到连接已建立后,您可以使用以下命令行来尝试您的模型。
curl -X POST http://localhost:8080/v1/chat/completions \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"messages":[{"role":"system", "content": "You are a helpful assistant. Answer each question in one sentence."}, {"role":"user", "content": "Who is Robert Oppenheimer?"}], "model":"llama-2-chat"}'
四、总结
此教程用于基于 WasmEdge 系统的部署,可根据自身需求定制部署环境,灵活调整配置参数,满足个性化需求。对模型和数据拥有完全控制权,可自由进行二次开发和扩展。