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

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