MAC 本地搭建Dify环境

Dify 介绍

Dify 是一款开源的大语言模型(LLM) 应用开发平台。它融合了后端即服务(Backend as Service)和 LLMOps 的理念,使开发者可以快速搭建生产级的生成式 AI 应用。即使你是非技术人员,也能参与到 AI 应用的定义和数据运营过程中。

由于 Dify 内置了构建 LLM 应用所需的关键技术栈,包括对数百个模型的支持、直观的 Prompt 编排界面、高质量的 RAG 引擎以及灵活的 Agent 框架,并同时提供了一套易用的界面和 API。这为开发者节省了许多重复造轮子的时间,使其可以专注在创新和业务需求上。

Clone Dify 代码:

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

前端环境搭建

安装nodejs

Web 前端服务启动需要用到 Node.js v18.x (LTS) 、NPM 版本 8.x.x 或 Yarn。选择对应操作系统的 v18.x 以上的安装包下载并安装,建议 stable 版本,已自带 NPM。

详见Mac 安装Nodejs

启动步骤

(1)进入 web 目录 cd web

(2)安装依赖包 npm install

(3)配置环境变量。在当前目录下/web下创建文件 .env.local,并复制.env.example中的内容

(4)构建代码 npm run build

(5)启动 web 服务 npm run start

(6)访问:访问 http://127.0.0.1:3000

后端环境搭建

环境准备

Operating System Software Description
macOS 10.14 or later Docker Desktop Set the Docker Virtual Machine (VM) to use at least 2 virtual CPUs (vCPU) and 8 GB of initial memory. Otherwise, the installation may fail. For more information, see Install Docker Desktop on Mac.
Linux platforms Docker 19.03 or later Docker Compose 1.25.1 or later 请参阅安装 Docker安装 Docker Compose 以获取更多信息。
Windows with WSL 2 enabled Docker Desktop

我的操作系统是mac,直接进行Docker Desktop安装即可。

安装Docker

Docker Desktop 下载安装即可。

安装Docker之后进行版本验证

bash 复制代码
(base) % docker-compose -version

docker-compose version 1.29.2, build 5becea4c

(base) % docker --version

Docker version 20.10.7, build f0df350

Anaconda 安装

详见MAC 安装miniconda

# 创建名为 dify 的 Python 3.10 环境
conda create --name dify python=3.10
# 切换至 dify Python 环境
conda activate dify

Python 3.10.x 安装

有些mac出厂自带python,但是版本可能不是最新的,大家可以试试自己的Mac,测试步骤是:(1)打开terminal终端(2)输入命令:python或者python3,如果出现python的版本号则代表已经安装,但还是推荐大家进行更新,无需卸载旧版本,直接安装新版本即可

Download Python | Python.org

Git 安装

bash 复制代码
brew install git

验证 git version,显示版本号就说明安装成功了。

bash 复制代码
git version
git version 2.39.3 (Apple Git-146)

PostgreSQL 安装

1.安装命令:brew install postgresql

bash 复制代码
brew install postgresql
  1. 检查是否安装成功 brew list
bash 复制代码
% brew list
==> Formulae
abseil			krb5			pcre2
aws-elasticbeanstalk	libiconv		postgresql@14
brotli			libmagic		protobuf
c-ares			libnghttp2		protobuf-c
ca-certificates		libuv			python@3.10
certifi			libyaml			python@3.11
clamav			lz4			readline
gdbm			mc			redis
gradle			mpdecimal		sqlite
icu4c			node			xz
jansson			openjdk			yara
json-c			openssl@1.1
jsoncpp			openssl@3

==> Casks
jd-gui		table-tool
  1. 启动 brew services start postgresql
bash 复制代码
% brew services start postgresql
Warning: Formula postgresql was renamed to postgresql@14.
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
  1. 检查是否启动成功 brew services list
bash 复制代码
% brew services list
Name          Status  User File
clamav        none         
postgresql@14 started rsun ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist
redis         started rsun ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
  1. 初始化数据库

    initdb /usr/local/var/postgres

  2. 创建user

    createuser -s postgres

Redis 安装

1.安装 brew install redis

  1. 启动Redis brew services start redis
bash 复制代码
% brew services start redis
Service `redis` already started, use `brew services restart redis` to restart.

启动DIfy后端

1.进入 api 目录cd api

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

cp .env.example .env

3. 生成随机密钥,并替换 .env 中 SECRET_KEY 的值

openssl rand -base64 42

vim .env 打开 .env文件,修改SECRET_KEY=新生成的key

  1. 安装依赖包 pip3 install -r requirements.txt

5. 执行数据库迁移将数据库结构迁移至最新版本

flask db upgrade

 flask db upgrade  
 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
Error: While importing 'app', an ImportError was raised:

Traceback (most recent call last):
  File "/Users/rsun/miniconda3/lib/python3.12/site-packages/flask/cli.py", line 245, in locate_app
    __import__(module_name)
  File "/Users/rsun/software/dify-main/api/app.py", line 6, in <module>
    from gevent import monkey
ModuleNotFoundError: No module named 'gevent'

解决办法:

pip install -U --force-reinstall --no-binary :all: gevent

6. 启动 API 服务

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

7. 启动 worker 服务

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

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

总结

以上为本地源码部署启动的Dify方式。

下次进入Dify方法

启动:

  1. conda activate dify

  2. Redis: brew services restart redis

  3. Postgresql: brew services start postgresql

4.后端程序:

cd api目录

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

4.前端程序:

cd web目录

启动 web 服务 npm run start

相关推荐
五阿哥爱跳舞5 小时前
MAC备忘录空白解决方案
macos
乌恩大侠7 小时前
【Xcode Command Line Tools】安装指南
macos·fpga开发·c
GEEKVIP9 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
GEEKVIP9 小时前
如何在 Windows 10 上恢复未保存/删除的 Word 文档
macos·ios·智能手机·电脑·word·笔记本电脑·iphone
奇客软件10 小时前
iPhone使用技巧:如何恢复变砖的 iPhone 或 iPad
数码相机·macos·ios·电脑·笔记本电脑·iphone·ipad
前端张三11 小时前
Mac 电脑pink 后端ip地址进行本地联调
服务器·tcp/ip·macos
缘友一世17 小时前
macos安装mongodb
数据库·mongodb·macos
笑非不退1 天前
macOS开发环境配置与应用开发
macos
colorknight1 天前
1.2.3 HuggingFists安装说明-MacOS安装
人工智能·低代码·macos·huggingface·数据科学·ai agent
GEEKVIP2 天前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone