template.en.mdx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Completion App API
  4. For high-quality text generation, such as articles, summaries, and translations, use the completion-messages API with user input. Text generation relies on the model parameters and prompt templates set in Dify Prompt Engineering.
  5. <div>
  6. ### Authentication
  7. Service API of Dify authenticates using an `API-Key`.
  8. It is suggested that developers store the `API-Key` in the backend instead of sharing or storing it in the client side to avoid the leakage of the `API-Key`, which may lead to property loss.
  9. All API requests should include your `API-Key` in the **`Authorization`** HTTP Header, as shown below:
  10. <CodeGroup title="Code">
  11. ```javascript
  12. Authorization: Bearer {API_KEY}
  13. ```
  14. </CodeGroup>
  15. </div>
  16. ---
  17. <Heading
  18. url='/completion-messages'
  19. method='POST'
  20. title='Create Completion Message'
  21. name='#Create-Completion-Message'
  22. />
  23. <Row>
  24. <Col>
  25. Create a Completion Message to support the question-and-answer mode.
  26. ### Request Body
  27. <Properties>
  28. <Property name='inputs' type='object' key='inputs'>
  29. (Optional) Provide user input fields as key-value pairs, corresponding to variables in Prompt Eng. Key is the variable name, Value is the parameter value. If the field type is Select, the submitted Value must be one of the preset choices.
  30. <ul>
  31. {!!props.variables.length && props.variables.map(
  32. val => (
  33. <SubProperty name={val.key} type={val.type} key={val.key}>
  34. {val.name ? `${val.name}` : ''}
  35. </SubProperty>
  36. )
  37. )}
  38. </ul>
  39. </Property>
  40. <Property name='response_mode' type='string' key='response_mode'>
  41. - Blocking type, waiting for execution to complete and returning results. (Requests may be interrupted if the process is long)
  42. - streaming returns. Implementation of streaming return based on SSE (**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**).
  43. </Property>
  44. <Property name='user' type='string' key='user'>
  45. The user identifier, defined by the developer, must ensure uniqueness within the app.
  46. </Property>
  47. <Property name='files' type='array[object]' key='files'>
  48. Uploaded files.
  49. - `type` file type: currently only `image` is supported.
  50. - `transfer_method` transfer method:
  51. - `remote_url`: Image address.
  52. - `local_file`: upload file.
  53. - `upload_file_id` Upload file ID. Required when `transfer_method` is `local_file`.
  54. - `url` image address. Required when `transfer_method` is `remote_url`.
  55. </Property>
  56. </Properties>
  57. </Col>
  58. <Col sticky>
  59. <CodeGroup title="Request" tag="POST" label="/completion-messages" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/completion-messages' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-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`}>
  60. ```bash {{ title: 'cURL' }}
  61. curl --location --request POST '${props.appDetail.api_base_url}/completion-messages' \
  62. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  63. --header 'Content-Type: application/json' \
  64. --data-raw '{
  65. "inputs": {},
  66. "response_mode": "streaming",
  67. "user": "abc-123"
  68. }'
  69. ```
  70. </CodeGroup>
  71. ### blocking
  72. <CodeGroup title="Response">
  73. ```json {{ title: 'Response' }}
  74. {
  75. "id": "0b089b9a-24d9-48cc-94f8-762677276261",
  76. "answer": "how are you?",
  77. "created_at": 1679586667
  78. }
  79. ```
  80. </CodeGroup>
  81. ### streaming
  82. <CodeGroup title="Response">
  83. ```streaming {{ title: 'Response' }}
  84. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  85. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  86. ```
  87. </CodeGroup>
  88. </Col>
  89. </Row>
  90. ---
  91. <Heading
  92. url='/messages/{message_id}/feedbacks'
  93. method='POST'
  94. title='Message feedback (like)'
  95. name='#feedbacks'
  96. />
  97. <Row>
  98. <Col>
  99. Rate received messages on behalf of end-users with likes or dislikes. This data is visible in the Logs & Annotations page and used for future model fine-tuning.
  100. ### Path Params
  101. <Properties>
  102. <Property name='message_id' type='string' key='message_id'>
  103. Message ID
  104. </Property>
  105. </Properties>
  106. ### Request Body
  107. <Properties>
  108. <Property name='rating' type='string' key='rating'>
  109. like or dislike, null is undo
  110. </Property>
  111. <Property name='user' type='string' key='user'>
  112. The user identifier, defined by the developer, must ensure uniqueness within the app.
  113. </Property>
  114. </Properties>
  115. </Col>
  116. <Col sticky>
  117. <CodeGroup title="Request" tag="POST" label="/messages/{message_id}/feedbacks" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n --data-raw '{ \n "rating": "like",\n "user": "abc-123"\n}'`}>
  118. ```bash {{ title: 'cURL' }}
  119. curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
  120. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  121. --header 'Content-Type: application/json' \
  122. --data-raw '{
  123. "rating": "like",
  124. "user": "abc-123"
  125. }'
  126. ```
  127. </CodeGroup>
  128. <CodeGroup title="Response">
  129. ```json {{ title: 'Response' }}
  130. {
  131. "has_more": false,
  132. "data": [
  133. {
  134. "id": "WAz8eIbvDR60rouK",
  135. "username": "FrankMcCallister",
  136. "phone_number": "1-800-759-3000",
  137. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  138. "display_name": null,
  139. "conversation_id": "xgQQXg3hrtjh7AvZ",
  140. "created_at": 692233200
  141. },
  142. {
  143. "id": "hSIhXBhNe8X1d8Et"
  144. // ...
  145. }
  146. ]
  147. }
  148. ```
  149. </CodeGroup>
  150. </Col>
  151. </Row>
  152. ---
  153. <Heading
  154. url='/files/upload'
  155. method='POST'
  156. title='Upload files'
  157. name='#files-upload'
  158. />
  159. <Row>
  160. <Col>
  161. Upload files to the server for text generation and dialogue. Uploaded files are only available to the current end user.
  162. ### Request Body
  163. The request method is `multipart/form-data`
  164. <Properties>
  165. <Property name='file' type='file' key='file'>
  166. The file to upload. Currently supports png, jpg, jpeg, webp, gif format files.
  167. </Property>
  168. <Property name='user' type='string' key='user'>
  169. User ID, rules are defined by the developer, and it is necessary to ensure that the user ID is unique within the application.
  170. </Property>
  171. </Properties>
  172. </Col>
  173. <Col sticky>
  174. <CodeGroup title="Request" tag="POST" label="/files/upload" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif] \\\n--form 'user=abc-123'`}>
  175. ```bash {{ title: 'cURL' }}
  176. curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \
  177. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  178. --form 'file=@"/path/to/file"'
  179. ```
  180. </CodeGroup>
  181. <CodeGroup title="Response">
  182. ```json {{ title: 'Response' }}
  183. {
  184. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  185. "name": "example.png",
  186. "size": 1024,
  187. "extension": "png",
  188. "mime_type": "image/png",
  189. "created_by": 123,
  190. "created_at": 1577836800,
  191. }
  192. ```
  193. </CodeGroup>
  194. </Col>
  195. </Row>
  196. ---
  197. <Heading
  198. url='/parameters'
  199. method='GET'
  200. title='Obtain application parameter information'
  201. name='#parameters'
  202. />
  203. <Row>
  204. <Col>
  205. Content include:
  206. 1. Retrieve configured Input parameters, including variable names, field names, types, and default values. Typically used for displaying these fields in a form or filling in default values after the client loads.
  207. 2. Configuration of uploading images, including whether to enable image uploading, the number of uploaded images and the uploading method. Note: This configuration is only available when using a model that supports multimodality.
  208. ### Query
  209. <Properties>
  210. <Property name='user' type='string' key='user'>
  211. The user identifier, defined by the developer, must ensure uniqueness within the app.
  212. </Property>
  213. </Properties>
  214. </Col>
  215. <Col sticky>
  216. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  217. ```bash {{ title: 'cURL' }}
  218. curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  219. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  220. ```
  221. </CodeGroup>
  222. <CodeGroup title="Response">
  223. ```json {{ title: 'Response' }}
  224. {
  225. "introduction": "nice to meet you",
  226. "user_input_form": [
  227. {
  228. "text-input": {
  229. "label": "a",
  230. "variable": "a",
  231. "required": true,
  232. "max_length": 48,
  233. "default": ""
  234. }
  235. }
  236. {
  237. // ...
  238. }
  239. ],
  240. "file_upload": {
  241. "image": {
  242. "enabled": true,
  243. "number_limits": 3,
  244. "transfer_methods": [
  245. "remote_url",
  246. "local_file"
  247. ]
  248. }
  249. }
  250. }
  251. ```
  252. </CodeGroup>
  253. </Col>
  254. </Row>