template.zh.mdx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx'
  3. # 文本生成型应用 API
  4. 可用于生成高质量文本的应用,例如生成文章、摘要、翻译等,通过调用 completion-messages 接口,发送用户输入得到生成文本结果。用于生成文本的模型参数和提示词模版取决于开发者在 Dify 提示词编排页的设置。
  5. <div>
  6. ### 鉴权
  7. Dify Service API 使用 `API-Key` 进行鉴权。
  8. 建议开发者把 `API-Key` 放在后端存储,而非分享或者放在客户端存储,以免 `API-Key` 泄露,导致财产损失。
  9. 所有 API 请求都应在 **`Authorization`** HTTP Header 中包含您的 `API-Key`,如下所示:
  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='创建文本补全消息'
  21. name='#Create-Completion-Message'
  22. />
  23. <Row>
  24. <Col>
  25. 创建文本补全消息,支持一问一答模式。
  26. ### Request Body
  27. <Properties>
  28. <Property name='inputs' type='object' key='inputs'>
  29. (选填)以键值对方式提供用户输入字段,与提示词编排中的变量对应。Key 为变量名称,Value 是参数值。如果字段类型为 Select,传入的 Value 需为预设选项之一。
  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 阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断)
  42. - streaming 流式返回。基于 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. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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='消息反馈(点赞)'
  86. name='#feedbacks'
  87. />
  88. <Row>
  89. <Col>
  90. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  91. ### Path Params
  92. <Properties>
  93. <Property name='message_id' type='string' key='message_id'>
  94. 消息 ID
  95. </Property>
  96. </Properties>
  97. ### Request Body
  98. <Properties>
  99. <Property name='rating' type='string' key='rating'>
  100. like 或 dislike, 空值为撤销
  101. </Property>
  102. <Property name='user' type='string' key='user'>
  103. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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='获取应用配置信息'
  148. name='#parameters'
  149. />
  150. <Row>
  151. <Col>
  152. 获取已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  153. ### Query
  154. <Properties>
  155. <Property name='user' type='string' key='user'>
  156. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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>