template_workflow.en.mdx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. - `log_id` (string) Unique log ID
  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 relatied 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 relatied 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 relatied 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: ping` Ping event every 10 seconds to keep the connection alive.
  137. ### Errors
  138. - 400, `invalid_param`, abnormal parameter input
  139. - 400, `app_unavailable`, App configuration unavailable
  140. - 400, `provider_not_initialize`, no available model credential configuration
  141. - 400, `provider_quota_exceeded`, model invocation quota insufficient
  142. - 400, `model_currently_not_support`, current model unavailable
  143. - 400, `workflow_request_error`, workflow execution failed
  144. - 500, internal server error
  145. </Col>
  146. <Col sticky>
  147. <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`}>
  148. ```bash {{ title: 'cURL' }}
  149. curl -X POST '${props.appDetail.api_base_url}/workflows/run' \
  150. --header 'Authorization: Bearer {api_key}' \
  151. --header 'Content-Type: application/json' \
  152. --data-raw '{
  153. "inputs": {},
  154. "response_mode": "streaming",
  155. "user": "abc-123"
  156. }'
  157. ```
  158. </CodeGroup>
  159. ### Blocking Mode
  160. <CodeGroup title="Response">
  161. ```json {{ title: 'Response' }}
  162. {
  163. "log_id": "djflajgkldjgd",
  164. "task_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  165. "data": {
  166. "id": "fdlsjfjejkghjda",
  167. "workflow_id": "fldjaslkfjlsda",
  168. "status": "succeeded",
  169. "outputs": {
  170. "text": "Nice to meet you."
  171. },
  172. "error": null,
  173. "elapsed_time": 0.875,
  174. "total_tokens": 3562,
  175. "total_steps": 8,
  176. "created_at": 1705407629,
  177. "finished_at": 1727807631
  178. }
  179. }
  180. ```
  181. </CodeGroup>
  182. ### Streaming Mode
  183. <CodeGroup title="Response">
  184. ```streaming {{ title: 'Response' }}
  185. 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}}
  186. 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}}
  187. 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}}
  188. 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}}
  189. ```
  190. </CodeGroup>
  191. </Col>
  192. </Row>
  193. ---
  194. <Heading
  195. url='/workflows/:task_id/stop'
  196. method='POST'
  197. title='Stop Generate'
  198. name='#stop-generatebacks'
  199. />
  200. <Row>
  201. <Col>
  202. Only supported in streaming mode.
  203. ### Path
  204. - `task_id` (string) Task ID, can be obtained from the streaming chunk return
  205. ### Request Body
  206. - `user` (string) Required
  207. User identifier, used to define the identity of the end-user, must be consistent with the user passed in the send message interface.
  208. ### Response
  209. - `result` (string) Always returns "success"
  210. </Col>
  211. <Col sticky>
  212. ### Request Example
  213. <CodeGroup title="Request" tag="POST" label="/workflows/:task_id/stop" targetCode={`curl -X POST '${props.appDetail.api_base_url}/workflows/:task_id/stop' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json' \\\n--data-raw '{"user": "abc-123"}'`}>
  214. ```bash {{ title: 'cURL' }}
  215. curl -X POST '${props.appDetail.api_base_url}/workflows/:task_id/stop' \
  216. -H 'Authorization: Bearer {api_key}' \
  217. -H 'Content-Type: application/json' \
  218. --data-raw '{
  219. "user": "abc-123"
  220. }'
  221. ```
  222. </CodeGroup>
  223. ### Response Example
  224. <CodeGroup title="Response">
  225. ```json {{ title: 'Response' }}
  226. {
  227. "result": "success"
  228. }
  229. ```
  230. </CodeGroup>
  231. </Col>
  232. </Row>
  233. ---
  234. <Heading
  235. url='/parameters'
  236. method='GET'
  237. title='Get Application Information'
  238. name='#parameters'
  239. />
  240. <Row>
  241. <Col>
  242. Used at the start of entering the page to obtain information such as features, input parameter names, types, and default values.
  243. ### Query
  244. <Properties>
  245. <Property name='user' type='string' key='user'>
  246. User identifier, defined by the developer's rules, must be unique within the application.
  247. </Property>
  248. </Properties>
  249. ### Response
  250. - `user_input_form` (array[object]) User input form configuration
  251. - `text-input` (object) Text input control
  252. - `label` (string) Variable display label name
  253. - `variable` (string) Variable ID
  254. - `required` (bool) Whether it is required
  255. - `default` (string) Default value
  256. - `paragraph` (object) Paragraph text input control
  257. - `label` (string) Variable display label name
  258. - `variable` (string) Variable ID
  259. - `required` (bool) Whether it is required
  260. - `default` (string) Default value
  261. - `select` (object) Dropdown control
  262. - `label` (string) Variable display label name
  263. - `variable` (string) Variable ID
  264. - `required` (bool) Whether it is required
  265. - `default` (string) Default value
  266. - `options` (array[string]) Option values
  267. - `file_upload` (object) File upload configuration
  268. - `image` (object) Image settings
  269. Currently only supports image types: `png`, `jpg`, `jpeg`, `webp`, `gif`
  270. - `enabled` (bool) Whether it is enabled
  271. - `number_limits` (int) Image number limit, default is 3
  272. - `transfer_methods` (array[string]) List of transfer methods, remote_url, local_file, must choose one
  273. - `system_parameters` (object) System parameters
  274. - `image_file_size_limit` (string) Image file upload size limit (MB)
  275. </Col>
  276. <Col sticky>
  277. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  278. ```bash {{ title: 'cURL' }}
  279. curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  280. --header 'Authorization: Bearer {api_key}'
  281. ```
  282. </CodeGroup>
  283. <CodeGroup title="Response">
  284. ```json {{ title: 'Response' }}
  285. {
  286. "user_input_form": [
  287. {
  288. "paragraph": {
  289. "label": "Query",
  290. "variable": "query",
  291. "required": true,
  292. "default": ""
  293. }
  294. }
  295. ],
  296. "file_upload": {
  297. "image": {
  298. "enabled": false,
  299. "number_limits": 3,
  300. "detail": "high",
  301. "transfer_methods": [
  302. "remote_url",
  303. "local_file"
  304. ]
  305. }
  306. },
  307. "system_parameters": {
  308. "image_file_size_limit": "10"
  309. }
  310. }
  311. ```
  312. </CodeGroup>
  313. </Col>
  314. </Row>