一、Dify文档
参考官方文档来操作,基本没太大的问题。一些细节,我在本篇文章中补充了出来。
这篇文章主要讲以源码的方式启动后端服务,前端服务使用容器启动。
dify 文档地址
Dify 本地源码部署文档(有本地源码部署,我们才能在源码上继续做修改)
二、写在前边
先知道要花费多少资源
这里docker 容器,占用了8个G的内存!

三、本地源码跑dify (后端)
创建名为 dify 的 Python 3.10 环境
conda create --name dify python=3.10
切换至 dify Python 环境
conda activate dify
3.1 拉取代码
可以进入到自己的代码常用目录,再拉取代码
git clone https://github.com/langgenius/dify.git

进入到dify目录可以看到
请记住web 和 api的路径,后边会用到

3.2 安装dockerdesk
Mac 使用本地源码的方式运行,也需要先安装docker。安装docker 是因为dify 需要以docker容器的方式来运行 PostgresSQL / Redis / Weaviate
接着上一步从git上拉取的代码,先进入到dify目录,再进入到docker目录,然后使用docker compose来启动这些依赖的容器。
3.3 以docker的方式启动dify所需的数据库等服务
cd docker

docker compose -f docker-compose.middleware.yaml up -d
可以看到在拉取

拉取成功

看到有这些容器在运行

3.4 本地源码启动
分为前端和后端,并不一定都需要。例如只想调试后端,可以把前端以docker的方式来运行。不要安装前端那些环境。
启动步骤
3.4.1 进入 api 目录
**cd api
3.4.2 复制环境变量配置文件**
cp .env.example .env
3.4.3 生成随机密钥,并替换 .env 中 SECRET_KEY 的值
openssl rand -base64 42
得到一个密钥
复制密钥,vim .env文件
修改SECRET_KEY=生成等密钥
3.4.4 安装依赖
这里下载依赖的时候,使用阿里的源会更快一些。速度从几十kb直接到数10M
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

3.4.5 执行数据库迁移将数据库结构迁移至最新版本
flask db upgrade

3.4.5 启动服务
flask run --host 0.0.0.0 --port=5001 --debug
启动有一个警告错误,先不用处理,不影响。

3.4.6 启动 Worker 服务
用于消费异步队列任务,如数据集文件导入、更新数据集文档等异步操作。 Linux / MacOS 启动:
复制
celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail --loglevel INFO
如果使用 Windows 系统启动,请替换为该命令:
复制
celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail --loglevel INFO
正确输出:
复制
-------------- celery@TAKATOST.lan v5.2.7 (dawn-chorus)
--- ***** -----
-- ******* ---- macOS-10.16-x86_64-i386-64bit 2023-07-31 12:58:08
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: app:0x7fb568572a10
- ** ---------- .> transport: redis://:**@localhost:6379/1
- ** ---------- .> results: postgresql://postgres:**@localhost:5432/dify
- *** --- * --- .> concurrency: 1 (gevent)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> dataset exchange=dataset(direct) key=dataset
.> generation exchange=generation(direct) key=generation
.> mail exchange=mail(direct) key=mail
[tasks]
. tasks.add_document_to_index_task.add_document_to_index_task
. tasks.clean_dataset_task.clean_dataset_task
. tasks.clean_document_task.clean_document_task
. tasks.clean_notion_document_task.clean_notion_document_task
. tasks.create_segment_to_index_task.create_segment_to_index_task
. tasks.deal_dataset_vector_index_task.deal_dataset_vector_index_task
. tasks.document_indexing_sync_task.document_indexing_sync_task
. tasks.document_indexing_task.document_indexing_task
. tasks.document_indexing_update_task.document_indexing_update_task
. tasks.enable_segment_to_index_task.enable_segment_to_index_task
. tasks.generate_conversation_summary_task.generate_conversation_summary_task
. tasks.mail_invite_member_task.send_invite_member_mail_task
. tasks.remove_document_from_index_task.remove_document_from_index_task
. tasks.remove_segment_from_index_task.remove_segment_from_index_task
. tasks.update_segment_index_task.update_segment_index_task
. tasks.update_segment_keyword_index_task.update_segment_keyword_index_task
[2023-07-31 12:58:08,831: INFO/MainProcess] Connected to redis://:**@localhost:6379/1
[2023-07-31 12:58:08,840: INFO/MainProcess] mingle: searching for neighbors
[2023-07-31 12:58:09,873: INFO/MainProcess] mingle: all alone
[2023-07-31 12:58:09,886: INFO/MainProcess] pidbox: Connected to redis://:**@localhost:6379/1.
[2023-07-31 12:58:09,890: INFO/MainProcess] celery@TAKATOST.lan ready.
四、用docker的方式启动前端
4.1 文档
作为后端人员,如果不需要前端源码启动,那就可以以docker的方式来启动!
4.2 本地构建镜像
我是用本地代码来构建的镜像,耗时挺长,第一次738s!

这里看一下构建好的镜像
docker images

4.3 docker启动前端
docker run -it -p 3000:3000 -e CONSOLE_URL=http://127.0.0.1:5001 -e APP_URL=http://127.0.0.1:5001 dify-web
容器启动的结果

4.4 这里访问前端项目

五、起停服务汇总
后端
数据库等服务,以docker容器的方式启动
后台服务
flask run --host 0.0.0.0 --port=5001 --debug
启动worker服务
..
前端服务,以docker启动
docker run -it -p 3000:3000 -e CONSOLE_URL=http://127.0.0.1:5001 -e APP_URL=http://127.0.0.1:5001 dify-web