template_chat.en.mdx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. <Property name='files' type='array[object]' key='files'>
  54. Uploaded files.
  55. - `type` file type: currently only `image` is supported.
  56. - `transfer_method` transfer method:
  57. - `remote_url`: Image address.
  58. - `local_file`: upload file.
  59. - `upload_file_id` Upload file ID. Required when `transfer_method` is `local_file`.
  60. - `url` image address. Required when `transfer_method` is `remote_url`.
  61. </Property>
  62. </Properties>
  63. </Col>
  64. <Col sticky>
  65. <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`}>
  66. ```bash {{ title: 'cURL' }}
  67. curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \
  68. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  69. --header 'Content-Type: application/json' \
  70. --data-raw '{
  71. "inputs": {},
  72. "query": "eh",
  73. "response_mode": "streaming",
  74. "conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
  75. "user": "abc-123"
  76. }'
  77. ```
  78. </CodeGroup>
  79. ### blocking
  80. <CodeGroup title="Response">
  81. ```json {{ title: 'Response' }}
  82. {
  83. "answer": "Hi, is there anything I can help you?",
  84. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  85. "created_at": 1679587005,
  86. "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
  87. }
  88. ```
  89. </CodeGroup>
  90. ### streaming
  91. <CodeGroup title="Response">
  92. ```streaming {{ title: 'Response' }}
  93. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  94. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  95. ```
  96. </CodeGroup>
  97. </Col>
  98. </Row>
  99. ---
  100. <Heading
  101. url='/messages/{message_id}/feedbacks'
  102. method='POST'
  103. title='Message terminal user feedback, like'
  104. name='#feedbacks'
  105. />
  106. <Row>
  107. <Col>
  108. 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.
  109. ### Path Params
  110. <Properties>
  111. <Property name='message_id' type='string' key='message_id'>
  112. Message ID
  113. </Property>
  114. </Properties>
  115. ### Request Body
  116. <Properties>
  117. <Property name='rating' type='string' key='rating'>
  118. like or dislike, null is undo
  119. </Property>
  120. <Property name='user' type='string' key='user'>
  121. The user identifier, defined by the developer, must ensure uniqueness within the app.
  122. </Property>
  123. </Properties>
  124. </Col>
  125. <Col sticky>
  126. <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}'`}>
  127. ```bash {{ title: 'cURL' }}
  128. curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
  129. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  130. --header 'Content-Type: application/json' \
  131. --data-raw '{
  132. "rating": "like",
  133. "user": "abc-123"
  134. }'
  135. ```
  136. </CodeGroup>
  137. <CodeGroup title="Response">
  138. ```json {{ title: 'Response' }}
  139. {
  140. "has_more": false,
  141. "data": [
  142. {
  143. "id": "WAz8eIbvDR60rouK",
  144. "conversation_id": "xgQQXg3hrtjh7AvZ",
  145. "inputs": {},
  146. "query": "...",
  147. "answer": "...",
  148. "feedback": "like",
  149. "created_at": 692233200
  150. },
  151. {
  152. "id": "hSIhXBhNe8X1d8Et"
  153. // ...
  154. }
  155. ]
  156. }
  157. ```
  158. </CodeGroup>
  159. </Col>
  160. </Row>
  161. ---
  162. <Heading
  163. url='/messages/{message_id}/suggested'
  164. method='GET'
  165. title='next suggested questions'
  166. name='#suggested'
  167. />
  168. <Row>
  169. <Col>
  170. Get next questions suggestions for the current message
  171. ### Path Params
  172. <Properties>
  173. <Property name='message_id' type='string' key='message_id'>
  174. Message ID
  175. </Property>
  176. </Properties>
  177. </Col>
  178. <Col sticky>
  179. <CodeGroup title="Request" tag="GET" label="/messages/{message_id}/suggested" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json'`}>
  180. ```bash {{ title: 'cURL' }}
  181. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  182. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  183. --header 'Content-Type: application/json' \
  184. ```
  185. </CodeGroup>
  186. <CodeGroup title="Response">
  187. ```json {{ title: 'Response' }}
  188. {
  189. "result": "success",
  190. "data": [
  191. "a",
  192. "b",
  193. "c"
  194. ]
  195. ]
  196. }
  197. ```
  198. </CodeGroup>
  199. </Col>
  200. </Row>
  201. ---
  202. <Heading
  203. url='/messages'
  204. method='GET'
  205. title='Get the chat history message'
  206. name='#messages'
  207. />
  208. <Row>
  209. <Col>
  210. 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.
  211. ### Query
  212. <Properties>
  213. <Property name='conversation_id' type='string' key='conversation_id'>
  214. Conversation ID
  215. </Property>
  216. <Property name='first_id' type='string' key='first_id'>
  217. ID of the first chat record on the current page. The default is none.
  218. </Property>
  219. <Property name='limit' type='int' key='limit'>
  220. How many chats are returned in one request
  221. </Property>
  222. <Property name='user' type='string' key='user'>
  223. The user identifier, defined by the developer, must ensure uniqueness within the app.
  224. </Property>
  225. </Properties>
  226. </Col>
  227. <Col sticky>
  228. <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'`}>
  229. ```bash {{ title: 'cURL' }}
  230. curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  231. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  232. ```
  233. </CodeGroup>
  234. <CodeGroup title="Response">
  235. ```json {{ title: 'Response' }}
  236. {
  237. "has_more": false,
  238. "data": [
  239. {
  240. "id": "WAz8eIbvDR60rouK",
  241. "username": "FrankMcCallister",
  242. "phone_number": "1-800-759-3000",
  243. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  244. "display_name": null,
  245. "conversation_id": "xgQQXg3hrtjh7AvZ",
  246. "created_at": 692233200
  247. },
  248. {
  249. "id": "hSIhXBhNe8X1d8Et"
  250. // ...
  251. }
  252. ]
  253. }
  254. ```
  255. </CodeGroup>
  256. </Col>
  257. </Row>
  258. ---
  259. <Heading
  260. url='/conversations'
  261. method='GET'
  262. title='Get conversation list'
  263. name='#conversations'
  264. />
  265. <Row>
  266. <Col>
  267. Gets the session list of the current user. By default, the last 20 sessions are returned.
  268. ### Query
  269. <Properties>
  270. <Property name='last_id' type='string' key='last_id'>
  271. The ID of the last record on the current page, default none.
  272. </Property>
  273. <Property name='limit' type='int' key='limit'>
  274. "How many chats are returned in one request
  275. </Property>
  276. <Property name='user' type='string' key='user'>
  277. The user identifier, defined by the developer, must ensure uniqueness within the app.
  278. </Property>
  279. </Properties>
  280. </Col>
  281. <Col sticky>
  282. <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'`}>
  283. ```bash {{ title: 'cURL' }}
  284. curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  285. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  286. ```
  287. </CodeGroup>
  288. <CodeGroup title="Response">
  289. ```json {{ title: 'Response' }}
  290. {
  291. "limit": 20,
  292. "has_more": false,
  293. "data": [
  294. {
  295. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  296. "name": "New chat",
  297. "inputs": {
  298. "book": "book",
  299. "myName": "Lucy"
  300. },
  301. "status": "normal",
  302. "created_at": 1679667915
  303. },
  304. {
  305. "id": "hSIhXBhNe8X1d8Et"
  306. // ...
  307. }
  308. ]
  309. }
  310. ```
  311. </CodeGroup>
  312. </Col>
  313. </Row>
  314. ---
  315. <Heading
  316. url='/conversations/{converation_id}/name'
  317. method='POST'
  318. title='Conversation renaming'
  319. name='#rename'
  320. />
  321. <Row>
  322. <Col>
  323. Rename conversations; the name is displayed in multi-session client interfaces.
  324. ### Request Body
  325. <Properties>
  326. <Property name='name' type='string' key='name'>
  327. New name
  328. </Property>
  329. <Property name='user' type='string' key='user'>
  330. The user identifier, defined by the developer, must ensure uniqueness within the app.
  331. </Property>
  332. </Properties>
  333. </Col>
  334. <Col sticky>
  335. <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}'`}>
  336. ```bash {{ title: 'cURL' }}
  337. curl --location --request POST '${props.appDetail.api_base_url}/conversations/{converation_id}/name' \
  338. --header 'Content-Type: application/json' \
  339. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  340. --data-raw '{
  341. "name": "",
  342. "user": "abc-123"
  343. }'
  344. ```
  345. </CodeGroup>
  346. <CodeGroup title="Response">
  347. ```json {{ title: 'Response' }}
  348. {
  349. "result": "success"
  350. }
  351. ```
  352. </CodeGroup>
  353. </Col>
  354. </Row>
  355. ---
  356. <Heading
  357. url='/conversations/{converation_id}'
  358. method='DELETE'
  359. title='Conversation deletion'
  360. name='#delete'
  361. />
  362. <Row>
  363. <Col>
  364. Delete conversation.
  365. ### Request Body
  366. <Properties>
  367. <Property name='user' type='string' key='user'>
  368. The user identifier, defined by the developer, must ensure uniqueness within the app.
  369. </Property>
  370. </Properties>
  371. </Col>
  372. <Col sticky>
  373. <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}'`}>
  374. ```bash {{ title: 'cURL' }}
  375. curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \
  376. --header 'Content-Type: application/json' \
  377. --header 'Accept: application/json' \
  378. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  379. --data '{
  380. "user": "abc-123"
  381. }'
  382. ```
  383. </CodeGroup>
  384. <CodeGroup title="Response">
  385. ```json {{ title: 'Response' }}
  386. {
  387. "result": "success"
  388. }
  389. ```
  390. </CodeGroup>
  391. </Col>
  392. </Row>
  393. ---
  394. <Heading
  395. url='/files/upload'
  396. method='POST'
  397. title='Upload files'
  398. name='#files-upload'
  399. />
  400. <Row>
  401. <Col>
  402. Upload files to the server for text generation and dialogue. Uploaded files are only available to the current end user.
  403. ### Request Body
  404. The request method is `multipart/form-data`
  405. <Properties>
  406. <Property name='file' type='file' key='file'>
  407. The file to upload. Currently supports png, jpg, jpeg, webp, gif format files.
  408. </Property>
  409. <Property name='user' type='string' key='user'>
  410. User ID, rules are defined by the developer, and it is necessary to ensure that the user ID is unique within the application.
  411. </Property>
  412. </Properties>
  413. </Col>
  414. <Col sticky>
  415. <CodeGroup title="Request" tag="POST" label="/files/upload" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif] \\\n--form 'user=abc-123'`}>
  416. ```bash {{ title: 'cURL' }}
  417. curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \
  418. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  419. --form 'file=@"/path/to/file"'
  420. ```
  421. </CodeGroup>
  422. <CodeGroup title="Response">
  423. ```json {{ title: 'Response' }}
  424. {
  425. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  426. "name": "example.png",
  427. "size": 1024,
  428. "extension": "png",
  429. "mime_type": "image/png",
  430. "created_by": 123,
  431. "created_at": 1577836800,
  432. }
  433. ```
  434. </CodeGroup>
  435. </Col>
  436. </Row>
  437. ---
  438. <Heading
  439. url='/audio-to-text'
  440. method='POST'
  441. title='speech to text'
  442. name='#audio'
  443. />
  444. <Row>
  445. <Col>
  446. Speech to text, only supports openai model.
  447. ### Request Body
  448. <Properties>
  449. <Property name='file' type='file' key='file'>
  450. Audio file.
  451. File uploads are currently limited to 15 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.
  452. </Property>
  453. </Properties>
  454. </Col>
  455. <Col sticky>
  456. <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]'`}>
  457. ```bash {{ title: 'cURL' }}
  458. curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \
  459. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  460. --form 'file=@localfile;type=audio/mp3'
  461. ```
  462. </CodeGroup>
  463. <CodeGroup title="Response">
  464. ```json {{ title: 'Response' }}
  465. {
  466. "text": ""
  467. }
  468. ```
  469. </CodeGroup>
  470. </Col>
  471. </Row>
  472. ---
  473. <Heading
  474. url='/parameters'
  475. method='GET'
  476. title='Obtain application parameter information'
  477. name='#rename'
  478. />
  479. <Row>
  480. <Col>
  481. Content include:
  482. 1. 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.
  483. 2. Configuration of uploading images, including whether to enable image uploading, the number of uploaded images and the uploading method. Note: This configuration is only available when using a model that supports multimodality.
  484. ### Query
  485. <Properties>
  486. <Property name='user' type='string' key='user'>
  487. The user identifier, defined by the developer, must ensure uniqueness within the app.
  488. </Property>
  489. </Properties>
  490. </Col>
  491. <Col sticky>
  492. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  493. ```bash {{ title: 'cURL' }}
  494. curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  495. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  496. ```
  497. </CodeGroup>
  498. <CodeGroup title="Response">
  499. ```json {{ title: 'Response' }}
  500. {
  501. "introduction": "nice to meet you",
  502. "user_input_form": [
  503. {
  504. "text-input": {
  505. "label": "a",
  506. "variable": "a",
  507. "required": true,
  508. "max_length": 48,
  509. "default": ""
  510. }
  511. }
  512. {
  513. // ...
  514. }
  515. ],
  516. "file_upload": {
  517. "image": {
  518. "enabled": true,
  519. "number_limits": 3,
  520. "transfer_methods": [
  521. "remote_url",
  522. "local_file"
  523. ]
  524. }
  525. }
  526. }
  527. ```
  528. </CodeGroup>
  529. </Col>
  530. </Row>