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