template.en.mdx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. </Properties>
  48. </Col>
  49. <Col sticky>
  50. <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`}>
  51. ```bash {{ title: 'cURL' }}
  52. curl --location --request POST '${props.appDetail.api_base_url}/completion-messages' \
  53. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  54. --header 'Content-Type: application/json' \
  55. --data-raw '{
  56. "inputs": {},
  57. "response_mode": "streaming",
  58. "user": "abc-123"
  59. }'
  60. ```
  61. </CodeGroup>
  62. ### blocking
  63. <CodeGroup title="Response">
  64. ```json {{ title: 'Response' }}
  65. {
  66. "id": "0b089b9a-24d9-48cc-94f8-762677276261",
  67. "answer": "how are you?",
  68. "created_at": 1679586667
  69. }
  70. ```
  71. </CodeGroup>
  72. ### streaming
  73. <CodeGroup title="Response">
  74. ```streaming {{ title: 'Response' }}
  75. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  76. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  77. ```
  78. </CodeGroup>
  79. </Col>
  80. </Row>
  81. ---
  82. <Heading
  83. url='/messages/{message_id}/feedbacks'
  84. method='POST'
  85. title='Message feedback (like)'
  86. name='#feedbacks'
  87. />
  88. <Row>
  89. <Col>
  90. 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.
  91. ### Path Params
  92. <Properties>
  93. <Property name='message_id' type='string' key='message_id'>
  94. Message ID
  95. </Property>
  96. </Properties>
  97. ### Request Body
  98. <Properties>
  99. <Property name='rating' type='string' key='rating'>
  100. like or dislike, null is undo
  101. </Property>
  102. <Property name='user' type='string' key='user'>
  103. The user identifier, defined by the developer, must ensure uniqueness within the app.
  104. </Property>
  105. </Properties>
  106. </Col>
  107. <Col sticky>
  108. <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}'`}>
  109. ```bash {{ title: 'cURL' }}
  110. curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
  111. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  112. --header 'Content-Type: application/json' \
  113. --data-raw '{
  114. "rating": "like",
  115. "user": "abc-123"
  116. }'
  117. ```
  118. </CodeGroup>
  119. <CodeGroup title="Response">
  120. ```json {{ title: 'Response' }}
  121. {
  122. "has_more": false,
  123. "data": [
  124. {
  125. "id": "WAz8eIbvDR60rouK",
  126. "username": "FrankMcCallister",
  127. "phone_number": "1-800-759-3000",
  128. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  129. "display_name": null,
  130. "conversation_id": "xgQQXg3hrtjh7AvZ",
  131. "created_at": 692233200
  132. },
  133. {
  134. "id": "hSIhXBhNe8X1d8Et"
  135. // ...
  136. }
  137. ]
  138. }
  139. ```
  140. </CodeGroup>
  141. </Col>
  142. </Row>
  143. ---
  144. <Heading
  145. url='/parameters'
  146. method='GET'
  147. title='Obtain application parameter information'
  148. name='#parameters'
  149. />
  150. <Row>
  151. <Col>
  152. 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.
  153. ### Query
  154. <Properties>
  155. <Property name='user' type='string' key='user'>
  156. The user identifier, defined by the developer, must ensure uniqueness within the app.
  157. </Property>
  158. </Properties>
  159. </Col>
  160. <Col sticky>
  161. <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'`}>
  162. ```bash {{ title: 'cURL' }}
  163. curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  164. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  165. ```
  166. </CodeGroup>
  167. <CodeGroup title="Response">
  168. ```json {{ title: 'Response' }}
  169. {
  170. "introduction": "nice to meet you",
  171. "variables": [
  172. {
  173. "key": "book",
  174. "name": "Which book?",
  175. "description": null,
  176. "type": "string",
  177. "default": null,
  178. "options": null
  179. },
  180. {
  181. // ...
  182. ]
  183. }
  184. ```
  185. </CodeGroup>
  186. </Col>
  187. </Row>