星球的眼睛 发表于 前天 12:25

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
https://i-blog.csdnimg.cn/blog_migrate/dcaf87f84c48ff7f30ac0646e3976c94.png
(3)配置环境变量。在当前目次下/web下创建文件 .env.local,并复制.env.example中的内容
(4)构建代码 npm run build
https://i-blog.csdnimg.cn/blog_migrate/f3f48a0d310994289c53d247ee96b26f.png
https://i-blog.csdnimg.cn/blog_migrate/9ef3e86cba451b0adfa302053902d131.png
(5)启动 web 服务 npm run start
https://i-blog.csdnimg.cn/blog_migrate/64784c6c8cdc4fd6b2763c6feb0b52da.png
(6)访问:访问 http://127.0.0.1:3000
https://i-blog.csdnimg.cn/blog_migrate/9159cf9fdeb456749c279eac01698269.png
后端环境搭建

环境准备

Operating SystemSoftwareDescription 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之后举行版本验证
(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 安装

brew install git 验证 git version,表现版本号就说明安装成功了。
git version
git version 2.39.3 (Apple Git-146)  PostgreSQL 安装

1.安装下令:brew install postgresql
brew install postgresql 2. 查抄是否安装成功 brew list
% 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 3. 启动 brew services start postgresql
% brew services start postgresql
Warning: Formula postgresql was renamed to postgresql@14.
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14) 4. 查抄是否启动成功  brew services list
% brew services list
Name          StatusUser File
clamav      none         
postgresql@14 started rsun ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist
redis         started rsun ~/Library/LaunchAgents/homebrew.mxcl.redis.plist 5. 初始化数据库
initdb /usr/local/var/postgres 6. 创建user
createuser -s postgres Redis 安装

1.安装 brew install redis
2. 启动Redis  brew services start redis
% 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
4. 安装依赖包  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
https://i-blog.csdnimg.cn/blog_migrate/0676c92aaba101d71c6fcd35f6d56985.png


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




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: MAC 本地搭建Dify环境