AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


Dify v0.6.9源码部署
发布日期:2024-07-08 08:58:54 浏览次数: 2301


一.前置条件

克隆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

解释命令如下所示:

参数解释
celeryCelery 命令行工具。
-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_versionAlembic 版本控制表
account_integrates账号整合信息表
api_requestsAPI 请求记录表
app_dataset_joins应用数据集关联表
celery_taskmetaCelery 任务元数据表
celery_tasksetmetaCelery 任务集合元数据表
dataset_process_rules数据集处理规则表
dataset_queries数据集查询记录表
dify_setupsDify 设置表
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_tokensAPI 令牌表
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



53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询