基于Ollama安装deepseek-r1模型搭建本地知识库

发布于:2025-02-13 ⋅ 阅读:(14) ⋅ 点赞:(0)

本实验主要使用win系统安装ollama部署deepseek-r1(1.5b、7b、8b、14b、32b等参数)并搭建本地知识库(个人学习研究为主,欢迎大佬指正!)

安装Ollama

Ollama是一款跨平台推理框架客户端(MacOS、Windows、Linux),旨在无缝部署大型语言模型 (LLM),例如 Llama 2、Mistral、Llava 等。Ollama 的一键式设置支持 LLM 的本地执行,通过将数据保存在自己的机器上,提供增强的数据隐私和安全性。

使用win系统安装

官网

  • 下载win版本即可

在这里插入图片描述
在这里插入图片描述

  • 下载后双击默认会安装在C盘

在这里插入图片描述

  • 创建大模型下载目录并配置系统变量

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

OLLAMA_HOST 
0.0.0.0
OLLAMA_ORIGINS
*
OLLAMA_MODELS
C:\OLLAMA_MODELS(自定义模型保存位置,按照自身需求设置路径)
  • 打开powershell执行ollama能查看到命令即可

在这里插入图片描述

命令使用

  • serve
    启动 Ollama 服务器,使其处于运行状态,等待处理请求

用法示例:

ollama serve
  • create
    从 Modelfile 创建一个新的模型

用法示例:

ollama create my-model
  • show

显示特定模型的信息,如版本、参数等
用法示例:

ollama show my-model
  • run
    运行一个模型,通常用于推理任务(如聊天、生成文本等)

用法示例:

ollama run my-model
  • stop
    停止正在运行的模型。

用法示例:

ollama stop my-model
  • pull
    从注册表(服务器)拉取一个模型到本地

用法示例:

ollama pull my-model
  • push
    将本地模型推送到注册表(服务器)

用法示例:

ollama push my-model
  • list
    列出本地可用的模型

用法示例:

ollama list
  • ps
    列出当前正在运行的模型。

用法示例:

ollama ps
  • cp
    复制一个模型。

用法示例:

ollama cp my-model new-model
  • rm
    删除本地的一个模型。

用法示例:

ollama rm my-model
  • 对于多行输入,可以使用以下方式换行"“”:

用法示例:

>>> """Hello,
... world!
... """
I'm a basic program that prints the famous "Hello, world!" message to the console.

使用sh脚本安装

系统Ubuntu2404:注意需要配置显卡,不然只能跑内存

root@huhy:~# curl -fsSL https://ollama.com/install.sh | sh
>>> Cleaning up old version at /usr/local/lib/ollama
>>> Installing ollama to /usr/local
>>> Downloading Linux amd64 bundle
######################################################################## 100.0%
>>> Creating ollama user...
>>> Adding ollama user to render group...
>>> Adding ollama user to video group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
Created symlink /etc/systemd/system/default.target.wants/ollama.service → /etc/systemd/system/ollama.service.
>>> The Ollama API is now available at 127.0.0.1:11434.
>>> Install complete. Run "ollama" from the command line.
WARNING: No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode.
root@huhy:~# systemctl status ollama
● ollama.service - Ollama Service
     Loaded: loaded (/etc/systemd/system/ollama.service; enabled; preset: enabled)
     Active: active (running) since Tue 2025-02-11 07:28:07 UTC; 2min 29s ago
   Main PID: 4219 (ollama)
      Tasks: 9 (limit: 4556)
     Memory: 30.4M (peak: 30.6M)
        CPU: 88ms
     CGroup: /system.slice/ollama.service
             └─4219 /usr/local/bin/ollama serve

Feb 11 07:28:07 huhy ollama[4219]: [GIN-debug] GET    /api/tags                 --> github.com/ollama/ollama/server.(*S>
Feb 11 07:28:07 huhy ollama[4219]: [GIN-debug] GET    /api/version              --> github.com/ollama/ollama/server.(*S>
Feb 11 07:28:07 huhy ollama[4219]: [GIN-debug] HEAD   /                         --> github.com/ollama/ollama/server.(*S>
Feb 11 07:28:07 huhy ollama[4219]: [GIN-debug] HEAD   /api/tags                 --> github.com/ollama/ollama/server.(*S>
Feb 11 07:28:07 huhy ollama[4219]: [GIN-debug] HEAD   /api/version              --> github.com/ollama/ollama/server.(*S>
Feb 11 07:28:07 huhy ollama[4219]: time=2025-02-11T07:28:07.851Z level=INFO source=routes.go:1238 msg="Listening on 127>
Feb 11 07:28:07 huhy ollama[4219]: time=2025-02-11T07:28:07.853Z level=INFO source=routes.go:1267 msg="Dynamic LLM libr>
Feb 11 07:28:07 huhy ollama[4219]: time=2025-02-11T07:28:07.856Z level=INFO source=gpu.go:226 msg="looking for compatib>
Feb 11 07:28:07 huhy ollama[4219]: time=2025-02-11T07:28:07.861Z level=INFO source=gpu.go:392 msg="no compatible GPUs w>
Feb 11 07:28:07 huhy ollama[4219]: time=2025-02-11T07:28:07.861Z level=INFO source=types.go:131 msg="inference compute
root@huhy:~# ollama run deepseek-r1:1.5b
pulling manifest
pulling aabd4debf0c8... 100% ▕██████████████████████████████████████████████████████████████████████████▏ 1.1 GB
pulling 369ca498f347... 100% ▕██████████████████████████████████████████████████████████████████████████▏  387 B
pulling 6e4c38e1172f... 100% ▕██████████████████████████████████████████████████████████████████████████▏ 1.1 KB
pulling f4d24e9138dd... 100% ▕██████████████████████████████████████████████████████████████████████████▏  148 B
pulling a85fe2a2e58e... 100% ▕██████████████████████████████████████████████████████████████████████████▏  487 B
verifying sha256 digest
writing manifest
success
>>> Send a message (/? for h
>>> 你好呀
<think>

</think>

你好!很高兴见到你,有什么我可以帮忙的吗?😊
  • 配置监听地址
vim /etc/systemd/system/ollama.service
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
#Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
Environment="OLLAMA_HOST=0.0.0.0" # 将上面行注释,添加此行
[Install]
WantedBy=default.target
systemctl daemon-reload
systemctl restart ollama
root@huhy:~# ss -tlun | grep 11
tcp   LISTEN 0      4096               *:11434            *:*

使用docker安装

系统Ubuntu2404:

  • 安装docker
apt -y install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt-get -y install docker-ce
  • 配置daemon文件
vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry-mirrors.yunyuan.co"],
  "insecure-registries" : ["0.0.0.0/0"]
}
systemctl daemon-reload
systemctl restart docker
  • 配置生产存储库:使用 NVIDIA GPU 驱动程序
    官网
curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
  • 可选)配置存储库以使用实验性软件包:
sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list
  • 安装 NVIDIA Container Toolkit 软件包:
apt update
apt install -y nvidia-container-toolkit

配置docker:官网

  • 使用以下命令配置容器运行时nvidia-ctk:
    该nvidia-ctk命令会修改/etc/docker/daemon.json主机上的文件。文件已更新,以便 Docker 可以使用 NVIDIA 容器运行时
nvidia-ctk runtime configure --runtime=docker
systemctl daemon-reload
systemctl restart docker

模式

  • 要为以Rootless 模式运行的 Docker 配置容器运行时,请按照以下步骤操作:
    使用以下命令配置容器运行时nvidia-ctk:
nvidia-ctk runtime configure --runtime=docker --config=$HOME/.config/docker/daemon.json
  • 重新启动 Rootless Docker 守护进程:
systemctl --user restart docker
  • /etc/nvidia-container-runtime/config.toml使用以下命令进行配置:sudo nvidia-ctk
nvidia-ctk config --set nvidia-container-cli.no-cgroups --in-place
  • 启动容器
docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
  • 要使用带有 AMD GPU 的 Docker 运行 Ollama,请使用rocm标签和以下命令:
docker run -d --device /dev/kfd --device /dev/dri -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama:rocm

下载大模型

官网
电脑系统信息:

硬件信息 配置
操作系统 W11专业版
CPU 12th Gen Intel® Core™ i5-12500H 2.50 GHz
RAM 64.0 GB
显卡 3050ti
显存 36.0 GB

官网说明:至少有 8 GB 的 RAM 来运行 7B 型号,16 GB 的 RAM 来运行 13B 型号,32 GB 的 RAM 来运行 33B 型号

  • 官网基本上支持市面的大部分模型:本实验以deepseek为例:
    对应模型参数所需磁盘空间如下:我的建议是磁盘空间允许的话可以都下载试试,根据自己电脑的实际情况来进行判断那个模型参数更适合自己电脑

在这里插入图片描述

  • 下载模型:命令格式如下(注意大多时候下载模型可能会报错,受网络影响,一般早上下载比较好,多试几次反复尝试!)
ollama run deepseek-r1:模型参数
PS C:\Users\huhy> ollama run deepseek-r1:1.5b
pulling manifest
pulling aabd4debf0c8... 100% ▕████████████████████████████████████████████████████████▏ 1.1 GB
pulling 369ca498f347... 100% ▕████████████████████████████████████████████████████████▏  387 B
pulling 6e4c38e1172f... 100% ▕████████████████████████████████████████████████████████▏ 1.1 KB
pulling f4d24e9138dd... 100% ▕████████████████████████████████████████████████████████▏  148 B
pulling a85fe2a2e58e... 100% ▕████████████████████████████████████████████████████████▏  487 B
verifying sha256 digest
writing manifest
success
PS C:\Users\huhy> ollama list
NAME                ID              SIZE      MODIFIED
deepseek-r1:32b     38056bbcbb2d    19 GB     45 seconds ago
deepseek-r1:14b     ea35dfe18182    9.0 GB    2 hours ago
deepseek-r1:8b      28f8fd6cdc67    4.9 GB    2 hours ago
deepseek-r1:7b      0a8c26691023    4.7 GB    2 hours ago
deepseek-r1:1.5b    a42b25d8c10a    1.1 GB    2 hours ago

()从 GGUF 导入

GGUF(GPT-Generated Unified Format)是一种专门为大型语言模型设计的文件格式,旨在优化模型的存储、加载和推理效率。它通常用于本地部署的模型(如基于LLaMA、Falcon等架构的模型),尤其是在资源受限的设备上(如个人电脑或嵌入式设备)

GGUF模型的核心特点

  • 高效存储
    GGUF采用量化技术(如4-bit、8-bit量化),大幅减少模型文件大小,同时尽量保持模型性能。
    适合在本地设备上存储和运行,尤其是显存或内存有限的场景。

  • 快速加载
    GGUF格式针对加载速度进行了优化,能够更快地将模型加载到内存或显存中,减少启动延迟。

  • 跨平台兼容
    GGUF格式设计时考虑了跨平台支持,可以在不同操作系统(如Windows、Linux、macOS)和设备(如CPU、GPU)上运行。

  • 模块化设计
    GGUF支持将模型拆分为多个文件,便于分块加载或更新,特别适合大型模型。

  • 易于部署
    GGUF格式通常与开源推理框架(如llama.cpp)配合使用,能够简化模型的本地部署流程。

创建一个名为 的文件Modelfile,其中FROM包含要导入的模型的本地文件路径的指令。(注意makefile文件和gguf文件要在同一目录)

FROM ./vicuna-33b.Q4_0.gguf

在 Ollama 中创建模型

ollama create example -f Modelfile

运行模型

ollama run example

搭建本地知识库

安装Dify

GitHub官网

Dify 是一个开源 LLM 应用开发平台。其直观的界面结合了代理 AI 工作流、RAG 管道、代理功能、模型管理、可观察性功能等,让您可以快速从原型转向生产

  • 使用docker compose 启动Dify,使用Ubuntu2404系统安装docker和docker compose
apt -y install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt-get -y install docker-ce
  • 配置daemon
cat > /etc/docker/daemon.json << EOF

{
  "registry-mirrors": ["https://registry-mirrors.yunyuan.co"],
  "insecure-registries" : ["0.0.0.0/0"]
}
EOF

systemctl daemon-reload
systemctl restart docker
  • 下载Dify文件

在这里插入图片描述

  • 上传后解压
apt install -y unzip
unzip dify-main.zip
cd dify-main/
cd docker
cp .env.example .env
docker compose up -d
root@huhy:~/dify-main/docker# docker compose up -d
[+] Running 74/74
 ✔ sandbox Pulled                                                                                                                                                                                         316.6s
 ✔ worker Pulled                                                                                                                                                                                          284.7s
 ✔ weaviate Pulled                                                                                                                                                                                        247.6s
 ✔ nginx Pulled                                                                                                                                                                                            12.3s
 ✔ api Pulled                                                                                                                                                                                             284.7s
 ✔ web Pulled                                                                                                                                                                                             241.4s
 ✔ db Pulled                                                                                                                                                                                              112.2s
 ✔ redis Pulled                                                                                                                                                                                           175.1s
 ✔ ssrf_proxy Pulled                                                                                                                                                                                       85.7s
[+] Running 11/11
 ✔ Network docker_default             Created                                                                                                                                                               0.1s
 ✔ Network docker_ssrf_proxy_network  Created                                                                                                                                                               0.1s
 ✔ Container docker-sandbox-1         Started                                                                                                                                                               1.7s
 ✔ Container docker-db-1              Started                                                                                                                                                               1.7s
 ✔ Container docker-web-1             Started                                                                                                                                                               1.8s
 ✔ Container docker-redis-1           Started                                                                                                                                                               2.0s
 ✔ Container docker-ssrf_proxy-1      Started                                                                                                                                                               1.9s
 ✔ Container docker-weaviate-1        Started                                                                                                                                                               1.8s
 ✔ Container docker-worker-1          Started                                                                                                                                                               3.2s
 ✔ Container docker-api-1             Started                                                                                                                                                               3.2s
 ✔ Container docker-nginx-1           Started                                                                                                                                                               4.0s
root@huhy:~/dify-main/docker# docker compose ps
NAME                  IMAGE                              COMMAND                  SERVICE      CREATED          STATUS                             PORTS
docker-api-1          langgenius/dify-api:0.15.3         "/bin/bash /entrypoi…"   api          14 seconds ago   Up 12 seconds                      5001/tcp
docker-db-1           postgres:15-alpine                 "docker-entrypoint.s…"   db           15 seconds ago   Up 14 seconds (healthy)            5432/tcp
docker-nginx-1        nginx:latest                       "sh -c 'cp /docker-e…"   nginx        14 seconds ago   Up 11 seconds                      0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp
docker-redis-1        redis:6-alpine                     "docker-entrypoint.s…"   redis        15 seconds ago   Up 13 seconds (health: starting)   6379/tcp
docker-sandbox-1      langgenius/dify-sandbox:0.2.10     "/main"                  sandbox      15 seconds ago   Up 14 seconds (health: starting)
docker-ssrf_proxy-1   ubuntu/squid:latest                "sh -c 'cp /docker-e…"   ssrf_proxy   15 seconds ago   Up 13 seconds                      3128/tcp
docker-weaviate-1     semitechnologies/weaviate:1.19.0   "/bin/weaviate --hos…"   weaviate     15 seconds ago   Up 14 seconds
docker-web-1          langgenius/dify-web:0.15.3         "/bin/sh ./entrypoin…"   web          15 seconds ago   Up 14 seconds                      3000/tcp
docker-worker-1       langgenius/dify-api:0.15.3         "/bin/bash /entrypoi…"   worker       14 seconds ago   Up 12 seconds                      5001/tcp
root@huhy:~/dify-main/docker#
  • 前端界面访问:可以通过浏览器通过http://localhost/install访问 Dify 仪表板并开始初始化过程

在这里插入图片描述

  • 输入密码登录
    在这里插入图片描述
  • 右上角账号中找到设置,选择模型供应商
    在这里插入图片描述
  • 选择Ollama,添加大模型:只需要修改名称和地址即可,注意地址是本地还是远程
    名称一定要对应不然会报错:An error occurred during credentials validation
PS C:\Users\huhy> ollama list
NAME                ID              SIZE      MODIFIED
deepseek-r1:32b     38056bbcbb2d    19 GB     9 hours ago
deepseek-r1:14b     ea35dfe18182    9.0 GB    11 hours ago
deepseek-r1:8b      28f8fd6cdc67    4.9 GB    11 hours ago
deepseek-r1:7b      0a8c26691023    4.7 GB    11 hours ago
deepseek-r1:1.5b    a42b25d8c10a    1.1 GB    11 hours ago

在这里插入图片描述

  • 继续添加Text Embedding。这是为知识库添加bge-large模型
    bge-large 是一个 Embedding(文本向量化)模型,全名是 BAAI General Embedding (BGE),由 BAAI(北京智源人工智能研究院) 开发,主要用于 语义搜索、文本检索、相似度计算、RAG(检索增强生成) 等任务
    bge-large 主要应用场景:
    • ✅ 语义搜索(Semantic Search)
    • ✅ 智能问答(QA Retrieval)
    • ✅ 知识库增强(RAG)(如 LlamaIndex, LangChain)
    • ✅ 文本相似度匹配(Sentence Similarity)
    • ✅ 推荐系统(Recommendation Systems)
PS C:\Users\huhy> ollama pull bge-large
pulling manifest
pulling 92b37e50807d... 100% ▕████████████████████████████████████████████████████████▏ 670 MB
pulling a406579cd136... 100% ▕████████████████████████████████████████████████████████▏ 1.1 KB
pulling 917eef6a95d7... 100% ▕████████████████████████████████████████████████████████▏  337 B
verifying sha256 digest
writing manifest
success

在这里插入图片描述

  • 最终添加如下:
    在这里插入图片描述

  • 创建本地知识库

在这里插入图片描述
在这里插入图片描述

  • 注意选择Embedding 模型后保存下一步

在这里插入图片描述
在这里插入图片描述

  • 创建应用

在这里插入图片描述

  • 选择聊天助手

在这里插入图片描述

  • 添加上下问,导入本地知识库

在这里插入图片描述

  • 发布聊天:可以根据场景选择发布方式

在这里插入图片描述
在这里插入图片描述

对比参数模型

分别测试不同参数的模型回答问题的准确性:文档中提到金奖的数量为参数队伍总数的10%,直接向AI提问(现在参加云计算竞赛的选手有65支,那金奖数量会有多少?)

在这里插入图片描述

  • 同时测试1.5b、7b、8b、14b模型

  • 1:对四个模型同时提问

在这里插入图片描述

  • 2:最小的1.5b模型先开始回答

在这里插入图片描述

  • 3:紧接着7b模型

在这里插入图片描述

  • 4:1.5b(还计算了铜牌和银牌,但是有点问题)和7b模型都是回答准确,然后8b开始回答

在这里插入图片描述

  • 5:最后是14b模型

在这里插入图片描述

  • 6:总体来说,模型越小速度越快,但思考深度方面相反

在这里插入图片描述

最后单独测试一下32b模型:显存肉眼可见的上升了,并且回答问题时十分卡顿,但相对来说回答的时候要更加考虑的全面一些

在这里插入图片描述