Dify v0.6.9源码部署

一.前置条件

克隆Dify v0.6.9代码:

git clone https://github.com/langgenius/dify.git

在启用业务服务之前,需要先部署 PostgresSQL / Redis / Weaviate(如果本地没有的话),可以通过以下命令启动:

cd docker
docker compose -f docker-compose.middleware.yaml up -d

增加数据卷db_data_postgres:

version: '3'
services:
  # The postgres database.
  db:
    image: postgres:15-alpine
    restart: always
    environment:
      # The password for the default postgres user.
      POSTGRES_PASSWORD: difyai123456
      # The name of the default postgres database.
      POSTGRES_DB: dify
      # postgres data directory
      PGDATA: /var/lib/postgresql/data/pgdata
    volumes:
      - db_data_postgres:/var/lib/postgresql/data
    ports:
      - "5432:5432"
volumes:
  db_data_postgres: 

PyCharm界面如下所示:

Docker Desktop界面如下所示:

Portainer界面如下所示:

二.服务端部署

1.进入 api 目录

cd api

2.复制环境变量配置文件

cp .env.example .env

3.生成随机密钥,并替换 .envSECRET_KEY 的值

openssl rand -base64 42
sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env

4.安装依赖包

pip install -r requirements.txt

5.执行数据库迁移

将数据库结构迁移至最新版本。

flask db upgrade

6.启动 API 服务

flask run --host 0.0.0.0 --port=5001 --debug

正确输出:

(dify-0.6.9) root@MM-202203161213:/mnt/l/20230620_LLM_Model/20230923_LLMOps/dify-0.6.9/api# flask run --host 0.0.0.0 --port=5001 --debug
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
/root/.virtualenvs/dify-0.6.9/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
 * 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
 * Running on http://172.29.255.17:5001
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug: * Restarting with stat
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
/root/.virtualenvs/dify-0.6.9/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
WARNING:werkzeug: * Debugger is active!
INFO:werkzeug: * Debugger PIN: 130-911-538

7.启动 Worker 服务

用于消费异步队列任务,如数据集文件导入、更新数据集文档等异步操作。 Linux / MacOS 启动:

celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail --loglevel INFO

解释命令如下所示:

参数 解释
celery Celery 命令行工具。
-A app.celery 指定 Celery 应用实例的位置,这里是 app.celery,表示在 app 模块中的 celery 实例。
worker 启动一个 Celery worker 进程。
-P gevent 指定并发池为 gevent,这是一个基于协程的并发库,用于处理并发任务。
-c 1 指定并发工作进程的数量为 1。
-Q dataset,generation,mail 指定这个 worker 处理的任务队列为 dataset、generation 和 mail。
–loglevel INFO 将日志级别设置为 INFO,以获取详细的运行信息。

如果使用 Windows 系统启动,请替换为该命令:

celery -A app.celery worker -P solo --without-gossip --without-mingle -Q dataset,generation,mail --loglevel INFO

正确输出:

三.前端页面部署

Web 前端服务启动的基础条件推荐为需要用到 Node.js v18.x (LTS)NPM 版本 8.x.x Yarn

1.进入 web 目录

cd web

2.安装依赖包

npm install

3.配置环境变量

在当前目录下创建文件 .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=

4.构建代码

npm run build

5.启动 Web 服务

npm run dev

正常启动后,终端会输出如下信息:

PS L:\20230620_LLM_Model\20230923_LLMOps\dify-0.6.9\web> npm run dev  

> dify-web@0.6.9 dev
> next dev          

   ▲ Next.js 14.1.0
   - Local:        http://localhost:3000
   - Environments: .env.local           

   automatically enabled Fast Refresh for 1 custom loader
 ✓ Ready in 17.1s

访问 http://127.0.0.1:3000 即可使用本地部署的 Dify。设置管理员密码:

根据邮箱和密码登录Dify平台:

然后可看到探索、工作室、知识库、工具界面:

四.Dify数据表

1.Dify技术栈

Dify平台用到技术栈主要是Celery、Docker、Flask、Nginx、Postgresql、Python、React Flow、React、Redis、Weaviate等。分析源码系统比较核心的还是搞懂数据表结构和业务操作流程,相对来说前后端数据库框架还是比较固定和成熟的。

2.Dify表结构

SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';

已经生成包含两列(表名字和解释)的详细表格,如下所示:

表名字 解释
alembic_version Alembic 版本控制表
account_integrates 账号整合信息表
api_requests API 请求记录表
app_dataset_joins 应用数据集关联表
celery_taskmeta Celery 任务元数据表
celery_tasksetmeta Celery 任务集合元数据表
dataset_process_rules 数据集处理规则表
dataset_queries 数据集查询记录表
dify_setups Dify 设置表
end_users 终端用户表
installed_apps 已安装应用表
invitation_codes 邀请码表
message_chains 消息链表
message_feedbacks 消息反馈表
operation_logs 操作日志表
pinned_conversations 置顶会话表
saved_messages 保存的消息表
accounts 账户信息表
data_source_bindings 数据源绑定表
tool_providers 工具提供商表
document_segments 文档分段表
provider_models 提供商模型表
tenant_default_models 租户默认模型表
tenant_preferred_model_providers 租户首选模型提供商表
providers 提供商表
provider_orders 提供商订单表
documents 文档表
dataset_retriever_resources 数据集检索资源表
api_tokens API 令牌表
api_based_extensions 基于 API 的扩展表
upload_files 上传文件表
datasets 数据集表
dataset_collection_bindings 数据集集合绑定表
message_annotations 消息注释表
app_annotation_hit_histories 应用注释命中历史表
app_annotation_settings 应用注释设置表
tenants 租户表
tool_builtin_providers 工具内置提供商表
tool_published_apps 工具发布应用表
tool_model_invokes 工具模型调用表
tool_conversation_variables 工具对话变量表
tool_files 工具文件表
message_files 消息文件表
app_model_configs 应用模型配置表
tenant_account_joins 租户账户关联表
dataset_keyword_tables 数据集关键字表
embeddings 嵌入表
workflow_app_logs 工作流应用日志表
workflow_node_executions 工作流节点执行表
workflow_runs 工作流运行表
workflows 工作流表
apps 应用表
conversations 会话表
messages 消息表
message_agent_thoughts 消息代理想法表
tag_bindings 标签绑定表
tags 标签表
recommended_apps 推荐应用表
sites 站点表
tool_api_providers 工具 API 提供商表
tool_label_bindings 工具标签绑定表
tool_workflow_providers 工具工作流提供商表

3.Postgres数据库

(1)information_schema

information_schema 是一个符合 SQL 标准的模式,它包含了视图,这些视图提供关于数据库元数据的信息,如表、列、视图和约束等。它提供了数据库的自描述能力,使得应用程序可以查询这些视图来发现数据库结构和约束。

(2)pg_catalog

pg_catalog 是 PostgreSQL 的系统模式,包含了系统表和视图,这些系统表和视图存储了关于数据库系统对象和元数据的信息。这些对象包括表、列、索引、函数和数据类型等。pg_catalog 是 PostgreSQL 特有的,提供了对数据库对象的低级访问。

(3)public

public 是 PostgreSQL 中的默认模式,所有新创建的数据库对象如果没有指定模式名,就会被放置在public模式中。用户通常在public模式中创建他们的表、视图、序列和函数等。

最后就可以愉快的打断点调试源代码了。

参考文献

[1] 本地源码启动:https://docs.dify.ai/v/zh-hans/getting-started/install-self-hosted/local-source-code

[2] 本地部署相关:https://docs.dify.ai/v/zh-hans/learn-more/faq/install-faq

[3] 文本转语音遇到这个错误怎么办:https://docs.dify.ai/v/zh-hans/learn-more/faq/install-faq#id-15.-wen-ben-zhuan-yu-yin-yu-dao-zhe-ge-cuo-wu-zen-mo-ban

相关推荐

  1. xv6剖析 009

    2024-07-12 07:28:01       36 阅读
  2. 利用playbook部署lamp

    2024-07-12 07:28:01       52 阅读
  3. mediasoup(一)编译及部署

    2024-07-12 07:28:01       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 07:28:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 07:28:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 07:28:01       58 阅读
  4. Python语言-面向对象

    2024-07-12 07:28:01       69 阅读

热门阅读

  1. RabbitMQ保证消息被成功发送和消费

    2024-07-12 07:28:01       23 阅读
  2. Python实现一对多WebSocket发送给指定多个客户端

    2024-07-12 07:28:01       26 阅读
  3. React 18 + Babel 7 + Webpack 5 开发环境搭建

    2024-07-12 07:28:01       27 阅读
  4. flutter常用库的介绍(1)

    2024-07-12 07:28:01       31 阅读
  5. 用Python和TensorFlow实现图像分类:从零开始

    2024-07-12 07:28:01       28 阅读
  6. 鸿蒙开发工程师面试题-架构篇

    2024-07-12 07:28:01       29 阅读
  7. 递推(C语言)

    2024-07-12 07:28:01       18 阅读
  8. C语言 输出图案

    2024-07-12 07:28:01       26 阅读
  9. Python Linux下编译

    2024-07-12 07:28:01       27 阅读
  10. kafka面试题(基础-进阶-高阶)

    2024-07-12 07:28:01       26 阅读
  11. 跨平台开发新纪元:Xcode的多平台应用构建指南

    2024-07-12 07:28:01       33 阅读
  12. 偶现bug解决策略

    2024-07-12 07:28:01       23 阅读
  13. Xcode打包与发布全攻略:将你的应用带上App Store

    2024-07-12 07:28:01       34 阅读