You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template_workflow.zh.mdx 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Workflow 应用 API
  4. Workflow 应用无会话支持,适合用于翻译/文章写作/总结 AI 等等。
  5. <div>
  6. ### Base URL
  7. <CodeGroup title="Code" targetCode={props.appDetail.api_base_url}>
  8. ```javascript
  9. ```
  10. </CodeGroup>
  11. ### Authentication
  12. Service API 使用 `API-Key` 进行鉴权。
  13. <i>**强烈建议开发者把 `API-Key` 放在后端存储,而非分享或者放在客户端存储,以免 `API-Key` 泄露,导致财产损失。**</i>
  14. 所有 API 请求都应在 **`Authorization`** HTTP Header 中包含您的 `API-Key`,如下所示:
  15. <CodeGroup title="Code">
  16. ```javascript
  17. Authorization: Bearer {API_KEY}
  18. ```
  19. </CodeGroup>
  20. </div>
  21. ---
  22. <Heading
  23. url='/workflows/run'
  24. method='POST'
  25. title='执行 workflow'
  26. name='#Execute-Workflow'
  27. />
  28. <Row>
  29. <Col>
  30. 执行 workflow,没有已发布的 workflow,不可执行。
  31. ### Request Body
  32. - `inputs` (object) Required
  33. 允许传入 App 定义的各变量值。
  34. inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。变量可以是文件列表类型。
  35. 文件列表类型变量适用于传入文件结合文本理解并回答问题,仅当模型支持该类型文件解析能力时可用。如果该变量是文件列表类型,该变量对应的值应是列表格式,其中每个元素应包含以下内容:
  36. - `type` (string) 支持类型:
  37. - `document` 具体类型包含:'TXT', 'MD', 'MARKDOWN', 'PDF', 'HTML', 'XLSX', 'XLS', 'DOCX', 'CSV', 'EML', 'MSG', 'PPTX', 'PPT', 'XML', 'EPUB'
  38. - `image` 具体类型包含:'JPG', 'JPEG', 'PNG', 'GIF', 'WEBP', 'SVG'
  39. - `audio` 具体类型包含:'MP3', 'M4A', 'WAV', 'WEBM', 'AMR'
  40. - `video` 具体类型包含:'MP4', 'MOV', 'MPEG', 'MPGA'
  41. - `custom` 具体类型包含:其他文件类型
  42. - `transfer_method` (string) 传递方式,`remote_url` 图片地址 / `local_file` 上传文件
  43. - `url` (string) 图片地址(仅当传递方式为 `remote_url` 时)
  44. - `upload_file_id` (string) 上传文件 ID(仅当传递方式为 `local_file` 时)
  45. - `response_mode` (string) Required
  46. 返回响应模式,支持:
  47. - `streaming` 流式模式(推荐)。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现类似打字机输出方式的流式返回。
  48. - `blocking` 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。
  49. <i>由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。</i>
  50. - `user` (string) Required
  51. 用户标识,用于定义终端用户的身份,方便检索、统计。
  52. 由开发者定义规则,需保证用户标识在应用内唯一。API 无法访问 WebApp 创建的会话。
  53. - `files` (array[object]) 可选
  54. - `trace_id` (string) Optional
  55. 链路追踪ID。适用于与业务系统已有的trace组件打通,实现端到端分布式追踪等场景。如果未指定,系统将自动生成 `trace_id`。支持以下三种方式传递,具体优先级依次为:
  56. 1. Header:推荐通过 HTTP Header `X-Trace-Id` 传递,优先级最高。
  57. 2. Query 参数:通过 URL 查询参数 `trace_id` 传递。
  58. 3. Request Body:通过请求体字段 `trace_id` 传递(即本字段)。
  59. ### Response
  60. 当 `response_mode` 为 `blocking` 时,返回 CompletionResponse object。
  61. 当 `response_mode` 为 `streaming`时,返回 ChunkCompletionResponse object 流式序列。
  62. ### CompletionResponse
  63. 返回完整的 App 结果,`Content-Type` 为 `application/json` 。
  64. - `workflow_run_id` (string) workflow 执行 ID
  65. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  66. - `data` (object) 详细内容
  67. - `id` (string) workflow 执行 ID
  68. - `workflow_id` (string) 关联 Workflow ID
  69. - `status` (string) 执行状态, `running` / `succeeded` / `failed` / `stopped`
  70. - `outputs` (json) Optional 输出内容
  71. - `error` (string) Optional 错误原因
  72. - `elapsed_time` (float) Optional 耗时(s)
  73. - `total_tokens` (int) Optional 总使用 tokens
  74. - `total_steps` (int) 总步数(冗余),默认 0
  75. - `created_at` (timestamp) 开始时间
  76. - `finished_at` (timestamp) 结束时间
  77. ### ChunkCompletionResponse
  78. 返回 App 输出的流式块,`Content-Type` 为 `text/event-stream`。
  79. 每个流式块均为 data: 开头,块之间以 `\n\n` 即两个换行符分隔,如下所示:
  80. <CodeGroup>
  81. ```streaming {{ title: 'Response' }}
  82. data: {"event": "text_chunk", "workflow_run_id": "b85e5fc5-751b-454d-b14e-dc5f240b0a31", "task_id": "bd029338-b068-4d34-a331-fc85478922c2", "data": {"text": "\u4e3a\u4e86", "from_variable_selector": ["1745912968134", "text"]}}\n\n
  83. ```
  84. </CodeGroup>
  85. 流式块中根据 `event` 不同,结构也不同,包含以下类型:
  86. - `event: workflow_started` workflow 开始执行
  87. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  88. - `workflow_run_id` (string) workflow 执行 ID
  89. - `event` (string) 固定为 `workflow_started`
  90. - `data` (object) 详细内容
  91. - `id` (string) workflow 执行 ID
  92. - `workflow_id` (string) 关联 Workflow ID
  93. - `created_at` (timestamp) 开始时间
  94. - `event: node_started` node 开始执行
  95. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  96. - `workflow_run_id` (string) workflow 执行 ID
  97. - `event` (string) 固定为 `node_started`
  98. - `data` (object) 详细内容
  99. - `id` (string) workflow 执行 ID
  100. - `node_id` (string) 节点 ID
  101. - `node_type` (string) 节点类型
  102. - `title` (string) 节点名称
  103. - `index` (int) 执行序号,用于展示 Tracing Node 顺序
  104. - `predecessor_node_id` (string) 前置节点 ID,用于画布展示执行路径
  105. - `inputs` (object) 节点中所有使用到的前置节点变量内容
  106. - `created_at` (timestamp) 开始时间
  107. - `event: text_chunk` 文本片段
  108. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  109. - `workflow_run_id` (string) workflow 执行 ID
  110. - `event` (string) 固定为 `text_chunk`
  111. - `data` (object) 详细内容
  112. - `text` (string) 文本内容
  113. - `from_variable_selector` (array) 文本来源路径,帮助开发者了解文本是由哪个节点的哪个变量生成的
  114. - `event: node_finished` node 执行结束,成功失败同一事件中不同状态
  115. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  116. - `workflow_run_id` (string) workflow 执行 ID
  117. - `event` (string) 固定为 `node_finished`
  118. - `data` (object) 详细内容
  119. - `id` (string) node 执行 ID
  120. - `node_id` (string) 节点 ID
  121. - `index` (int) 执行序号,用于展示 Tracing Node 顺序
  122. - `predecessor_node_id` (string) optional 前置节点 ID,用于画布展示执行路径
  123. - `inputs` (object) 节点中所有使用到的前置节点变量内容
  124. - `process_data` (json) Optional 节点过程数据
  125. - `outputs` (json) Optional 输出内容
  126. - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped`
  127. - `error` (string) Optional 错误原因
  128. - `elapsed_time` (float) Optional 耗时(s)
  129. - `execution_metadata` (json) 元数据
  130. - `total_tokens` (int) optional 总使用 tokens
  131. - `total_price` (decimal) optional 总费用
  132. - `currency` (string) optional 货币,如 `USD` / `RMB`
  133. - `created_at` (timestamp) 开始时间
  134. - `event: workflow_finished` workflow 执行结束,成功失败同一事件中不同状态
  135. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  136. - `workflow_run_id` (string) workflow 执行 ID
  137. - `event` (string) 固定为 `workflow_finished`
  138. - `data` (object) 详细内容
  139. - `id` (string) workflow 执行 ID
  140. - `workflow_id` (string) 关联 Workflow ID
  141. - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped`
  142. - `outputs` (json) Optional 输出内容
  143. - `error` (string) Optional 错误原因
  144. - `elapsed_time` (float) Optional 耗时(s)
  145. - `total_tokens` (int) Optional 总使用 tokens
  146. - `total_steps` (int) 总步数(冗余),默认 0
  147. - `created_at` (timestamp) 开始时间
  148. - `finished_at` (timestamp) 结束时间
  149. - `event: tts_message` TTS 音频流事件,即:语音合成输出。内容是Mp3格式的音频块,使用 base64 编码后的字符串,播放的时候直接解码即可。(开启自动播放才有此消息)
  150. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  151. - `message_id` (string) 消息唯一 ID
  152. - `audio` (string) 语音合成之后的音频块使用 Base64 编码之后的文本内容,播放的时候直接 base64 解码送入播放器即可
  153. - `created_at` (int) 创建时间戳,如:1705395332
  154. - `event: tts_message_end` TTS 音频流结束事件,收到这个事件表示音频流返回结束。
  155. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  156. - `message_id` (string) 消息唯一 ID
  157. - `audio` (string) 结束事件是没有音频的,所以这里是空字符串
  158. - `created_at` (int) 创建时间戳,如:1705395332
  159. - `event: ping` 每 10s 一次的 ping 事件,保持连接存活。
  160. ### Errors
  161. - 400,`invalid_param`,传入参数异常
  162. - 400,`app_unavailable`,App 配置不可用
  163. - 400,`provider_not_initialize`,无可用模型凭据配置
  164. - 400,`provider_quota_exceeded`,模型调用额度不足
  165. - 400,`model_currently_not_support`,当前模型不可用
  166. - 400,`workflow_request_error`,workflow 执行失败
  167. - 500,服务内部异常
  168. </Col>
  169. <Col sticky>
  170. <CodeGroup title="Request" tag="POST" label="/workflows/run" targetCode={`curl -X POST '${props.appDetail.api_base_url}/workflows/run' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "inputs": ${JSON.stringify(props.inputs)},\n "response_mode": "streaming",\n "user": "abc-123"\n}'\n`}>
  171. ```bash {{ title: 'cURL' }}
  172. curl -X POST '${props.appDetail.api_base_url}/workflows/run' \
  173. --header 'Authorization: Bearer {api_key}' \
  174. --header 'Content-Type: application/json' \
  175. --data-raw '{
  176. "inputs": {},
  177. "response_mode": "streaming",
  178. "user": "abc-123"
  179. }'
  180. ```
  181. </CodeGroup>
  182. <CodeGroup title="Example: file array as an input variable">
  183. ```json {{ title: 'File variable example' }}
  184. {
  185. "inputs": {
  186. "{variable_name}":
  187. [
  188. {
  189. "transfer_method": "local_file",
  190. "upload_file_id": "{upload_file_id}",
  191. "type": "{document_type}"
  192. }
  193. ]
  194. }
  195. }
  196. ```
  197. </CodeGroup>
  198. ### Blocking Mode
  199. <CodeGroup title="Response">
  200. ```json {{ title: 'Response' }}
  201. {
  202. "workflow_run_id": "djflajgkldjgd",
  203. "task_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  204. "data": {
  205. "id": "fdlsjfjejkghjda",
  206. "workflow_id": "fldjaslkfjlsda",
  207. "status": "succeeded",
  208. "outputs": {
  209. "text": "Nice to meet you."
  210. },
  211. "error": null,
  212. "elapsed_time": 0.875,
  213. "total_tokens": 3562,
  214. "total_steps": 8,
  215. "created_at": 1705407629,
  216. "finished_at": 1727807631
  217. }
  218. }
  219. ```
  220. </CodeGroup>
  221. ### Streaming Mode
  222. <CodeGroup title="Response">
  223. ```streaming {{ title: 'Response' }}
  224. 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", "created_at": 1679586595}}
  225. 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}}
  226. 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}}
  227. 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}}
  228. 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"}
  229. 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": ""}
  230. ```
  231. </CodeGroup>
  232. <CodeGroup title="File upload sample code">
  233. ```json {{ title: 'File upload sample code' }}
  234. import requests
  235. import json
  236. def upload_file(file_path, user):
  237. upload_url = "https://api.dify.ai/v1/files/upload"
  238. headers = {
  239. "Authorization": "Bearer app-xxxxxxxx",
  240. }
  241. try:
  242. print("上传文件中...")
  243. with open(file_path, 'rb') as file:
  244. files = {
  245. 'file': (file_path, file, 'text/plain') # 确保文件以适当的MIME类型上传
  246. }
  247. data = {
  248. "user": user,
  249. "type": "TXT" # 设置文件类型为TXT
  250. }
  251. response = requests.post(upload_url, headers=headers, files=files, data=data)
  252. if response.status_code == 201: # 201 表示创建成功
  253. print("文件上传成功")
  254. return response.json().get("id") # 获取上传的文件 ID
  255. else:
  256. print(f"文件上传失败,状态码: {response.status_code}")
  257. return None
  258. except Exception as e:
  259. print(f"发生错误: {str(e)}")
  260. return None
  261. def run_workflow(file_id, user, response_mode="blocking"):
  262. workflow_url = "https://api.dify.ai/v1/workflows/run"
  263. headers = {
  264. "Authorization": "Bearer app-xxxxxxxxx",
  265. "Content-Type": "application/json"
  266. }
  267. data = {
  268. "inputs": {
  269. "orig_mail": [{
  270. "transfer_method": "local_file",
  271. "upload_file_id": file_id,
  272. "type": "document"
  273. }]
  274. },
  275. "response_mode": response_mode,
  276. "user": user
  277. }
  278. try:
  279. print("运行工作流...")
  280. response = requests.post(workflow_url, headers=headers, json=data)
  281. if response.status_code == 200:
  282. print("工作流执行成功")
  283. return response.json()
  284. else:
  285. print(f"工作流执行失败,状态码: {response.status_code}")
  286. return {"status": "error", "message": f"Failed to execute workflow, status code: {response.status_code}"}
  287. except Exception as e:
  288. print(f"发生错误: {str(e)}")
  289. return {"status": "error", "message": str(e)}
  290. # 使用示例
  291. file_path = "{your_file_path}"
  292. user = "difyuser"
  293. # 上传文件
  294. file_id = upload_file(file_path, user)
  295. if file_id:
  296. # 文件上传成功,继续运行工作流
  297. result = run_workflow(file_id, user)
  298. print(result)
  299. else:
  300. print("文件上传失败,无法执行工作流")
  301. ```
  302. </CodeGroup>
  303. </Col>
  304. </Row>
  305. ---
  306. <Heading
  307. url='/workflows/run/:workflow_run_id'
  308. method='GET'
  309. title='获取workflow执行情况'
  310. name='#get-workflow-run-detail'
  311. />
  312. <Row>
  313. <Col>
  314. 根据 workflow 执行 ID 获取 workflow 任务当前执行结果
  315. ### Path
  316. - `workflow_run_id` (string) workflow 执行 ID,可在流式返回 Chunk 中获取
  317. ### Response
  318. - `id` (string) workflow 执行 ID
  319. - `workflow_id` (string) 关联的 Workflow ID
  320. - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped`
  321. - `inputs` (json) 任务输入内容
  322. - `outputs` (json) 任务输出内容
  323. - `error` (string) 错误原因
  324. - `total_steps` (int) 任务执行总步数
  325. - `total_tokens` (int) 任务执行总 tokens
  326. - `created_at` (timestamp) 任务开始时间
  327. - `finished_at` (timestamp) 任务结束时间
  328. - `elapsed_time` (float) 耗时 (s)
  329. </Col>
  330. <Col sticky>
  331. ### Request Example
  332. <CodeGroup title="Request" tag="GET" label="/workflows/run/:workflow_run_id" targetCode={`curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_run_id' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json'`}>
  333. ```bash {{ title: 'cURL' }}
  334. curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_run_id' \
  335. -H 'Authorization: Bearer {api_key}' \
  336. -H 'Content-Type: application/json'
  337. ```
  338. </CodeGroup>
  339. ### Response Example
  340. <CodeGroup title="Response">
  341. ```json {{ title: 'Response' }}
  342. {
  343. "id": "b1ad3277-089e-42c6-9dff-6820d94fbc76",
  344. "workflow_id": "19eff89f-ec03-4f75-b0fc-897e7effea02",
  345. "status": "succeeded",
  346. "inputs": "{\"sys.files\": [], \"sys.user_id\": \"abc-123\"}",
  347. "outputs": null,
  348. "error": null,
  349. "total_steps": 3,
  350. "total_tokens": 0,
  351. "created_at": 1705407629,
  352. "finished_at": 1727807631,
  353. "elapsed_time": 30.098514399956912
  354. }
  355. ```
  356. </CodeGroup>
  357. </Col>
  358. </Row>
  359. ---
  360. <Heading
  361. url='/workflows/tasks/:task_id/stop'
  362. method='POST'
  363. title='停止响应'
  364. name='#stop-generatebacks'
  365. />
  366. <Row>
  367. <Col>
  368. 仅支持流式模式。
  369. ### Path
  370. - `task_id` (string) 任务 ID,可在流式返回 Chunk 中获取
  371. ### Request Body
  372. - `user` (string) Required
  373. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。API 无法访问 WebApp 创建的会话。
  374. ### Response
  375. - `result` (string) 固定返回 "success"
  376. </Col>
  377. <Col sticky>
  378. ### Request Example
  379. <CodeGroup title="Request" tag="POST" label="/workflows/tasks/:task_id/stop" targetCode={`curl -X POST '${props.appDetail.api_base_url}/workflows/tasks/:task_id/stop' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json' \\\n--data-raw '{"user": "abc-123"}'`}>
  380. ```bash {{ title: 'cURL' }}
  381. curl -X POST '${props.appDetail.api_base_url}/workflows/tasks/:task_id/stop' \
  382. -H 'Authorization: Bearer {api_key}' \
  383. -H 'Content-Type: application/json' \
  384. --data-raw '{
  385. "user": "abc-123"
  386. }'
  387. ```
  388. </CodeGroup>
  389. ### Response Example
  390. <CodeGroup title="Response">
  391. ```json {{ title: 'Response' }}
  392. {
  393. "result": "success"
  394. }
  395. ```
  396. </CodeGroup>
  397. </Col>
  398. </Row>
  399. ---
  400. <Heading
  401. url='/files/upload'
  402. method='POST'
  403. title='上传文件'
  404. name='#files-upload'
  405. />
  406. <Row>
  407. <Col>
  408. 上传文件并在发送消息时使用,可实现图文多模态理解。
  409. 支持您的工作流程所支持的任何格式。
  410. <i>上传的文件仅供当前终端用户使用。</i>
  411. ### Request Body
  412. 该接口需使用 `multipart/form-data` 进行请求。
  413. <Properties>
  414. <Property name='file' type='file' key='file'>
  415. 要上传的文件。
  416. </Property>
  417. <Property name='user' type='string' key='user'>
  418. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。服务 API 不会共享 WebApp 创建的对话。
  419. </Property>
  420. </Properties>
  421. ### Response
  422. 成功上传后,服务器会返回文件的 ID 和相关信息。
  423. - `id` (uuid) ID
  424. - `name` (string) 文件名
  425. - `size` (int) 文件大小(byte)
  426. - `extension` (string) 文件后缀
  427. - `mime_type` (string) 文件 mime-type
  428. - `created_by` (uuid) 上传人 ID
  429. - `created_at` (timestamp) 上传时间
  430. ### Errors
  431. - 400,`no_file_uploaded`,必须提供文件
  432. - 400,`too_many_files`,目前只接受一个文件
  433. - 400,`unsupported_preview`,该文件不支持预览
  434. - 400,`unsupported_estimate`,该文件不支持估算
  435. - 413,`file_too_large`,文件太大
  436. - 415,`unsupported_file_type`,不支持的扩展名,当前只接受文档类文件
  437. - 503,`s3_connection_failed`,无法连接到 S3 服务
  438. - 503,`s3_permission_denied`,无权限上传文件到 S3
  439. - 503,`s3_file_too_large`,文件超出 S3 大小限制
  440. </Col>
  441. <Col sticky>
  442. <CodeGroup title="Request" tag="POST" label="/files/upload" targetCode={`curl -X POST '${props.appDetail.api_base_url}/files/upload' \\\n--header 'Authorization: Bearer {api_key}' \\\n--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif]' \\\n--form 'user=abc-123'`}>
  443. ```bash {{ title: 'cURL' }}
  444. curl -X POST '${props.appDetail.api_base_url}/files/upload' \
  445. --header 'Authorization: Bearer {api_key}' \
  446. --form 'file=@"/path/to/file"'
  447. ```
  448. </CodeGroup>
  449. <CodeGroup title="Response">
  450. ```json {{ title: 'Response' }}
  451. {
  452. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  453. "name": "example.png",
  454. "size": 1024,
  455. "extension": "png",
  456. "mime_type": "image/png",
  457. "created_by": 123,
  458. "created_at": 1577836800,
  459. }
  460. ```
  461. </CodeGroup>
  462. </Col>
  463. </Row>
  464. ---
  465. <Heading
  466. url='/workflows/logs'
  467. method='GET'
  468. title='获取 workflow 日志'
  469. name='#Get-Workflow-Logs'
  470. />
  471. <Row>
  472. <Col>
  473. 倒序返回 workflow 日志
  474. ### Query
  475. <Properties>
  476. <Property name='keyword' type='string' key='keyword'>
  477. 关键字
  478. </Property>
  479. <Property name='status' type='string' key='status'>
  480. 执行状态 succeeded/failed/stopped
  481. </Property>
  482. <Property name='page' type='int' key='page'>
  483. 当前页码, 默认1.
  484. </Property>
  485. <Property name='limit' type='int' key='limit'>
  486. 每页条数, 默认20.
  487. </Property>
  488. <Property name='created_by_end_user_session_id' type='str' key='created_by_end_user_session_id'>
  489. 由哪个endUser创建,例如,`abc-123`.
  490. </Property>
  491. <Property name='created_by_account' type='str' key='created_by_account'>
  492. 由哪个邮箱账户创建,例如,lizb@test.com.
  493. </Property>
  494. </Properties>
  495. ### Response
  496. - `page` (int) 当前页码
  497. - `limit` (int) 每页条数
  498. - `total` (int) 总条数
  499. - `has_more` (bool) 是否还有更多数据
  500. - `data` (array[object]) 当前页码的数据
  501. - `id` (string) 标识
  502. - `workflow_run` (object) Workflow 执行日志
  503. - `id` (string) 标识
  504. - `version` (string) 版本
  505. - `status` (string) 执行状态,`running` / `succeeded` / `failed` / `stopped`
  506. - `error` (string) (可选) 错误
  507. - `elapsed_time` (float) 耗时,单位秒
  508. - `total_tokens` (int) 消耗的 token 数量
  509. - `total_steps` (int) 执行步骤长度
  510. - `created_at` (timestamp) 开始时间
  511. - `finished_at` (timestamp) 结束时间
  512. - `created_from` (string) 来源
  513. - `created_by_role` (string) 角色
  514. - `created_by_account` (string) (可选) 帐号
  515. - `created_by_end_user` (object) 用户
  516. - `id` (string) 标识
  517. - `type` (string) 类型
  518. - `is_anonymous` (bool) 是否匿名
  519. - `session_id` (string) 会话标识
  520. - `created_at` (timestamp) 创建时间
  521. </Col>
  522. <Col sticky>
  523. <CodeGroup title="Request" tag="GET" label="/workflows/logs" targetCode={`curl -X GET '${props.appDetail.api_base_url}/workflows/logs'\\\n --header 'Authorization: Bearer {api_key}'`}>
  524. ```bash {{ title: 'cURL' }}
  525. curl -X GET '${props.appDetail.api_base_url}/workflows/logs?limit=1'
  526. --header 'Authorization: Bearer {api_key}'
  527. ```
  528. </CodeGroup>
  529. ### Response Example
  530. <CodeGroup title="Response">
  531. ```json {{ title: 'Response' }}
  532. {
  533. "page": 1,
  534. "limit": 1,
  535. "total": 7,
  536. "has_more": true,
  537. "data": [
  538. {
  539. "id": "e41b93f1-7ca2-40fd-b3a8-999aeb499cc0",
  540. "workflow_run": {
  541. "id": "c0640fc8-03ef-4481-a96c-8a13b732a36e",
  542. "version": "2024-08-01 12:17:09.771832",
  543. "status": "succeeded",
  544. "error": null,
  545. "elapsed_time": 1.3588523610014818,
  546. "total_tokens": 0,
  547. "total_steps": 3,
  548. "created_at": 1726139643,
  549. "finished_at": 1726139644
  550. },
  551. "created_from": "service-api",
  552. "created_by_role": "end_user",
  553. "created_by_account": null,
  554. "created_by_end_user": {
  555. "id": "7f7d9117-dd9d-441d-8970-87e5e7e687a3",
  556. "type": "service_api",
  557. "is_anonymous": false,
  558. "session_id": "abc-123"
  559. },
  560. "created_at": 1726139644
  561. }
  562. ]
  563. }
  564. ```
  565. </CodeGroup>
  566. </Col>
  567. </Row>
  568. ---
  569. <Heading
  570. url='/info'
  571. method='GET'
  572. title='获取应用基本信息'
  573. name='#info'
  574. />
  575. <Row>
  576. <Col>
  577. 用于获取应用的基本信息
  578. ### Response
  579. - `name` (string) 应用名称
  580. - `description` (string) 应用描述
  581. - `tags` (array[string]) 应用标签
  582. - `mode` (string) 应用模式
  583. - 'author_name' (string) 作者名称
  584. </Col>
  585. <Col>
  586. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info' \\\n-H 'Authorization: Bearer {api_key}'`}>
  587. ```bash {{ title: 'cURL' }}
  588. curl -X GET '${props.appDetail.api_base_url}/info' \
  589. -H 'Authorization: Bearer {api_key}'
  590. ```
  591. </CodeGroup>
  592. <CodeGroup title="Response">
  593. ```json {{ title: 'Response' }}
  594. {
  595. "name": "My App",
  596. "description": "This is my app.",
  597. "tags": [
  598. "tag1",
  599. "tag2"
  600. ],
  601. "mode": "workflow",
  602. "author_name": "Dify"
  603. }
  604. ```
  605. </CodeGroup>
  606. </Col>
  607. </Row>
  608. ---
  609. <Heading
  610. url='/parameters'
  611. method='GET'
  612. title='获取应用参数'
  613. name='#parameters'
  614. />
  615. <Row>
  616. <Col>
  617. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  618. ### Response
  619. - `user_input_form` (array[object]) 用户输入表单配置
  620. - `text-input` (object) 文本输入控件
  621. - `label` (string) 控件展示标签名
  622. - `variable` (string) 控件 ID
  623. - `required` (bool) 是否必填
  624. - `default` (string) 默认值
  625. - `paragraph` (object) 段落文本输入控件
  626. - `label` (string) 控件展示标签名
  627. - `variable` (string) 控件 ID
  628. - `required` (bool) 是否必填
  629. - `default` (string) 默认值
  630. - `select` (object) 下拉控件
  631. - `label` (string) 控件展示标签名
  632. - `variable` (string) 控件 ID
  633. - `required` (bool) 是否必填
  634. - `default` (string) 默认值
  635. - `options` (array[string]) 选项值
  636. - `file_upload` (object) 文件上传配置
  637. - `document` (object) 文档设置
  638. 当前仅支持文档类型:`txt`, `md`, `markdown`, `pdf`, `html`, `xlsx`, `xls`, `docx`, `csv`, `eml`, `msg`, `pptx`, `ppt`, `xml`, `epub`。
  639. - `enabled` (bool) 是否启用
  640. - `number_limits` (int) 文档数量限制,默认为 3
  641. - `transfer_methods` (array[string]) 传输方式列表:`remote_url`, `local_file`,必须选择一个。
  642. - `image` (object) 图片设置
  643. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`。
  644. - `enabled` (bool) 是否启用
  645. - `number_limits` (int) 图片数量限制,默认为 3
  646. - `transfer_methods` (array[string]) 传输方式列表:`remote_url`, `local_file`,必须选择一个。
  647. - `audio` (object) 音频设置
  648. 当前仅支持音频类型:`mp3`, `m4a`, `wav`, `webm`, `amr`。
  649. - `enabled` (bool) 是否启用
  650. - `number_limits` (int) 音频数量限制,默认为 3
  651. - `transfer_methods` (array[string]) 传输方式列表:`remote_url`, `local_file`,必须选择一个。
  652. - `video` (object) 视频设置
  653. 当前仅支持视频类型:`mp4`, `mov`, `mpeg`, `mpga`。
  654. - `enabled` (bool) 是否启用
  655. - `number_limits` (int) 视频数量限制,默认为 3
  656. - `transfer_methods` (array[string]) 传输方式列表:`remote_url`, `local_file`,必须选择一个。
  657. - `custom` (object) 自定义设置
  658. - `enabled` (bool) 是否启用
  659. - `number_limits` (int) 自定义数量限制,默认为 3
  660. - `transfer_methods` (array[string]) 传输方式列表:`remote_url`, `local_file`,必须选择一个。
  661. - `system_parameters` (object) 系统参数
  662. - `file_size_limit` (int) 文档上传大小限制 (MB)
  663. - `image_file_size_limit` (int) 图片文件上传大小限制(MB)
  664. - `audio_file_size_limit` (int) 音频文件上传大小限制 (MB)
  665. - `video_file_size_limit` (int) 视频文件上传大小限制 (MB)
  666. </Col>
  667. <Col sticky>
  668. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'`}>
  669. ```bash {{ title: 'cURL' }}
  670. curl -X GET '${props.appDetail.api_base_url}/parameters' \
  671. --header 'Authorization: Bearer {api_key}'
  672. ```
  673. </CodeGroup>
  674. <CodeGroup title="Response">
  675. ```json {{ title: 'Response' }}
  676. {
  677. "user_input_form": [
  678. {
  679. "paragraph": {
  680. "label": "Query",
  681. "variable": "query",
  682. "required": true,
  683. "default": ""
  684. }
  685. }
  686. ],
  687. "file_upload": {
  688. "image": {
  689. "enabled": false,
  690. "number_limits": 3,
  691. "detail": "high",
  692. "transfer_methods": [
  693. "remote_url",
  694. "local_file"
  695. ]
  696. }
  697. },
  698. "system_parameters": {
  699. "file_size_limit": 15,
  700. "image_file_size_limit": 10,
  701. "audio_file_size_limit": 50,
  702. "video_file_size_limit": 100
  703. }
  704. }
  705. ```
  706. </CodeGroup>
  707. </Col>
  708. </Row>
  709. ---
  710. <Heading
  711. url='/site'
  712. method='GET'
  713. title='获取应用 WebApp 设置'
  714. name='#site'
  715. />
  716. <Row>
  717. <Col>
  718. 用于获取应用的 WebApp 设置
  719. ### Response
  720. - `title` (string) WebApp 名称
  721. - `icon_type` (string) 图标类型,`emoji`-表情,`image`-图片
  722. - `icon` (string) 图标,如果是 `emoji` 类型,则是 emoji 表情符号,如果是 `image` 类型,则是图片 URL
  723. - `icon_background` (string) hex 格式的背景色
  724. - `icon_url` (string) 图标 URL
  725. - `description` (string) 描述
  726. - `copyright` (string) 版权信息
  727. - `privacy_policy` (string) 隐私政策链接
  728. - `custom_disclaimer` (string) 自定义免责声明
  729. - `default_language` (string) 默认语言
  730. - `show_workflow_steps` (bool) 是否显示工作流详情
  731. </Col>
  732. <Col>
  733. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/site' \\\n-H 'Authorization: Bearer {api_key}'`}>
  734. ```bash {{ title: 'cURL' }}
  735. curl -X GET '${props.appDetail.api_base_url}/site' \
  736. -H 'Authorization: Bearer {api_key}'
  737. ```
  738. </CodeGroup>
  739. <CodeGroup title="Response">
  740. ```json {{ title: 'Response' }}
  741. {
  742. "title": "My App",
  743. "icon_type": "emoji",
  744. "icon": "😄",
  745. "icon_background": "#FFEAD5",
  746. "icon_url": null,
  747. "description": "This is my app.",
  748. "copyright": "all rights reserved",
  749. "privacy_policy": "",
  750. "custom_disclaimer": "All generated by AI",
  751. "default_language": "en-US",
  752. "show_workflow_steps": false,
  753. }
  754. ```
  755. </CodeGroup>
  756. </Col>
  757. </Row>
  758. ___