template_chat.en.mdx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. <div>
  6. ### Authentication
  7. Service API of Dify authenticates using an `API-Key`.
  8. It is suggested that developers store the `API-Key` in the backend instead of sharing or storing it in the client side to avoid the leakage of the `API-Key`, which may lead to property loss.
  9. All API requests should include your `API-Key` in the **`Authorization`** HTTP Header, as shown below:
  10. <CodeGroup title="Code">
  11. ```javascript
  12. Authorization: Bearer {API_KEY}
  13. ```
  14. </CodeGroup>
  15. </div>
  16. ---
  17. <Heading
  18. url='/chat-messages'
  19. method='POST'
  20. title='Create chat message'
  21. name='#Create-Chat-Message'
  22. />
  23. <Row>
  24. <Col>
  25. Create a new conversation message or continue an existing dialogue.
  26. ### Request Body
  27. <Properties>
  28. <Property name='inputs' type='object' key='inputs'>
  29. (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.
  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='query' type='string' key='query'>
  41. User input/question content
  42. </Property>
  43. <Property name='response_mode' type='string' key='response_mode'>
  44. - Blocking type, waiting for execution to complete and returning results. (Requests may be interrupted if the process is long)
  45. - 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)**).
  46. </Property>
  47. <Property name='conversation_id' type='string' key='conversation_id'>
  48. (Required) Conversation ID: <strong>‼️ leave it empty for first-time (eg. conversation_id: "") conversation ‼️</strong>; pass conversation_id from context to continue dialogue.
  49. </Property>
  50. <Property name='user' type='string' key='user'>
  51. The user identifier, defined by the developer, must ensure uniqueness within the app.
  52. </Property>
  53. </Properties>
  54. </Col>
  55. <Col sticky>
  56. <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`}>
  57. ```bash {{ title: 'cURL' }}
  58. curl --location --request POST 'https://cloud.langgenius.dev/api/chat-messages' \
  59. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  60. --header 'Content-Type: application/json' \
  61. --data-raw '{
  62. "inputs": {},
  63. "query": "eh",
  64. "response_mode": "streaming",
  65. "conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
  66. "user": "abc-123"
  67. }'
  68. ```
  69. </CodeGroup>
  70. ### blocking
  71. <CodeGroup title="Response">
  72. ```json {{ title: 'Response' }}
  73. {
  74. "answer": "Hi, is there anything I can help you?",
  75. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  76. "created_at": 1679587005,
  77. "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
  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='Message terminal user feedback, like'
  95. name='#feedbacks'
  96. />
  97. <Row>
  98. <Col>
  99. 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.
  100. ### Path Params
  101. <Properties>
  102. <Property name='message_id' type='string' key='message_id'>
  103. Message ID
  104. </Property>
  105. </Properties>
  106. ### Request Body
  107. <Properties>
  108. <Property name='rating' type='string' key='rating'>
  109. like or dislike, null is undo
  110. </Property>
  111. <Property name='user' type='string' key='user'>
  112. The user identifier, defined by the developer, must ensure uniqueness within the app.
  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 'https://cloud.langgenius.dev/api/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. "conversation_id": "xgQQXg3hrtjh7AvZ",
  136. "inputs": {},
  137. "query": "...",
  138. "answer": "...",
  139. "feedback": "like",
  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='/messages'
  155. method='GET'
  156. title='Get the chat history message'
  157. name='#messages'
  158. />
  159. <Row>
  160. <Col>
  161. 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.
  162. ### Query
  163. <Properties>
  164. <Property name='conversation_id' type='string' key='conversation_id'>
  165. Conversation ID
  166. </Property>
  167. <Property name='first_id' type='string' key='first_id'>
  168. ID of the first chat record on the current page. The default is none.
  169. </Property>
  170. <Property name='limit' type='int' key='limit'>
  171. How many chats are returned in one request
  172. </Property>
  173. <Property name='user' type='string' key='user'>
  174. The user identifier, defined by the developer, must ensure uniqueness within the app.
  175. </Property>
  176. </Properties>
  177. </Col>
  178. <Col sticky>
  179. <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'`}>
  180. ```bash {{ title: 'cURL' }}
  181. curl --location --request GET 'https://cloud.langgenius.dev/api/messages?user=abc-123&conversation_id='
  182. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  183. ```
  184. </CodeGroup>
  185. <CodeGroup title="Response">
  186. ```json {{ title: 'Response' }}
  187. {
  188. "has_more": false,
  189. "data": [
  190. {
  191. "id": "WAz8eIbvDR60rouK",
  192. "username": "FrankMcCallister",
  193. "phone_number": "1-800-759-3000",
  194. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  195. "display_name": null,
  196. "conversation_id": "xgQQXg3hrtjh7AvZ",
  197. "created_at": 692233200
  198. },
  199. {
  200. "id": "hSIhXBhNe8X1d8Et"
  201. // ...
  202. }
  203. ]
  204. }
  205. ```
  206. </CodeGroup>
  207. </Col>
  208. </Row>
  209. ---
  210. <Heading
  211. url='/conversations'
  212. method='GET'
  213. title='Get conversation list'
  214. name='#conversations'
  215. />
  216. <Row>
  217. <Col>
  218. Gets the session list of the current user. By default, the last 20 sessions are returned.
  219. ### Query
  220. <Properties>
  221. <Property name='last_id' type='string' key='last_id'>
  222. The ID of the last record on the current page, default none.
  223. </Property>
  224. <Property name='limit' type='int' key='limit'>
  225. "How many chats are returned in one request
  226. </Property>
  227. <Property name='user' type='string' key='user'>
  228. The user identifier, defined by the developer, must ensure uniqueness within the app.
  229. </Property>
  230. </Properties>
  231. </Col>
  232. <Col sticky>
  233. <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'`}>
  234. ```bash {{ title: 'cURL' }}
  235. curl --location --request GET 'https://cloud.langgenius.dev/api/conversations?user=abc-123&last_id=&limit=20' \
  236. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  237. ```
  238. </CodeGroup>
  239. <CodeGroup title="Response">
  240. ```json {{ title: 'Response' }}
  241. {
  242. "limit": 20,
  243. "has_more": false,
  244. "data": [
  245. {
  246. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  247. "name": "New chat",
  248. "inputs": {
  249. "book": "book",
  250. "myName": "Lucy"
  251. },
  252. "status": "normal",
  253. "created_at": 1679667915
  254. },
  255. {
  256. "id": "hSIhXBhNe8X1d8Et"
  257. // ...
  258. }
  259. ]
  260. }
  261. ```
  262. </CodeGroup>
  263. </Col>
  264. </Row>
  265. ---
  266. <Heading
  267. url='/conversations/{converation_id}/name'
  268. method='POST'
  269. title='Conversation renaming'
  270. name='#rename'
  271. />
  272. <Row>
  273. <Col>
  274. Rename conversations; the name is displayed in multi-session client interfaces.
  275. ### Request Body
  276. <Properties>
  277. <Property name='name' type='string' key='name'>
  278. New name
  279. </Property>
  280. <Property name='user' type='string' key='user'>
  281. The user identifier, defined by the developer, must ensure uniqueness within the app.
  282. </Property>
  283. </Properties>
  284. </Col>
  285. <Col sticky>
  286. <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}'`}>
  287. ```bash {{ title: 'cURL' }}
  288. curl --location --request POST 'https://cloud.langgenius.dev/api/conversations/{converation_id}/name' \
  289. --header 'Content-Type: application/json' \
  290. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  291. --data-raw '{
  292. "name": "",
  293. "user": "abc-123"
  294. }'
  295. ```
  296. </CodeGroup>
  297. <CodeGroup title="Response">
  298. ```json {{ title: 'Response' }}
  299. {
  300. "result": "success"
  301. }
  302. ```
  303. </CodeGroup>
  304. </Col>
  305. </Row>
  306. ---
  307. <Heading
  308. url='/conversations/{converation_id}'
  309. method='DELETE'
  310. title='Conversation deletion'
  311. name='#delete'
  312. />
  313. <Row>
  314. <Col>
  315. Delete conversation.
  316. ### Request Body
  317. <Properties>
  318. <Property name='user' type='string' key='user'>
  319. The user identifier, defined by the developer, must ensure uniqueness within the app.
  320. </Property>
  321. </Properties>
  322. </Col>
  323. <Col sticky>
  324. <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}'`}>
  325. ```bash {{ title: 'cURL' }}
  326. curl --location --request DELETE 'https://cloud.langgenius.dev/api/conversations/{convsation_id}' \
  327. --header 'Content-Type: application/json' \
  328. --header 'Accept: application/json' \
  329. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  330. --data '{
  331. "user": "abc-123"
  332. }'
  333. ```
  334. </CodeGroup>
  335. <CodeGroup title="Response">
  336. ```json {{ title: 'Response' }}
  337. {
  338. "result": "success"
  339. }
  340. ```
  341. </CodeGroup>
  342. </Col>
  343. </Row>
  344. ---
  345. <Heading
  346. url='/audio-to-text'
  347. method='POST'
  348. title='speech to text'
  349. name='#audio'
  350. />
  351. <Row>
  352. <Col>
  353. Speech to text, only supports openai model.
  354. ### Request Body
  355. <Properties>
  356. <Property name='file' type='file' key='file'>
  357. Audio file.
  358. File uploads are currently limited to 15 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.
  359. </Property>
  360. </Properties>
  361. </Col>
  362. <Col sticky>
  363. <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]'`}>
  364. ```bash {{ title: 'cURL' }}
  365. curl --location --request POST 'https://cloud.langgenius.dev/api/conversations/name' \
  366. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  367. --form 'file=@localfile;type=audio/mp3'
  368. ```
  369. </CodeGroup>
  370. <CodeGroup title="Response">
  371. ```json {{ title: 'Response' }}
  372. {
  373. "text": ""
  374. }
  375. ```
  376. </CodeGroup>
  377. </Col>
  378. </Row>
  379. ---
  380. <Heading
  381. url='/parameters'
  382. method='GET'
  383. title='Obtain application parameter information'
  384. name='#rename'
  385. />
  386. <Row>
  387. <Col>
  388. 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.
  389. ### Query
  390. <Properties>
  391. <Property name='user' type='string' key='user'>
  392. The user identifier, defined by the developer, must ensure uniqueness within the app.
  393. </Property>
  394. </Properties>
  395. </Col>
  396. <Col sticky>
  397. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  398. ```bash {{ title: 'cURL' }}
  399. curl --location --request GET 'https://cloud.langgenius.dev/api/parameters?user=abc-123' \
  400. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  401. ```
  402. </CodeGroup>
  403. <CodeGroup title="Response">
  404. ```json {{ title: 'Response' }}
  405. {
  406. "introduction": "nice to meet you",
  407. "variables": [
  408. {
  409. "key": "book",
  410. "name": "book",
  411. "description": null,
  412. "type": "string",
  413. "default": null,
  414. "options": null
  415. },
  416. {
  417. // ...
  418. }
  419. ]
  420. }
  421. ```
  422. </CodeGroup>
  423. </Col>
  424. </Row>