Dify本地源码启动教程
Dify本地源码启动教程.
一,前置条件
操作系统 | 软件 | 说明 |
---|---|---|
macOS 10.14 or later |
Docker Desktop |
将 Docker 虚拟机(VM)设置为使用至少 2 个虚拟 CPU(vCPU)和 8 GB 的初始内存。否则,安装可能会失败。有关更多信息,请参阅在 Mac 上安装 Docker Desktop。 |
Linux platforms |
Docker 19.03 or later Docker Compose 1.25.1 or later |
请参阅安装 Docker 和安装 Docker Compose 以获取更多信息。 |
Windows with WSL 2 enabled |
Docker Desktop |
我们建议将源代码和其他数据绑定到 Linux 容器中时,将其存储在 Linux 文件系统中,而不是 Windows 文件系统中。有关更多信息,请参阅使用 WSL 2 后端在 Windows 上安装 Docker Desktop |
由于是源码部署,还要对Dify进行改造,所以在Windows系统进行部署。本次部署使用win11系统。
Dify官网建议源码在linux系统下启动,所以需要在Windows下安装WSL2,启动linux子系统。本次安装WSL2使用的是Ubuntu 20.04.6系统。
同时需要在Windows系统安装Docker Desktop。
下载后正常安装Docker Desktop即可。然后注册账号进行登录。后续需要在Docker Desktop上拉取镜像。
二、部署过程
1. 拉取源码
在Windows系统,拉取源码即可:
git clone https://github.com/langgenius/dify.git
2. 拉取必要镜像
首先,打开拉取的Dify源码代码,在docker文件夹中,打开docker-compose.middleware.yaml文件,看里面定义的镜像已经版本。
包括:
image: postgres:15-alpine
image: redis:6-alpine
image: semitechnologies/weaviate(此处注意,官网定义的版本在Docker Desktop中不能拉取到,所以把版本去掉了,拉取最新版本镜像)
image: langgenius/dify-sandbox:0.2.1
image: ubuntu/squid:latest
在Docker Desktop中搜索上面镜像,点击pull进行拉取(不要使用Docker Desktop启动镜像),如下图所示:
3. 启动容器
通过WSL2系统进入到下载的Dify源码文件夹中,进入docker 文件夹。
在启用业务服务之前,我们需要先部署 PostgresSQL / Redis / Weaviate(如果本地没有的话),可以通过以下命令启动:
cd docker
cp middleware.env.example middleware.env
docker compose -f docker-compose.middleware.yaml up -d
注:Windows系统安装了Docker Desktop后,WSL2系统也可以使用docker命令。
这里遇到一个坑,安装官网操作,上述命令应该能正常启动docker容器。但是在实际操作中postgres容器启动报错。报错信息是:
initdb: error: could not change permissions of directory "/var/lib/postgresql/data/pgdata"
没有操作/var/lib/postgresql/data/pgdata的权限。通过查看docker-compose.middleware.yaml中的定义,如下图:
在PGDATA和volumes中定义了此路径。
这里的解决方案是绕了一个弯解决此问题,因为我是用源码启动进行Dify源码的学习,所以数据是否要挂载出来并没有太大影响,所以我选择不进行此路径的挂载。同时,PGDATA的系统变量我也不再进行设置,而是使用它的默认值。
这里,我用Docker Desktop来启动postgresql镜像,并没有使用docker compose来启动。
首先,把WSL中使用docker compose启动的postgresql容器stop,然后将其rm删掉。因为这个容器一直报错一直重启,无法正常使用。然后在Windows的Docker Desktop下,启动postgresql镜像,并按照docker-compose.middleware.yaml中的配置来设置启动参数(volumes和PGDATA除外),如下图所示:
这样可以正常启动postgres容器,且在WSL2中也可以正常使用。
4. 启动后台服务
后台服务包括一个api service(API 接口服务),一个Worker Asynchronous Queue Consumption Service(Worker 异步队列消费服务)。需要启动这两个服务。
4.1.安装基础环境
服务器启动需要 Python 3.10.x。建议使用 pyenv 快速安装 Python 环境。
要安装其他 Python 版本,请使用 pyenv install
。
注意:如果pyenv安装不成功,可以直接在官网下载window版本的python3.10版本,直接安装。
pyenv install 3.10
要切换到 "3.10" Python 环境,请使用以下命令:
pyenv global 3.10
准备好python环境后。
4.2.启动步骤
-
进入 api 目录
cd api
-
复制环境变量配置文件
cp .env.example .env
-
生成随机密钥,并替换
.env
中SECRET_KEY
的值openssl rand -base64 42 sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env
-
安装依赖包
Dify API 服务使用 Poetry 来管理依赖。您可以执行
poetry shell
来激活环境。
Poetry 是一个 Python 依赖管理和打包工具,它旨在简化包的安装、依赖管理和项目发布过程。
在大多数情况下,可以使用 Python 的包管理器pip
来安装 Poetry。pip install poetry
这将下载并安装 Poetry 及其依赖。
安装完成后,你可以通过运行以下命令来验证 Poetry 是否已正确安装:poetry --version
安装api服务需要的python依赖
poetry env use 3.10 poetry install
**注意**,如果poetry env use 3.10安装不成功,显示报错。
将3.10直接替换成python的安装地址上。
1.通过“poetry env list”命令查看环境是否为空,下图表示.venv的虚拟环境准备成功。如果为空,执行下面(1.1,1.2)的命令。
1.1.通过"which python"命令查看python的安装地址,例如:“/d/soft/devTool/python/install/python”。
1.2.执行“poetry env use /d/soft/devTool/python/install/python”。
2.执行“poetry env info”命令,检查虚拟环境是否准备成功。如果成功,执行“poetry install”安装需要的python环境依赖。 -
执行数据库迁移
将数据库结构迁移至最新版本,初始化postgres表数据。
poetry shell flask db upgrade
**注意:**执行到这里时,出现了报错,具体报错博主没有进行记录。大概错误也是少python包。根据报错提示,pip install或者poetry install对应的包,再执行此命令,就可以成功。
-
启动 API 服务
flask run --host 0.0.0.0 --port=5001 --debug
正确输出:
* Debug mode: on INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5001 INFO:werkzeug:Press CTRL+C to quit INFO:werkzeug: * Restarting with stat WARNING:werkzeug: * Debugger is active! INFO:werkzeug: * Debugger PIN: 695-801-919
-
启动 Worker 服务
用于消费异步队列任务,如数据集文件导入、更新数据集文档等异步操作。 Linux / MacOS 启动:
celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail,ops_trace --loglevel INFO
如果使用 Windows 系统启动,请替换为该命令:
celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail,ops_trace --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.
三、前端页面部署
Web 前端客户端页面服务
1.安装基础环境
Web 前端服务启动需要用到 Node.js v18.x (LTS) 、NPM 版本 8.x.x 或 Yarn。
-
安装 NodeJS + NPM
进入 https://nodejs.org/en/download,选择对应操作系统的 v18.x 以上的安装包下载并安装,建议 stable 版本,已自带 NPM。
2.启动步骤
-
进入 web 目录
cd web
-
安装依赖包
npm install
-
配置环境变量。在当前目录下创建文件
.env.local
,并复制.env.example
中的内容。根据需求修改这些环境变量的值:# For production release, change this to PRODUCTION NEXT_PUBLIC_DEPLOY_ENV=DEVELOPMENT # The deployment edition, SELF_HOSTED NEXT_PUBLIC_EDITION=SELF_HOSTED # The base URL of console application, refers to the Console base URL of WEB service if console domain is # different from api or web app domain. # example: http://cloud.dify.ai/console/api NEXT_PUBLIC_API_PREFIX=http://localhost:5001/console/api # The URL for Web APP, refers to the Web App base URL of WEB service if web app domain is different from # console or api domain. # example: http://udify.app/api NEXT_PUBLIC_PUBLIC_API_PREFIX=http://localhost:5001/api # SENTRY NEXT_PUBLIC_SENTRY_DSN= NEXT_PUBLIC_SENTRY_ORG= NEXT_PUBLIC_SENTRY_PROJECT=
-
构建代码
npm run build
-
启动 web 服务,建议通过“npm run dev”启动,或者以下的命令启动:
npm run start # or yarn start # or pnpm start
正常启动后,终端会输出如下信息:
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
warn - You have enabled experimental feature (appDir) in next.config.js.
warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.
info - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback
四、访问 Dify
最后,访问 http://127.0.0.1:3000 即可使用本地部署的 Dify。
更多推荐
所有评论(0)