【DeepSeek部署实战】一键本地推理,DeepSeek-R1 蒸馏模型 + llama.cpp 部署教程

系列篇章💥

No.文章
01【DeepSeek应用实践】DeepSeek接入Word、WPS方法详解:无需代码,轻松实现智能办公助手功能
02【DeepSeek应用实践】通义灵码 + DeepSeek:AI 编程助手的实战指南
03【DeepSeek应用实践】Cline集成DeepSeek:开源AI编程助手,终端与Web开发的超强助力
04【DeepSeek开发入门】DeepSeek API 开发初体验
05【DeepSeek开发入门】DeepSeek API高级开发指南(推理与多轮对话机器人实践)
06【DeepSeek开发入门】Function Calling 函数功能应用实战指南
07【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:本地部署与API服务快速上手
08【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:Web聊天机器人部署指南
09【DeepSeek部署实战】DeepSeek-R1-Distill-Qwen-7B:基于vLLM 搭建高性能推理服务器
10【DeepSeek部署实战】基于Ollama快速部署DeepSeek-R1系列模型实战指南(Linux)
11【DeepSeek部署实战】基于Ollama+Chatbox零成本部署DeepSeek-R1系列模型攻略(Windows)
12【DeepSeek开发实战】DeepSeek-R1-Distill-Qwen与LangChain的本地大模型应用开发指南
13【DeepSeek部署实战】一键本地推理,DeepSeek-R1 蒸馏模型 + llama.cpp 部署教程


前言

在当今快速发展的 AI 领域,本地运行大型语言模型(LLM)的需求日益增长。llama.cpp 是一个高性能的 C/C++ 库,专门用于运行 LLM,支持多种硬件加速选项。本文将详细介绍如何使用 llama.cpp 在本地运行 DeepSeek-R1 蒸馏模型(1.5B-GGUF),帮助您在消费级硬件上实现高效推理。

一、环境准备

在开始之前,需要确保开发环境已经准备好。这包括安装必要的工具和库,以及配置好适合的硬件环境。

1. 下载llama.cpp

首先,需要从 GitHub 克隆 llama.cpp 仓库。llama.cpp 提供了简洁的 API 和高效的内存管理,非常适合在本地运行复杂的模型。

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

执行如下:
在这里插入图片描述

2. 编译llama.cpp

根据您的硬件配置,选择合适的编译命令。llama.cpp支持CPU和GPU两种运行模式,您可以根据需求选择。

  • CPU 构建(适用于无GPU环境):
    如果您的设备没有GPU,或者您希望仅使用CPU进行推理,可以使用以下命令进行编译:

    cmake -B build
    cmake --build build --config Release
    

    这将生成适用于CPU的可执行文件。

  • GPU 构建(支持CUDA加速):
    如果您的设备支持CUDA,并且您希望利用GPU加速模型推理,可以使用以下命令:

    cmake -B build -DGGML_CUDA=ON
    cmake --build build --config Release
    

执行如下:
在这里插入图片描述

3. 安装相关依赖

为了运行模型并使用相关的功能,您需要安装一些Python依赖。这些依赖将帮助您下载模型文件,并在Python环境中与模型进行交互。
使用modelscope库可以方便地下载模型文件,我们需要先安装modelscope:

pip install modelscope

安装如下:
在这里插入图片描述

安装openai,方便后面代码使用openai风格的API接口

pip install openai==0.28

安装如下:
在这里插入图片描述

4. 准备GGUF模型文件

在运行模型之前,需要获取模型文件。DeepSeek-R1蒸馏模型是一个经过优化的版本,适合在本地运行,同时保持较高的推理性能。
在这里插入图片描述

我们从modelscope仓库下载DeepSeek-R1蒸馏版模型。下面我们以Q6_K量化为例,展示如何下载模型文件。

modelscope download --model unsloth/DeepSeek-R1-Distill-Qwen-1.5B-GGUF DeepSeek-R1-Distill-Qwen-1.5B-Q6_K.gguf --local_dir /root/autodl-tmp/models

完整如下:
在这里插入图片描述

下载完成后,模型文件将保存到models目录。
在这里插入图片描述

直接下载:如果你喜欢直接下载文件,可以通过以下链接手动下载GGUF文件,并将其保存到models目录:

二、启动模型服务(一键启动)

在下载并准备好了模型文件之后,接下来需要启动模型服务。llama.cpp提供了一个轻量级的HTTP服务器,可以方便地运行和访问模型。

根据您的操作系统和硬件配置,选择合适的命令启动模型服务:

  • Windows示例(使用预编译二进制)
    如果您使用的是Windows系统,并且已经下载了预编译的二进制文件,可以使用以下命令启动服务器:
start cmd.exe /k "llama-server.exe -m models/DeepSeek-R1-Distill-Qwen-1.5B-Q6_K.gguf -c 15000 -ngl 999"

Linux/Mac示例(源码编译后)
如果您使用的是Linux或Mac系统,并且已经从源码编译了llama.cpp,可以使用以下命令启动服务器:

./build/bin/llama-server -m /root/autodl-tmp/models/DeepSeek-R1-Distill-Qwen-1.5B-Q6_K.gguf --port 8080 -ngl 40

启动完成如下:
在这里插入图片描述

关键参数说明

  • -c 15000:设置上下文长度(根据内存调整)。上下文长度决定了模型在推理时可以处理的最大文本长度。
  • -ngl 999:设置GPU层数(设为0则纯CPU运行)。如果您希望利用GPU加速,可以根据您的硬件配置调整这个参数。
  • --host 127.0.0.1:绑定本地端口。这将使服务器仅在本地运行,确保安全性。

在启动服务后,您可以通过以下方式验证模型是否正常运行:

使用验证(一):浏览器界面体验

打开浏览器并访问http://127.0.0.1:8080,您应该能够看到一个简单的Web界面,用于测试模型的推理功能。
在这里插入图片描述

使用验证(二):bash脚本调用API

在命令行窗口,可以使用以下脚本:

curl http://localhost:8080/v1/chat/completions \
 -H "Content-Type: application/json" \
 -d '{
"model": "DeepSeek-R1-Distill-Qwen-1.5B",
"messages": [{"role": "user", "content": "请介绍一下deepseek"}],
"temperature": 0
}'

模型返回如下:

{"choices":[{"finish_reason":"stop","index":0,"message":{"role":"assistant","content":"<think>\n\n</think>\n\n深度求索人工智能基础技术研究有限公司(简称“深度求索”或“DeepSeek”),成立于2023年,是一家专注于实现AGI的中国公司。"}}],"created":1740921610,"model":"DeepSeek-R1-Distill-Qwen-1.5B","system_fingerprint":"b4798-1782cdfe","object":"chat.completion","usage":{"completion_tokens":42,"prompt_tokens":7,"total_tokens":49},"id":"chatcmpl-cmfV78hIDgvBQkL1kh6LivxpthPOgA3p","timings":{"prompt_n":5,"prompt_ms":24.283,"prompt_per_token_ms":4.8566,"prompt_per_second":205.90536589383518,"predicted_n":42,"predicted_ms":173.381,"predicted_per_token_ms":4.128119047619047,"predicted_per_second":242.24107601178906}}

使用验证(三):Python脚本调用API调用

在Python环境中使用模型,可以使用以下脚本:

import openai

openai.api_base = "http://127.0.0.1:8080/v1"
openai.api_key = "EMPTY"  # Not used

response = openai.Completion.create(
model="local",
prompt="What is 1+1?"
)

print(response.choices[0].text)

这将调用模型并输出推理结果,如下:

 *and* 1+1.
Wait, no, that's just one question. Maybe I should try another approach. Wait, perhaps I'm overcomplicating things.

Actually, I need to solve the equation: 1+1=10 in some base. Hmm, okay, so let's think about number bases. In base 10, we know that 1+1=2. But in other bases, the representation changes. So, if I have the equation 1+1=10 in some base, what would that base be?

Wait, so 10 in base b is equal to 1*b + 0 = b in base 10. Similarly, 1+1=2 in base 10. So, in the equation, 1+1=10 in base b translates to 2 = b in base 10. So, solving for b, we get b=2. So, the base is 2. Hmm, that seems straightforward, but maybe I'm missing something.

Alternatively, maybe the equation isn't 1+1=10 in base b, but perhaps 1+1=10 in base 10? No, that doesn't make sense because in base 10, 1+1=2, which isn't 10. So, that can't be. So, I think my initial reasoning is correct.

But just to double-check, let's see: if in base b, 1+1 equals 10, then in base 10, this would mean 1*b + 1 = 1*b + 0? Wait, no, 10 in base b is equal to 1*b + 0. So, 1+1=2, but 10 in base b is equal to b. So, 2 must equal b. Therefore, b is 2.

But, just to make sure I haven't overlooked something, let me think about another approach. In base b, the digits must satisfy 0 ≤ digit < b. So, in the equation 1+1=10, the digits are 1,1, and 0. In base 2, the digits can only be 0 or 1, which is fine because the digits in the equation are 1 and 0. So, base 2 is acceptable.

Another way to think about it: when adding 1 and 1 in base b, if the result is 10, which is equal to b in base 10. So, since 1+1=2 in base 10, that must be equal to b. Therefore, b=2.

Alternatively, maybe we can consider that in base b, the number 10 represents b in base 10. So, 1+1=2, and in base b, 2 must equal 10, meaning that 2= b. Hence, b=2.

So, after thinking through different angles, I'm confident that the base is 2.

**Final Answer**
The base is \boxed{2}.
</think>

To solve the equation \(1 + 1 = 10\) in some base, we need to determine the base \(b\) such that the equation holds true.

1. In any base \(b\), the number \(10\) represents \(b\) in base 10.
2. The equation \(1 + 1 = 10\) translates to \(1 + 1 = b\) in base 10.
3. Simplifying this, we get \(2 = b\).

Thus, the base \(b\) is 2. This is confirmed because in base 2, the number \(10\) represents 2 in base 10, and the addition \(1 + 1\) in base 2 results in 10.

Therefore, the base is \(\boxed{2}\).

三、性能优化与问题排查

在运行模型时,您可能会遇到一些性能问题或错误。以下是一些优化建议和常见问题的解决方案:

1. 硬件适配建议

  • 显存不足:如果您遇到显存不足的问题,可以尝试降低-ngl值(减少GPU层数),或者使用低量化版本(如Q4_K_M)。低量化版本可以在减少内存占用的同时,保持较高的推理性能。
  • 内存限制:对于32GB内存的设备,建议运行Q4量化版。1.5B模型至少需要8GB内存。如果您的设备内存较小,建议选择更小的模型版本。

2. 常见错误解决

  • Tokenizer错误:如果您遇到Tokenizer相关的错误,请确保llama.cpp版本不低于b4514。更新到最新版本通常可以解决这类问题。
  • 模型加载失败:如果模型加载失败,请检查GGUF文件的完整性,或者尝试重新下载模型文件。确保下载的文件与您选择的模型版本一致。

四、总结

通过llama.cpp运行DeepSeek-R1蒸馏版模型,您可以在消费级硬件上体验高性能推理。llama.cpp提供了灵活的配置选项,支持多种硬件加速方式,并且易于部署。建议优先使用预编译二进制文件以简化部署流程,并根据硬件配置调整量化参数与GPU层数。如果您需要更高的推理精度,可以尝试运行更大参数版本的模型,但请注意这需要专业级硬件设备支持。希望本指南能帮助您顺利运行DeepSeek-R1模型,享受高效、灵活的本地推理体验!

在这里插入图片描述

🎯🔖更多专栏系列文章:AI大模型提示工程完全指南AI大模型探索之路(零基础入门)AI大模型预训练微调进阶AI大模型开源精选实践AI大模型RAG应用探索实践🔥🔥🔥 其他专栏可以查看博客主页📑

😎 作者介绍:资深程序老猿,从业10年+、互联网系统架构师,目前专注于AIGC的探索(优快云博客之星|AIGC领域优质创作者)
📖专属社群:欢迎关注【小兵的AI视界】公众号或扫描下方👇二维码,回复‘入群’ 即刻上车,获取邀请链接。
💘领取三大专属福利:1️⃣免费赠送AI+编程📚500本,2️⃣AI技术教程副业资料1套,3️⃣DeepSeek资料教程1套🔥(限前500人)
如果文章内容对您有所触动,别忘了点赞、⭐关注,收藏!加入我们,一起携手同行AI的探索之旅,开启智能时代的大门!

<think>好的,我现在需要帮用户介绍如何在本地使用Local AI、llama.cppDeepSeek部署RAG的详细流程。首先,我需要确认自己对各个组件的理解是否正确。RAG是检索增强生成,结合了检索外部知识和生成模型的能力,对吧?Local AI可能是指本地运行的AI框架,llama.cpp是C++实现的LLM推理引擎,DeepSeek可能是一个特定的模型或者数据集?可能需要进一步确认。 用户的需求是详细的部署流程,所以需要分步骤说明。首先,可能需要环境准备,比如安装依赖、下载模型等。然后,RAG通常包括文档处理、向量数据库、检索和生成几个部分。我需要考虑如何整合这些组件。 首先,安装llama.cpp,需要从GitHub克隆仓库,编译,可能需要安装依赖如CMake、Python等。然后下载DeepSeek模型,可能是Hugging Face格式,需要转换成gguf格式以便llama.cpp使用。转换步骤可能需要使用llama.cpppython脚本。 接下来,处理用户文档,使用嵌入模型生成向量,建立向量数据库。这里可能需要用到Local AI提供的嵌入模型,或者Hugging Face的某个模型。然后,将查询时检索相关文档,输入到DeepSeek模型中生成答案。 需要详细说明每个步骤的具体操作,比如转换模型的命令、处理文档的代码示例、如何启动llama.cpp的API服务,以及如何集成检索和生成部分。可能还需要提到使用FAISS或Chroma作为向量数据库,以及如何将检索到的上下文传递给生成模型。 需要注意的是,用户可能对某些步骤不熟悉,比如模型转换、向量数据库的构建,所以需要给出具体的命令和代码示例。同时,要确保所有步骤在本地运行,不需要外部API,符合Local AI的要求。另外,DeepSeek模型的具体名称和下载位置可能需要确认,是否有现成的gguf版本,或者是否需要自己转换。 可能遇到的坑:模型转换时的参数设置,向量数据库的匹配问题,以及如何有效将检索结果整合到生成模型的输入中。需要提示用户可能需要的调整,比如上下文长度限制,或者检索top_k的设置。 总结下来,步骤大概是:环境准备→模型下载与转换→文档处理与向量数据库构建→RAG流程集成→测试与优化。每个步骤需要详细命令和代码,确保用户能一步步操作。</think>以下是在本地使用 **Local AI + llama.cpp + DeepSeek** 部署 RAG(Retrieval-Augmented Generation)的详细流程: --- ### **1. 环境准备** #### **1.1 安装依赖** ```bash # 基础依赖 sudo apt-get update && sudo apt-get install -y build-essential cmake python3-pip # Python 依赖 pip install numpy pandas sentence-transformers faiss-cpu langchain flask ``` #### **1.2 克隆 llama.cpp 仓库** ```bash git clone https://github.com/ggerganov/llama.cpp cd llama.cpp && make ``` --- ### **2. 模型准备** #### **2.1 下载 DeepSeek 模型** - 从 Hugging Face 下载模型(如 `deepseek-ai/deepseek-llm-7b-chat`): ```bash git lfs install git clone https://huggingface.co/deepseek-ai/deepseek-llm-7b-chat ``` #### **2.2 转换模型为 GGUF 格式** ```bash # 安装转换依赖 pip install -r llama.cpp/requirements.txt # 转换 HF 模型到 GGUF python3 llama.cpp/convert-hf-to-gguf.py deepseek-llm-7b-chat --outfile deepseek-7b-chat.gguf # 量化(可选,减少内存占用) ./llama.cpp/quantize deepseek-7b-chat.gguf deepseek-7b-chat-Q4_K_M.gguf Q4_K_M ``` --- ### **3. 构建向量数据库** #### **3.1 准备本地文档** 将文本文件(如 PDF、TXT)存放在 `documents/` 目录下。 #### **3.2 生成文档向量** ```python from sentence_transformers import SentenceTransformer from langchain.vectorstores import FAISS # 加载嵌入模型本地运行) embed_model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2") # 读取文档并分块 documents = [...] # 从本地文件读取文本并分块 # 生成向量 doc_embeddings = embed_model.encode([doc.text for doc in documents]) # 构建向量数据库 vector_db = FAISS.from_embeddings(doc_embeddings, documents) vector_db.save_local("faiss_index") ``` --- ### **4. 部署 RAG 流程** #### **4.1 启动 llama.cpp 服务** ```bash ./llama.cpp/server -m deepseek-7b-chat-Q4_K_M.gguf --ctx-size 2048 --port 8000 ``` #### **4.2 实现 RAG 逻辑** ```python from flask import Flask, request, jsonify from sentence_transformers import SentenceTransformer from langchain.vectorstores import FAISS app = Flask(__name__) embed_model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2") vector_db = FAISS.load_local("faiss_index", embed_model) @app.route("/ask", methods=["POST"]) def ask(): # 检索 query = request.json["query"] query_embed = embed_model.encode([query]) docs = vector_db.similarity_search_by_vector(query_embed[0], k=3) # 构造提示词 context = "\n".join([doc.text for doc in docs]) prompt = f"基于以下上下文回答问题:\n{context}\n\n问题:{query}\n回答:" # 调用本地模型生成 response = requests.post( "http://localhost:8000/completion", json={"prompt": prompt, "temperature": 0.7, "max_tokens": 512} ) return jsonify({"answer": response.json()["content"]}) if __name__ == "__main__": app.run(port=5000) ``` --- ### **5. 测试与优化** #### **5.1 发送查询请求** ```bash curl -X POST http://localhost:5000/ask -H "Content-Type: application/json" -d '{"query": "如何解释量子计算?"}' ``` #### **5.2 优化方向** - **性能优化**:调整 `k`(检索文档数量)和 `max_tokens`(生成长度)。 - **模型优化**:尝试不同量化版本的 DeepSeek 模型(如 Q5_K_M)。 - **检索优化**:使用更精确的嵌入模型(如 `bge-large-zh-v1.5`)。 --- ### **关键点说明** 1. **本地化运行**:所有组件(模型、向量数据库、服务)均在本地运行,无需网络。 2. **硬件要求**: - 7B 模型需至少 8GB 内存(量化后)。 - GPU 加速需编译 llama.cpp 时启用 CUDA。 3. **扩展性**:可替换为其他开源模型(如 Qwen、ChatGLM3)。 通过以上步骤,你可以在本地低成本实现一个基于私有数据的智能问答系统。
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寻道AI小兵

🐳 感谢你的巨浪支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值