当前位置: 首页 > news >正文

【人工智能-agent】--使用python调用dify工作流

在dify中搭建好了工作流,然后需要在python中使用,怎么办呢,

dify提供了Workflow 应用 API,使用curl网络响应可以直接访问结果

curl -X POST 'http://127.0.0.1/v1/workflows/run' \
--header 'Authorization: Bearer {api_key}' \
--header 'Content-Type: application/json' \
--data-raw '{"inputs": {},"response_mode": "streaming","user": "abc-123"
}'

目录

1.传入参数

1.1.input

1.2. response_mode

2.输出参数

2.1.CompletionResponse

2.2.ChunkCompletionResponse

3.Errors

4.获取workflow执行情况

5.上传文件

6.获取应用的基本信息

7.获取参数信息

8.python连接dify实例一(输入文本)

9.python连接dify实例二(输入文件)


1.传入参数

1.1.input

inputs (object) Required 允许传入 App 定义的各变量值。 inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。变量可以是文件列表类型。 文件列表类型变量适用于传入文件结合文本理解并回答问题,仅当模型支持该类型文件解析能力时可用。如果该变量是文件列表类型,该变量对应的值应是列表格式,其中每个元素应包含以下内容:

​type (string)​​具体类型​​传递方式 (transfer_method)​
​document​TXT, MD, MARKDOWN, PDF, HTML, XLSX, XLS, DOCX, CSV, EML, MSG, PPTX, PPT, XML, EPUBremote_url 或 local_file
​image​JPG, JPEG, PNG, GIF, WEBP, SVGremote_url 或 local_file
​audio​MP3, M4A, WAV, WEBM, AMRremote_url 或 local_file
​video​MP4, MOV, MPEG, MPGAremote_url 或 local_file
​custom​其他未列出的文件类型remote_url 或 local_file
  • transfer_method (string) 传递方式,remote_url 图片地址 / local_file 上传文件
  • url (string) 图片地址(仅当传递方式为 remote_url 时)
  • upload_file_id (string) 上传文件 ID(仅当传递方式为 local_file 时)
{"inputs": {"{variable_name}": [{"transfer_method": "local_file","upload_file_id": "{upload_file_id}","type": "{document_type}"}]}
}

1.2. response_mode

  • response_mode (string) Required 返回响应模式,支持:
    • streaming 流式模式(推荐)。基于 SSE(Server-Sent Events)实现类似打字机输出方式的流式返回。
    • blocking 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。 由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。

Response

当 response_mode 为 blocking 时,返回 CompletionResponse object。 当 response_mode 为 streaming时,返回 ChunkCompletionResponse object 流式序列。

  • user (string) Required 用户标识,用于定义终端用户的身份,方便检索、统计。 由开发者定义规则,需保证用户标识在应用内唯一。

2.输出参数

Response

当 response_mode 为 blocking 时,返回 CompletionResponse object。 当 response_mode 为 streaming时,返回 ChunkCompletionResponse object 流式序列。

2.1.CompletionResponse

返回完整的 App 结果,Content-Type 为 application/json 

​字段​​类型​​说明​
​workflow_run_id​stringWorkflow 执行 ID
​task_id​string任务 ID,用于请求跟踪和停止响应接口
​data​object详细内容
​data.id​stringWorkflow 执行 ID(与 workflow_run_id 相同)
​data.workflow_id​string关联的 Workflow ID
​data.status​string执行状态:
running(运行中)
succeeded(成功)
failed(失败)
stopped(已停止)
​data.outputs​json输出内容(执行成功时返回)
​data.error​string错误原因(执行失败时返回)
​data.elapsed_time​float耗时(秒)
​data.total_tokens​int总使用 tokens
​data.total_steps​int总步数(冗余),默认 0
​data.created_at​timestamp开始时间
​data.finished_at​timestamp结束时间(仅当 status 不是 running 时返回)
{"workflow_run_id": "djflajgkldjgd","task_id": "9da23599-e713-473b-982c-4328d4f5c78a","data": {"id": "fdlsjfjejkghjda","workflow_id": "fldjaslkfjlsda","status": "succeeded","outputs": {"text": "Nice to meet you."},"error": null,"elapsed_time": 0.875,"total_tokens": 3562,"total_steps": 8,"created_at": 1705407629,"finished_at": 1727807631}
}

2.2.ChunkCompletionResponse

返回 App 输出的流式块,Content-Type 为 text/event-stream。 每个流式块均为 data: 开头,块之间以 \n\n 即两个换行符分隔,如下所示:

  data: {"event": "workflow_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "sequence_number": 1, "created_at": 1679586595}}data: {"event": "node_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "created_at": 1679586595}}data: {"event": "node_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "execution_metadata": {"total_tokens": 63127864, "total_price": 2.378, "currency": "USD"},  "created_at": 1679586595}}data: {"event": "workflow_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "total_tokens": 63127864, "total_steps": "1", "created_at": 1679586595, "finished_at": 1679976595}}data: {"event": "tts_message", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"}data: {"event": "tts_message_end", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": ""}
​event 类型​​说明​​必填字段​​可选字段​
workflow_startedWorkflow 开始执行task_idworkflow_run_ideventdata
data.iddata.workflow_iddata.sequence_numberdata.created_at
node_started节点开始执行task_idworkflow_run_ideventdata
data.iddata.node_iddata.node_typedata.titledata.indexdata.inputsdata.created_at
data.predecessor_node_id
text_chunk文本片段流式输出task_idworkflow_run_ideventdata.textdata.from_variable_selector
node_finished节点执行结束(成功/失败)task_idworkflow_run_ideventdata
data.iddata.node_iddata.indexdata.inputsdata.statusdata.execution_metadatadata.created_at
data.predecessor_node_iddata.process_datadata.outputsdata.errordata.elapsed_timedata.total_tokensdata.total_pricedata.currency
workflow_finishedWorkflow 执行结束(成功/失败)task_idworkflow_run_ideventdata
data.iddata.workflow_iddata.statusdata.total_stepsdata.created_at
data.outputsdata.errordata.elapsed_timedata.total_tokensdata.finished_at
tts_messageTTS 音频流片段(Base64 编码)task_idmessage_idaudiocreated_at
tts_message_endTTS 音频流结束task_idmessage_idcreated_ataudio(空字符串)
ping心跳保活(每 10s 一次)

3.Errors

  • 400,invalid_param,传入参数异常
  • 400,app_unavailable,App 配置不可用
  • 400,provider_not_initialize,无可用模型凭据配置
  • 400,provider_quota_exceeded,模型调用额度不足
  • 400,model_currently_not_support,当前模型不可用
  • 400,workflow_request_error,workflow 执行失败
  • 500,服务内部异常

4.获取workflow执行情况

根据 workflow 执行 ID 获取 workflow 任务当前执行结果

Path

  • workflow_run_id (string) workflow_run_id,可在流式返回 Chunk 中获取

Response

  • id (string) workflow 执行 ID
  • workflow_id (string) 关联的 Workflow ID
  • status (string) 执行状态 running / succeeded / failed / stopped
  • inputs (json) 任务输入内容
  • outputs (json) 任务输出内容
  • error (string) 错误原因
  • total_steps (int) 任务执行总步数
  • total_tokens (int) 任务执行总 tokens
  • created_at (timestamp) 任务开始时间
  • finished_at (timestamp) 任务结束时间
  • elapsed_time (float) 耗时(s)
curl -X GET 'http://127.0.0.1/v1/workflows/run/:workflow_run_id' \
-H 'Authorization: Bearer {api_key}' \
-H 'Content-Type: application/json'
{"id": "b1ad3277-089e-42c6-9dff-6820d94fbc76","workflow_id": "19eff89f-ec03-4f75-b0fc-897e7effea02","status": "succeeded","inputs": "{\"sys.files\": [], \"sys.user_id\": \"abc-123\"}","outputs": null,"error": null,"total_steps": 3,"total_tokens": 0,"created_at": 1705407629,"finished_at": 1727807631,"elapsed_time": 30.098514399956912
}

5.上传文件

上传文件并在发送消息时使用,可实现图文多模态理解。 支持您的工作流程所支持的任何格式。 上传的文件仅供当前终端用户使用。

​分类​​字段/参数​​类型​​必填​​说明​
​请求方式​POST-使用 multipart/form-data 格式
​请求参数​filefile要上传的文件(二进制数据)
userstring用户标识,需与消息接口的 user 一致
​成功响应​iduuid文件唯一 ID
namestring文件名(含扩展名)
sizeint文件大小(字节)
extensionstring文件后缀(如 PDF
mime_typestring文件的 MIME 类型(如 application/pdf
created_byuuid上传用户 ID
created_attimestamp上传时间
​错误响应​400 no_file_uploaded--未提供文件
400 too_many_files--只允许上传单个文件
400 unsupported_preview--文件不支持预览
400 unsupported_estimate--文件不支持估算
413 file_too_large--文件大小超过服务器限制
415 unsupported_file_type--文件类型不支持(仅接受文档类文件)
503 s3_connection_failed--无法连接 S3 服务
503 s3_permission_denied--无 S3 上传权限
503 s3_file_too_large--文件超过 S3 大小限制
curl -X POST 'http://127.0.0.1/v1/files/upload' \
--header 'Authorization: Bearer {api_key}' \
--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif] \
--form 'user=abc-123'
{"id": "72fa9618-8f89-4a37-9b33-7e1178a24a67","name": "example.png","size": 1024,"extension": "png","mime_type": "image/png","created_by": 123,"created_at": 1577836800,
}

6.获取应用的基本信息

$response = curl.exe -X GET 'http://127.0.0.1/v1/info' -H 'Authorization: Bearer app-1aarTqkLkyhE48MvprdQcf3V'
[System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::ASCII.GetBytes($response)) | ConvertFrom-Json

7.获取参数信息

用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。

​字段/控件类型​​子字段​​类型​​必填​​说明​
text-input-object-文本输入控件
labelstring控件展示标签名
variablestring控件 ID(唯一标识)
requiredbool是否必填(true/false
defaultstring默认值
paragraph-object-段落文本输入控件(多行文本)
labelstring控件展示标签名
variablestring控件 ID
requiredbool是否必填
defaultstring默认值
select-object-下拉控件
labelstring控件展示标签名
variablestring控件 ID
requiredbool是否必填
defaultstring默认选中值
optionsarray[string]选项列表(如 ["选项1", "选项2"]
file_upload-object-文件上传配置
file_upload.image-object-图片上传设置
enabledbool是否开启图片上传(true/false
number_limitsint图片数量限制(默认 3
transfer_methodsarray[string]传递方式:remote_url(远程URL)或 local_file(本地文件),至少选一个
system_parameters-object-系统参数配置
file_size_limitint文档上传大小限制(MB)
image_file_size_limitint图片文件上传大小限制(MB)
audio_file_size_limitint音频文件上传大小限制(MB)
video_file_size_limitint视频文件上传大小限制(MB)
curl.exe -X GET 'http://127.0.0.1/v1/parameters' -H 'Authorization: Bearer app-1aarTqkLkyhE48MvprdQcf3V'

8.python连接dify实例一(输入文本)

其实就是对api内容的解析过程

import requests
import jsondef run_dify_workflow():# 配置参数url = "http://127.0.0.1/v1/workflows/run"api_key = "app-1aarTqkLkyhE48MvprdQcf3V"  # 你的API密钥user_id = "abc-123"  # 用户标识# 请求头headers = {"Authorization": f"Bearer {api_key}","Content-Type": "application/json;charset=UTF-8","Accept-Charset": "UTF-8"}# 请求体payload = {"inputs": {"input": "普钢综合,2025.1.2-2.2的数据,并画出其相关的趋势图表。"},"response_mode": "streaming","user": user_id}try:# 发送POST请求response = requests.post(url,headers=headers,json=payload,stream=True  # 重要:启用流式响应)print(f"请求成功,状态码: {response.status_code}")# 检查响应状态if response.status_code != 200:print(f"请求失败,状态码: {response.status_code}")print(f"错误信息: {response.text}")return# 处理流式响应print("开始接收流式响应...")for line in response.iter_lines():if line:# 确保使用正确的编码解码decoded_line = line.decode('utf-8').strip()if decoded_line.startswith('data:'):decoded_line = decoded_line[5:].strip()try:# 解析JSON数据if decoded_line:  # 确保不是空字符串data = json.loads(decoded_line)print("收到数据:", json.dumps(data, ensure_ascii=False, indent=2))except json.JSONDecodeError as e:print("收到非JSON数据:", decoded_line)except requests.exceptions.RequestException as e:print(f"请求发生错误: {e}")if __name__ == "__main__":run_dify_workflow()

9.python连接dify实例二(输入文件)


import requests
import json
import urllib.parse
import osdef upload_file(file_path, user):upload_url = "http://10.24.39.143/v1/files/upload"headers = {"Authorization": "Bearer app-Qgy3rAk5SJgyEL4L2ut3Ijnn",}try:print("上传文件中...")# 将文件名编码为 URL 编码encoded_file_name = urllib.parse.quote(os.path.basename(file_path), safe="")with open(file_path, 'rb') as file:files = {'file': (encoded_file_name, file, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')  # 确保文件以适当的MIME类型上传}data = {"user": user,"type": "xlsx"  # 设置文件类型为TXT}response = requests.post(upload_url, headers=headers, files=files, data=data)if response.status_code == 201:  # 201 表示创建成功print("文件上传成功")return response.json().get("id")  # 获取上传的文件 IDelse:print(f"文件上传失败,状态码: {response.status_code}")return Noneexcept Exception as e:print(f"发生错误: {str(e)}")return Nonedef run_workflow(file_ids, user, response_mode="blocking"):workflow_url = "http://10.24.39.143/v1/workflows/run"headers = {"Authorization": "Bearer app-Qgy3rAk5SJgyEL4L2ut3Ijnn","Content-Type": "application/json"}# 假设 file_ids 是一个字典,包含 test 和 guifan 的文件 IDdata = {"inputs": {"test": [  # test 字段需要文件列表{"transfer_method": "local_file","upload_file_id": file_ids["test"],"type": "document"}]# "guifan": {  # guifan 字段需要单文件#     "transfer_method": "local_file",#     # "upload_file_id": file_ids["guifan"],#     "type": "document"# }},"response_mode": response_mode,"user": user}try:print("运行工作流...")response = requests.post(workflow_url, headers=headers, json=data)if response.status_code == 200:print("工作流执行成功")return response.json()else:print(f"工作流执行失败,状态码: {response.status_code}")print("错误详情:", response.text)  # 打印错误详情return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}except Exception as e:print(f"发生错误: {str(e)}")return {"status": "error", "message": str(e)}# 使用示例
file_paths = {"test": "C:\pythonProject\项目五(钢铁价格预测)\我的钢铁网\gyp4.xlsx",# "guifan": "processed_data/小类.xlsx"
}file_ids = {}# 上传文件
for field, path in file_paths.items():file_id = upload_file(path, "difyuser")if file_id:file_ids[field] = file_idelse:print(f"{field} 文件上传失败,无法执行工作流")exit()# 文件上传成功,继续运行工作流
result = run_workflow(file_ids, "difyuser")
print(result)

 

http://www.xdnf.cn/news/651367.html

相关文章:

  • Win10/Win11终极C盘清理指南
  • 深入剖析Java中的伪共享:原理、检测与解决方案
  • RT-Thread源码阅读(3)——内核对象管理
  • ES6基础
  • 考研408《计算机组成原理》复习笔记,第二章(3)数值数据的运算(浮点数计算篇)
  • 新一代WebP2P视频物联技术:EasyRTC嵌入式音视频通信SDK助力音视频实时通信场景应用
  • 基于SpringBoot+RabbitMQ完成应用通信
  • 编程日志5.19
  • 一根网线可以有两个ip地址吗?怎么实现
  • 洛谷-P1957 口算练习题
  • 基于大模型的胃肠道功能紊乱手术全程预测与干预方案研究
  • Elasticsearch 分片驱逐(Shard Exclusion)方式简析:`_name`、`_ip`、`_host`
  • 图论 判断是否有环
  • (自用)Java学习-5.13(Redis,OSS)
  • 第三十七天打卡
  • 【EdgeYOLO】《EdgeYOLO: An Edge-Real-Time Object Detector》
  • 分布式爬虫架构设计
  • Python 实现简单OCR文本识别
  • HTML应用指南:利用GET请求获取全国捞王锅物料理门店位置信息
  • cannot access ‘/etc/mysql/debian.cnf‘: No such file or directory
  • 解决 iTerm2 中 nvm 不生效的问题(Mac 环境)
  • “安康杯”安全生产知识竞赛活动流程方案
  • 课上实验111111
  • 4、docker compose
  • 汽配快车道:助力汽车零部件行业的产业重构与数字化出海
  • 关于OT IIOT系统远程访问的零信任安全
  • 从“黑箱”到透明化:MES如何重构生产执行全流程?
  • NV211NV212美光科技颗粒NV219NV220
  • Python——day37早停策略和模型权重的保存
  • 第九届水动力学与能源电力系统国际学术会议(HEEPS 2025)