Windows 环境下 llama.cpp 编译运行指南

在大模型落地场景中,本地轻量化部署因低延迟、高隐私性、无需依赖云端算力等优势,成为开发者与 AI 爱好者的热门需求。本文聚焦 Windows 环境,详细拆解 llama.cpp 工具的编译流程,并指导如何通过 modelscope 下载 GGUF 格式的模型,最终实现模型本地启动与 API 服务搭建。

1、编译源码之前需要自行安装 Visual Studio 18 2026 开发工具,并打开VS2026 x64 兼容工具命令提示,执行如下命令克隆仓库。

bash 复制代码
Microsoft Visual Studio 18.0> git clone https://github.com/ggml-org/llama.cpp
Microsoft Visual Studio 18.0> mkdir build
Microsoft Visual Studio 18.0> cd build

2、基础编译(仅 CPU 支持)或者选用GPU 加速编译(已安装 CUDA Toolkit)版。

如果只使用 CPU 模式则执行如下配置

bash 复制代码
Microsoft Visual Studio 18.0> cmake .. -G "Visual Studio 18 2026" -A x64 -DLLAMA_CURL=OFF
Microsoft Visual Studio 18.0> cmake --build . --config Release

如果已安装 CUDA Toolkit 工具包,则添加 -DLLAMA_CUDA=ON 开启 GPU 加速支持

bash 复制代码
Microsoft Visual Studio 18.0> cmake .. -G "Visual Studio 18 2026" -A x64 -DLLAMA_CUDA=ON
Microsoft Visual Studio 18.0> cmake --build . --config Release

编译完成后则会在build目录生成对应的可执行文件,其中的llama-server.exe则为主程序

3、下载 GGUF 格式的模型权重文件,模型权重已同步上线两大主流渠道,开发者任选其一即可。

以魔搭社区为例,直接使用官方PIP包拉取仓库内容到本地,下载后的保存位置为 C:\Users\Admin\dir 执行以下命令完成模型下载。

bash 复制代码
# 安装魔搭
C:> pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple modelscope

# 下载 Qwen2.5-1.5B-Instruct-GGUF 完整仓库模型文件
C:> modelscope download --model Qwen/Qwen2.5-1.5B-Instruct-GGUF

# 下载 Qwen2.5-1.5B-Instruct-GGUF 仓库下 qwen2.5-1.5b-instruct-q4_k_m.gguf 模型文件
C:> modelscope download --model Qwen/Qwen2.5-1.5B-Instruct-GGUF qwen2.5-1.5b-instruct-q4_k_m.gguf --local_dir ./dir

4、将下载好的qwen2.5-1.5b-instruct-q4_k_m.gguf模型文件,放在llama-server.exe同级目录下,同时打开命令行工具,执行启动命令:

bash 复制代码
# 执行命令行启动
C:> chcp 65001
C:> llama-cli.exe -m qwen2.5-1.5b-instruct-q4_k_m.gguf -i -c 4096

# 执行启动CPU版本服务端
C:> llama-server.exe -m qwen2.5-1.5b-instruct-q4_k_m.gguf --host 127.0.0.1 --port 11433 -c 4096

# 执行驱动GPU加速版服务端
C:> llama-server.exe -m qwen2.5-1.5b-instruct-q4_k_m.gguf --host 127.0.0.1 --port 11433 -c 1024 --n-gpu-layers 32

5、直接调用 /completion 接口,原生Python标准库,无需额外第三方包,验证服务可用性。

python 复制代码
import json
from urllib import request, error

url = "http://127.0.0.1:11433/completion"
headers = {"Content-Type": "application/json"}

prompt = """<|im_start|>user
你好,简单介绍一下自己<|im_end|>
<|im_start|>assistant
"""

data = {
    "model": "qwen2.5-1.5b-instruct-q4_k_m.gguf",
    "prompt": prompt,
    "temperature": 0.7,
    "max_tokens": 512,
    "ctx_size": 4096,
    "stop": ["<|im_end|>"],
    "stream": False
}

try:
    data_json = json.dumps(data).encode("utf-8")
    req = request.Request(url, data=data_json, headers=headers, method="POST")
    with request.urlopen(req, timeout=60) as response:
        result = json.loads(response.read().decode("utf-8"))

    print("生成结果:")
    print(result["content"].strip())

except error.HTTPError as e:
    print(f"调用失败(HTTP错误):{e.code} - {e.reason}")
except error.URLError as e:
    print(f"调用失败(连接/网络错误):{e.reason}")
except Exception as e:
    print(f"调用失败(其他异常):{e}")

运行main.py,正常输出示例如下,代表本地大模型服务部署完成,可以对外提供推理服务。

bash 复制代码
C:> main.py
生成结果:
您好!我是来自阿里巴巴的AI助手,我叫通义千问。
相关推荐
ι:2 小时前
DeepSeek Harness 桌面一键启动与网页背景修改指南
windows·学习·deepseekharness
zuozewei3 小时前
7DGroup 开源 dsh-7d-tray-win 系统托盘插件
windows·代码复审
深念Y4 小时前
Wine 运行 HiTool 踩坑记录
linux·windows·容器·桌面·wine·虚拟器
玖釉-4 小时前
nvpro_core2 源码与架构解析:NVIDIA Vulkan 图形开发基础框架
c++·windows·图形渲染
淡海水4 小时前
01-08-运行时-虚方法分派与接口调用的底层实现
linux·windows·microsoft·c#·虚方法·vtable·vsd
无证驾驶梁嗖嗖5 小时前
两台电脑网线直连SMB共享
windows·文件传输·smb
刻BITTER5 小时前
让 Intel NPU 跑AI 大模型?一次 llama.cpp OpenVINO 后端的完整实测
人工智能·llama·openvino
笨鸟先飞,勤能补拙19 小时前
从预测到决策:人工智能与机器学习的系统方法、工程闭环与现实边界
大数据·人工智能·windows·python·机器学习·密码学
程序员-李俞20 小时前
Mistral OCR 4真正改变的不是“识字”:文档AI正在变成Agent的数据入口
人工智能·windows·ai作画·aigc·ocr·ai编程·ai写作