文章

大模型记录.

跟上时代潮流,自己来动手.

大模型记录.

干瞪眼五分钟,愣一个字没动,下意识在想怎么写标题写大纲,怎么写才能最完美。

干!写起来先!

想做这个的原因

实际工作中更偏向于写业务的算法,但是想着自己怎么着也叫“算法工程师”,却对大模型几乎一窍不通,这怎么行?

于是乎想着部署一个LLM,动手学习一下,补充一下知识。

看着看着,想到公司需要在入职培训的时候投入大量的时间和精力,因为连锁超市的门店员工普遍干不了多久。

所以我做这个有几个目的:

  1. 动手实践一下部署LLM,边动手边学。而不是一直先想太多,导致没动力去迈出第一步。
  2. 往入职培训助手那去靠拢,万一公司需要呢。不要也行,就当练手。
  3. 对现在的主流,有一个认识。如果以后有要结合的地方,可以更快上手。

设备

  1. 远程服务器:Linux,x86_64
  2. 代理服务器:Linux
  3. 本地:Mac Mini M4 24G

远程服务器在某内部,无法直接从外部访问,使用了一个 代理服务器来做端口穿透。通过这种方式,外部请求可以通过代理服务器转发到远程服务器。

Ollama

Ollama部署

Ollama官网:click here

远程服务器

安装

官网的curl -fsSL https://ollama.com/install.sh | sh太慢了。

click here 这里下载对应版本的 tgz,建议使用迅雷下载。

我有百度云会员,故用 bypy传至远程服务器。

接着按照官方 linux 步骤进行配置,默认端口 11434。

Ollama 常用的环境变量ollama help serve(!需要学习!)

安全性

启动服务后,通过sudo lsof -i -P -n | grep 11434得知 Ollama 只在本地接口(127.0.0.1)上监听端口 11434,没有把端口暴露给公网。

若后续需要将端口暴露到公网,则!另外学习!

建立隧道

ssh -L 11434:localhost:11434 username@111.111.111.111 -p 1111

输入密码建立成功后,在新 cmd 中输入curl http://localhost:11434得到Ollama is running表示成功。

Quickstart

ollama run llama3.2,等待模型参数下载后,即可在远程服务器进行对话。

本地

调用

确保远程已经 download了想使用的模型,以及建立了 ssh 隧道。

本地输入

1
2
3
4
5
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "你好吗?请你只输出一个字。",
  "stream": false
}'

本地输出 Ollama 推理 API 的标准响应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	"model": "llama3.2",
	"created_at": "2025-04-11T01:50:39.889219666Z",
	"response": "好",
	"done": true,
	"done_reason": "stop",
	"context": [128006, 9125, 128007, 271, 38766, 1303, 33025, 2696, 25, 6790, 220, 2366, 18, 271, 128009, 128006, 882, 128007, 271, 57668, 53901, 103054, 39045, 57668, 92780, 67117, 220, 16, 109820, 19113, 128009, 128006, 78191, 128007, 271, 53901],
	"total_duration": 77196406,
	"load_duration": 28357222,
	"prompt_eval_count": 36,
	"prompt_eval_duration": 23391493,
	"eval_count": 2,
	"eval_duration": 24445403
}

open-webUI 部署

open-webUI 官网:click here

远程服务器

安装

先注册 HuggingFace ,然后安装 HuggingFacepip install huggingface-hub,然后登录huggingface-cli login, 在官网创建 token(read access 全部点上),然后在 cmd 输入 token。

记得先运行 Ollama。

接着安装 open-webui,pip install open-webui,然后启动open-webui serve

建立隧道

ssh -L 8080:localhost:8080 username@111.111.111.111 -p 1111

本地

网页输入http://localhost:8080/,自动连接 Ollama。

探索 Ollama 部署自定义模型

Ollama 官网导入模型手册(没参考):click here

llama.cpp官网 github:click here

hf下载模型

模型可以在https://huggingface.co/,或者huggingface镜像网站https://hf-mirror.com/,或者魔搭社区进行下载,我用魔搭社区的python脚本进行下载,下载速度直接拉满,执行前需要先运行pip install modelscope 。

1
2
3
4
5
6
7
from modelscope import snapshot_download
 
#模型存放路径
model_path = r'D:\huggingface'
#模型名字
name = 'itpossible/Chinese-Mistral-7B-Instruct-v0.1'
model_dir = snapshot_download(name, cache_dir=model_path, revision='master')

hf模型转为gguf

git clone llama.cpp用于转换,git clone太慢,还是bypy靠谱

安装相关环境pip install -r llama.cpp/requirements/requirements-convert_hf_to_gguf.txt

1
2
3
4
# 如果不量化,保留模型的效果
python llama.cpp/convert_hf_to_gguf.py ./qwen2_0.5b_instruct  --outtype f16 --verbose --outfile qwen2_0.5b_instruct_f16.gguf
# 如果需要量化(加速并有损效果),直接执行下面脚本就可以
python llama.cpp/convert_hf_to_gguf.py ./qwen2_0.5b_instruct  --outtype q8_0 --verbose --outfile qwen2_0.5b_instruct_q8_0.gguf

创建Modelfile

确保ollama正在运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM /mnt/workspace/qwen2-0.5b-instruct-q8_0.gguf

# set the temperature to 0.7 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
TEMPLATE """<|im_start|>system
<|im_end|>
<|im_start|>user
<|im_end|>
<|im_start|>assistant
<|im_end|>"""
# set the system message
SYSTEM """
You are a helpful assistant.
"""

创建与运行自定义模型

1
ollama create qwen2_0.5b_instruct --file ./ModelFile
1
ollama run qwen2_0.5b_instruct

vLLM

微调大模型

项目:RAGAssistant

公司大部分是doc docx,其中包含了图片和表格。

ragflow(搞几天都没法用)

按照文档安装,用华为云下载完整版ragflow:v0.17.2,成功后 ssh -L 8080:localhost:80 username@ip -p 6000,本地http://localhost:8080/

遇到的问题

  1. Question: ragflow-mysql failed to setup : ragflow-mysql is unhealthy #6511

You can change the interval, timeout time and retries times in docker-compose-base.yml healthcheck to 20s, 20s, 10.

  1. 下载慢,在/etc/docker/daemon.json中添加镜像源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    {
      "registry-mirrors": [
        "https://registry.docker-cn.com",
        "https://hub.rat.dev",
        "https://dockerpull.org",
        "https://hub.geekery.cn",
        "http://hub-mirror.c.163.com",
        "http://docker.mirrors.ustc.edu.cn",
        "https://3laho3y3.mirror.aliyuncs.com"
      ],
      "insecure-registries": [
        "docker.mirrors.ustc.edu.cn",
        "registry.docker-cn.com"
      ],
      "iptables": false
    }
    
  2. docker compose -f docker-compose-gpu.yml up -d执行报错Error response from daemon: could not select device driver "nvidia" with capabilities: [[gpu]]

    1. 前提:nvidia-smi 可以正常执行, docker 正常安装且容器可以正常运行

    2. 检查nvidia-container-toolkit 是否已正确安装dpkg -l | grep nvidia-container,看到nvidia-container-runtime,nvidia-container-toolkit表示已安装,如果没有,则进行安装

      1
      2
      3
      4
      5
      6
      7
      8
      
      # 1. 设置 NVIDIA 的 GPG Key 和 apt 源
      distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
        && curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | sudo apt-key add - \
        && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | sudo tee /etc/apt/sources.list.d/nvidia-container.list
            
      # 2. 安装 NVIDIA container runtime
      sudo apt-get update
      sudo apt-get install -y nvidia-container-toolkit
      
    3. 确认 /etc/docker/daemon.json 文件中包含以下内容(或新增)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      
      {
        "default-runtime": "nvidia",
        "runtimes": {
          "nvidia": {
            "path": "nvidia-container-runtime",
            "runtimeArgs": []
          }
        }
      }
      
    4. 重启 Docker 服务sudo systemctl restart docker

    5. 测试Docker是否支持GPUdocker run --rm --gpus all nvidia/cuda:12.0.1-base-ubuntu22.04 nvidia-smi,输出类似nvida-smi的框则成功

ragflow-deepdoc

后续

探索下 vllm

探索量化

探索构建数据集

探索微调

参考链接

ollama+open-webui,本地部署自己的大模型

将 HuggingFace 模型转换为 GGUF 及使用 ollama 运行——以Qwen为例

本文由作者按照 CC BY 4.0 进行授权