template_chat.zh.mdx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx'
  3. # 对话型应用 API
  4. 可用于大部分场景的对话型应用,采用一问一答模式与用户持续对话。要开始一个对话请调用 chat-messages 接口,通过继续传入返回的 conversation_id 可持续保持该会话。
  5. <Heading
  6. url='/chat-messages'
  7. method='POST'
  8. title='发送对话消息'
  9. name='#Create-Chat-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='conversation_id' type='string' key='conversation_id'>
  36. (选填)会话标识符,首次对话可为空,如果要继续对话请传入上下文返回的 conversation_id
  37. </Property>
  38. <Property name='user' type='string' key='user'>
  39. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  40. </Property>
  41. </Properties>
  42. </Col>
  43. <Col sticky>
  44. <CodeGroup title="Request" tag="POST" label="/chat-messages" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/chat-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": "eh",\n "response_mode": "streaming",\n "conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",\n "user": "abc-123"\n}'\n`}>
  45. ```bash {{ title: 'cURL' }}
  46. curl --location --request POST 'https://cloud.langgenius.dev/api/chat-messages' \
  47. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  48. --header 'Content-Type: application/json' \
  49. --data-raw '{
  50. "inputs": {},
  51. "query": "eh",
  52. "response_mode": "streaming",
  53. "conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
  54. "user": "abc-123"
  55. }'
  56. ```
  57. </CodeGroup>
  58. ### blocking
  59. <CodeGroup title="Response">
  60. ```json {{ title: 'Response' }}
  61. {
  62. "answer": "Hi, is there anything I can help you?",
  63. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  64. "created_at": 1679587005,
  65. "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
  66. }
  67. ```
  68. </CodeGroup>
  69. ### streaming
  70. <CodeGroup title="Response">
  71. ```streaming {{ title: 'Response' }}
  72. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  73. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  74. ```
  75. </CodeGroup>
  76. </Col>
  77. </Row>
  78. ---
  79. <Heading
  80. url='/messages/{message_id}/feedbacks'
  81. method='POST'
  82. title='消息反馈(点赞)'
  83. name='#feedbacks'
  84. />
  85. <Row>
  86. <Col>
  87. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  88. ### Path Params
  89. <Properties>
  90. <Property name='message_id' type='string' key='message_id'>
  91. 消息 ID
  92. </Property>
  93. </Properties>
  94. ### Request Body
  95. <Properties>
  96. <Property name='rating' type='string' key='rating'>
  97. like 或 dislike, 空值为撤销
  98. </Property>
  99. <Property name='user' type='string' key='user'>
  100. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  101. </Property>
  102. </Properties>
  103. </Col>
  104. <Col sticky>
  105. <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}'`}>
  106. ```bash {{ title: 'cURL' }}
  107. curl --location --request POST 'https://cloud.langgenius.dev/api/messages/{message_id}/feedbacks' \
  108. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  109. --header 'Content-Type: application/json' \
  110. --data-raw '{
  111. "rating": "like",
  112. "user": "abc-123"
  113. }'
  114. ```
  115. </CodeGroup>
  116. <CodeGroup title="Response">
  117. ```json {{ title: 'Response' }}
  118. {
  119. "has_more": false,
  120. "data": [
  121. {
  122. "id": "WAz8eIbvDR60rouK",
  123. "username": "FrankMcCallister",
  124. "phone_number": "1-800-759-3000",
  125. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  126. "display_name": null,
  127. "conversation_id": "xgQQXg3hrtjh7AvZ",
  128. "last_active_at": 705103200,
  129. "created_at": 692233200
  130. },
  131. {
  132. "id": "hSIhXBhNe8X1d8Et"
  133. // ...
  134. }
  135. ]
  136. }
  137. ```
  138. </CodeGroup>
  139. </Col>
  140. </Row>
  141. ---
  142. <Heading
  143. url='/messages'
  144. method='GET'
  145. title='获取会话历史消息'
  146. name='#messages'
  147. />
  148. <Row>
  149. <Col>
  150. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,即:倒序返回。
  151. ### Query
  152. <Properties>
  153. <Property name='conversation_id' type='string' key='conversation_id'>
  154. 会话 ID
  155. </Property>
  156. <Property name='first_id' type='string' key='first_id'>
  157. 当前页第一条聊天记录的 ID,默认 none
  158. </Property>
  159. <Property name='limit' type='int' key='limit'>
  160. 一次请求返回多少条聊天记录
  161. </Property>
  162. <Property name='user' type='string' key='user'>
  163. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  164. </Property>
  165. </Properties>
  166. </Col>
  167. <Col sticky>
  168. <CodeGroup title="Request" tag="GET" label="/messages" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id=' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  169. ```bash {{ title: 'cURL' }}
  170. curl --location --request GET 'https://cloud.langgenius.dev/api/messages?user=abc-123&conversation_id='
  171. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  172. ```
  173. </CodeGroup>
  174. <CodeGroup title="Response">
  175. ```json {{ title: 'Response' }}
  176. {
  177. "has_more": false,
  178. "data": [
  179. {
  180. "id": "WAz8eIbvDR60rouK",
  181. "username": "FrankMcCallister",
  182. "phone_number": "1-800-759-3000",
  183. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  184. "display_name": null,
  185. "conversation_id": "xgQQXg3hrtjh7AvZ",
  186. "last_active_at": 705103200,
  187. "created_at": 692233200
  188. },
  189. {
  190. "id": "hSIhXBhNe8X1d8Et"
  191. // ...
  192. }
  193. ]
  194. }
  195. ```
  196. </CodeGroup>
  197. </Col>
  198. </Row>
  199. ---
  200. <Heading
  201. url='/conversations'
  202. method='GET'
  203. title='获取会话列表'
  204. name='#conversations'
  205. />
  206. <Row>
  207. <Col>
  208. 获取当前用户的会话列表,默认返回最近的 20 条。
  209. ### Query
  210. <Properties>
  211. <Property name='last_id' type='string' key='last_id'>
  212. 当前页最后面一条记录的 ID,默认 none
  213. </Property>
  214. <Property name='limit' type='int' key='limit'>
  215. 一次请求返回多少条记录
  216. </Property>
  217. <Property name='user' type='string' key='user'>
  218. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  219. </Property>
  220. </Properties>
  221. </Col>
  222. <Col sticky>
  223. <CodeGroup title="Request" tag="GET" label="/conversations" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20'\\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  224. ```bash {{ title: 'cURL' }}
  225. curl --location --request GET 'https://cloud.langgenius.dev/api/conversations?user=abc-123&last_id=&limit=20' \
  226. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  227. ```
  228. </CodeGroup>
  229. <CodeGroup title="Response">
  230. ```json {{ title: 'Response' }}
  231. {
  232. "limit": 20,
  233. "has_more": false,
  234. "data": [
  235. {
  236. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  237. "name": "New chat",
  238. "inputs": {
  239. "book": "book",
  240. "myName": "Lucy"
  241. },
  242. "status": "normal",
  243. "created_at": 1679667915
  244. },
  245. {
  246. "id": "hSIhXBhNe8X1d8Et"
  247. // ...
  248. }
  249. ]
  250. }
  251. ```
  252. </CodeGroup>
  253. </Col>
  254. </Row>
  255. ---
  256. <Heading
  257. url='/conversations/{converation_id}/name'
  258. method='POST'
  259. title='会话重命名'
  260. name='#rename'
  261. />
  262. <Row>
  263. <Col>
  264. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  265. ### Request Body
  266. <Properties>
  267. <Property name='name' type='string' key='name'>
  268. 新的名称
  269. </Property>
  270. <Property name='user' type='string' key='user'>
  271. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  272. </Property>
  273. </Properties>
  274. </Col>
  275. <Col sticky>
  276. <CodeGroup title="Request" tag="POST" label="/conversations/{converation_id}/name" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "name": "", \n "user": "abc-123"\n}'`}>
  277. ```bash {{ title: 'cURL' }}
  278. curl --location --request POST 'https://cloud.langgenius.dev/api/conversations/name' \
  279. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  280. --header 'Content-Type: application/json' \
  281. --data-raw '{
  282. "name": "",
  283. "user": "abc-123"
  284. }'
  285. ```
  286. </CodeGroup>
  287. <CodeGroup title="Response">
  288. ```json {{ title: 'Response' }}
  289. {
  290. "result": "success"
  291. }
  292. ```
  293. </CodeGroup>
  294. </Col>
  295. </Row>
  296. ---
  297. <Heading
  298. url='/parameters'
  299. method='GET'
  300. title='获取应用配置信息'
  301. name='#rename'
  302. />
  303. <Row>
  304. <Col>
  305. 获取已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  306. ### Query
  307. <Properties>
  308. <Property name='user' type='string' key='user'>
  309. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  310. </Property>
  311. </Properties>
  312. </Col>
  313. <Col sticky>
  314. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl --location --request GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  315. ```bash {{ title: 'cURL' }}
  316. curl --location --request GET 'https://cloud.langgenius.dev/api/parameters?user=abc-123' \
  317. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  318. ```
  319. </CodeGroup>
  320. <CodeGroup title="Response">
  321. ```json {{ title: 'Response' }}
  322. {
  323. "introduction": "nice to meet you",
  324. "variables": [
  325. {
  326. "key": "book",
  327. "name": "book",
  328. "description": null,
  329. "type": "string",
  330. "default": null,
  331. "options": null
  332. },
  333. {
  334. // ...
  335. }
  336. ]
  337. }
  338. ```
  339. </CodeGroup>
  340. </Col>
  341. </Row>