本地轻量化部署因低延迟、高隐私性、无需依赖云端算力等优势,成为开发者热门需求。本文聚焦 Windows 10/11(64 位)环境,详解 llama.cpp 编译流程及 Qwen 模型本地部署。
1. 克隆代码
打开管理员权限的 PowerShell/CMD,执行以下命令:
git clone https://github.com/ggml-org/llama.cpp
mkdir build
cd build
2. 编译配置
仅 CPU 支持或启用 GPU 加速(需安装 CUDA Toolkit)。
CPU 模式:
cmake .. -G "Visual Studio 17 2022" -A x64 -DLLAMA_CURL=OFF
cmake --build . --config Release
GPU 模式:
cmake .. -G "Visual Studio 17 2022" -A x64 -DLLAMA_CUDA=ON
cmake --build . --config Release
3. 下载模型
使用 ModelScope 下载 GGUF 格式的 Qwen 模型(以 7B 为例)。
pip install modelscope
modelscope download --model Xorbits/Qwen-7B-Chat-GGUF
下载后保存位置通常为 \modelscope\hub\models\Xorbits。
4. 启动服务
运行模型启动 API 服务,默认监听 8080 端口。
# CPU 版
chcp 65001
llama-cli.exe -m qwen.gguf -i -c 4096
# GPU 加速版 (Server)
llama-server.exe -m qwen-7b-chat.Q4_0.gguf -c 4096 --port 8080 --host 127.0.0.1 --n-gpu-layers -1
5. 测试调用
服务启动后,可通过 curl 测试。
curl http://localhost:8080/completion -H "Content-Type: application/json" -d '{ "prompt": "你好", "temperature": 0.7, "max_tokens": 512 }'
6. Python 脚本调用
基础非流式调用
import requests
import json
url = "http://localhost:8080/completion"
headers = {"Content-Type": "application/json"}
data = {
"model": "qwen.gguf",
"prompt": "你好,请用 100 字介绍一下通义千问",
: ,
: ,
: ,
: []
}
:
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=)
response.raise_for_status()
result = response.json()
()
(result[])
Exception e:
()

