? ? ? 最近在研究AI Agent,發現大家都在用Dify,但Dify部署起來總是面臨各種問題,而且我在部署和應用測試過程中也都遇到了,因此記錄如下,供大家參考。Dify總體來說比較靈活,擴展性比較強,適合基于它做智能聊天機器人,文本生成,工作流任務,Agent應用建設等場景,要比其他客戶端工具更加靈活,更加適合二次開發,但Dify部署起來非常麻煩,會自動部署10個容器,任何一個出現問題,應用都無法正常使用。后續我會與Autogen對比,看看在Agent方面哪個更適合垂直行業AI應用的快速落地。
一、docker部署
1.查看docker-compose版本
docker-compose --version
2.復制環境配置文件
進入dify/docker目錄,復制命令如下
cp .env.example .env
3.Docker Engine中的國內鏡像配置
打開docker desktop設置功能,修改docker的Docker Engine中的國內鏡像地址(否則會出現連接不上錯誤),具體如下
{"builder": {"gc": {"defaultKeepStorage": "20GB","enabled": true}},"experimental": false,"registry-mirrors": [# 增加部分"https://docker.m.daocloud.io","https://docker.imgdb.de","https://docker-0.unsee.tech","https://docker.hlmirror.com","https://docker.1ms.run","https://func.ink","https://lispy.org","https://docker.xiaogenban1993.com"]
}
4.拉取并啟動dify容器
拉取數據、依賴包進行安裝配置。
docker compose up -d
5.配置代理
打開docker desktop設置功能,進入到Resources--》proxy下,設置代理服務器地址。
6.常見錯誤問題的解決方式
錯誤1:docker部署的容器啟動過程中出現db-1總是重啟
需要修改docker下的docker-compose.yaml文件db配置,具體如下
services:db:image: postgres:15-alpinevolumes:- postgres-data:/var/lib/postgresql/data #增加這一行#- ./volumes/db/data:/var/lib/postgresql/data # 注釋掉
# 在該文件結尾處增加
volumes:oradata:dify_es01_data:postgres-data:# 增加的內容。
然后執行容器的重新加載,具體如下。
docker compose down -v # 卸載
docker compose up # 重新加載
docker restart #重啟docker
錯誤2:瀏覽器訪問時出現502錯誤
查看服務情況
docker inspect --format '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-api-1
docker inspect --format '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-web-1
或者運行以下命令,查看服務詳細信息,在返回結果找到IPAddress對應的值,即為ip地址。
docker inspect docker-web-1
docker inspect docker-api-1
比如返回結果如下。
web:172.18.0.4
api:172.18.0.9
然后修改nginx配置文件(比如D:\dify11\docker\nginx\conf.d\default.conf)內容如下:
這種方式重啟docker后或重啟nginx后會沖掉,或者ip地址會有變化,永久修改方式詳見下面的方法。
# Please do not directly edit this file. Instead, modify the .env variables related to NGINX configuration.
# 主要這里的ip地址要根據docker中顯示的ip地址進行修改,查詢方式詳見上面的docker inspect
server {listen 80;server_name _;location /console/api {proxy_pass http://172.18.0.9:5001;include proxy.conf;}location /api {proxy_pass http://172.18.0.9:5001;include proxy.conf;}location /v1 {proxy_pass http://172.18.0.9:5001;include proxy.conf;}location /files {proxy_pass http://172.18.0.9:5001;include proxy.conf;}location /explore {proxy_pass http://172.18.0.4:3000;include proxy.conf;}location /e/ {proxy_pass http://plugin_daemon:5002;proxy_set_header Dify-Hook-Url ://;include proxy.conf;}location / {proxy_pass http://172.18.0.4:3000;include proxy.conf;}
}
永久修改default.conf配置文件方式如下,但這種方式容易導致nginx配置出錯。
在docker目錄,新建python程序,內容如下。但目前該程序容易導致安裝nginx無法啟動,解決辦法就是將重啟容器那一段不執行(需要手動重啟nginx對應的容器)
#!/root/anaconda3/envs/llm/bin/python
# -*- coding: utf-8 -*-
'''
@file: dify-nginx.py
@date: 2025/3/11 15:36
@remark: 更新nginx配置信息,參考其他網友代碼。
@input:dify-nginx.py
@output:dify-nginx.py
'''
import os
import dockerclient = docker.from_env()
# 獲取當前 Python 腳本所在的文件夾的絕對路徑
current_dir = os.path.dirname(os.path.abspath(__file__))# 讀取文件
def file_get_contents(file_path):with open(file_path, "r", encoding="utf-8") as file:return file.read()# 寫入文件
def file_put_contents(file_path, content):with open(file_path, "w", encoding="utf-8") as file:file.write(content)# 獲取容器 IP
def get_container_ip(container_name):container = client.containers.get(container_name)return container.attrs['NetworkSettings']['Networks']['docker_default']['IPAddress']# 重啟容器
def restart_container(container_name):# 原始內容container = client.containers.get(container_name)container.restart()def main():# 獲取 API 和 Web 容器的 IP 地址api_container_ip = get_container_ip('docker-api-1')web_container_ip = get_container_ip('docker-web-1')print(f'api_container_ip: {api_container_ip}')print(f'web_container_ip: {web_container_ip}')# 拼接模板文件路徑和目標文件路徑template_path = os.path.join(current_dir, 'nginx/conf.d/default.conf.template')target_path = os.path.join(current_dir, 'nginx/conf.d/default.conf')# 讀取模板文件內容并替換 IP 地址tpl_contents = file_get_contents(template_path)tpl_contents = tpl_contents.replace('http://api', f'http://{api_container_ip}')tpl_contents = tpl_contents.replace('http://web', f'http://{web_container_ip}')# 將修改后的內容寫入目標配置文件file_put_contents(target_path, tpl_contents)# # 重啟 Nginx 容器,記得注釋掉# restart_container('docker-nginx-1')# print("Nginx container restarted with updated configuration.")
if __name__ == '__main__':main()
運行命令:python dify-nginx.py
錯誤3:plugin_daemon安裝依賴庫出錯
解決辦法如下。主要修改docker目錄下的docker-compose.yaml文件內容(比如E:\dify2\docker\docker-compose.yaml)。增加國內鏡像地址,記得注釋掉原來的同名設置,如何需要代理,記得在最前面加上HTTP_PROXY,具體如下。
plugin_daemon:image: langgenius/dify-plugin-daemon:0.0.3-localrestart: alwaysenvironment:# Use the shared environment variables.<<: *shared-api-worker-env# HTTP_PROXY=http://ip:8080# HTTPS_PROXY=http://ip:8080# ... 中間省略# 注釋掉原來的,新增如下內容# 設置 pip 使用國內鏡像源PIP_MIRROR_URL: https://pypi.tuna.tsinghua.edu.cn/simple# 設置超時時間PYTHON_ENV_INIT_TIMEOUT: ${PYTHON_ENV_INIT_TIMEOUT:-640}
錯誤4:vobe文件找不到
這種錯誤一般需要在api對應的容器中,配置HTTP_PROXY代理,配置方式和錯誤3類似,具體不細說,但有時候不管用,所以需要手動更新幾個文件路徑(比如/api/tiktoken_ext/file),主要是vocab.bpe、encoder.json、r50k_base.tiktoken等5個文件。主要修改openai_public.py文件內容,文件位置大概在api容器的/app/api/.venv/lib/python3.12/site-packages/tiktoken_ext目錄下。
# openai_public.py文件
def gpt2():mergeable_ranks = data_gym_to_mergeable_bpe_ranks(vocab_bpe_file="/api/tiktoken_ext/file/vocab.bpe",encoder_json_file="/api/tiktoken_ext/file/encoder.json",vocab_bpe_hash="1ce1664773c50f3e0cc8842619a93edc4624525b728b188a9e0be33b7726adc5",encoder_json_hash="196139668be63f3b5d6574427317ae82f612a97c5d1cdaf36ed2256dbf636783",)return {"name": "gpt2","explicit_n_vocab": 50257,"pat_str": r50k_pat_str,"mergeable_ranks": mergeable_ranks,"special_tokens": {ENDOFTEXT: 50256},}
基本上就是以上四類問題了,如果你遇到了新的問題,那恭喜你中獎了,解決辦法目前看只能采取完全刪除容器和鏡像,重啟docker和系統后,換個好的網絡環境進行拉取安裝了。
二、應用實踐
1.安裝ollama插件
使用瀏覽器訪問dify,首次安裝需要設置管理員用戶密碼信息等,按照向導執行即可,打開插件,安裝ollama插件,最好下載到本地后(需要科學上網),選擇本地安裝。
在用戶--設置,配置大模型服務和embedding模型服務,一般配置本機或局域網內的ollama
服務。
注意:market中提供的好多插件安裝過程會導致容器啟動異常,建議不要裝太多插件,只安裝用到的插件,其他用自定義代碼塊來實現。
2.創建知識庫
按照要求完成知識庫創建,再加一下私域知識進去,worker容器會自動分塊存儲。
3.創建第一個聊天助手
在工作室創建聊天助手應用,在知識庫部分關聯本地知識,然后進行對話,具體如下。