template_advanced_chat.en.mdx 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
  3. # Advanced Chat App API
  4. Chat applications support session persistence, allowing previous chat history to be used as context for responses. This can be applicable for chatbots, customer service AI, etc.
  5. <div>
  6. ### Base URL
  7. <CodeGroup title="Code" targetCode={props.appDetail.api_base_url}>
  8. ```javascript
  9. ```
  10. </CodeGroup>
  11. ### Authentication
  12. The Service API uses `API-Key` authentication.
  13. <i>**Strongly recommend storing your API Key on the server-side, not shared or stored on the client-side, to avoid possible API-Key leakage that can lead to serious consequences.**</i>
  14. For all API requests, include your API Key in the `Authorization`HTTP Header, as shown below:
  15. <CodeGroup title="Code">
  16. ```javascript
  17. Authorization: Bearer {API_KEY}
  18. ```
  19. </CodeGroup>
  20. </div>
  21. ---
  22. <Heading
  23. url='/chat-messages'
  24. method='POST'
  25. title='Send Chat Message'
  26. name='#Send-Chat-Message'
  27. />
  28. <Row>
  29. <Col>
  30. Send a request to the chat application.
  31. ### Request Body
  32. <Properties>
  33. <Property name='query' type='string' key='query'>
  34. User Input/Question content
  35. </Property>
  36. <Property name='inputs' type='object' key='inputs'>
  37. Allows the entry of various variable values defined by the App.
  38. The `inputs` parameter contains multiple key/value pairs, with each key corresponding to a specific variable and each value being the specific value for that variable. Default `{}`
  39. </Property>
  40. <Property name='response_mode' type='string' key='response_mode'>
  41. The mode of response return, supporting:
  42. - `streaming` Streaming mode (recommended), implements a typewriter-like output through SSE ([Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)).
  43. - `blocking` Blocking mode, returns result after execution is complete. (Requests may be interrupted if the process is long)
  44. Due to Cloudflare restrictions, the request will be interrupted without a return after 100 seconds.
  45. </Property>
  46. <Property name='user' type='string' key='user'>
  47. User identifier, used to define the identity of the end-user for retrieval and statistics.
  48. Should be uniquely defined by the developer within the application.
  49. </Property>
  50. <Property name='conversation_id' type='string' key='conversation_id'>
  51. Converation ID, to continue the conversation based on previous chat records, it is necessary to pass the previous message's conversation_id.
  52. </Property>
  53. <Property name='files' type='array[object]' key='files'>
  54. File list, suitable for inputting files (images) combined with text understanding and answering questions, available only when the model supports Vision capability.
  55. - `type` (string) Supported type: `image` (currently only supports image type)
  56. - `transfer_method` (string) Transfer method, `remote_url` for image URL / `local_file` for file upload
  57. - `url` (string) Image URL (when the transfer method is `remote_url`)
  58. - `upload_file_id` (string) Uploaded file ID, which must be obtained by uploading through the File Upload API in advance (when the transfer method is `local_file`)
  59. </Property>
  60. <Property name='auto_generate_name' type='bool' key='auto_generate_name'>
  61. Auto-generate title, default is `true`.
  62. If set to `false`, can achieve async title generation by calling the conversation rename API and setting `auto_generate` to `true`.
  63. </Property>
  64. </Properties>
  65. ### Response
  66. When response_mode is blocking, return a CompletionResponse object.
  67. When response_mode is streaming, return a ChunkCompletionResponse stream.
  68. ### ChatCompletionResponse
  69. Returns the complete App result, `Content-Type` is `application/json`.
  70. - `message_id` (string) Unique message ID
  71. - `conversation_id` (string) Conversation ID
  72. - `mode` (string) App mode, fixed as `chat`
  73. - `answer` (string) Complete response content
  74. - `metadata` (object) Metadata
  75. - `usage` (Usage) Model usage information
  76. - `retriever_resources` (array[RetrieverResource]) Citation and Attribution List
  77. - `created_at` (int) Message creation timestamp, e.g., 1705395332
  78. ### ChunkChatCompletionResponse
  79. Returns the stream chunks outputted by the App, `Content-Type` is `text/event-stream`.
  80. Each streaming chunk starts with `data:`, separated by two newline characters `\n\n`, as shown below:
  81. <CodeGroup>
  82. ```streaming {{ title: 'Response' }}
  83. data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "663c5084-a254-4040-8ad3-51f2a3c1a77c", "answer": "Hi", "created_at": 1705398420}\n\n
  84. ```
  85. </CodeGroup>
  86. The structure of the streaming chunks varies depending on the `event`:
  87. - `event: message` LLM returns text chunk event, i.e., the complete text is output in a chunked fashion.
  88. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  89. - `message_id` (string) Unique message ID
  90. - `conversation_id` (string) Conversation ID
  91. - `answer` (string) LLM returned text chunk content
  92. - `created_at` (int) Creation timestamp, e.g., 1705395332
  93. - `event: message_file` Message file event, a new file has created by tool
  94. - `id` (string) File unique ID
  95. - `type` (string) File type,only allow "image" currently
  96. - `belongs_to` (string) Belongs to, it will only be an 'assistant' here
  97. - `url` (string) Remote url of file
  98. - `conversation_id` (string) Conversation ID
  99. - `event: message_end` Message end event, receiving this event means streaming has ended.
  100. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  101. - `message_id` (string) Unique message ID
  102. - `conversation_id` (string) Conversation ID
  103. - `metadata` (object) Metadata
  104. - `usage` (Usage) Model usage information
  105. - `retriever_resources` (array[RetrieverResource]) Citation and Attribution List
  106. - `event: message_replace` Message content replacement event.
  107. When output content moderation is enabled, if the content is flagged, then the message content will be replaced with a preset reply through this event.
  108. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  109. - `message_id` (string) Unique message ID
  110. - `conversation_id` (string) Conversation ID
  111. - `answer` (string) Replacement content (directly replaces all LLM reply text)
  112. - `created_at` (int) Creation timestamp, e.g., 1705395332
  113. - `event: workflow_started` workflow starts execution
  114. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  115. - `workflow_run_id` (string) Unique ID of workflow execution
  116. - `event` (string) fixed to `workflow_started`
  117. - `data` (object) detail
  118. - `id` (string) Unique ID of workflow execution
  119. - `workflow_id` (string) ID of relatied workflow
  120. - `sequence_number` (int) Self-increasing serial number, self-increasing in the App, starting from 1
  121. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  122. - `event: node_started` node execution started
  123. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  124. - `workflow_run_id` (string) Unique ID of workflow execution
  125. - `event` (string) fixed to `node_started`
  126. - `data` (object) detail
  127. - `id` (string) Unique ID of workflow execution
  128. - `node_id` (string) ID of node
  129. - `node_type` (string) type of node
  130. - `title` (string) name of node
  131. - `index` (int) Execution sequence number, used to display Tracing Node sequence
  132. - `predecessor_node_id` (string) optional Prefix node ID, used for canvas display execution path
  133. - `inputs` (array[object]) Contents of all preceding node variables used in the node
  134. - `created_at` (timestamp) timestamp of start, e.g., 1705395332
  135. - `event: node_finished` node execution ends, success or failure in different states in the same event
  136. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  137. - `workflow_run_id` (string) Unique ID of workflow execution
  138. - `event` (string) fixed to `node_finished`
  139. - `data` (object) detail
  140. - `id` (string) Unique ID of workflow execution
  141. - `node_id` (string) ID of node
  142. - `node_type` (string) type of node
  143. - `title` (string) name of node
  144. - `index` (int) Execution sequence number, used to display Tracing Node sequence
  145. - `predecessor_node_id` (string) optional Prefix node ID, used for canvas display execution path
  146. - `inputs` (array[object]) Contents of all preceding node variables used in the node
  147. - `process_data` (json) Optional node process data
  148. - `outputs` (json) Optional content of output
  149. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  150. - `error` (string) Optional reason of error
  151. - `elapsed_time` (float) Optional total seconds to be used
  152. - `execution_metadata` (json) meta data
  153. - `total_tokens` (int) optional tokens to be used
  154. - `total_price` (decimal) optional Total cost
  155. - `currency` (string) optional e.g. `USD` / `RMB`
  156. - `created_at` (timestamp) timestamp of start, e.g., 1705395332
  157. - `event: workflow_finished` workflow execution ends, success or failure in different states in the same event
  158. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  159. - `workflow_run_id` (string) Unique ID of workflow execution
  160. - `event` (string) fixed to `workflow_finished`
  161. - `data` (object) detail
  162. - `id` (string) ID of workflow execution
  163. - `workflow_id` (string) ID of relatied workflow
  164. - `status` (string) status of execution, `running` / `succeeded` / `failed` / `stopped`
  165. - `outputs` (json) Optional content of output
  166. - `error` (string) Optional reason of error
  167. - `elapsed_time` (float) Optional total seconds to be used
  168. - `total_tokens` (int) Optional tokens to be used
  169. - `total_steps` (int) default 0
  170. - `created_at` (timestamp) start time
  171. - `finished_at` (timestamp) end time
  172. - `event: error`
  173. Exceptions that occur during the streaming process will be output in the form of stream events, and reception of an error event will end the stream.
  174. - `task_id` (string) Task ID, used for request tracking and the below Stop Generate API
  175. - `message_id` (string) Unique message ID
  176. - `status` (int) HTTP status code
  177. - `code` (string) Error code
  178. - `message` (string) Error message
  179. - `event: ping` Ping event every 10 seconds to keep the connection alive.
  180. ### Errors
  181. - 404, Conversation does not exists
  182. - 400, `invalid_param`, abnormal parameter input
  183. - 400, `app_unavailable`, App configuration unavailable
  184. - 400, `provider_not_initialize`, no available model credential configuration
  185. - 400, `provider_quota_exceeded`, model invocation quota insufficient
  186. - 400, `model_currently_not_support`, current model unavailable
  187. - 400, `completion_request_error`, text generation failed
  188. - 500, internal server error
  189. </Col>
  190. <Col sticky>
  191. <CodeGroup title="Request" tag="POST" label="/chat-messages" targetCode={`curl -X POST '${props.appDetail.api_base_url}/chat-messages' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "inputs": ${JSON.stringify(props.inputs)},\n "query": "What are the specs of the iPhone 13 Pro Max?",\n "response_mode": "streaming",\n "conversation_id": "",\n "user": "abc-123",\n "files": [\n {\n "type": "image",\n "transfer_method": "remote_url",\n "url": "https://cloud.dify.ai/logo/logo-site.png"\n }\n ]\n}'`}>
  192. ```bash {{ title: 'cURL' }}
  193. curl -X POST '${props.appDetail.api_base_url}/chat-messages' \
  194. --header 'Authorization: Bearer {api_key}' \
  195. --header 'Content-Type: application/json' \
  196. --data-raw '{
  197. "inputs": {},
  198. "query": "eh",
  199. "response_mode": "streaming",
  200. "conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
  201. "user": "abc-123"
  202. }'
  203. ```
  204. </CodeGroup>
  205. ### Blocking Mode
  206. <CodeGroup title="Response">
  207. ```json {{ title: 'Response' }}
  208. {
  209. "event": "message",
  210. "message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  211. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  212. "mode": "chat",
  213. "answer": "iPhone 13 Pro Max specs are listed heere:...",
  214. "metadata": {
  215. "usage": {
  216. "prompt_tokens": 1033,
  217. "prompt_unit_price": "0.001",
  218. "prompt_price_unit": "0.001",
  219. "prompt_price": "0.0010330",
  220. "completion_tokens": 128,
  221. "completion_unit_price": "0.002",
  222. "completion_price_unit": "0.001",
  223. "completion_price": "0.0002560",
  224. "total_tokens": 1161,
  225. "total_price": "0.0012890",
  226. "currency": "USD",
  227. "latency": 0.7682376249867957
  228. },
  229. "retriever_resources": [
  230. {
  231. "position": 1,
  232. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  233. "dataset_name": "iPhone",
  234. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  235. "document_name": "iPhone List",
  236. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  237. "score": 0.98457545,
  238. "content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\"\n\"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""
  239. }
  240. ]
  241. },
  242. "created_at": 1705407629
  243. }
  244. ```
  245. </CodeGroup>
  246. ### Streaming Mode
  247. <CodeGroup title="Response">
  248. ```streaming {{ title: 'Response' }}
  249. data: {"event": "workflow_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "sequence_number": 1, "created_at": 1679586595}}
  250. data: {"event": "node_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "created_at": 1679586595}}
  251. data: {"event": "node_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "execution_metadata": {"total_tokens": 63127864, "total_price": 2.378, "currency": "USD"}, "created_at": 1679586595}}
  252. data: {"event": "workflow_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "total_tokens": 63127864, "total_steps": "1", "created_at": 1679586595, "finished_at": 1679976595}}
  253. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " I", "created_at": 1679586595}
  254. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": "'m", "created_at": 1679586595}
  255. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " glad", "created_at": 1679586595}
  256. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " to", "created_at": 1679586595}
  257. data: {"event": "message", "message_id": : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " meet", "created_at": 1679586595}
  258. data: {"event": "message", "message_id": : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " you", "created_at": 1679586595}
  259. data: {"event": "message_end", "id": "5e52ce04-874b-4d27-9045-b3bc80def685", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "metadata": {"usage": {"prompt_tokens": 1033, "prompt_unit_price": "0.001", "prompt_price_unit": "0.001", "prompt_price": "0.0010330", "completion_tokens": 135, "completion_unit_price": "0.002", "completion_price_unit": "0.001", "completion_price": "0.0002700", "total_tokens": 1168, "total_price": "0.0013030", "currency": "USD", "latency": 1.381760165997548, "retriever_resources": [{"position": 1, "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb", "dataset_name": "iPhone", "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00", "document_name": "iPhone List", "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a", "score": 0.98457545, "content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\"\n\"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""}]}}}
  260. ```
  261. </CodeGroup>
  262. </Col>
  263. </Row>
  264. ---
  265. <Heading
  266. url='/files/upload'
  267. method='POST'
  268. title='File Upload'
  269. name='#file-upload'
  270. />
  271. <Row>
  272. <Col>
  273. Upload a file (currently only images are supported) for use when sending messages, enabling multimodal understanding of images and text.
  274. Supports png, jpg, jpeg, webp, gif formats.
  275. Uploaded files are for use by the current end-user only.
  276. ### Request Body
  277. This interface requires a `multipart/form-data` request.
  278. - `file` (File) Required
  279. The file to be uploaded.
  280. - `user` (string) Required
  281. User identifier, defined by the developer's rules, must be unique within the application.
  282. ### Response
  283. After a successful upload, the server will return the file's ID and related information.
  284. - `id` (uuid) ID
  285. - `name` (string) File name
  286. - `size` (int) File size (bytes)
  287. - `extension` (string) File extension
  288. - `mime_type` (string) File mime-type
  289. - `created_by` (uuid) End-user ID
  290. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  291. ### Errors
  292. - 400, `no_file_uploaded`, a file must be provided
  293. - 400, `too_many_files`, currently only one file is accepted
  294. - 400, `unsupported_preview`, the file does not support preview
  295. - 400, `unsupported_estimate`, the file does not support estimation
  296. - 413, `file_too_large`, the file is too large
  297. - 415, `unsupported_file_type`, unsupported extension, currently only document files are accepted
  298. - 503, `s3_connection_failed`, unable to connect to S3 service
  299. - 503, `s3_permission_denied`, no permission to upload files to S3
  300. - 503, `s3_file_too_large`, file exceeds S3 size limit
  301. - 500, internal server error
  302. </Col>
  303. <Col sticky>
  304. ### Request Example
  305. <CodeGroup title="Request" tag="POST" label="/files/upload" targetCode={`curl -X POST '${props.appDetail.api_base_url}/files/upload' \\\n--header 'Authorization: Bearer {api_key}' \\\n--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif] \\\n--form 'user=abc-123'`}>
  306. ```bash {{ title: 'cURL' }}
  307. curl -X POST '${props.appDetail.api_base_url}/files/upload' \
  308. --header 'Authorization: Bearer {api_key}' \
  309. --form 'file=@"/path/to/file"'
  310. ```
  311. </CodeGroup>
  312. ### Response Example
  313. <CodeGroup title="Response">
  314. ```json {{ title: 'Response' }}
  315. {
  316. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  317. "name": "example.png",
  318. "size": 1024,
  319. "extension": "png",
  320. "mime_type": "image/png",
  321. "created_by": "6ad1ab0a-73ff-4ac1-b9e4-cdb312f71f13",
  322. "created_at": 1577836800,
  323. }
  324. ```
  325. </CodeGroup>
  326. </Col>
  327. </Row>
  328. ---
  329. <Heading
  330. url='/chat-messages/:task_id/stop'
  331. method='POST'
  332. title='Stop Generate'
  333. name='#stop-generatebacks'
  334. />
  335. <Row>
  336. <Col>
  337. Only supported in streaming mode.
  338. ### Path
  339. - `task_id` (string) Task ID, can be obtained from the streaming chunk return
  340. ### Request Body
  341. - `user` (string) Required
  342. User identifier, used to define the identity of the end-user, must be consistent with the user passed in the send message interface.
  343. ### Response
  344. - `result` (string) Always returns "success"
  345. </Col>
  346. <Col sticky>
  347. ### Request Example
  348. <CodeGroup title="Request" tag="POST" label="/chat-messages/:task_id/stop" targetCode={`curl -X POST '${props.appDetail.api_base_url}/chat-messages/:task_id/stop' \\\n-H 'Authorization: Bearer {api_key}' \\\n-H 'Content-Type: application/json' \\\n--data-raw '{"user": "abc-123"}'`}>
  349. ```bash {{ title: 'cURL' }}
  350. curl -X POST '${props.appDetail.api_base_url}/chat-messages/:task_id/stop' \
  351. -H 'Authorization: Bearer {api_key}' \
  352. -H 'Content-Type: application/json' \
  353. --data-raw '{
  354. "user": "abc-123"
  355. }'
  356. ```
  357. </CodeGroup>
  358. ### Response Example
  359. <CodeGroup title="Response">
  360. ```json {{ title: 'Response' }}
  361. {
  362. "result": "success"
  363. }
  364. ```
  365. </CodeGroup>
  366. </Col>
  367. </Row>
  368. ---
  369. <Heading
  370. url='/messages/:message_id/feedbacks'
  371. method='POST'
  372. title='Message Feedback'
  373. name='#feedbacks'
  374. />
  375. <Row>
  376. <Col>
  377. End-users can provide feedback messages, facilitating application developers to optimize expected outputs.
  378. ### Path
  379. <Properties>
  380. <Property name='message_id' type='string' key='message_id'>
  381. Message ID
  382. </Property>
  383. </Properties>
  384. ### Request Body
  385. <Properties>
  386. <Property name='rating' type='string' key='rating'>
  387. Upvote as `like`, downvote as `dislike`, revoke upvote as `null`
  388. </Property>
  389. <Property name='user' type='string' key='user'>
  390. User identifier, defined by the developer's rules, must be unique within the application.
  391. </Property>
  392. </Properties>
  393. ### Response
  394. - `result` (string) Always returns "success"
  395. </Col>
  396. <Col sticky>
  397. <CodeGroup title="Request" tag="POST" label="/messages/:message_id/feedbacks" targetCode={`curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks \\\n --header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "rating": "like",\n "user": "abc-123"\n}'`}>
  398. ```bash {{ title: 'cURL' }}
  399. curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks' \
  400. --header 'Authorization: Bearer {api_key}' \
  401. --header 'Content-Type: application/json' \
  402. --data-raw '{
  403. "rating": "like",
  404. "user": "abc-123"
  405. }'
  406. ```
  407. </CodeGroup>
  408. <CodeGroup title="Response">
  409. ```json {{ title: 'Response' }}
  410. {
  411. "result": "success"
  412. }
  413. ```
  414. </CodeGroup>
  415. </Col>
  416. </Row>
  417. ---
  418. <Heading
  419. url='/messages/{message_id}/suggested'
  420. method='GET'
  421. title='next suggested questions'
  422. name='#suggested'
  423. />
  424. <Row>
  425. <Col>
  426. Get next questions suggestions for the current message
  427. ### Path Params
  428. <Properties>
  429. <Property name='message_id' type='string' key='message_id'>
  430. Message ID
  431. </Property>
  432. </Properties>
  433. </Col>
  434. <Col sticky>
  435. <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'`}>
  436. ```bash {{ title: 'cURL' }}
  437. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  438. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  439. --header 'Content-Type: application/json' \
  440. ```
  441. </CodeGroup>
  442. <CodeGroup title="Response">
  443. ```json {{ title: 'Response' }}
  444. {
  445. "result": "success",
  446. "data": [
  447. "a",
  448. "b",
  449. "c"
  450. ]
  451. }
  452. ```
  453. </CodeGroup>
  454. </Col>
  455. </Row>
  456. ---
  457. <Heading
  458. url='/messages'
  459. method='GET'
  460. title='Get Conversation History Messages'
  461. name='#messages'
  462. />
  463. <Row>
  464. <Col>
  465. Returns historical chat records in a scrolling load format, with the first page returning the latest `{limit}` messages, i.e., in reverse order.
  466. ### Query
  467. <Properties>
  468. <Property name='conversation_id' type='string' key='conversation_id'>
  469. Conversation ID
  470. </Property>
  471. <Property name='user' type='string' key='user'>
  472. User identifier, used to define the identity of the end-user for retrieval and statistics.
  473. Should be uniquely defined by the developer within the application.
  474. </Property>
  475. <Property name='first_id' type='string' key='first_id'>
  476. The ID of the first chat record on the current page, default is null.
  477. </Property>
  478. <Property name='limit' type='int' key='limit'>
  479. How many chat history messages to return in one request, default is 20.
  480. </Property>
  481. </Properties>
  482. ### Response
  483. - `data` (array[object]) Message list
  484. - `id` (string) Message ID
  485. - `conversation_id` (string) Conversation ID
  486. - `inputs` (array[object]) User input parameters.
  487. - `query` (string) User input / question content.
  488. - `message_files` (array[object]) Message files
  489. - `id` (string) ID
  490. - `type` (string) File type, image for images
  491. - `url` (string) Preview image URL
  492. - `belongs_to` (string) belongs to,user orassistant
  493. - `answer` (string) Response message content
  494. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  495. - `feedback` (object) Feedback information
  496. - `rating` (string) Upvote as `like` / Downvote as `dislike`
  497. - `retriever_resources` (array[RetrieverResource]) Citation and Attribution List
  498. - `has_more` (bool) Whether there is a next page
  499. - `limit` (int) Number of returned items, if input exceeds system limit, returns system limit amount
  500. </Col>
  501. <Col sticky>
  502. <CodeGroup title="Request" tag="GET" label="/messages" targetCode={`curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='\\\n --header 'Authorization: Bearer {api_key}'`}>
  503. ```bash {{ title: 'cURL' }}
  504. curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  505. --header 'Authorization: Bearer {api_key}'
  506. ```
  507. </CodeGroup>
  508. ### Response Example
  509. <CodeGroup title="Response">
  510. ```json {{ title: 'Response' }}
  511. {
  512. "limit": 20,
  513. "has_more": false,
  514. "data": [
  515. {
  516. "id": "a076a87f-31e5-48dc-b452-0061adbbc922",
  517. "conversation_id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  518. "inputs": {
  519. "name": "dify"
  520. },
  521. "query": "iphone 13 pro",
  522. "answer": "The iPhone 13 Pro, released on September 24, 2021, features a 6.1-inch display with a resolution of 1170 x 2532. It is equipped with a Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard) processor, 6 GB of RAM, and offers storage options of 128 GB, 256 GB, 512 GB, and 1 TB. The camera is 12 MP, the battery capacity is 3095 mAh, and it runs on iOS 15.",
  523. "message_files": [],
  524. "feedback": null,
  525. "retriever_resources": [
  526. {
  527. "position": 1,
  528. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  529. "dataset_name": "iPhone",
  530. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  531. "document_name": "iPhone List",
  532. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  533. "score": 0.98457545,
  534. "content": "\"Model\",\"Release Date\",\"Display Size\",\"Resolution\",\"Processor\",\"RAM\",\"Storage\",\"Camera\",\"Battery\",\"Operating System\"\n\"iPhone 13 Pro Max\",\"September 24, 2021\",\"6.7 inch\",\"1284 x 2778\",\"Hexa-core (2x3.23 GHz Avalanche + 4x1.82 GHz Blizzard)\",\"6 GB\",\"128, 256, 512 GB, 1TB\",\"12 MP\",\"4352 mAh\",\"iOS 15\""
  535. }
  536. ],
  537. "created_at": 1705569239,
  538. }
  539. ]
  540. }
  541. ```
  542. </CodeGroup>
  543. </Col>
  544. </Row>
  545. ---
  546. <Heading
  547. url='/conversations'
  548. method='GET'
  549. title='Get Conversations'
  550. name='#conversations'
  551. />
  552. <Row>
  553. <Col>
  554. Retrieve the conversation list for the current user, defaulting to the most recent 20 entries.
  555. ### Query
  556. <Properties>
  557. <Property name='user' type='string' key='user'>
  558. User identifier, used to define the identity of the end-user for retrieval and statistics.
  559. Should be uniquely defined by the developer within the application.
  560. </Property>
  561. <Property name='last_id' type='string' key='last_id'>
  562. The ID of the last record on the current page, default is null.
  563. </Property>
  564. <Property name='limit' type='int' key='limit'>
  565. How many records to return in one request, default is the most recent 20 entries.
  566. </Property>
  567. <Property name='pinned' type='bool' key='pinned'>
  568. Return only pinned conversations as `true`, only non-pinned as `false`
  569. </Property>
  570. </Properties>
  571. ### Response
  572. - `data` (array[object]) List of conversations
  573. - `id` (string) Conversation ID
  574. - `name` (string) Conversation name, by default, is a snippet of the first question asked by the user in the conversation.
  575. - `inputs` (array[object]) User input parameters.
  576. - `introduction` (string) Introduction
  577. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  578. - `has_more` (bool)
  579. - `limit` (int) Number of entries returned, if input exceeds system limit, system limit number is returned
  580. </Col>
  581. <Col sticky>
  582. <CodeGroup title="Request" tag="GET" label="/conversations" targetCode={`curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20'`}>
  583. ```bash {{ title: 'cURL' }}
  584. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  585. --header 'Authorization: Bearer {api_key}'
  586. ```
  587. </CodeGroup>
  588. <CodeGroup title="Response">
  589. ```json {{ title: 'Response' }}
  590. {
  591. "limit": 20,
  592. "has_more": false,
  593. "data": [
  594. {
  595. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  596. "name": "New chat",
  597. "inputs": {
  598. "book": "book",
  599. "myName": "Lucy"
  600. },
  601. "status": "normal",
  602. "created_at": 1679667915
  603. },
  604. {
  605. "id": "hSIhXBhNe8X1d8Et"
  606. // ...
  607. }
  608. ]
  609. }
  610. ```
  611. </CodeGroup>
  612. </Col>
  613. </Row>
  614. ---
  615. <Heading
  616. url='/conversations/:conversation_id'
  617. method='DELETE'
  618. title='Delete Conversation'
  619. name='#delete'
  620. />
  621. <Row>
  622. <Col>
  623. Delete a conversation.
  624. ### Path
  625. - `conversation_id` (string) Conversation ID
  626. ### Request Body
  627. <Properties>
  628. <Property name='user' type='string' key='user'>
  629. The user identifier, defined by the developer, must ensure uniqueness within the application.
  630. </Property>
  631. </Properties>
  632. ### Response
  633. - `result` (string) Always returns "success"
  634. </Col>
  635. <Col sticky>
  636. <CodeGroup title="Request" tag="DELETE" label="/conversations/:conversation_id" targetCode={`curl -X DELETE '${props.appDetail.api_base_url}/conversations/:conversation_id' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "user": "abc-123"\n}'`}>
  637. ```bash {{ title: 'cURL' }}
  638. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \
  639. --header 'Content-Type: application/json' \
  640. --header 'Accept: application/json' \
  641. --header 'Authorization: Bearer {api_key}' \
  642. --data '{
  643. "user": "abc-123"
  644. }'
  645. ```
  646. </CodeGroup>
  647. <CodeGroup title="Response">
  648. ```json {{ title: 'Response' }}
  649. {
  650. "result": "success"
  651. }
  652. ```
  653. </CodeGroup>
  654. </Col>
  655. </Row>
  656. ---
  657. <Heading
  658. url='/conversations/:conversation_id/name'
  659. method='POST'
  660. title='Conversation Rename'
  661. name='#rename'
  662. />
  663. <Row>
  664. <Col>
  665. ### Request Body
  666. <Properties>
  667. <Property name='name' type='string' key='name'>
  668. The name of the conversation. This parameter can be omitted if `auto_generate` is set to `true`.
  669. </Property>
  670. <Property name='auto_generate' type='bool' key='auto_generate'>
  671. Automatically generate the title, default is `false`
  672. </Property>
  673. <Property name='user' type='string' key='user'>
  674. The user identifier, defined by the developer, must ensure uniqueness within the application.
  675. </Property>
  676. </Properties>
  677. ### Response
  678. - `id` (string) Conversation ID
  679. - `name` (string) Conversation name
  680. - `inputs` array[object] User input parameters.
  681. - `introduction` (string) Introduction
  682. - `created_at` (timestamp) Creation timestamp, e.g., 1705395332
  683. </Col>
  684. <Col sticky>
  685. <CodeGroup title="Request" tag="POST" label="/conversations/:conversation_id/name" targetCode={`curl -X POST '${props.appDetail.api_base_url}/conversations/name' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "name": "", \n "user": "abc-123"\n}'`}>
  686. ```bash {{ title: 'cURL' }}
  687. curl -X POST '${props.appDetail.api_base_url}/conversations/:conversation_id/name' \
  688. --header 'Content-Type: application/json' \
  689. --header 'Authorization: Bearer {api_key}' \
  690. --data-raw '{
  691. "name": "",
  692. "user": "abc-123"
  693. }'
  694. ```
  695. </CodeGroup>
  696. <CodeGroup title="Response">
  697. ```json {{ title: 'Response' }}
  698. {
  699. "id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  700. "name": "Chat vs AI",
  701. "inputs": {},
  702. "introduction": "",
  703. "created_at": 1705569238
  704. }
  705. ```
  706. </CodeGroup>
  707. </Col>
  708. </Row>
  709. ---
  710. <Heading
  711. url='/audio-to-text'
  712. method='POST'
  713. title='Speech to Text'
  714. name='#audio'
  715. />
  716. <Row>
  717. <Col>
  718. This endpoint requires a multipart/form-data request.
  719. ### Request Body
  720. <Properties>
  721. <Property name='file' type='file' key='file'>
  722. Audio file.
  723. Supported formats: `['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  724. File size limit: 15MB
  725. </Property>
  726. <Property name='user' type='string' key='user'>
  727. User identifier, defined by the developer's rules, must be unique within the application.
  728. </Property>
  729. </Properties>
  730. ### Response
  731. - `text` (string) Output text
  732. </Col>
  733. <Col sticky>
  734. <CodeGroup title="Request" tag="POST" label="/audio-to-text" targetCode={`curl -X POST '${props.appDetail.api_base_url}/audio-to-text' \\\n--header 'Authorization: Bearer {api_key}' \\\n--form 'file=@localfile;type=audio/[mp3|mp4|mpeg|mpga|m4a|wav|webm]'`}>
  735. ```bash {{ title: 'cURL' }}
  736. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  737. --header 'Authorization: Bearer {api_key}' \
  738. --form 'file=@localfile;type=audio/mp3'
  739. ```
  740. </CodeGroup>
  741. <CodeGroup title="Response">
  742. ```json {{ text: 'hello' }}
  743. {
  744. "text": ""
  745. }
  746. ```
  747. </CodeGroup>
  748. </Col>
  749. </Row>
  750. ---
  751. <Heading
  752. url='/text-to-audio'
  753. method='POST'
  754. title='text to audio'
  755. name='#audio'
  756. />
  757. <Row>
  758. <Col>
  759. Text to speech.
  760. ### Request Body
  761. <Properties>
  762. <Property name='text' type='str' key='text'>
  763. Speech generated content。
  764. </Property>
  765. <Property name='user' type='string' key='user'>
  766. The user identifier, defined by the developer, must ensure uniqueness within the app.
  767. </Property>
  768. <Property name='streaming' type='bool' key='streaming'>
  769. Whether to enable streaming output, true、false。
  770. </Property>
  771. </Properties>
  772. </Col>
  773. <Col sticky>
  774. <CodeGroup title="Request" tag="POST" label="/text-to-audio" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'text=Hello Dify;user=abc-123;streaming=false`}>
  775. ```bash {{ title: 'cURL' }}
  776. curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \
  777. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  778. --form 'file=Hello Dify;user=abc-123;streaming=false'
  779. ```
  780. </CodeGroup>
  781. <CodeGroup title="headers">
  782. ```json {{ title: 'headers' }}
  783. {
  784. "Content-Type": "audio/wav"
  785. }
  786. ```
  787. </CodeGroup>
  788. </Col>
  789. </Row>
  790. ---
  791. <Heading
  792. url='/parameters'
  793. method='GET'
  794. title='Get Application Information'
  795. name='#parameters'
  796. />
  797. <Row>
  798. <Col>
  799. Used at the start of entering the page to obtain information such as features, input parameter names, types, and default values.
  800. ### Query
  801. <Properties>
  802. <Property name='user' type='string' key='user'>
  803. User identifier, defined by the developer's rules, must be unique within the application.
  804. </Property>
  805. </Properties>
  806. ### Response
  807. - `opening_statement` (string) Opening statement
  808. - `suggested_questions` (array[string]) List of suggested questions for the opening
  809. - `suggested_questions_after_answer` (object) Suggest questions after enabling the answer.
  810. - `enabled` (bool) Whether it is enabled
  811. - `speech_to_text` (object) Speech to text
  812. - `enabled` (bool) Whether it is enabled
  813. - `retriever_resource` (object) Citation and Attribution
  814. - `enabled` (bool) Whether it is enabled
  815. - `annotation_reply` (object) Annotation reply
  816. - `enabled` (bool) Whether it is enabled
  817. - `user_input_form` (array[object]) User input form configuration
  818. - `text-input` (object) Text input control
  819. - `label` (string) Variable display label name
  820. - `variable` (string) Variable ID
  821. - `required` (bool) Whether it is required
  822. - `default` (string) Default value
  823. - `paragraph` (object) Paragraph text input control
  824. - `label` (string) Variable display label name
  825. - `variable` (string) Variable ID
  826. - `required` (bool) Whether it is required
  827. - `default` (string) Default value
  828. - `select` (object) Dropdown control
  829. - `label` (string) Variable display label name
  830. - `variable` (string) Variable ID
  831. - `required` (bool) Whether it is required
  832. - `default` (string) Default value
  833. - `options` (array[string]) Option values
  834. - `file_upload` (object) File upload configuration
  835. - `image` (object) Image settings
  836. Currently only supports image types: `png`, `jpg`, `jpeg`, `webp`, `gif`
  837. - `enabled` (bool) Whether it is enabled
  838. - `number_limits` (int) Image number limit, default is 3
  839. - `transfer_methods` (array[string]) List of transfer methods, remote_url, local_file, must choose one
  840. - `system_parameters` (object) System parameters
  841. - `image_file_size_limit` (string) Image file upload size limit (MB)
  842. </Col>
  843. <Col sticky>
  844. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123'`}>
  845. ```bash {{ title: 'cURL' }}
  846. curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  847. --header 'Authorization: Bearer {api_key}'
  848. ```
  849. </CodeGroup>
  850. <CodeGroup title="Response">
  851. ```json {{ title: 'Response' }}
  852. {
  853. "opening_statement": "Hello!",
  854. "suggested_questions_after_answer": {
  855. "enabled": true
  856. },
  857. "speech_to_text": {
  858. "enabled": true
  859. },
  860. "retriever_resource": {
  861. "enabled": true
  862. },
  863. "annotation_reply": {
  864. "enabled": true
  865. },
  866. "user_input_form": [
  867. {
  868. "paragraph": {
  869. "label": "Query",
  870. "variable": "query",
  871. "required": true,
  872. "default": ""
  873. }
  874. }
  875. ],
  876. "file_upload": {
  877. "image": {
  878. "enabled": false,
  879. "number_limits": 3,
  880. "detail": "high",
  881. "transfer_methods": [
  882. "remote_url",
  883. "local_file"
  884. ]
  885. }
  886. },
  887. "system_parameters": {
  888. "image_file_size_limit": "10"
  889. }
  890. }
  891. ```
  892. </CodeGroup>
  893. </Col>
  894. </Row>
  895. ---
  896. <Heading
  897. url='/meta'
  898. method='GET'
  899. title='Get Application Meta Information'
  900. name='#meta'
  901. />
  902. <Row>
  903. <Col>
  904. Used to get icons of tools in this application
  905. ### Query
  906. <Properties>
  907. <Property name='user' type='string' key='user'>
  908. User identifier, defined by the developer's rules, must be unique within the application.
  909. </Property>
  910. </Properties>
  911. ### Response
  912. - `tool_icons`(object[string]) tool icons
  913. - `tool_name` (string)
  914. - `icon` (object|string)
  915. - (object) icon object
  916. - `background` (string) background color in hex format
  917. - `content`(string) emoji
  918. - (string) url of icon
  919. </Col>
  920. <Col>
  921. <CodeGroup title="Request" tag="GET" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/meta?user=abc-123' \\\n-H 'Authorization: Bearer {api_key}'`}>
  922. ```bash {{ title: 'cURL' }}
  923. curl -X GET '${props.appDetail.api_base_url}/meta?user=abc-123' \
  924. -H 'Authorization: Bearer {api_key}'
  925. ```
  926. </CodeGroup>
  927. <CodeGroup title="Response">
  928. ```json {{ title: 'Response' }}
  929. {
  930. "tool_icons": {
  931. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  932. "api_tool": {
  933. "background": "#252525",
  934. "content": "\ud83d\ude01"
  935. }
  936. }
  937. }
  938. ```
  939. </CodeGroup>
  940. </Col>
  941. </Row>