template.zh.mdx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx'
  3. # 文本生成型应用 API
  4. 可用于生成高质量文本的应用,例如生成文章、摘要、翻译等,通过调用 completion-messages 接口,发送用户输入得到生成文本结果。用于生成文本的模型参数和提示词模版取决于开发者在 Dify 提示词编排页的设置。
  5. <Heading
  6. url='/completion-messages'
  7. method='POST'
  8. title='创建文本补全消息'
  9. name='#Create-Completion-Message'
  10. />
  11. <Row>
  12. <Col>
  13. 创建文本补全消息,支持一问一答模式。
  14. ### Request Body
  15. <Properties>
  16. <Property name='inputs' type='object' key='inputs'>
  17. (选填)以键值对方式提供用户输入字段,与提示词编排中的变量对应。Key 为变量名称,Value 是参数值。如果字段类型为 Select,传入的 Value 需为预设选项之一。
  18. <ul>
  19. {!!props.variables.length && props.variables.map(
  20. val => (
  21. <SubProperty name={val.key} type={val.type} key={val.key}>
  22. {val.name ? `${val.name}` : ''}
  23. </SubProperty>
  24. )
  25. )}
  26. </ul>
  27. </Property>
  28. <Property name='query' type='string' key='query'>
  29. 用户输入的文本正文。
  30. </Property>
  31. <Property name='response_mode' type='string' key='response_mode'>
  32. - blocking 阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断)
  33. - streaming 流式返回。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现流式返回。
  34. </Property>
  35. <Property name='user' type='string' key='user'>
  36. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  37. </Property>
  38. </Properties>
  39. </Col>
  40. <Col sticky>
  41. <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 "query": "Hi",\n "response_mode": "streaming",\n "user": "abc-123"\n}'\n`}>
  42. ```bash {{ title: 'cURL' }}
  43. curl --location --request POST 'https://cloud.langgenius.dev/api/completion-messages' \
  44. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  45. --header 'Content-Type: application/json' \
  46. --data-raw '{
  47. "inputs": {},
  48. "query": "Hi",
  49. "response_mode": "streaming",
  50. "user": "abc-123"
  51. }'
  52. ```
  53. </CodeGroup>
  54. ### blocking
  55. <CodeGroup title="Response">
  56. ```json {{ title: 'Response' }}
  57. {
  58. "id": "0b089b9a-24d9-48cc-94f8-762677276261",
  59. "answer": "how are you?",
  60. "created_at": 1679586667
  61. }
  62. ```
  63. </CodeGroup>
  64. ### streaming
  65. <CodeGroup title="Response">
  66. ```streaming {{ title: 'Response' }}
  67. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  68. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  69. ```
  70. </CodeGroup>
  71. </Col>
  72. </Row>
  73. ---
  74. <Heading
  75. url='/messages/{message_id}/feedbacks'
  76. method='POST'
  77. title='消息反馈(点赞)'
  78. name='#feedbacks'
  79. />
  80. <Row>
  81. <Col>
  82. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  83. ### Path Params
  84. <Properties>
  85. <Property name='message_id' type='string' key='message_id'>
  86. 消息 ID
  87. </Property>
  88. </Properties>
  89. ### Request Body
  90. <Properties>
  91. <Property name='rating' type='string' key='rating'>
  92. like 或 dislike, 空值为撤销
  93. </Property>
  94. <Property name='user' type='string' key='user'>
  95. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  96. </Property>
  97. </Properties>
  98. </Col>
  99. <Col sticky>
  100. <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}'`}>
  101. ```bash {{ title: 'cURL' }}
  102. curl --location --request POST 'https://cloud.langgenius.dev/api/messages/{message_id}/feedbacks' \
  103. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  104. --header 'Content-Type: application/json' \
  105. --data-raw '{
  106. "rating": "like",
  107. "user": "abc-123"
  108. }'
  109. ```
  110. </CodeGroup>
  111. <CodeGroup title="Response">
  112. ```json {{ title: 'Response' }}
  113. {
  114. "has_more": false,
  115. "data": [
  116. {
  117. "id": "WAz8eIbvDR60rouK",
  118. "username": "FrankMcCallister",
  119. "phone_number": "1-800-759-3000",
  120. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  121. "display_name": null,
  122. "conversation_id": "xgQQXg3hrtjh7AvZ",
  123. "created_at": 692233200
  124. },
  125. {
  126. "id": "hSIhXBhNe8X1d8Et"
  127. // ...
  128. }
  129. ]
  130. }
  131. ```
  132. </CodeGroup>
  133. </Col>
  134. </Row>
  135. ---
  136. <Heading
  137. url='/parameters'
  138. method='GET'
  139. title='获取应用配置信息'
  140. name='#parameters'
  141. />
  142. <Row>
  143. <Col>
  144. 获取已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  145. ### Query
  146. <Properties>
  147. <Property name='user' type='string' key='user'>
  148. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  149. </Property>
  150. </Properties>
  151. </Col>
  152. <Col sticky>
  153. <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'`}>
  154. ```bash {{ title: 'cURL' }}
  155. curl --location --request GET 'https://cloud.langgenius.dev/api/parameters?user=abc-123' \
  156. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  157. ```
  158. </CodeGroup>
  159. <CodeGroup title="Response">
  160. ```json {{ title: 'Response' }}
  161. {
  162. "introduction": "nice to meet you",
  163. "variables": [
  164. {
  165. "key": "book",
  166. "name": "Which book?",
  167. "description": null,
  168. "type": "string",
  169. "default": null,
  170. "options": null
  171. },
  172. {
  173. // ...
  174. ]
  175. }
  176. ```
  177. </CodeGroup>
  178. </Col>
  179. </Row>