template.zh.mdx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. <Property name='files' type='array[object]' key='files'>
  48. 上传的文件。
  49. - `type` 文件类型:目前仅支持 `image`。
  50. - `transfer_method` 传递方式:
  51. - `remote_url`: 图片地址。
  52. - `local_file`: 上传文件。
  53. - `upload_file_id` 上传文件 ID。`transfer_method` 为 `local_file` 时必填。
  54. - `url` 图片地址。`transfer_method` 为 `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='消息反馈(点赞)'
  95. name='#feedbacks'
  96. />
  97. <Row>
  98. <Col>
  99. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  100. ### Path Params
  101. <Properties>
  102. <Property name='message_id' type='string' key='message_id'>
  103. 消息 ID
  104. </Property>
  105. </Properties>
  106. ### Request Body
  107. <Properties>
  108. <Property name='rating' type='string' key='rating'>
  109. like 或 dislike, 空值为撤销
  110. </Property>
  111. <Property name='user' type='string' key='user'>
  112. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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='上传文件'
  157. name='#files-upload'
  158. />
  159. <Row>
  160. <Col>
  161. 上传文件到服务器,供文本生成、对话使用。上传的文件仅供当前终端用户使用。
  162. ### Request Body
  163. 请求方式为 `multipart/form-data`。
  164. <Properties>
  165. <Property name='file' type='file' key='file'>
  166. 要上传的文件。目前支持 png, jpg, jpeg, webp, gif 格式文件。
  167. </Property>
  168. <Property name='user' type='string' key='user'>
  169. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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='获取应用配置信息'
  201. name='#parameters'
  202. />
  203. <Row>
  204. <Col>
  205. 内容包括:
  206. 1. 已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  207. 2. 上传图片的配置,包括是否开启上传图片,上传图片的数量和上传方式。注意:这个配置只有使用支持多模态的模型时才会生效。
  208. ### Query
  209. <Properties>
  210. <Property name='user' type='string' key='user'>
  211. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  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>