template.en.mdx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Completion App API
  4. The text generation application 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='/completion-messages'
  24. method='POST'
  25. title='Create Completion Message'
  26. name='#Create-Completion-Message'
  27. />
  28. <Row>
  29. <Col>
  30. Send a request to the text generation application.
  31. ### Request Body
  32. <Properties>
  33. <Property name='inputs' type='object' key='inputs'>
  34. Allows the entry of various variable values defined by the App.
  35. 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.
  36. The text generation application requires at least one key/value pair to be inputted.
  37. - `query` (string) Required
  38. The input text, the content to be processed.
  39. </Property>
  40. <Property name='response_mode' type='string' key='response_mode'>
  41. The mode of response return, supporting:
  42. - `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)).
  43. - `blocking` Blocking mode, returns result after execution is complete. (Requests may be interrupted if the process is long)
  44. <i>Due to Cloudflare restrictions, the request will be interrupted without a return after 100 seconds.</i>
  45. </Property>
  46. <Property name='user' type='string' key='user'>
  47. User identifier, used to define the identity of the end-user for retrieval and statistics.
  48. Should be uniquely defined by the developer within the application.
  49. </Property>
  50. <Property name='files' type='array[object]' key='files'>
  51. File list, suitable for inputting files (images) combined with text understanding and answering questions, available only when the model supports Vision capability.
  52. - `type` (string) Supported type: `image` (currently only supports image type)
  53. - `transfer_method` (string) Transfer method, `remote_url` for image URL / `local_file` for file upload
  54. - `url` (string) Image URL (when the transfer method is `remote_url`)
  55. - `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`)
  56. </Property>
  57. </Properties>
  58. ### Response
  59. When `response_mode` is `blocking`, return a CompletionResponse object.
  60. When `response_mode` is `streaming`, return a ChunkCompletionResponse stream.
  61. ### ChatCompletionResponse
  62. Returns the complete App result, `Content-Type` is `application/json`.
  63. - `message_id` (string) Unique message ID
  64. - `mode` (string) App mode, fixed as `chat`
  65. - `answer` (string) Complete response content
  66. - `metadata` (object) Metadata
  67. - `usage` (Usage) Model usage information
  68. - `retriever_resources` (array[RetrieverResource]) Citation and Attribution List
  69. - `created_at` (int) Message creation timestamp, e.g., 1705395332
  70. ### ChunkChatCompletionResponse
  71. Returns the stream chunks outputted by the App, `Content-Type` is `text/event-stream`.
  72. Each streaming chunk starts with `data:`, separated by two newline characters `\n\n`, as shown below:
  73. <CodeGroup>
  74. ```streaming {{ title: 'Response' }}
  75. data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "663c5084-a254-4040-8ad3-51f2a3c1a77c", "answer": "Hi", "created_at": 1705398420}\n\n
  76. ```
  77. </CodeGroup>
  78. The structure of the streaming chunks varies depending on the `event`:
  79. - `event: message` LLM returns text chunk event, i.e., the complete text is output in a chunked fashion.
  80. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  81. - `message_id` (string) Unique message ID
  82. - `answer` (string) LLM returned text chunk content
  83. - `created_at` (int) Creation timestamp, e.g., 1705395332
  84. - `event: message_end` Message end event, receiving this event means streaming has ended.
  85. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  86. - `message_id` (string) Unique message ID
  87. - `metadata` (object) Metadata
  88. - `usage` (Usage) Model usage information
  89. - `retriever_resources` (array[RetrieverResource]) Citation and Attribution List
  90. - `event: message_replace` Message content replacement event.
  91. When output content moderation is enabled, if the content is flagged, then the message content will be replaced with a preset reply through this event.
  92. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  93. - `message_id` (string) Unique message ID
  94. - `answer` (string) Replacement content (directly replaces all LLM reply text)
  95. - `created_at` (int) Creation timestamp, e.g., 1705395332
  96. - `event: error`
  97. Exceptions that occur during the streaming process will be output in the form of stream events, and reception of an error event will end the stream.
  98. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  99. - `message_id` (string) Unique message ID
  100. - `status` (int) HTTP status code
  101. - `code` (string) Error code
  102. - `message` (string) Error message
  103. - `event: ping` Ping event every 10 seconds to keep the connection alive.
  104. ### Errors
  105. - 404, Conversation does not exists
  106. - 400, `invalid_param`, abnormal parameter input
  107. - 400, `app_unavailable`, App configuration unavailable
  108. - 400, `provider_not_initialize`, no available model credential configuration
  109. - 400, `provider_quota_exceeded`, model invocation quota insufficient
  110. - 400, `model_currently_not_support`, current model unavailable
  111. - 400, `completion_request_error`, text generation failed
  112. - 500, internal server error
  113. </Col>
  114. <Col sticky>
  115. <CodeGroup title="Request" tag="POST" label="/completion-messages" targetCode={`curl -X POST '${props.appDetail.api_base_url}/completion-messages' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "inputs": {"query": "Hello, world!"},\n "response_mode": "streaming",\n "user": "abc-123"\n}'\n`}>
  116. ```bash {{ title: 'cURL' }}
  117. curl -X POST '${props.appDetail.api_base_url}/completion-messages' \
  118. --header 'Authorization: Bearer {api_key}' \
  119. --header 'Content-Type: application/json' \
  120. --data-raw '{
  121. "inputs": {
  122. "query": "Hello, world!"
  123. },
  124. "response_mode": "streaming",
  125. "user": "abc-123"
  126. }'
  127. ```
  128. </CodeGroup>
  129. ### Blocking Mode
  130. <CodeGroup title="Response">
  131. ```json {{ title: 'Response' }}
  132. {
  133. "event": "message",
  134. "message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  135. "mode": "completion",
  136. "answer": "Hello World!...",
  137. "metadata": {
  138. "usage": {
  139. "prompt_tokens": 1033,
  140. "prompt_unit_price": "0.001",
  141. "prompt_price_unit": "0.001",
  142. "prompt_price": "0.0010330",
  143. "completion_tokens": 128,
  144. "completion_unit_price": "0.002",
  145. "completion_price_unit": "0.001",
  146. "completion_price": "0.0002560",
  147. "total_tokens": 1161,
  148. "total_price": "0.0012890",
  149. "currency": "USD",
  150. "latency": 0.7682376249867957
  151. }
  152. },
  153. "created_at": 1705407629
  154. }
  155. ```
  156. </CodeGroup>
  157. ### Streaming Mode
  158. <CodeGroup title="Response">
  159. ```streaming {{ title: 'Response' }}
  160. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  161. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": "'m", "created_at": 1679586595}
  162. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " glad", "created_at": 1679586595}
  163. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " to", "created_at": 1679586595}
  164. data: {"event": "message", "message_id": : "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " meet", "created_at": 1679586595}
  165. data: {"event": "message", "message_id": : "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " you", "created_at": 1679586595}
  166. data: {"event": "message_end", "id": "5e52ce04-874b-4d27-9045-b3bc80def685", "metadata": {"usage": {"prompt_tokens": 1033, "prompt_unit_price": "0.001", "prompt_price_unit": "0.001", "prompt_price": "0.0010330", "completion_tokens": 135, "completion_unit_price": "0.002", "completion_price_unit": "0.001", "completion_price": "0.0002700", "total_tokens": 1168, "total_price": "0.0013030", "currency": "USD", "latency": 1.381760165997548}}}
  167. ```
  168. </CodeGroup>
  169. </Col>
  170. </Row>
  171. ---
  172. <Heading
  173. url='/files/upload'
  174. method='POST'
  175. title='File Upload'
  176. name='#file-upload'
  177. />
  178. <Row>
  179. <Col>
  180. Upload a file (currently only images are supported) for use when sending messages, enabling multimodal understanding of images and text.
  181. Supports png, jpg, jpeg, webp, gif formats.
  182. <i>Uploaded files are for use by the current end-user only.</i>
  183. ### Request Body
  184. This interface requires a `multipart/form-data` request.
  185. - `file` (File) Required
  186. The file to be uploaded.
  187. - `user` (string) Required
  188. User identifier, defined by the developer's rules, must be unique within the application.
  189. ### Response
  190. After a successful upload, the server will return the file's ID and related information.
  191. - `id` (uuid) ID
  192. - `name` (string) File name
  193. - `size` (int) File size (bytes)
  194. - `extension` (string) File extension
  195. - `mime_type` (string) File mime-type
  196. - `created_by` (uuid) End-user ID
  197. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  198. ### Errors
  199. - 400, `no_file_uploaded`, a file must be provided
  200. - 400, `too_many_files`, currently only one file is accepted
  201. - 400, `unsupported_preview`, the file does not support preview
  202. - 400, `unsupported_estimate`, the file does not support estimation
  203. - 413, `file_too_large`, the file is too large
  204. - 415, `unsupported_file_type`, unsupported extension, currently only document files are accepted
  205. - 503, `s3_connection_failed`, unable to connect to S3 service
  206. - 503, `s3_permission_denied`, no permission to upload files to S3
  207. - 503, `s3_file_too_large`, file exceeds S3 size limit
  208. - 500, internal server error
  209. </Col>
  210. <Col sticky>
  211. ### Request Example
  212. <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'`}>
  213. ```bash {{ title: 'cURL' }}
  214. curl -X POST '${props.appDetail.api_base_url}/files/upload' \
  215. --header 'Authorization: Bearer {api_key}' \
  216. --form 'file=@"/path/to/file"'
  217. ```
  218. </CodeGroup>
  219. ### Response Example
  220. <CodeGroup title="Response">
  221. ```json {{ title: 'Response' }}
  222. {
  223. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  224. "name": "example.png",
  225. "size": 1024,
  226. "extension": "png",
  227. "mime_type": "image/png",
  228. "created_by": "6ad1ab0a-73ff-4ac1-b9e4-cdb312f71f13",
  229. "created_at": 1577836800,
  230. }
  231. ```
  232. </CodeGroup>
  233. </Col>
  234. </Row>
  235. ---
  236. <Heading
  237. url='/completion-messages/:task_id/stop'
  238. method='POST'
  239. title='Stop Generate'
  240. name='#stop-generatebacks'
  241. />
  242. <Row>
  243. <Col>
  244. Only supported in streaming mode.
  245. ### Path
  246. - `task_id` (string) Task ID, can be obtained from the streaming chunk return
  247. Request Body
  248. - `user` (string) Required
  249. User identifier, used to define the identity of the end-user, must be consistent with the user passed in the send message interface.
  250. ### Response
  251. - `result` (string) Always returns "success"
  252. </Col>
  253. <Col sticky>
  254. ### Request Example
  255. <CodeGroup title="Request" tag="POST" label="/completion-messages/:task_id/stop" targetCode={`curl -X POST '${props.appDetail.api_base_url}/completion-messages/:task_id/stop' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json' \\\n--data-raw '{ "user": "abc-123"}'`}>
  256. ```bash {{ title: 'cURL' }}
  257. curl -X POST '${props.appDetail.api_base_url}/completion-messages/:task_id/stop' \
  258. -H 'Authorization: Bearer {api_key}' \
  259. -H 'Content-Type: application/json' \
  260. --data-raw '{
  261. "user": "abc-123"
  262. }'
  263. ```
  264. </CodeGroup>
  265. ### Response Example
  266. <CodeGroup title="Response">
  267. ```json {{ title: 'Response' }}
  268. {
  269. "result": "success"
  270. }
  271. ```
  272. </CodeGroup>
  273. </Col>
  274. </Row>
  275. ---
  276. <Heading
  277. url='/messages/:message_id/feedbacks'
  278. method='POST'
  279. title='Message Feedback'
  280. name='#feedbacks'
  281. />
  282. <Row>
  283. <Col>
  284. End-users can provide feedback messages, facilitating application developers to optimize expected outputs.
  285. ### Path
  286. <Properties>
  287. <Property name='message_id' type='string' key='message_id'>
  288. Message ID
  289. </Property>
  290. </Properties>
  291. ### Request Body
  292. <Properties>
  293. <Property name='rating' type='string' key='rating'>
  294. Upvote as `like`, downvote as `dislike`, revoke upvote as `null`
  295. </Property>
  296. <Property name='user' type='string' key='user'>
  297. User identifier, defined by the developer's rules, must be unique within the application.
  298. </Property>
  299. </Properties>
  300. ### Response
  301. - `result` (string) Always returns "success"
  302. </Col>
  303. <Col sticky>
  304. <CodeGroup title="Request" tag="POST" label="/messages/:message_id/feedbacks" targetCode={`curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks \\\n --header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "rating": "like",\n "user": "abc-123"\n}'`}>
  305. ```bash {{ title: 'cURL' }}
  306. curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks' \
  307. --header 'Authorization: Bearer {api_key}' \
  308. --header 'Content-Type: application/json' \
  309. --data-raw '{
  310. "rating": "like",
  311. "user": "abc-123"
  312. }'
  313. ```
  314. </CodeGroup>
  315. <CodeGroup title="Response">
  316. ```json {{ title: 'Response' }}
  317. {
  318. "result": "success"
  319. }
  320. ```
  321. </CodeGroup>
  322. </Col>
  323. </Row>
  324. ---
  325. <Heading
  326. url='/parameters'
  327. method='GET'
  328. title='Get Application Information'
  329. name='#parameters'
  330. />
  331. <Row>
  332. <Col>
  333. Used at the start of entering the page to obtain information such as features, input parameter names, types, and default values.
  334. ### Query
  335. <Properties>
  336. <Property name='user' type='string' key='user'>
  337. User identifier, defined by the developer's rules, must be unique within the application.
  338. </Property>
  339. </Properties>
  340. ### Response
  341. - `opening_statement` (string) Opening statement
  342. - `suggested_questions` (array[string]) List of suggested questions for the opening
  343. - `suggested_questions_after_answer` (object) Suggest questions after enabling the answer.
  344. - `enabled` (bool) Whether it is enabled
  345. - `speech_to_text` (object) Speech to text
  346. - `enabled` (bool) Whether it is enabled
  347. - `retriever_resource` (object) Citation and Attribution
  348. - `enabled` (bool) Whether it is enabled
  349. - `annotation_reply` (object) Annotation reply
  350. - `enabled` (bool) Whether it is enabled
  351. - `user_input_form` (array[object]) User input form configuration
  352. - `text-input` (object) Text input control
  353. - `label` (string) Variable display label name
  354. - `variable` (string) Variable ID
  355. - `required` (bool) Whether it is required
  356. - `default` (string) Default value
  357. - `paragraph` (object) Paragraph text input control
  358. - `label` (string) Variable display label name
  359. - `variable` (string) Variable ID
  360. - `required` (bool) Whether it is required
  361. - `default` (string) Default value
  362. - `select` (object) Dropdown control
  363. - `label` (string) Variable display label name
  364. - `variable` (string) Variable ID
  365. - `required` (bool) Whether it is required
  366. - `default` (string) Default value
  367. - `options` (array[string]) Option values
  368. - `file_upload` (object) File upload configuration
  369. - `image` (object) Image settings
  370. Currently only supports image types: `png`, `jpg`, `jpeg`, `webp`, `gif`
  371. - `enabled` (bool) Whether it is enabled
  372. - `number_limits` (int) Image number limit, default is 3
  373. - `transfer_methods` (array[string]) List of transfer methods, remote_url, local_file, must choose one
  374. - `system_parameters` (object) System parameters
  375. - `image_file_size_limit` (string) Image file upload size limit (MB)
  376. </Col>
  377. <Col sticky>
  378. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  379. ```bash {{ title: 'cURL' }}
  380. curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  381. --header 'Authorization: Bearer {api_key}'
  382. ```
  383. </CodeGroup>
  384. <CodeGroup title="Response">
  385. ```json {{ title: 'Response' }}
  386. {
  387. "opening_statement": "Hello!",
  388. "suggested_questions_after_answer": {
  389. "enabled": true
  390. },
  391. "speech_to_text": {
  392. "enabled": true
  393. },
  394. "retriever_resource": {
  395. "enabled": true
  396. },
  397. "annotation_reply": {
  398. "enabled": true
  399. },
  400. "user_input_form": [
  401. {
  402. "paragraph": {
  403. "label": "Query",
  404. "variable": "query",
  405. "required": true,
  406. "default": ""
  407. }
  408. }
  409. ],
  410. "file_upload": {
  411. "image": {
  412. "enabled": false,
  413. "number_limits": 3,
  414. "detail": "high",
  415. "transfer_methods": [
  416. "remote_url",
  417. "local_file"
  418. ]
  419. }
  420. },
  421. "system_parameters": {
  422. "image_file_size_limit": "10"
  423. }
  424. }
  425. ```
  426. </CodeGroup>
  427. </Col>
  428. </Row>
  429. ---
  430. <Heading
  431. url='/text-to-audio'
  432. method='POST'
  433. title='text to audio'
  434. name='#audio'
  435. />
  436. <Row>
  437. <Col>
  438. Text to speech.
  439. ### Request Body
  440. <Properties>
  441. <Property name='text' type='str' key='text'>
  442. Speech generated content。
  443. </Property>
  444. <Property name='user' type='string' key='user'>
  445. The user identifier, defined by the developer, must ensure uniqueness within the app.
  446. </Property>
  447. <Property name='streaming' type='bool' key='streaming'>
  448. Whether to enable streaming output, true、false。
  449. </Property>
  450. </Properties>
  451. </Col>
  452. <Col sticky>
  453. <CodeGroup title="Request" tag="POST" label="/text-to-audio" targetCode={`curl -o text-to-audio.mp3 -X POST '${props.appDetail.api_base_url}/text-to-audio' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "text": "Hello Dify",\n "user": "abc-123",\n "streaming": false\n}'`}>
  454. ```bash {{ title: 'cURL' }}
  455. curl -o text-to-audio.mp3 -X POST '${props.appDetail.api_base_url}/text-to-audio' \
  456. --header 'Authorization: Bearer {api_key}' \
  457. --header 'Content-Type: application/json' \
  458. --data-raw '{
  459. "text": "Hello Dify",
  460. "user": "abc-123",
  461. "streaming": false
  462. }'
  463. ```
  464. </CodeGroup>
  465. <CodeGroup title="headers">
  466. ```json {{ title: 'headers' }}
  467. {
  468. "Content-Type": "audio/wav"
  469. }
  470. ```
  471. </CodeGroup>
  472. </Col>
  473. </Row>