dify 本地源码启动
1. 环境准备
1.1. 基础环境
我使用的是 Ubuntu 20.04 环境,首先需要安装 docker 27.3.1和 docker compose
我的 docker compose 安装的 v2 版本。
1.2. clone dify 代码
git clone https://github.com/langgenius/dify.git
1.3. 部署依赖服务
在启用业务服务之前,我们需要先部署 PostgreSQL / Redis / Weaviate(如果本地没有的话),可以通过以下命令启动:
cd docker
cp middleware.env.example middleware.env
docker compose -f docker-compose.middleware.yaml up -d
# 关闭时,使用stop
docker compose stop
2. 服务端部署
2.1. 创建 conda 环境
官方推荐:服务器启动需要 Python 3.11 或 3.12。建议使用 pyenv 快速安装 Python 环境。(安装失败)
使用 conda 解决。
conda create --name dify311 python=3.11
conda activate dify311
2.2. 部署后端服务
2.2.1. 修改环境配置
# 后端服务在api文件夹中
cd api
# 拷贝环境配置
cp .env.example .env
修改 SECRET_KEY
# 生成随机密钥,并替换 .env 中 SECRET_KEY 的值。
openssl rand -base64 42
sed -i 's/SECRET_KEY=.*/SECRET_KEY=<your_value>/' .env
也可以使用 vim 直接修改.env 文件中 SECRET_KEY 的值。
2.2.2. 安装依赖包
官方推荐:Dify API 服务使用 Poetry 来管理依赖。你可以执行 poetry shell 来激活环境。(安装失败)
使用 pip 解决。
# 首先进入 api 目录
cd api
# 执行 poetry show 查看安装依赖的包
poetry show
# 将依赖包重定向输出到 requirements.txt 文件中。并修改 requirements.txt 包的格式。
poetry show >> requirements.txt
# 安装环境(时间漫长,使用清华源)
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
requirements.txt 文件有417行,也就是417个依赖包。
requirements.txt 实例如下:
aiofiles==24.1.0
aiohappyeyeballs==2.4.4
aiohttp==3.11.11
aiohttp-retry==2.9.1
aiomysql==0.2.0
aiosignal==1.3.2
alembic==1.14.0
alibabacloud-credentials==0.3.6
alibabacloud-endpoint-util==0.0.3
alibabacloud-gateway-spi==0.0.2
alibabacloud-gpdb20160503==3.8.3
alibabacloud-openapi-util==0.2.2
alibabacloud-openplatform20191219==2.0.0
alibabacloud-oss-sdk==0.1.0
alibabacloud-oss-util==0.0.6
alibabacloud-tea==0.4.0
alibabacloud-tea-fileform==0.0.5
alibabacloud-tea-openapi==0.3.12
alibabacloud-tea-util==0.3.13
alibabacloud-tea-xml==0.0.2
aliyun-python-sdk-core==2.16.0
aliyun-python-sdk-kms==2.16.5
amqp==5.3.1
aniso8601==9.0.1
annotated-types==0.7.0
anthropic==0.23.1
anyio==4.8.0
arxiv==2.1.0
asgiref==3.8.1
async-timeout==5.0.1
attrs==23.2.0
authlib==1.3.1
azure-ai-inference==1.0.0b6
azure-ai-ml==1.20.0
azure-common==1.1.28
azure-core==1.32.0
azure-identity==1.16.1
azure-mgmt-core==1.5.0
azure-storage-blob==12.13.0
azure-storage-file-datalake==12.8.0
azure-storage-file-share==12.20.0
backoff==2.2.1
bce-python-sdk==0.9.25
bcrypt==4.2.1
beautifulsoup4==4.12.2
billiard==4.2.1
blinker==1.9.0
boto3==1.35.74
botocore==1.35.94
build==1.2.2.post1
bottleneck==1.4.2
brotli==1.1.0
bs4==0.0.2
cachetools==5.3.3
celery==5.4.0
certifi==2024.12.14
cffi==1.17.1
chardet==5.1.0
charset-normalizer==3.4.1
chroma-hnswlib==0.7.6
chromadb==0.5.20
circuitbreaker==2.0.0
click==8.1.8
click-didyoumean==0.3.1
click-plugins==1.1.1
click-repl==0.3.0
clickhouse-connect==0.7.19
cloudpickle==2.2.1
cloudscraper==1.2.71
cohere==5.2.6
colorama==0.4.6
coloredlogs==15.0.1
contourpy==1.3.1
cos-python-sdk-v5==1.9.30
couchbase==4.3.4
crcmod==1.7
cryptography==44.0.0
cssselect==1.2.0
cycler==0.12.1
dashscope==1.17.1
dataclass-wizard==0.34.0
dataclasses-json==0.6.7
db-dtypes==1.3.1
decorator==5.1.1
defusedxml==0.7.1
deprecated==1.2.15
deprecation==2.1.0
dill==0.3.9
distro==1.9.0
docker==7.1.0
docstring-parser==0.16
duckdb==1.1.3
duckduckgo-search==6.3.7
durationpy==0.9
elastic-transport==8.17.0
elasticsearch==8.14.0
emoji==2.14.0
esdk-obs-python==3.24.6.1
et-xmlfile==2.0.0
eval-type-backport==0.2.2
fal-client==0.5.6
fastapi==0.115.6
fastavro==1.10.0
feedfinder2==0.0.4
feedparser==6.0.10
filelock==3.16.1
filetype==1.2.0
fire==0.7.0
flasgger==0.9.7.1
flask==3.1.0
flask-compress==1.17
flask-cors==4.0.2
flask-login==0.6.3
flask-migrate==4.0.7
flask-restful==0.3.10
flask-sock==0.7.0
flask-sqlalchemy==3.1.1
flatbuffers==24.12.23
fontmeta==1.6.1
fonttools==4.55.3
frozendict==2.4.6
frozenlist==1.5.0
fsspec==2024.12.0
future==1.0.0
gevent==24.11.1
gmpy2==2.2.1
google==3.0.0
google-ai-generativelanguage==0.6.9
google-api-core==2.18.0
google-api-python-client==2.90.0
google-auth==2.29.0
google-auth-httplib2==0.2.0
google-cloud-aiplatform==1.49.0
google-cloud-bigquery==3.27.0
google-cloud-core==2.4.1
google-cloud-resource-manager==1.14.0
google-cloud-storage==2.16.0
google-crc32c==1.6.0
google-generativeai==0.8.1
google-pasta==0.2.0
google-resumable-media==2.7.2
googleapis-common-protos==1.63.0
gotrue==2.11.1
greenlet==3.1.1
grpc-google-iam-v1==0.14.0
grpcio==1.67.1
grpcio-status==1.62.3
grpcio-tools==1.62.3
gunicorn==23.0.0
h11==0.14.0
h2==4.1.0
hiredis==3.1.0
hpack==4.0.0
html5lib==1.1
httpcore==1.0.7
httplib2==0.22.0
httptools==0.6.4
httpx==0.27.2
httpx-sse==0.4.0
huggingface-hub==0.16.4
humanfriendly==10.0
hyperframe==6.0.1
idna==3.10
importlib-metadata==6.11.0
importlib-resources==6.5.2
iniconfig==2.0.0
isodate==0.7.2
itsdangerous==2.2.0
jieba==0.42.1
jieba3k==0.35.1
jinja2==3.1.5
jiter==0.8.2
jmespath==0.10.0
joblib==1.4.2
jsonlines==4.0.0
jsonpath-ng==1.6.1
jsonpath-python==1.0.6
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
kaleido==0.2.1
kiwisolver==1.4.8
kombu==5.4.2
kubernetes==31.0.0
langdetect==1.0.9
langfuse==2.51.5
langsmith==0.1.147
levenshtein==0.26.1
litellm==1.51.3
llvmlite==0.43.0
loguru==0.7.3
lxml==5.3.0
lz4==4.3.3
mailchimp-transactional==1.0.56
mako==1.3.8
markdown==3.5.2
markdown-it-py==3.0.0
markupsafe==3.0.2
marshmallow==3.24.1
matplotlib==3.8.4
mdurl==0.1.2
milvus-lite==2.4.11
mistune==3.1.0
mmh3==5.0.1
mock==4.0.3
monotonic==1.6
mplfonts==0.0.10
mpmath==1.3.0
msal==1.31.1
msal-extensions==1.2.0
msrest==0.7.1
multidict==6.1.0
multiprocess==0.70.17
multitasking==0.0.11
mypy-extensions==1.0.0
ndjson==0.3.1
nest-asyncio==1.6.0
newspaper3k==0.2.8
nltk==3.9.1
nomic==3.1.3
novita-client==0.5.7
numba==0.60.0
numexpr==2.9.0
numpy==1.26.4
oauthlib==3.2.2
oci==2.135.2
odfpy==1.4.1
olefile==0.47
onnxruntime==1.20.1
openai==1.52.2
opencensus==0.11.4
opencensus-context==0.1.3
opencensus-ext-azure==1.1.14
opencensus-ext-logging==0.1.1
opendal==0.45.13
openpyxl==3.1.5
opensearch-py==2.4.0
opentelemetry-api==1.29.0
opentelemetry-exporter-otlp-proto-grpc==1.15.0
opentelemetry-instrumentation==0.50b0
opentelemetry-instrumentation-asgi==0.50b0
opentelemetry-instrumentation-fastapi==0.50b0
opentelemetry-proto==1.15.0
opentelemetry-sdk==1.29.0
opentelemetry-semantic-conventions==0.50b0
opentelemetry-util-http==0.50b0
opik==1.3.4
oracledb==2.2.1
orjson==3.10.13
oss2==2.18.5
overrides==7.7.0
packaging==24.2
pandas==2.2.3
pandas-stubs==2.2.3.241126
pathos==0.3.3
peewee==3.17.8
pgvecto-rs==0.2.2
pgvector==0.2.5
pillow==11.1.0
platformdirs==4.3.6
plotly==5.24.1
pluggy==1.5.0
ply==3.11
portalocker==2.10.1
postgrest==0.17.2
posthog==3.7.5
pox==0.3.5
ppft==1.7.6.9
primp==0.10.0
prompt-toolkit==3.0.48
propcache==0.2.1
proto-plus==1.25.0
protobuf==4.25.5
psutil==6.1.1
psycogreen==1.0.2
psycopg2-binary==2.9.10
py==1.11.0
pyarrow==18.1.0
pyasn1==0.6.1
pyasn1-modules==0.4.1
pycparser==2.22
pycryptodome==3.19.1
pydantic==2.9.2
pydantic-core==2.23.4
pydantic-extra-types==2.9.0
pydantic-settings==2.6.1
pydash==8.0.4
pydub==0.25.1
pygments==2.19.1
pyjwt==2.8.0
pymilvus==2.5.3
pymochow==1.3.1
pymysql==1.1.1
pyobvector==0.1.18
pyopenssl==24.3.0
pypandoc==1.14
pyparsing==3.2.1
pypdf==5.1.0
pypdfium2==4.30.1
pypika==0.48.9
pypng==0.20220715.0
pyproject-hooks==1.2.0
pytest==8.3.4
python-calamine==0.3.1
python-dateutil==2.9.0.post0
python-docx==1.1.2
python-dotenv==1.0.1
python-iso639==2024.10.22
python-magic==0.4.27
python-oxmsg==0.0.1
python-pptx==1.0.2
pytz==2024.2
pyxlsb==1.0.10
pyyaml==6.0.2
qdrant-client==1.7.3
qrcode==7.4.2
rank-bm25==0.2.2
rapidfuzz==3.11.0
readabilipy==0.2.0
realtime==2.1.0
redis==5.0.8
referencing==0.35.1
regex==2024.11.6
replicate==0.22.0
requests==2.31.0
requests-file==2.1.0
requests-oauthlib==2.0.0
requests-toolbelt==1.0.0
resend==0.7.2
retry==0.9.2
rich==13.9.4
rpds-py==0.22.3
rsa==4.9
s3transfer==0.10.4
safetensors==0.4.5
sagemaker==2.231.0
sagemaker-core==1.0.16
schema==0.7.7
scikit-learn==1.5.2
scipy==1.15.0
sentry-sdk==1.44.1
setuptools==75.7.0
sgmllib3k==1.0.0
shapely==2.0.6
shellingham==1.5.4
simple-websocket==1.1.0
six==1.17.0
smdebug-rulesconfig==1.0.1
sniffio==1.3.1
socksio==1.0.0
soupsieve==2.6
sqlalchemy==2.0.35
sqlparse==0.5.3
starlette==0.41.0
storage3==0.8.2
strictyaml==1.7.3
supabase==2.8.1
supafunc==0.6.2
sympy==1.13.3
tabulate==0.9.0
tblib==3.0.0
tcvectordb==1.3.2
tenacity==9.0.0
tencentcloud-sdk-python-common==3.0.1298
tencentcloud-sdk-python-hunyuan==3.0.1298
termcolor==2.5.0
threadpoolctl==3.5.0
tidb-vector==0.0.9
tiktoken==0.8.0
tinysegmenter==0.3
tldextract==5.1.3
tokenizers==0.15.2
toml==0.10.2
tos==2.7.2
tqdm==4.67.1
transformers==4.35.2
twilio==9.0.5
typer==0.15.1
types-pytz==2024.2.0.20241221
types-requests==2.32.0.20241016
typing-extensions==4.12.2
typing-inspect==0.9.0
tzdata==2024.2
ujson==5.10.0
unstructured==0.16.12
unstructured-client==0.28.1
upstash-vector==0.6.0
uritemplate==4.1.1
uuid6==2024.7.10
uvicorn==0.34.0
uvloop==0.21.0
validators==0.21.0
vanna==0.7.5
vine==5.1.0
volcengine-compat==1.0.156
volcengine-python-sdk==1.0.103
watchfiles==1.0.3
wcwidth==0.2.13
weaviate-client==3.21.0
webencodings==0.5.1
websocket-client==1.7.0
websockets==13.1
werkzeug==3.1.3
wikipedia==1.4.0
wrapt==1.17.0
wsproto==1.2.0
xinference-client==0.15.2
xlrd==2.0.1
xlsxwriter==3.2.0
xmltodict==0.14.2
yarl==1.18.3
yfinance==0.2.51
youtube-transcript-api==0.6.3
zhipuai==2.1.5.20250106
zipp==3.21.0
zope-event==5.0
zope-interface==7.2
zstandard==0.23.0
执行数据库迁移
# 数据库迁移
flask db upgrade
2.2.3. 启动 API 服务
flask run --host 0.0.0.0 --port=5001 --debug
flask run --host 192.168.17.101 --port=5001 --debug
2.2.4. 启动 Worker 服务
用于消费异步队列任务,如知识库文件导入、更新知识库文档等异步操作。 Linux / MacOS 启动:
celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail --loglevel INFO
3. 前端页面部署
3.1. 安装环境
这里注意一定要根据指南安装 Node.js v18.x (LTS) ,不然后续install会报错。
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt-get install nodejs -y
安装依赖包
# 前端代码在 web 文件夹
cd web
npm install
# 如果安装很慢 切换源
npm config set registry https://registry.npm.taobao.org
错误参考 此处安装,没有使用改解决方案。
配置环境变量
在web目录下创建文件 .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://127.0.0.1:5001/console/api
NEXT_PUBLIC_API_PREFIX=http://192.168.17.101: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://127.0.0.1:5001/api
NEXT_PUBLIC_PUBLIC_API_PREFIX=http://192.168.17.101:5001/api
# Disable Next.js Telemetry (https://nextjs.org/telemetry)
NEXT_TELEMETRY_DISABLED=1
# Disable Upload Image as WebApp icon default is false
NEXT_PUBLIC_UPLOAD_IMAGE_AS_ICON=false
# The timeout for the text generation in millisecond
NEXT_PUBLIC_TEXT_GENERATION_TIMEOUT_MS=60000
# CSP https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
NEXT_PUBLIC_CSP_WHITELIST=
# The maximum number of top-k value for RAG.
NEXT_PUBLIC_TOP_K_MAX_VALUE=10
# The maximum number of tokens for segmentation
NEXT_PUBLIC_INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH=4000
## SENTRY
#NEXT_PUBLIC_SENTRY_DSN=http://192.168.17.101
#NEXT_PUBLIC_SENTRY_ORG=http://192.168.17.101
#NEXT_PUBLIC_SENTRY_PROJECT=http://192.168.17.101
# SENTRY
NEXT_PUBLIC_SENTRY_DSN=
NEXT_PUBLIC_SENTRY_ORG=
NEXT_PUBLIC_SENTRY_PROJECT=
最后构建代码
npm run build
3.2. 启动服务
npm run start
3.3. 访问 dify
最后访问 http://192.168.17.101:3000 即可使用本地部署的 Dify了~
4. 启动总结
# 进入 conda 环境
conda activate dify311
# 依赖服务
cd docker
docker compose -f docker-compose.middleware.yaml up -d
# 后端
cd api
# 启动 API 服务
flask run --host 192.168.17.101 --port=5001 --debug
# 启动 worker 服务
celery -A app.celery worker -P gevent -c 1 -Q dataset,generation,mail --loglevel INFO
# 前端
cd web
# 启动前端
npm run start
# 访问
http://192.168.17.101:3000