本地CPU跑大模型——编译使用llama.cpp项目,只用CPU跑Deepseek R1等大模型

1,573 阅读2分钟

无需显卡!只需CPU就能跑起来对话模型!十年前老电脑依旧可以跑AI!穷玩AI!llama.cpp项目编译部署模型!

编译llamp.cpp项目,在Window环境下跑对话模型

以通义千问模型为例

Step0 工具软件、模型文件的下载和安装

下载mingw-w64编译器和和配置环境变量配置
下载地址
hub.gitmirror.com/https://git…

环境变量Path里加入编译器的bin目录,然后重启电脑

下载git并安装

下载地址
mirrors.tuna.tsinghua.edu.cn/github-rele…

设置好PATH变量并且安装好git以后重启电脑

打开终端 键入git --version确保git安装成功

git --version

此时正常情况下命令行输出版本号 例如 git version 2.38.1.windows.1

配置git

git config --global user.name 你的用户名
git config --global user.email 你的邮箱

键入cmake --version确保编译器目录设置成功

cmake --version

此时正常情况下命令行输出版本号 例如

cmake version 3.27.8

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Step1 git获取源码

git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp

Step2 cmake编译源码

mkdir build
cd build
cmake ..
cmake --build . --config Release

Step3 进入build目录查看编译结果(build/bin/Release)

从源码目录【llama.cpp】文件夹中拷贝【prompts】文件夹到 【build/bin/Release/】 下

Step4. 下载通义千问模型

hf-mirror.com/ 下载所需的量化模型

文件名大小修改时间
Qwen2.5-7B-Instruct-1M-Q4_K_M4.68GB2025-01-25 15:38:57
DeepSeek-R1-Distill-Llama-8B-Q4_K_M5.73GB2025-01-20 13:09:12

Step5. 命令行启动大模型

例如

./llama.cpp/llama-cli \ 
    --model models/Qwen2.5-7B-Instruct-1M-Q4_K_M.gguf \
    -n 4096 \
    -f prompts/chat-with-qwen.txt
./llama.cpp/llama-cli \
    --model models/DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf \
    --cache-type-k q8_0 \
    --threads 16 \
    --prompt '<|User|>What is 1+1?<|Assistant|>' \
    -no-cnv

models/Qwen2.5-7B-Instruct-1M-Q4_K_M.gguf是量化模型存储的本地路径

为了方便起见,编写一个启动脚本

#!/bin/bash
# 获取符合条件的模型文件列表
options=()
while IFS= read -r -d '' file; do
    options+=("$file")
done < <(find /local-path-to/aimodels/ -maxdepth 1 -type f -name "*.gguf" -print0)

modelCount=${#options[@]}

# 打印并让用户选择模型
for ((i = 0; i < $modelCount; i++)); do
    echo "$(($i + 1)) => ${options[$i]}"
done

# 检查用户输入并选择模型
if [ $# -ge 1 ] && [ "$1" -gt 0 ] && [ "$1" -lt $(($modelCount + 1)) ]; then
    selectItem=${options[$(($1 - 1))]}
    # 获取与模型对应prompt文件路径的函数
    get_prompt_file() {
        case "$selectItem" in
        *"baichuan"*) promptFile="/local-path-to/aipro/prompts/chat-with-baichuan.txt" ;;
        *"qwen"*) promptFile="/local-path-to/aipro/prompts/chat-with-qwen.txt" ;;
        *) promptFile="/local-path-to/aipro/prompts/assistant.txt" ;;
        esac
        echo "$promptFile"
    }

    promptFile=$(get_prompt_file)
    echo "当前模型: 【$selectItem】"
    # 使用选定的模型和prompt文件执行LLAMA命令
    /local-path-to/aipro/llama/llama-cli.exe \
    -m $selectItem \
    -n 4096 \
    --color -i \
    -t 2 \
    -c 2048

fi

/local-path-to/aimodels/是量化模型存储的本地路径,/local-path-to/aipro/prompts/是系统提示词文件存储的本地路径

根据个人电脑CPU性能来选择相应的模型