template_chat.en.mdx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Chat App API
  4. For versatile conversational apps using a Q&A format, call the chat-messages API to initiate dialogue. Maintain ongoing conversations by passing the returned conversation_id. Response parameters and templates depend on LangGenius Prompt Eng. settings. **[Before you start, READ This !! What is a Bearer Token](https://swagger.io/docs/specification/authentication/bearer-authentication/)**
  5. <Heading
  6. url='/chat-messages'
  7. method='POST'
  8. title='Create chat message'
  9. name='#Create-Chat-Message'
  10. />
  11. <Row>
  12. <Col>
  13. Create a new conversation message or continue an existing dialogue.
  14. ### Request Body
  15. <Properties>
  16. <Property name='inputs' type='object' key='inputs'>
  17. (Optional) Provide user input fields as key-value pairs, corresponding to variables in Prompt Eng. Key is the variable name, Value is the parameter value. If the field type is Select, the submitted Value must be one of the preset choices.
  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. User input/question content
  30. </Property>
  31. <Property name='response_mode' type='string' key='response_mode'>
  32. - Blocking type, waiting for execution to complete and returning results. (Requests may be interrupted if the process is long)
  33. - streaming returns. Implementation of streaming return based on 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. (Required) Conversation ID: <strong>‼️ leave it empty for first-time (eg. conversation_id: "") conversation ‼️</strong>; pass conversation_id from context to continue dialogue.
  37. </Property>
  38. <Property name='user' type='string' key='user'>
  39. The user identifier, defined by the developer, must ensure uniqueness within the app.
  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": "",\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='Message terminal user feedback, like'
  83. name='#feedbacks'
  84. />
  85. <Row>
  86. <Col>
  87. Rate received messages on behalf of end-users with likes or dislikes. This data is visible in the Logs & Annotations page and used for future model fine-tuning.
  88. ### Path Params
  89. <Properties>
  90. <Property name='message_id' type='string' key='message_id'>
  91. Message ID
  92. </Property>
  93. </Properties>
  94. ### Request Body
  95. <Properties>
  96. <Property name='rating' type='string' key='rating'>
  97. like or dislike, null is undo
  98. </Property>
  99. <Property name='user' type='string' key='user'>
  100. The user identifier, defined by the developer, must ensure uniqueness within the app.
  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. "conversation_id": "xgQQXg3hrtjh7AvZ",
  124. "inputs": {},
  125. "query": "...",
  126. "answer": "...",
  127. "feedback": "like",
  128. "created_at": 692233200
  129. },
  130. {
  131. "id": "hSIhXBhNe8X1d8Et"
  132. // ...
  133. }
  134. ]
  135. }
  136. ```
  137. </CodeGroup>
  138. </Col>
  139. </Row>
  140. ---
  141. <Heading
  142. url='/messages'
  143. method='GET'
  144. title='Get the chat history message'
  145. name='#messages'
  146. />
  147. <Row>
  148. <Col>
  149. The first page returns the latest `limit` bar, which is in reverse order. Load previous pages by passing the `first_id` of the last message on the current page to the `first_id` parameter of the next request.
  150. ### Query
  151. <Properties>
  152. <Property name='conversation_id' type='string' key='conversation_id'>
  153. Conversation ID
  154. </Property>
  155. <Property name='first_id' type='string' key='first_id'>
  156. ID of the first chat record on the current page. The default is none.
  157. </Property>
  158. <Property name='limit' type='int' key='limit'>
  159. How many chats are returned in one request
  160. </Property>
  161. <Property name='user' type='string' key='user'>
  162. The user identifier, defined by the developer, must ensure uniqueness within the app.
  163. </Property>
  164. </Properties>
  165. </Col>
  166. <Col sticky>
  167. <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'`}>
  168. ```bash {{ title: 'cURL' }}
  169. curl --location --request GET 'https://cloud.langgenius.dev/api/messages?user=abc-123&conversation_id='
  170. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  171. ```
  172. </CodeGroup>
  173. <CodeGroup title="Response">
  174. ```json {{ title: 'Response' }}
  175. {
  176. "has_more": false,
  177. "data": [
  178. {
  179. "id": "WAz8eIbvDR60rouK",
  180. "username": "FrankMcCallister",
  181. "phone_number": "1-800-759-3000",
  182. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  183. "display_name": null,
  184. "conversation_id": "xgQQXg3hrtjh7AvZ",
  185. "created_at": 692233200
  186. },
  187. {
  188. "id": "hSIhXBhNe8X1d8Et"
  189. // ...
  190. }
  191. ]
  192. }
  193. ```
  194. </CodeGroup>
  195. </Col>
  196. </Row>
  197. ---
  198. <Heading
  199. url='/conversations'
  200. method='GET'
  201. title='Get conversation list'
  202. name='#conversations'
  203. />
  204. <Row>
  205. <Col>
  206. Gets the session list of the current user. By default, the last 20 sessions are returned.
  207. ### Query
  208. <Properties>
  209. <Property name='last_id' type='string' key='last_id'>
  210. The ID of the last record on the current page, default none.
  211. </Property>
  212. <Property name='limit' type='int' key='limit'>
  213. "How many chats are returned in one request
  214. </Property>
  215. <Property name='user' type='string' key='user'>
  216. The user identifier, defined by the developer, must ensure uniqueness within the app.
  217. </Property>
  218. </Properties>
  219. </Col>
  220. <Col sticky>
  221. <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'`}>
  222. ```bash {{ title: 'cURL' }}
  223. curl --location --request GET 'https://cloud.langgenius.dev/api/conversations?user=abc-123&last_id=&limit=20' \
  224. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  225. ```
  226. </CodeGroup>
  227. <CodeGroup title="Response">
  228. ```json {{ title: 'Response' }}
  229. {
  230. "limit": 20,
  231. "has_more": false,
  232. "data": [
  233. {
  234. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  235. "name": "New chat",
  236. "inputs": {
  237. "book": "book",
  238. "myName": "Lucy"
  239. },
  240. "status": "normal",
  241. "created_at": 1679667915
  242. },
  243. {
  244. "id": "hSIhXBhNe8X1d8Et"
  245. // ...
  246. }
  247. ]
  248. }
  249. ```
  250. </CodeGroup>
  251. </Col>
  252. </Row>
  253. ---
  254. <Heading
  255. url='/conversations/{converation_id}/name'
  256. method='POST'
  257. title='Conversation renaming'
  258. name='#rename'
  259. />
  260. <Row>
  261. <Col>
  262. Rename conversations; the name is displayed in multi-session client interfaces.
  263. ### Request Body
  264. <Properties>
  265. <Property name='name' type='string' key='name'>
  266. New name
  267. </Property>
  268. <Property name='user' type='string' key='user'>
  269. The user identifier, defined by the developer, must ensure uniqueness within the app.
  270. </Property>
  271. </Properties>
  272. </Col>
  273. <Col sticky>
  274. <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}'`}>
  275. ```bash {{ title: 'cURL' }}
  276. curl --location --request POST 'https://cloud.langgenius.dev/api/conversations/{converation_id}/name' \
  277. --header 'Content-Type: application/json' \
  278. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  279. --data-raw '{
  280. "name": "",
  281. "user": "abc-123"
  282. }'
  283. ```
  284. </CodeGroup>
  285. <CodeGroup title="Response">
  286. ```json {{ title: 'Response' }}
  287. {
  288. "result": "success"
  289. }
  290. ```
  291. </CodeGroup>
  292. </Col>
  293. </Row>
  294. ---
  295. <Heading
  296. url='/conversations/{converation_id}'
  297. method='DELETE'
  298. title='Conversation deletion'
  299. name='#delete'
  300. />
  301. <Row>
  302. <Col>
  303. Delete conversation.
  304. ### Request Body
  305. <Properties>
  306. <Property name='user' type='string' key='user'>
  307. The user identifier, defined by the developer, must ensure uniqueness within the app.
  308. </Property>
  309. </Properties>
  310. </Col>
  311. <Col sticky>
  312. <CodeGroup title="Request" tag="DELETE" label="/conversations/{converation_id}" targetCode={`curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "user": "abc-123"\n}'`}>
  313. ```bash {{ title: 'cURL' }}
  314. curl --location --request DELETE 'https://cloud.langgenius.dev/api/conversations/{convsation_id}' \
  315. --header 'Content-Type: application/json' \
  316. --header 'Accept: application/json' \
  317. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  318. --data '{
  319. "user": "abc-123"
  320. }'
  321. ```
  322. </CodeGroup>
  323. <CodeGroup title="Response">
  324. ```json {{ title: 'Response' }}
  325. {
  326. "result": "success"
  327. }
  328. ```
  329. </CodeGroup>
  330. </Col>
  331. </Row>
  332. ---
  333. <Heading
  334. url='/audio-to-text'
  335. method='POST'
  336. title='speech to text'
  337. name='#audio'
  338. />
  339. <Row>
  340. <Col>
  341. Speech to text, only supports openai model.
  342. ### Request Body
  343. <Properties>
  344. <Property name='file' type='file' key='file'>
  345. Audio file.
  346. File uploads are currently limited to 15 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.
  347. </Property>
  348. </Properties>
  349. </Col>
  350. <Col sticky>
  351. <CodeGroup title="Request" tag="POST" label="/audio-to-text" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/audio-to-text' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'file=@localfile;type=audio/[mp3|mp4|mpeg|mpga|m4a|wav|webm]'`}>
  352. ```bash {{ title: 'cURL' }}
  353. curl --location --request POST 'https://cloud.langgenius.dev/api/conversations/name' \
  354. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  355. --form 'file=@localfile;type=audio/mp3'
  356. ```
  357. </CodeGroup>
  358. <CodeGroup title="Response">
  359. ```json {{ title: 'Response' }}
  360. {
  361. "text": ""
  362. }
  363. ```
  364. </CodeGroup>
  365. </Col>
  366. </Row>
  367. ---
  368. <Heading
  369. url='/parameters'
  370. method='GET'
  371. title='Obtain application parameter information'
  372. name='#rename'
  373. />
  374. <Row>
  375. <Col>
  376. Retrieve configured Input parameters, including variable names, field names, types, and default values. Typically used for displaying these fields in a form or filling in default values after the client loads.
  377. ### Query
  378. <Properties>
  379. <Property name='user' type='string' key='user'>
  380. The user identifier, defined by the developer, must ensure uniqueness within the app.
  381. </Property>
  382. </Properties>
  383. </Col>
  384. <Col sticky>
  385. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  386. ```bash {{ title: 'cURL' }}
  387. curl --location --request GET 'https://cloud.langgenius.dev/api/parameters?user=abc-123' \
  388. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  389. ```
  390. </CodeGroup>
  391. <CodeGroup title="Response">
  392. ```json {{ title: 'Response' }}
  393. {
  394. "introduction": "nice to meet you",
  395. "variables": [
  396. {
  397. "key": "book",
  398. "name": "book",
  399. "description": null,
  400. "type": "string",
  401. "default": null,
  402. "options": null
  403. },
  404. {
  405. // ...
  406. }
  407. ]
  408. }
  409. ```
  410. </CodeGroup>
  411. </Col>
  412. </Row>