微信扫码
添加专属顾问
Dify 与 DMS 的强强联合,打造企业级 AI 应用开发新范式,实现从开发到调度的全链路闭环。 核心内容: 1. Dify 原生能力的局限与挑战 2. DMS Notebook 和 DMS Airflow 如何增强 Dify 功能 3. 销售数据分析机器人的完整开发案例
背景与挑战
dify 作为一款低代码 AI 应用开发平台,凭借其直观的可视化工作流编排能力,极大降低了大模型应用的开发门槛。然而,在实际企业级落地过程中,我们发现其原生能力仍存在两个关键瓶颈:
代码执行能力受限:Dify 内置的 Sandbox 节点虽支持基础 Python 代码执行,但无法安装自定义 Python 包,难以支撑复杂的业务逻辑、数据处理或算法调用;
缺乏自动化调度机制:Dify 原生架构不支持 Agent 或 Agentic Workflow 的定时触发、周期性运行与依赖编排,导致其难以融入企业级自动化运维体系。
为突破这些限制,我们在实践中探索出一套 “Dify + DMS Notebook + DMS Airflow”三位一体的一站式增强开发架构,有效补齐了 Dify 在执行能力与调度能力上的短板:
使用 DMS Notebook 扩展 Dify 的代码执行能力
为什么需要 Notebook?
在 Dify 中,若需调用 Pandas 进行数据清洗、使用 Prophet 做时间序列预测,或集成企业内部 SDK,Sandbox 节点往往无能为力。而 DMS Notebook 提供了完整的 Python 环境,支持:
步骤详解:构建一个销售数据分析 API 服务
import osfrom fastapi import FastAPI, HTTPException, Request, File, UploadFile, Path, Query, Form, Headerfrom fastapi.staticfiles import StaticFilesfrom typing import Optionalimport nest_asyncioimport asyncioimport httpximport io'''注意,Jupyter本身就在一个asyncio事件循环中运行。我们不能在已有循环中直接运行另一个循环,但nest_asyncio这个库可以“打补丁”,允许我们这样做。'''nest_asyncio.apply()app = FastAPI(title="Your Service Name", description="Description of Your Service")# 创建static目录(如果不存在)static_dir = "static"ifnot os.path.exists(static_dir): os.makedirs(static_dir)# 挂载静态文件服务app.mount("/static", StaticFiles(directory=static_dir), name="static")@app.get("/")async def root(): """ 根节点,返回服务基本信息 # --- 如何使用 curl 调用 --- curl -X GET "http://127.0.0.1:8000/" """ return { "message": "Service is running", "documentation": "/docs", "note": "..." }@app.post("/process-data/{item_id}")async def process_data( request: Request, # 使用 Request 对象来接收原始请求 # 路径参数 item_id: int = Path(..., title="物品ID", ge=1), # 查询参数 is_premium: bool = Query(False, description="是否为高级物品"), # 请求头参数 x_token: Optional[str] = Header(None, description="自定义的认证Token")): """ 接收 JSON 请求体、路径参数、查询参数和请求头。 # --- 如何使用 curl 调用 --- # -X POST: 指定请求方法 # URL: 包含路径参数 {item_id} 和查询参数 ?is_premium=true # -H: 添加请求头 (Header) # -d: 发送请求体 (Body),这里是 JSON 字符串 curl -X POST "http://127.0.0.1:8000/process-data/101?is_premium=true" \ -H "Content-Type: application/json" \ -H "X-Token: my-secret-token" \ -d '{"name": "笔记本电脑", "price": 7999.9, "tags": ["electronics", "office"]}' """ if x_token != "my-secret-token": raise HTTPException(status_code=401, detail="X-Token 无效") try: # 手动解析 JSON 请求体 json_body = await request.json() name = json_body.get("name") price = json_body.get("price") # 你的业务逻辑代码 if not name or not price: raise HTTPException(status_code=400, detail="请求体中缺少 'name' 或 'price'") return { "message": "数据处理成功", "received_data": { "item_id": item_id, "is_premium": is_premium, "x_token": x_token, "body": json_body } } except Exception as e: raise HTTPException(status_code=500, detail=f"服务执行错误: {str(e)}")@app.post("/upload-file")async def upload_file( # 表单数据 token: str = Form(...), # 上传文件 file: UploadFile = File(...)): """ 通过表单(form-data)上传文件和附带的文本信息。 # --- 如何使用 curl 调用 --- # -F: 用于发送 multipart/form-data # -F "file=@/path/to/your/file.txt": @符号表示后面是文件路径,curl会读取该文件内容作为上传数据 # -F "token=user123": 发送一个名为 token 的表单字段 # 注意: 请将 /path/to/your/file.txt 替换为你的本地文件真实路径 curl -X POST "http://127.0.0.1:8000/upload-file" \ -F "file=@./test_upload.txt" \ -F "token=my-form-token" """ # 为了让curl示例能工作,我们先创建一个示例文件 if not os.path.exists("test_upload.txt"): with open("test_upload.txt", "w") as f: f.write("This is a test file for curl upload.") try: contents = await file.read() file_location = os.path.join(static_dir, file.filename) with open(file_location, "wb") as f: f.write(contents) return { "message": "文件上传成功!", "token": token, "filename": file.filename, "file_size": len(contents), "file_url": f"/static/{file.filename}" } except Exception as e: raise HTTPException(status_code=500, detail=f"文件处理错误: {str(e)}")@app.get("/status")async def get_server_status(): """ 获取服务器状态。 # --- 如何使用 curl 调用 --- curl -X GET "http://127.0.0.1:8000/status" """ return {"status": "running"}async def run_server(host="127.0.0.1", port=8000): """在后台运行uvicorn服务器""" import uvicorn config = uvicorn.Config(app, host=host, port=port, log_level="info") server = uvicorn.Server(config) # uvicorn.run() 是一个阻塞调用,所以我们用更底层的Server.serve() await server.serve()task = asyncio.create_task(run_server(host="0.0.0.0", port=8000))# 等待服务启动await asyncio.sleep(2)# 创建一个异步HTTP客户端async with httpx.AsyncClient() as client: print("正在向 http://127.0.0.1:8000/status/ 发送请求...") # 发送POST请求 response = await client.get("http://127.0.0.1:8000/status") # 打印结果 if response.status_code == 200: print("服务启动成功") else: print("服务启动失败,请检查报错信息")接下来我们以构建一个单日销售数据分析的API为例,代码内容如下:
import osimport pandas as pdfrom fastapi import FastAPI, HTTPException, Request, File, UploadFilefrom fastapi.staticfiles import StaticFilesimport nest_asyncioimport asyncioimport httpximport io'''注意,Jupyter本身就在一个asyncio事件循环中运行。我们不能在已有循环中直接运行另一个循环,但nest_asyncio这个库可以“打补丁”,允许我们这样做。'''nest_asyncio.apply()app = FastAPI(title="Sales Data Analysis Service", description="Provides data analysis and chart generation capabilities for Dify")# 创建static目录(如果不存在)static_dir = "static"ifnot os.path.exists(static_dir):os.makedirs(static_dir)# 挂载静态文件服务app.mount("/static", StaticFiles(directory=static_dir), name="static")def load_sales_data_from_file(file_content: bytes):"""从上传的文件内容加载销售数据"""try:# 将字节内容转换为StringIO对象csv_string = file_content.decode('utf-8')df = pd.read_csv(io.StringIO(csv_string))# 验证必要的列是否存在required_columns = ['Date', 'Product', 'Price', 'Amount', 'Region']ifnot all(col in df.columns for col in required_columns):raise ValueError(f"CSV file must contain columns: {', '.join(required_columns)}")# 转换数据类型df['Date'] = pd.to_datetime(df['Date'])df['Price'] = pd.to_numeric(df['Price'], errors='coerce')df['Amount'] = pd.to_numeric(df['Amount'], errors='coerce')# 计算销售额 (Sales = Price × Amount)df['Sales'] = df['Price'] * df['Amount']return dfexcept Exception as e:raise HTTPException(status_code=500, detail=f"Error processing CSV file: {str(e)}")async def root():"""Root endpoint, returns service information"""return {"message": "Sales Data Analysis Service is running","documentation": "/docs","endpoints": ["POST /analysis/daily_sale_analysis"],"note": "Require a CSV file upload with columns: Date, Product, Price, Amount, Region"}async def daily_sale_analysis(file: UploadFile = File(...)):"""当日销售数据分析 - 分析上传文件中的销售数据"""try:# 验证文件类型ifnot file.filename.endswith('.csv'):raise HTTPException(status_code=400, detail="文件必须是CSV格式")# 读取上传的文件file_content = await file.read()df = load_sales_data_from_file(file_content)# 获取数据中的日期范围df['Date'] = pd.to_datetime(df['Date']).dt.dateunique_dates = sorted(df['Date'].unique())if len(unique_dates) == 0:raise HTTPException(status_code=400, detail="数据文件中没有有效的日期数据")# 如果有多个日期,取最新的日期作为分析目标target_date = unique_dates[-1] if len(unique_dates) > 1else unique_dates[0]# 筛选目标日期的数据daily_data = df[df['Date'] == target_date].copy()if daily_data.empty:raise HTTPException(status_code=400, detail=f"没有找到日期 {target_date} 的销售数据")# 基础统计total_sales = daily_data['Sales'].sum()total_orders = len(daily_data)total_quantity = daily_data['Amount'].sum()avg_order_value = total_sales / total_orders if total_orders > 0else0# 产品分析product_analysis = daily_data.groupby('Product').agg({'Sales': 'sum','Amount': 'sum','Price': 'mean'}).round(2)# 按销售额排序,取前5名产品top_products = product_analysis.sort_values('Sales', ascending=False).head(5)top_products_list = []for product, row in top_products.iterrows():top_products_list.append({"product": product,"sales": float(row['Sales']),"quantity": int(row['Amount']),"avg_price": float(row['Price'])})# 地区分析region_analysis = daily_data.groupby('Region').agg({'Sales': 'sum','Amount': 'sum'}).round(2)# 按销售额排序top_regions = region_analysis.sort_values('Sales', ascending=False)region_list = []for region, row in top_regions.iterrows():region_list.append({"region": region,"sales": float(row['Sales']),"quantity": int(row['Amount']),"percentage": round(float(row['Sales']) / total_sales * 100, 2)})# 价格区间分析daily_data['price_range'] = pd.cut(daily_data['Price'],bins=[0, 100, 500, 1000, 5000, float('inf')],labels=['0-100', '100-500', '500-1000', '1000-5000', '5000+'])price_range_analysis = daily_data.groupby('price_range').agg({'Sales': 'sum','Amount': 'sum'}).round(2)price_ranges = []for price_range, row in price_range_analysis.iterrows():ifnot pd.isna(row['Sales']) and row['Sales'] > 0:price_ranges.append({"range": str(price_range),"sales": float(row['Sales']),"quantity": int(row['Amount'])})# 生成洞察分析insights = []# 销售额洞察if total_sales > 100000:insights.append(f"当日销售表现优秀,总销售额达到 {total_sales:,.2f} 元")elif total_sales > 50000:insights.append(f"当日销售表现良好,总销售额为 {total_sales:,.2f} 元")else:insights.append(f"当日销售额为 {total_sales:,.2f} 元,可能需要关注销售策略")# 产品洞察if len(top_products_list) > 0:best_product = top_products_list[0]insights.append(f"最佳销售产品是 {best_product['product']},销售额 {best_product['sales']:,.2f} 元")# 地区洞察if len(region_list) > 0:best_region = region_list[0]insights.append(f"销售表现最佳的地区是 {best_region['region']},占总销售额的 {best_region['percentage']}%")# 订单洞察if avg_order_value > 1000:insights.append(f"平均订单价值较高,为 {avg_order_value:,.2f} 元,显示客户购买力强")return {"analysis_date": str(target_date),"summary": {"total_sales": round(float(total_sales), 2),"total_orders": int(total_orders),"total_quantity": int(total_quantity),"average_order_value": round(float(avg_order_value), 2)},"top_products": top_products_list,"region_analysis": region_list,"price_range_analysis": price_ranges,"insights": insights,"data_info": {"date_range": f"{unique_dates[0]} 到 {unique_dates[-1]}"if len(unique_dates) > 1else str(unique_dates[0]),"total_records": len(daily_data),"unique_products": len(daily_data['Product'].unique()),"unique_regions": len(daily_data['Region'].unique())}}except HTTPException:raiseexcept Exception as e:raise HTTPException(status_code=500, detail=f"当日销售数据分析错误: {str(e)}")async def get_server_status():"""获取服务器状态"""try:return {"status": "running"}except Exception as e:raise HTTPException(status_code=500, detail=f"Error getting server status: {str(e)}")async def run_server(host="127.0.0.1", port=8000):"""在后台运行uvicorn服务器"""import uvicornconfig = uvicorn.Config(app, host=host, port=port, log_level="info")server = uvicorn.Server(config)# uvicorn.run() 是一个阻塞调用,所以我们用更底层的Server.serve()await server.serve()task = asyncio.create_task(run_server(host="0.0.0.0", port=8000))# 等待服务启动await asyncio.sleep(2)# 创建一个异步HTTP客户端async with httpx.AsyncClient() as client:print("正在向 http://127.0.0.1:8000/status/ 发送请求...")# 发送POST请求response = await client.get("http://127.0.0.1:8000/status")# 打印结果if response.status_code == 200:print("服务启动成功")else:print("服务启动失败,请检查报错信息")
✅ 异步支持:Jupyter 内置 asyncio 事件循环,可直接使用 async/await。
在Dify on DMS实例中访问服务
现在我们使用这份模拟销售数据文件来访问API服务:
接下来以该Dify工作流为例子进行完整的服务调用;
在钉钉群组中添加一个自定义的机器人,并参考https://open.dingtalk.com/document/orgapp/robot-overview 了解如何获取钉钉机器人的access_token和sign_secret
在填写完你的钉钉机器人参数之后,点击右上角的运行-》从本地上传上传示例的销售数据,并点击开始运行;
钉钉群中的机器人成功发送了对该销售数据的分析报告;
点击右上角的发布-》发布更新发布工作流用于后面的定时调用;
使用 DMS Airflow 实现定时调度
创建DMS Airflow实例
参照以下链接在DMS中创建Airflow实例
https://help.aliyun.com/zh/dms/purchase-airflow-resources
https://help.aliyun.com/zh/dms/create-and-manage-an-airflow-instance
更多关于Airflow的操作,参照https://airflow.apache.org/docs/apache-airflow/stable/index.html
编写 DAG:每日自动触发销售分析
import pendulumimport requestsimport jsonfrom airflow.models.dag import DAGfrom airflow.operators.python import PythonOperatorfrom airflow.models import VariableCSV_FILE_PATH = "/yourpath/daily_sale_data.csv"DIFY_API_URL = "https://dify-dms.aliyuncs.com/v1" # 替换成你的 Dify Workflow API URL# 从 Airflow Variable 中安全地获取 API KeyDIFY_API_KEY = Variable.get("dify_api_key")APP_API_KEY= Variable.get("app_api_key")def call_dify_workflow_with_csv(**kwargs):"""读取 CSV 文件内容,并将其作为文件上传调用 Dify 工作流。"""print(f"准备从 '{CSV_FILE_PATH}' 读取文件...")try:with open(CSV_FILE_PATH, 'rb') as f:files_to_upload = {'file': ('daily_sale_data.csv', f, 'document/csv')}# 准备 API 请求的 headers 和 bodyheaders = {'Authorization': f'Bearer {APP_API_KEY}','DifyApiKey': f'{DIFY_API_KEY}',}file_upload_response=requests.post(DIFY_API_URL+'/files/upload',headers=headers,data={'user': 'airflow-user-demo'},files=files_to_upload,)print(file_upload_response.json())file_id=file_upload_response.json().get('id')headers.update({'Content-Type': 'application/json'})# 'inputs' 通常是json字符串# 'user' 是必须的,代表最终用户的标识符input_data = {'sales_data': {"type": "document","transfer_method": "local_file","upload_file_id": file_id}}data = {'inputs': input_data,'user': 'airflow-user-demo','response_mode': 'blocking',}print("开始调用 Dify API...")print(f"URL: {DIFY_API_URL}")response = requests.post(DIFY_API_URL+'/workflows/run',headers=headers,json=data,)# 检查请求是否成功response.raise_for_status()print(f"API 调用成功!状态码: {response.status_code}")# 打印响应内容print("--- Dify API Response ---")print(response.json()["data"]["outputs"]["answer"])print("\n--- End of Response ---")# 你也可以将完整的响应推送到 XComs,以便下游任务使用# ti = kwargs['ti']# ti.xcom_push(key='dify_response', value=full_response)except FileNotFoundError:print(f"错误:文件未找到于 '{CSV_FILE_PATH}'")raiseexcept requests.exceptions.RequestException as e:print(f"API 调用失败: {e}")raisewith DAG(dag_id="dify_workflow",start_date=pendulum.datetime(2023, 10, 27, tz="Asia/Shanghai"),# '0 8 * * *' 代表每天早上8:00 (UTC+8)# Airflow 默认使用 UTC,但 Cron 表达式本身不带时区,调度器会根据 DAG 的时区设置来解释schedule="0 8 * * *",catchup=False,tags=["dify", "api", "example"],doc_md="""### Dify 工作流调用 DAG此 DAG 每天早上8点执行,它会:1. 从本地文件系统读取一个 CSV 文件。2. 将该 CSV 文件作为附件,调用一个 Dify 工作流。3. 打印出 Dify API 的响应。""") as dag:run_dify_workflow = PythonOperator(task_id="call_dify",python_callable=call_dify_workflow_with_csv,)
📌 注意:通过API调用Dify工作流中想要上传本地文件,需要先通过/files/upload接口传入相应的文件获取文件id,再将文件id传入工作流中。
每日 8:00,系统自动调用 Dify 工作流,最终由钉钉机器人推送分析报告;
总结与思考
通过 DMS Notebook + DMS Airflow 对 Dify 的能力扩展,我们成功构建了一个具备以下特性的企业级 Agent 开发范式:
这套方案不仅解决了 Dify 当前的局限性,更重要的是,它保留了 Dify 的低代码优势,同时通过与成熟数据基础设施(Notebook + Airflow)的深度集成,实现了 “敏捷开发”与“工程可靠”的平衡。
🌟 核心理念:Agent 的价值不在于“全自动”,而在于“可扩展、可调度、可运维”。真正的生产级智能系统,一定是平台能力与工程实践的结合体
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-10-07
我应用AI的一些商业思路。
2026-09-26
腾讯内测 Answerbit ,下场做 GEO
2026-09-23
大模型在货拉拉营销广告的应用实践
2026-09-19
GEO信源分级落地:官网仅C级,A级引用率是商业内容的7.2倍
2026-09-11
60%的用户不再点击网页,替代SEO的GEO怎么做?
2026-09-11
零售业 FDE 案例:广告上线从最长 4 周到数天,乐购(Tesco)的 AI 改了什么?
2026-09-07
随身保典|做保险“超级个体”,打造你的影响力
2026-09-05
一文说清GEO:它到底有没有用?适合谁做?怎么判断是不是智商税?
2026-09-02
2026-08-07
2026-08-07
2026-08-06
2026-09-05
2026-08-06
2026-09-11
2026-09-19
2026-09-26
2026-09-02
2026-09-11
2026-09-11
2026-09-05
2026-08-07
2026-07-01
2026-03-16
2026-01-25
2025-09-30
欢迎您使用【53AI 官方网站】(以下简称“本网站”或“我们”)。本《会员服务协议》(以下简称“本协议”)是您(以下简称“会员”或“用户”)与【深圳市博思协创网络科技有限公司】之间关于注册、登录及使用本网站会员服务所订立的法律协议。
在您注册或登录前,请务必审慎阅读、充分理解各条款内容,特别是免除或限制责任的条款、知识产权条款、争议解决条款等。此类条款将以加粗形式提示您注意。 当您通过微信公众号授权、手机验证码验证或其他方式成功登录本网站时,即视为您已完全理解并同意接受本协议的全部内容。
一、 定义
本网站:指由【深圳市博思协创网络科技有限公司】运营的,域名为【53ai.com】的网站及相关移动端页面。
会员服务:指本网站向注册会员提供的知识库文章查阅、内容检索及其他相关增值服务。
知识库内容:指本网站发布的包括但不限于文字、图表、数据、研究报告、行业分析等数字化内容资源。
二、 账号注册与登录
登录方式:本网站支持以下登录方式,您可根据实际情况选择:
微信公众号授权登录:您同意将您的微信OpenID信息授权给本网站,用于创建或关联会员账号。
手机验证码登录:您需提供真实有效的手机号码,并通过短信验证码完成身份验证与登录/注册。
账号安全:您的账号仅限您本人使用,禁止赠与、借用、租用、转让或售卖。因您保管不善导致的账号被盗、密码泄露等损失,由您自行承担。
实名认证:根据相关法律法规要求,我们可能要求您在特定功能下完成实名认证。如您拒绝提供,可能无法使用部分或全部服务。
未成年人保护:若您未满18周岁,请在法定监护人的陪同下阅读本协议,并在征得监护人同意后使用本服务。
三、 服务内容与规范
知识库查阅权限:会员登录后,有权按照其会员等级对应的权限范围,在线浏览、检索本网站知识库中的相关文章及内容。
服务变更:我们有权根据业务发展需要,调整、变更或终止部分服务内容,并将以网站公告、公众号消息等方式提前通知。
禁止行为:您在使用服务时不得实施以下行为:
利用技术手段批量爬取、下载、转存知识库内容;
将知识库内容用于商业目的或未经授权地向第三方传播;
干扰本网站正常运行或侵犯其他用户合法权益;
发布违法违规信息或从事违反公序良俗的活动。
四、 知识产权声明
权利归属:本网站知识库中的排版设计、软件代码等内容的知识产权均归【公司全称】或原权利人所有,受《中华人民共和国著作权法》等法律保护。
有限许可:本网站授予会员一项非独占、不可转让、不可转授权的普通许可,仅限于个人学习、研究之目的在线查阅知识库内容。
侵权追责:未经书面许可,任何单位或个人不得以任何形式复制、转载、摘编、镜像、汇编或以其他方式使用上述内容。一经发现,我们保留追究其法律责任的权利。
五、 个人信息保护
我们重视对您个人信息的保护。关于我们如何收集、使用、存储和保护您的个人信息,请单独阅读 《隐私政策》。
您通过微信公众号授权或手机号验证所提供的信息,我们将严格按照《个人信息保护法》的规定处理,仅用于身份识别、服务提供及安全验证等必要用途。
您可以随时通过网站设置或联系客服行使查阅、更正、删除个人信息及撤回授权同意的权利。
六、 免责声明
内容准确性:知识库内容仅供参考,不构成专业建议。我们不对其完整性、准确性、时效性作任何明示或暗示的保证,您应自行判断并承担使用风险。
不可抗力:因自然灾害、政策法规变化、网络故障、第三方平台接口异常(如微信接口维护、运营商短信通道故障)等不可抗力导致的服务中断或延迟,我们不承担违约责任。
第三方链接:本网站可能包含指向第三方网站的链接,该等网站的内容和服务不受我们控制,请您自行甄别风险。
七、 违约责任
如您违反本协议约定,我们有权视情节采取警告、限制功能、暂停服务、注销账号等措施,并保留要求赔偿损失的权利。
如因您的违约行为导致我们遭受行政处罚、第三方索赔或商誉损失,您应承担全部赔偿责任(包括但不限于罚款、赔偿金、律师费、公证费等)。
八、 法律适用与争议解决
本协议的订立、执行和解释均适用中华人民共和国大陆地区法律。
因本协议产生的或与本协议有关的任何争议,双方应友好协商解决;协商不成的,任何一方均可向【公司所在地】有管辖权的人民法院提起诉讼。
九、 其他
本协议构成双方就本服务达成的完整协议,取代此前任何口头或书面约定。
本协议任一条款被认定为无效或不可执行的,不影响其他条款的效力。
我们对本协议享有最终解释权,并在法律允许的范围内保留随时修改的权利。修改后的协议一经公布即生效,继续使用服务即视为同意修订内容。