Label Studio+LLaMA Factory+Dify学习笔记
Label Studio部署准备数据集
采用Anaconda创建名为label_studio的虚拟环境
conda create -n label_studio python=3.9
激活该虚拟环境
conda activate label_studio
安装Label Studio
pip install label-studio
启动Label Studio
label-studio start
LLaMA Factory云端部署
由于本地电脑配置有限并且环境配置一直报错,这里直接使用云实例租云算力进行部署,我用的AutoDL:https://www.autodl.com/
- 启用 AutoDL 学术加速网络
source /etc/network_turbo # 激活平台内置加速通道
- 初始化环境
# 进入数据盘(所有操作在此进行,避免根目录爆盘)
cd /root/autodl-tmp
# 克隆LLaMA-Factory(推荐浅克隆)
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory
- 创建虚拟环境
conda create -n llama-factory python=3.12 -y
# 步骤2: 初始化Conda环境(解决错误核心)
conda init bash # 图片显示您使用bash shell
# 步骤3: 刷新配置使初始化生效
source ~/.bashrc
conda activate llama-factory
cd LLaMA-Factory
pip install -e ".[torch,metrics]" -i https://pypi.tuna.tsinghua.edu.cn/simple
- 下载模型与数据
# 方法1:直接克隆(需网络代理)
git clone https://huggingface.co/Qwen/Qwen-7B-Chat
# 方法2:使用国内镜像(无代理时)
git clone https://hf-mirror.com/Qwen/Qwen-7B-Chat
- 启动服务
conda activate llama-factory
llamafactory-cli webui
- 本地端口转发(Windows PowerShell/CMD)
ssh -CNg -L 7860:127.0.0.1:7860 root@connect.cqa1.seetacloud.com -p 13850
- 参数说明:
ssh -CNg -L 7860:127.0.0.1:7860 root@connect.xxxxxx.autodl.com -p 41871
connect.xxxxxx.autodl.com
→ 替换为你的实例地址41871
→ 替换为你的SSH端口7860
→ 必须和Web服务的端口一致(默认是7860)
- 操作提示:
- 执行命令后会提示输入密码(注意:粘贴无效,需手动输入)
- 输入密码时不会显示字符,输完直接按回车
- 如果成功,终端会"卡住"(无输出),表示转发已建立
- 本地浏览器访问:
http://127.0.0.1:7860
开始微调
Base 模型像一块“未雕刻的大理石”,预训练后掌握通用语言,但未针对任何任务打磨,适合拿来做科研或领域定制。Instruct 模型在此基础上用“指令-答案”数据做了精修,擅长一次性、明确指令的翻译、总结等任务,却不太会聊天。Chat 模型则再用大量对话做抛光,专精多轮闲聊、上下文追踪,语气自然,但遇到死板指令可能“话太多”。没带后缀的,一般就是 Base 的简化写法,功能相同。
如果我们需要进行的是监督指令微调,建议选择带 -Instruct
后缀的模型
分类 | 标识 | 含义 | 示例(模型名称) |
---|---|---|---|
功能与任务类型 | -Base | 基础模型,未经过特定任务微调,提供原始能力(用于二次开发)。 | Qwen3-14B-Base |
-Chat | 对话优化模型,支持交互式聊天、问答。 | DeepSeek-LLM-7B-Chat | |
-Instruct | 指令微调模型,擅长遵循具体任务指令。 | Qwen3-0.6B-Instruct | |
-Distill | 知识蒸馏模型,更小更快。 | DeepSeek-R1-1.5B-Distill | |
-Math | 专注数学推理任务。 | DeepSeek-Math-7B-Instruct | |
-Coder | 优化代码生成与编程任务。 | DeepSeek-Coder-V2-16B | |
多模态 | -VL | 视觉-语言多模态,支持图文输入输出。 | Kimi-VL-A3B-Instruct |
-Video | 视频多模态模型。 | LLaVA-NeXT-Video-7B-Chat | |
-Audio | 支持音频输入输出。 | Qwen2-Audio-7B | |
技术特性与优化 | -Int8/-Int4 | 量化模型,降低显存占用。 | Qwen2-VL-2B-Instruct-GPTQ-Int8 |
-AWQ/-GPTQ | 特定量化技术。 | Qwen2.5-VL-72B-Instruct-AWQ | |
-MoE | 混合专家模型。 | DeepSeek-MoE-16B-Chat | |
-RL | 强化学习优化。 | MiMo-7B-Instruct-RL | |
版本与变体标识 | -v0.1/-v0.2 | 模型版本号。 | Mistral-7B-v0.1 |
-Pure | 纯净版模型。 | Index-1.9B-Base | |
-Character | 角色对话模型。 | Index-1.9B-Character-Chat | |
-Long-Chat | 支持长上下文对话。 | Orion-14B-Long-Chat | |
领域与应用标识 | -RAG | 检索增强生成模型。 | Orion-14B-RAG-Chat |
-Chinese | 中文优化版本。 | Llama-3-70B-Chinese-Chat | |
-MT | 机器翻译专用模型。 | BLOOMZ-7B1-mt |
参数说明
Dify部署
- 克隆 Dify 源代码至本地环境
# 假设当前最新版本为 0.15.3
git clone https://github.com/langgenius/dify.git --branch 0.15.3
这里克隆因为网络问题一直出错,所以这里我直接官方下载源码解压到特定目录,所以下面所有涉及到目录执行命令的都要注意替换为:D:\APP\AIGC\Dify\docker
- 启动 Dify
进入 Dify 源代码的 Docker 目录
cd dify/docker
复制环境配置文件
cp .env.example .env
启动 Docker 容器 根据你系统上的 Docker Compose 版本,选择合适的命令来启动容器:
如果版本是 Docker Compose V2,使用以下命令:
docker compose up -d
运行命令后,你应该会看到类似以下的输出,显示所有容器的状态和端口映射:
[+] Running 11/11
✔ Network docker_ssrf_proxy_network Created 0.1s
✔ Network docker_default Created 0.0s
✔ Container docker-redis-1 Started 2.4s
✔ Container docker-ssrf_proxy-1 Started 2.8s
✔ Container docker-sandbox-1 Started 2.7s
✔ Container docker-web-1 Started 2.7s
✔ Container docker-weaviate-1 Started 2.4s
✔ Container docker-db-1 Started 2.7s
✔ Container docker-api-1 Started 6.5s
✔ Container docker-worker-1 Started 6.4s
✔ Container docker-nginx-1 Started 7.1s
但是由于网络问题我这里一直报错,docker compose up -d还是执行失败,这里采用了两个方法:
1. 修改配置(通过 Docker Desktop)
- 右键点击任务栏 Docker 图标 → Settings (设置)
- 左侧导航栏选择 Docker Engine
- 在右侧编辑框中 合并 你的现有配置和镜像源配置:
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"registry-mirrors": [
"https://registry.cn-hangzhou.aliyuncs.com",
"https://mirror.ccs.tencentyun.com"
]
}
验证是否生效
docker info
在输出中查找以下内容:
Registry Mirrors:
https://registry.cn-hangzhou.aliyuncs.com/
https://mirror.ccs.tencentyun.com/
2、如果还是不行,这里采用分步手动拉取所有镜像
- Dify 核心镜像
docker pull registry.cn-hangzhou.aliyuncs.com/langgenius/dify-api:0.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/langgenius/dify-web:0.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/langgenius/dify-sandbox:0.2.1
- 数据库和中间件
docker pull registry.cn-hangzhou.aliyuncs.com/library/postgres:15-alpine
docker pull registry.cn-hangzhou.aliyuncs.com/library/redis:6-alpine
docker pull registry.cn-hangzhou.aliyuncs.com/semitechnologies/weaviate:1.19.0
- 重命名镜像
docker tag registry.cn-hangzhou.aliyuncs.com/langgenius/dify-api:0.15.3 langgenius/dify-api:0.15.3
docker tag registry.cn-hangzhou.aliyuncs.com/langgenius/dify-web:0.15.3 langgenius/dify-web:0.15.3
最后检查是否所有容器都正常运行:
docker compose ps
在这个输出中,你应该可以看到包括 3 个业务服务 api / worker / web,以及 6 个基础组件 weaviate / db / redis / nginx / ssrf_proxy / sandbox 。
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
docker-api-1 langgenius/dify-api:0.6.13 "/bin/bash /entrypoi…" api About a minute ago Up About a minute 5001/tcp
docker-db-1 postgres:15-alpine "docker-entrypoint.s…" db About a minute ago Up About a minute (healthy) 5432/tcp
docker-nginx-1 nginx:latest "sh -c 'cp /docker-e…" nginx About a minute ago Up About a minute 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 About a minute ago Up About a minute (healthy) 6379/tcp
docker-sandbox-1 langgenius/dify-sandbox:0.2.1 "/main" sandbox About a minute ago Up About a minute
docker-ssrf_proxy-1 ubuntu/squid:latest "sh -c 'cp /docker-e…" ssrf_proxy About a minute ago Up About a minute 3128/tcp
docker-weaviate-1 semitechnologies/weaviate:1.19.0 "/bin/weaviate --hos…" weaviate About a minute ago Up About a minute
docker-web-1 langgenius/dify-web:0.6.13 "/bin/sh ./entrypoin…" web About a minute ago Up About a minute 3000/tcp
docker-worker-1 langgenius/dify-api:0.6.13 "/bin/bash /entrypoi…" worker About a minute ago Up About a minute 5001/tcp
通过这些步骤,你可以在本地成功安装 Dify。
- 更新 Dify
进入 dify 源代码的 docker 目录,按顺序执行以下命令:
cd dify/docker
docker compose down
git pull origin main
docker compose pull
docker compose up -d
- 同步环境变量配置 (重要!)
如果 .env.example 文件有更新,请务必同步修改你本地的 .env 文件。
检查 .env 文件中的所有配置项,确保它们与你的实际运行环境相匹配。你可能需要将 .env.example 中的新变量添加到 .env 文件中,并更新已更改的任何值。
- 访问 Dify
你可以先前往管理员初始化页面设置设置管理员账户:
# 本地环境
http://localhost:3000/install
# 服务器环境
http://your_server_ip:3000/install
Dify 主页面:
# 本地环境
http://localhost:3000
# 服务器环境
http://your_server_ip:3000
- 自定义配置
编辑 .env 文件中的环境变量值。然后重新启动 Dify:
docker compose down
docker compose up -d
完整的环境变量集合可以在 docker/.env.example 中找到。
接下来就是部署Ollama,安装之后在.env文件中最末尾添加
# 启用自定义模型
CUSTOM_MODEL_ENABLED=true
# 指定Olama的API地址(根据部署环境调整IP)
OLLAMA_API_BASE_URL=host.docker.internal:11434
Dify参数列表
参数 | Dify的解释 |
---|---|
温度 | 控制生成结果的多样性和随机性,数值越小,越严谨,数值越大越发散 |
最大标记 | 制定生成结果长度的上限,如果生成结果截断,可以调大该参数 |
TOP P | 控制生成结果的随机性,数值越小,随机性越弱,数值越大随机性越强 |
Logprobs | 是否返回所输出token的对数概率 |
Top Logprobs | 一个介于0-20的整数N,指定每个输出位置返回输出概率topN的token,且返回这些token的对数概率。 |
频率惩罚 | 介于-2到2之间的数字,如果该值为正,那么token会更具其所在文本中的出现频率受到相应的惩罚,降低模型重复相同内容的可能性 |
温度,Top P ,频率惩罚
温度 | 严谨性 | 随机性 | 适用场景 |
---|---|---|---|
0.0 | 高 | 低 | 数学解题/代码生成,严谨的逻辑场景 |
0.5 | 中 | 中 | 常规问答,中规中矩 |
1.0 | 低 | 高 | 创意写作,发散出去 |
空空如也!