template.zh.mdx 6.6 KB

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