template_workflow.zh.mdx 29 KB

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