template.en.mdx 20 KB

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