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 31KB

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