template_workflow.en.mdx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Workflow App API
  4. Workflow applications offers non-session support and is ideal for translation, article writing, summarization AI, and more.
  5. <div>
  6. ### Base URL
  7. <CodeGroup title="Code" targetCode={props.appDetail.api_base_url}>
  8. ```javascript
  9. ```
  10. </CodeGroup>
  11. ### Authentication
  12. The Service API uses `API-Key` authentication.
  13. <i>**Strongly recommend storing your API Key on the server-side, not shared or stored on the client-side, to avoid possible API-Key leakage that can lead to serious consequences.**</i>
  14. For all API requests, include your API Key in the `Authorization` HTTP Header, as shown below:
  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='Execute workflow'
  26. name='#Execute-Workflow'
  27. />
  28. <Row>
  29. <Col>
  30. Execute workflow, cannot be executed without a published workflow.
  31. ### Request Body
  32. - `inputs` (object) Required
  33. Allows the entry of various variable values defined by the App.
  34. The `inputs` parameter contains multiple key/value pairs, with each key corresponding to a specific variable and each value being the specific value for that variable.
  35. The workflow application requires at least one key/value pair to be inputted.
  36. - `response_mode` (string) Required
  37. The mode of response return, supporting:
  38. - `streaming` Streaming mode (recommended), implements a typewriter-like output through SSE ([Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)).
  39. - `blocking` Blocking mode, returns result after execution is complete. (Requests may be interrupted if the process is long)
  40. <i>Due to Cloudflare restrictions, the request will be interrupted without a return after 100 seconds.</i>
  41. - `user` (string) Required
  42. User identifier, used to define the identity of the end-user for retrieval and statistics.
  43. Should be uniquely defined by the developer within the application.
  44. - `files` (array[object]) Optional
  45. File list, suitable for inputting files (images) combined with text understanding and answering questions, available only when the model supports Vision capability.
  46. - `type` (string) Supported type: `image` (currently only supports image type)
  47. - `transfer_method` (string) Transfer method, `remote_url` for image URL / `local_file` for file upload
  48. - `url` (string) Image URL (when the transfer method is `remote_url`)
  49. - `upload_file_id` (string) Uploaded file ID, which must be obtained by uploading through the File Upload API in advance (when the transfer method is `local_file`)
  50. ### Response
  51. When `response_mode` is `blocking`, return a CompletionResponse object.
  52. When `response_mode` is `streaming`, return a ChunkCompletionResponse stream.
  53. ### CompletionResponse
  54. Returns the App result, `Content-Type` is `application/json`.
  55. - `workflow_run_id` (string) Unique ID of workflow execution
  56. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  57. - `data` (object) detail of result
  58. - `id` (string) ID of workflow execution
  59. - `workflow_id` (string) ID of related workflow
  60. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  61. - `outputs` (json) Optional content of output
  62. - `error` (string) Optional reason of error
  63. - `elapsed_time` (float) Optional total seconds to be used
  64. - `total_tokens` (int) Optional tokens to be used
  65. - `total_steps` (int) default 0
  66. - `created_at` (timestamp) start time
  67. - `finished_at` (timestamp) end time
  68. ### ChunkCompletionResponse
  69. Returns the stream chunks outputted by the App, `Content-Type` is `text/event-stream`.
  70. Each streaming chunk starts with `data:`, separated by two newline characters `\n\n`, as shown below:
  71. <CodeGroup>
  72. ```streaming {{ title: 'Response' }}
  73. data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "663c5084-a254-4040-8ad3-51f2a3c1a77c", "answer": "Hi", "created_at": 1705398420}\n\n
  74. ```
  75. </CodeGroup>
  76. The structure of the streaming chunks varies depending on the `event`:
  77. - `event: workflow_started` workflow starts execution
  78. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  79. - `workflow_run_id` (string) Unique ID of workflow execution
  80. - `event` (string) fixed to `workflow_started`
  81. - `data` (object) detail
  82. - `id` (string) Unique ID of workflow execution
  83. - `workflow_id` (string) ID of related workflow
  84. - `sequence_number` (int) Self-increasing serial number, self-increasing in the App, starting from 1
  85. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  86. - `event: node_started` node execution started
  87. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  88. - `workflow_run_id` (string) Unique ID of workflow execution
  89. - `event` (string) fixed to `node_started`
  90. - `data` (object) detail
  91. - `id` (string) Unique ID of workflow execution
  92. - `node_id` (string) ID of node
  93. - `node_type` (string) type of node
  94. - `title` (string) name of node
  95. - `index` (int) Execution sequence number, used to display Tracing Node sequence
  96. - `predecessor_node_id` (string) optional Prefix node ID, used for canvas display execution path
  97. - `inputs` (array[object]) Contents of all preceding node variables used in the node
  98. - `created_at` (timestamp) timestamp of start, e.g., 1705395332
  99. - `event: node_finished` node execution ends, success or failure in different states in the same event
  100. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  101. - `workflow_run_id` (string) Unique ID of workflow execution
  102. - `event` (string) fixed to `node_finished`
  103. - `data` (object) detail
  104. - `id` (string) Unique ID of workflow execution
  105. - `node_id` (string) ID of node
  106. - `node_type` (string) type of node
  107. - `title` (string) name of node
  108. - `index` (int) Execution sequence number, used to display Tracing Node sequence
  109. - `predecessor_node_id` (string) optional Prefix node ID, used for canvas display execution path
  110. - `inputs` (array[object]) Contents of all preceding node variables used in the node
  111. - `process_data` (json) Optional node process data
  112. - `outputs` (json) Optional content of output
  113. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  114. - `error` (string) Optional reason of error
  115. - `elapsed_time` (float) Optional total seconds to be used
  116. - `execution_metadata` (json) meta data
  117. - `total_tokens` (int) optional tokens to be used
  118. - `total_price` (decimal) optional Total cost
  119. - `currency` (string) optional e.g. `USD` / `RMB`
  120. - `created_at` (timestamp) timestamp of start, e.g., 1705395332
  121. - `event: workflow_finished` workflow execution ends, success or failure in different states in the same event
  122. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  123. - `workflow_run_id` (string) Unique ID of workflow execution
  124. - `event` (string) fixed to `workflow_finished`
  125. - `data` (object) detail
  126. - `id` (string) ID of workflow execution
  127. - `workflow_id` (string) ID of related workflow
  128. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  129. - `outputs` (json) Optional content of output
  130. - `error` (string) Optional reason of error
  131. - `elapsed_time` (float) Optional total seconds to be used
  132. - `total_tokens` (int) Optional tokens to be used
  133. - `total_steps` (int) default 0
  134. - `created_at` (timestamp) start time
  135. - `finished_at` (timestamp) end time
  136. - `event: tts_message` TTS audio stream event, that is, speech synthesis output. The content is an audio block in Mp3 format, encoded as a base64 string. When playing, simply decode the base64 and feed it into the player. (This message is available only when auto-play is enabled)
  137. - `task_id` (string) Task ID, used for request tracking and the stop response interface below
  138. - `message_id` (string) Unique message ID
  139. - `audio` (string) The audio after speech synthesis, encoded in base64 text content, when playing, simply decode the base64 and feed it into the player
  140. - `created_at` (int) Creation timestamp, e.g.: 1705395332
  141. - `event: tts_message_end` TTS audio stream end event, receiving this event indicates the end of the audio stream.
  142. - `task_id` (string) Task ID, used for request tracking and the stop response interface below
  143. - `message_id` (string) Unique message ID
  144. - `audio` (string) The end event has no audio, so this is an empty string
  145. - `created_at` (int) Creation timestamp, e.g.: 1705395332
  146. - `event: ping` Ping event every 10 seconds to keep the connection alive.
  147. ### Errors
  148. - 400, `invalid_param`, abnormal parameter input
  149. - 400, `app_unavailable`, App configuration unavailable
  150. - 400, `provider_not_initialize`, no available model credential configuration
  151. - 400, `provider_quota_exceeded`, model invocation quota insufficient
  152. - 400, `model_currently_not_support`, current model unavailable
  153. - 400, `workflow_request_error`, workflow execution failed
  154. - 500, internal server error
  155. </Col>
  156. <Col sticky>
  157. <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`}>
  158. ```bash {{ title: 'cURL' }}
  159. curl -X POST '${props.appDetail.api_base_url}/workflows/run' \
  160. --header 'Authorization: Bearer {api_key}' \
  161. --header 'Content-Type: application/json' \
  162. --data-raw '{
  163. "inputs": {},
  164. "response_mode": "streaming",
  165. "user": "abc-123"
  166. }'
  167. ```
  168. </CodeGroup>
  169. ### Blocking Mode
  170. <CodeGroup title="Response">
  171. ```json {{ title: 'Response' }}
  172. {
  173. "workflow_run_id": "djflajgkldjgd",
  174. "task_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  175. "data": {
  176. "id": "fdlsjfjejkghjda",
  177. "workflow_id": "fldjaslkfjlsda",
  178. "status": "succeeded",
  179. "outputs": {
  180. "text": "Nice to meet you."
  181. },
  182. "error": null,
  183. "elapsed_time": 0.875,
  184. "total_tokens": 3562,
  185. "total_steps": 8,
  186. "created_at": 1705407629,
  187. "finished_at": 1727807631
  188. }
  189. }
  190. ```
  191. </CodeGroup>
  192. ### Streaming Mode
  193. <CodeGroup title="Response">
  194. ```streaming {{ title: 'Response' }}
  195. 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}}
  196. 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}}
  197. 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}}
  198. 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}}
  199. 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"}
  200. 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": ""}
  201. ```
  202. </CodeGroup>
  203. </Col>
  204. </Row>
  205. ---
  206. <Heading
  207. url='/workflows/run/:workflow_id'
  208. method='GET'
  209. title='Get Workflow Run Detail'
  210. name='#get-workflow-run-detail'
  211. />
  212. <Row>
  213. <Col>
  214. Retrieve the current execution results of a workflow task based on the workflow execution ID.
  215. ### Path
  216. - `workflow_id` (string) Workflow ID, can be obtained from the streaming chunk return
  217. ### Response
  218. - `id` (string) ID of workflow execution
  219. - `workflow_id` (string) ID of related workflow
  220. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  221. - `inputs` (json) content of input
  222. - `outputs` (json) content of output
  223. - `error` (string) reason of error
  224. - `total_steps` (int) total steps of task
  225. - `total_tokens` (int) total tokens to be used
  226. - `created_at` (timestamp) start time
  227. - `finished_at` (timestamp) end time
  228. - `elapsed_time` (float) total seconds to be used
  229. </Col>
  230. <Col sticky>
  231. ### Request Example
  232. <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'`}>
  233. ```bash {{ title: 'cURL' }}
  234. curl -X GET '${props.appDetail.api_base_url}/workflows/run/:workflow_id' \
  235. -H 'Authorization: Bearer {api_key}' \
  236. -H 'Content-Type: application/json'
  237. ```
  238. </CodeGroup>
  239. ### Response Example
  240. <CodeGroup title="Response">
  241. ```json {{ title: 'Response' }}
  242. {
  243. "id": "b1ad3277-089e-42c6-9dff-6820d94fbc76",
  244. "workflow_id": "19eff89f-ec03-4f75-b0fc-897e7effea02",
  245. "status": "succeeded",
  246. "inputs": "{\"sys.files\": [], \"sys.user_id\": \"abc-123\"}",
  247. "outputs": null,
  248. "error": null,
  249. "total_steps": 3,
  250. "total_tokens": 0,
  251. "created_at": "Thu, 18 Jul 2024 03:17:40 -0000",
  252. "finished_at": "Thu, 18 Jul 2024 03:18:10 -0000",
  253. "elapsed_time": 30.098514399956912
  254. }
  255. ```
  256. </CodeGroup>
  257. </Col>
  258. </Row>
  259. ---
  260. <Heading
  261. url='/workflows/tasks/:task_id/stop'
  262. method='POST'
  263. title='Stop Generate'
  264. name='#stop-generatebacks'
  265. />
  266. <Row>
  267. <Col>
  268. Only supported in streaming mode.
  269. ### Path
  270. - `task_id` (string) Task ID, can be obtained from the streaming chunk return
  271. ### Request Body
  272. - `user` (string) Required
  273. User identifier, used to define the identity of the end-user, must be consistent with the user passed in the send message interface.
  274. ### Response
  275. - `result` (string) Always returns "success"
  276. </Col>
  277. <Col sticky>
  278. ### Request Example
  279. <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"}'`}>
  280. ```bash {{ title: 'cURL' }}
  281. curl -X POST '${props.appDetail.api_base_url}/workflows/tasks/:task_id/stop' \
  282. -H 'Authorization: Bearer {api_key}' \
  283. -H 'Content-Type: application/json' \
  284. --data-raw '{
  285. "user": "abc-123"
  286. }'
  287. ```
  288. </CodeGroup>
  289. ### Response Example
  290. <CodeGroup title="Response">
  291. ```json {{ title: 'Response' }}
  292. {
  293. "result": "success"
  294. }
  295. ```
  296. </CodeGroup>
  297. </Col>
  298. </Row>
  299. ---
  300. <Heading
  301. url='/parameters'
  302. method='GET'
  303. title='Get Application Information'
  304. name='#parameters'
  305. />
  306. <Row>
  307. <Col>
  308. Used at the start of entering the page to obtain information such as features, input parameter names, types, and default values.
  309. ### Query
  310. <Properties>
  311. <Property name='user' type='string' key='user'>
  312. User identifier, defined by the developer's rules, must be unique within the application.
  313. </Property>
  314. </Properties>
  315. ### Response
  316. - `user_input_form` (array[object]) User input form configuration
  317. - `text-input` (object) Text input control
  318. - `label` (string) Variable display label name
  319. - `variable` (string) Variable ID
  320. - `required` (bool) Whether it is required
  321. - `default` (string) Default value
  322. - `paragraph` (object) Paragraph text input control
  323. - `label` (string) Variable display label name
  324. - `variable` (string) Variable ID
  325. - `required` (bool) Whether it is required
  326. - `default` (string) Default value
  327. - `select` (object) Dropdown control
  328. - `label` (string) Variable display label name
  329. - `variable` (string) Variable ID
  330. - `required` (bool) Whether it is required
  331. - `default` (string) Default value
  332. - `options` (array[string]) Option values
  333. - `file_upload` (object) File upload configuration
  334. - `image` (object) Image settings
  335. Currently only supports image types: `png`, `jpg`, `jpeg`, `webp`, `gif`
  336. - `enabled` (bool) Whether it is enabled
  337. - `number_limits` (int) Image number limit, default is 3
  338. - `transfer_methods` (array[string]) List of transfer methods, remote_url, local_file, must choose one
  339. - `system_parameters` (object) System parameters
  340. - `image_file_size_limit` (string) Image file upload size limit (MB)
  341. </Col>
  342. <Col sticky>
  343. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  344. ```bash {{ title: 'cURL' }}
  345. curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  346. --header 'Authorization: Bearer {api_key}'
  347. ```
  348. </CodeGroup>
  349. <CodeGroup title="Response">
  350. ```json {{ title: 'Response' }}
  351. {
  352. "user_input_form": [
  353. {
  354. "paragraph": {
  355. "label": "Query",
  356. "variable": "query",
  357. "required": true,
  358. "default": ""
  359. }
  360. }
  361. ],
  362. "file_upload": {
  363. "image": {
  364. "enabled": false,
  365. "number_limits": 3,
  366. "detail": "high",
  367. "transfer_methods": [
  368. "remote_url",
  369. "local_file"
  370. ]
  371. }
  372. },
  373. "system_parameters": {
  374. "image_file_size_limit": "10"
  375. }
  376. }
  377. ```
  378. </CodeGroup>
  379. </Col>
  380. </Row>