You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

template_chat.zh.mdx 52KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx'
  3. # 对话型应用 API
  4. 对话应用支持会话持久化,可将之前的聊天记录作为上下文进行回答,可适用于聊天/客服 AI 等。
  5. <div>
  6. ### 基础 URL
  7. <CodeGroup title="Code" targetCode={props.appDetail.api_base_url}>
  8. ```javascript
  9. ```
  10. </CodeGroup>
  11. ### 鉴权
  12. Service API 使用 `API-Key` 进行鉴权。
  13. <i>**强烈建议开发者把 `API-Key` 放在后端存储,而非分享或者放在客户端存储,以免 `API-Key` 泄露,导致财产损失。**</i>
  14. 所有 API 请求都应在 **`Authorization`** HTTP Header 中包含您的 `API-Key`,如下所示:
  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='发送对话消息'
  26. name='#Create-Chat-Message'
  27. />
  28. <Row>
  29. <Col>
  30. 创建会话消息。
  31. ### Request Body
  32. <Properties>
  33. <Property name='query' type='string' key='query'>
  34. 用户输入/提问内容。
  35. </Property>
  36. <Property name='inputs' type='object' key='inputs'>
  37. 允许传入 App 定义的各变量值。
  38. inputs 参数包含了多组键值对(Key/Value pairs),每组的键对应一个特定变量,每组的值则是该变量的具体值。
  39. 默认 `{}`
  40. </Property>
  41. <Property name='response_mode' type='string' key='response_mode'>
  42. - `streaming` 流式模式(推荐)。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现类似打字机输出方式的流式返回。
  43. - `blocking` 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。
  44. <i>由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。</i>
  45. 注:Agent模式下不允许blocking。
  46. </Property>
  47. <Property name='user' type='string' key='user'>
  48. 用户标识,用于定义终端用户的身份,方便检索、统计。
  49. 由开发者定义规则,需保证用户标识在应用内唯一。
  50. </Property>
  51. <Property name='conversation_id' type='string' key='conversation_id'>
  52. (选填)会话 ID,需要基于之前的聊天记录继续对话,必须传之前消息的 conversation_id。
  53. </Property>
  54. <Property name='files' type='array[object]' key='files'>
  55. 上传的文件。
  56. - `type` (string) 支持类型:图片 `image`(目前仅支持图片格式) 。
  57. - `transfer_method` (string) 传递方式:
  58. - `remote_url`: 图片地址。
  59. - `local_file`: 上传文件。
  60. - `url` 图片地址。(仅当传递方式为 `remote_url` 时)。
  61. - `upload_file_id` 上传文件 ID。(仅当传递方式为 `local_file `时)。
  62. </Property>
  63. <Property name='auto_generate_name' type='bool' key='auto_generate_name'>
  64. (选填)自动生成标题,默认 `true`。 若设置为 `false`,则可通过调用会话重命名接口并设置 `auto_generate` 为 `true` 实现异步生成标题。
  65. </Property>
  66. </Properties>
  67. ### Response
  68. <Properties>
  69. 当 `response_mode` 为 `blocking` 时,返回 ChatCompletionResponse object。
  70. 当 `response_mode` 为 `streaming`时,返回 ChunkChatCompletionResponse object 流式序列。
  71. ### ChatCompletionResponse
  72. 返回完整的 App 结果,`Content-Type` 为 `application/json`。
  73. - `event` (string) 事件类型,固定为 `message`
  74. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  75. - `id` (string) 唯一ID
  76. - `message_id` (string) 消息唯一 ID
  77. - `conversation_id` (string) 会话 ID
  78. - `mode` (string) App 模式,固定为 chat
  79. - `answer` (string) 完整回复内容
  80. - `metadata` (object) 元数据
  81. - `usage` (Usage) 模型用量信息
  82. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  83. - `created_at` (int) 消息创建时间戳,如:1705395332
  84. ### ChunkChatCompletionResponse
  85. 返回 App 输出的流式块,`Content-Type` 为 `text/event-stream`。
  86. 每个流式块均为 data: 开头,块之间以 \n\n 即两个换行符分隔,如下所示:
  87. <CodeGroup>
  88. ```streaming {{ title: 'Response' }}
  89. data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "663c5084-a254-4040-8ad3-51f2a3c1a77c", "answer": "Hi", "created_at": 1705398420}\n\n
  90. ```
  91. </CodeGroup>
  92. 流式块中根据 event 不同,结构也不同:
  93. - `event: message` LLM 返回文本块事件,即:完整的文本以分块的方式输出。
  94. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  95. - `message_id` (string) 消息唯一 ID
  96. - `conversation_id` (string) 会话 ID
  97. - `answer` (string) LLM 返回文本块内容
  98. - `created_at` (int) 创建时间戳,如:1705395332
  99. - `event: agent_message` Agent模式下返回文本块事件,即:在Agent模式下,文章的文本以分块的方式输出(仅Agent模式下使用)
  100. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  101. - `message_id` (string) 消息唯一 ID
  102. - `conversation_id` (string) 会话 ID
  103. - `answer` (string) LLM 返回文本块内容
  104. - `created_at` (int) 创建时间戳,如:1705395332
  105. - `event: agent_thought` Agent模式下有关Agent思考步骤的相关内容,涉及到工具调用(仅Agent模式下使用)
  106. - `id` (string) agent_thought ID,每一轮Agent迭代都会有一个唯一的id
  107. - `task_id` (string) 任务ID,用于请求跟踪下方的停止响应接口
  108. - `message_id` (string) 消息唯一ID
  109. - `position` (int) agent_thought在消息中的位置,如第一轮迭代position为1
  110. - `thought` (string) agent的思考内容
  111. - `observation` (string) 工具调用的返回结果
  112. - `tool` (string) 使用的工具列表,以 ; 分割多个工具
  113. - `tool_input` (string) 工具的输入,JSON格式的字符串(object)。如:`{"dalle3": {"prompt": "a cute cat"}}`
  114. - `created_at` (int) 创建时间戳,如:1705395332
  115. - `message_files` (array[string]) 当前 `agent_thought` 关联的文件ID
  116. - `file_id` (string) 文件ID
  117. - `conversation_id` (string) 会话ID
  118. - `event: message_file` 文件事件,表示有新文件需要展示
  119. - `id` (string) 文件唯一ID
  120. - `type` (string) 文件类型,目前仅为image
  121. - `belongs_to` (string) 文件归属,user或assistant,该接口返回仅为 `assistant`
  122. - `url` (string) 文件访问地址
  123. - `conversation_id` (string) 会话ID
  124. - `event: message_end` 消息结束事件,收到此事件则代表流式返回结束。
  125. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  126. - `message_id` (string) 消息唯一 ID
  127. - `conversation_id` (string) 会话 ID
  128. - `metadata` (object) 元数据
  129. - `usage` (Usage) 模型用量信息
  130. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  131. - `event: tts_message` TTS 音频流事件,即:语音合成输出。内容是Mp3格式的音频块,使用 base64 编码后的字符串,播放的时候直接解码即可。(开启自动播放才有此消息)
  132. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  133. - `message_id` (string) 消息唯一 ID
  134. - `audio` (string) 语音合成之后的音频块使用 Base64 编码之后的文本内容,播放的时候直接 base64 解码送入播放器即可
  135. - `created_at` (int) 创建时间戳,如:1705395332
  136. - `event: tts_message_end` TTS 音频流结束事件,收到这个事件表示音频流返回结束。
  137. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  138. - `message_id` (string) 消息唯一 ID
  139. - `audio` (string) 结束事件是没有音频的,所以这里是空字符串
  140. - `created_at` (int) 创建时间戳,如:1705395332
  141. - `event: message_replace` 消息内容替换事件。
  142. 开启内容审查和审查输出内容时,若命中了审查条件,则会通过此事件替换消息内容为预设回复。
  143. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  144. - `message_id` (string) 消息唯一 ID
  145. - `conversation_id` (string) 会话 ID
  146. - `answer` (string) 替换内容(直接替换 LLM 所有回复文本)
  147. - `created_at` (int) 创建时间戳,如:1705395332
  148. - `event: error`
  149. 流式输出过程中出现的异常会以 stream event 形式输出,收到异常事件后即结束。
  150. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  151. - `message_id` (string) 消息唯一 ID
  152. - `status` (int) HTTP 状态码
  153. - `code` (string) 错误码
  154. - `message` (string) 错误消息
  155. - `event: ping` 每 10s 一次的 ping 事件,保持连接存活。
  156. ### Errors
  157. - 404,对话不存在
  158. - 400,`invalid_param`,传入参数异常
  159. - 400,`app_unavailable`,App 配置不可用
  160. - 400,`provider_not_initialize`,无可用模型凭据配置
  161. - 400,`provider_quota_exceeded`,模型调用额度不足
  162. - 400,`model_currently_not_support`,当前模型不可用
  163. - 400,`completion_request_error`,文本生成失败
  164. - 500,服务内部异常
  165. </Properties>
  166. </Col>
  167. <Col sticky>
  168. <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}'`}>
  169. ```bash {{ title: 'cURL' }}
  170. curl -X POST '${props.appDetail.api_base_url}/chat-messages' \
  171. -H 'Authorization: Bearer {api_key}' \
  172. -H 'Content-Type: application/json' \
  173. --data-raw '{
  174. "inputs": {
  175. "name": "dify"
  176. },
  177. "query": "What are the specs of the iPhone 13 Pro Max?",
  178. "conversation_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  179. "response_mode": "streaming",
  180. "user": "abc-123",
  181. "files": [
  182. {
  183. "type": "image",
  184. "transfer_method": "remote_url",
  185. "url": "https://cloud.dify.ai/logo/logo-site.png"
  186. }
  187. ]
  188. }'
  189. ```
  190. </CodeGroup>
  191. ### 阻塞模式
  192. <CodeGroup title="Response">
  193. ```json {{ title: 'Response' }}
  194. {
  195. "event": "message",
  196. "task_id": "c3800678-a077-43df-a102-53f23ed20b88",
  197. "id": "9da23599-e713-473b-982c-4328d4f5c78a",
  198. "message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  199. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  200. "mode": "chat",
  201. "answer": "iPhone 13 Pro Max specs are listed here:...",
  202. "metadata": {
  203. "usage": {
  204. "prompt_tokens": 1033,
  205. "prompt_unit_price": "0.001",
  206. "prompt_price_unit": "0.001",
  207. "prompt_price": "0.0010330",
  208. "completion_tokens": 128,
  209. "completion_unit_price": "0.002",
  210. "completion_price_unit": "0.001",
  211. "completion_price": "0.0002560",
  212. "total_tokens": 1161,
  213. "total_price": "0.0012890",
  214. "currency": "USD",
  215. "latency": 0.7682376249867957
  216. },
  217. "retriever_resources": [
  218. {
  219. "position": 1,
  220. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  221. "dataset_name": "iPhone",
  222. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  223. "document_name": "iPhone List",
  224. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  225. "score": 0.98457545,
  226. "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\""
  227. }
  228. ]
  229. },
  230. "created_at": 1705407629
  231. }
  232. ```
  233. </CodeGroup>
  234. ### 流式模式(基础助手)
  235. <CodeGroup title="Response">
  236. ```streaming {{ title: 'Response' }}
  237. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " I", "created_at": 1679586595}
  238. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": "'m", "created_at": 1679586595}
  239. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " glad", "created_at": 1679586595}
  240. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " to", "created_at": 1679586595}
  241. data: {"event": "message", "message_id" : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " meet", "created_at": 1679586595}
  242. data: {"event": "message", "message_id" : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " you", "created_at": 1679586595}
  243. 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\""}]}}
  244. data: {"event": "tts_message", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"}
  245. data: {"event": "tts_message_end", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": ""}
  246. ```
  247. </CodeGroup>
  248. ### 流式模式(智能助手)
  249. <CodeGroup title="Response">
  250. ```streaming {{ title: 'Response' }}
  251. data: {"event": "agent_thought", "id": "8dcf3648-fbad-407a-85dd-73a6f43aeb9f", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "position": 1, "thought": "", "observation": "", "tool": "", "tool_input": "", "created_at": 1705639511, "message_files": [], "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  252. data: {"event": "agent_thought", "id": "8dcf3648-fbad-407a-85dd-73a6f43aeb9f", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "position": 1, "thought": "", "observation": "", "tool": "dalle3", "tool_input": "{\"dalle3\": {\"prompt\": \"cute Japanese anime girl with white hair, blue eyes, bunny girl suit\"}}", "created_at": 1705639511, "message_files": [], "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  253. data: {"event": "message_file", "id": "d75b7a5c-ce5e-442e-ab1b-d6a5e5b557b0", "type": "image", "belongs_to": "assistant", "url": "http://127.0.0.1:5001/files/tools/d75b7a5c-ce5e-442e-ab1b-d6a5e5b557b0.png?timestamp=1705639526&nonce=70423256c60da73a9c96d1385ff78487&sign=7B5fKV9890YJuqchQvrABvW4AIupDvDvxGdu1EOJT94=", "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  254. data: {"event": "agent_thought", "id": "8dcf3648-fbad-407a-85dd-73a6f43aeb9f", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "position": 1, "thought": "", "observation": "image has been created and sent to user already, you should tell user to check it now.", "tool": "dalle3", "tool_input": "{\"dalle3\": {\"prompt\": \"cute Japanese anime girl with white hair, blue eyes, bunny girl suit\"}}", "created_at": 1705639511, "message_files": ["d75b7a5c-ce5e-442e-ab1b-d6a5e5b557b0"], "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  255. data: {"event": "agent_thought", "id": "67a99dc1-4f82-42d3-b354-18d4594840c8", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "position": 2, "thought": "", "observation": "", "tool": "", "tool_input": "", "created_at": 1705639511, "message_files": [], "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  256. data: {"event": "agent_message", "id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "answer": "I have created an image of a cute Japanese", "created_at": 1705639511, "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  257. data: {"event": "agent_message", "id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "answer": " anime girl with white hair and blue", "created_at": 1705639511, "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  258. data: {"event": "agent_message", "id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "answer": " eyes wearing a bunny girl" ,"created_at": 1705639511, "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  259. data: {"event": "agent_message", "id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "answer": " suit .", "created_at": 1705639511, "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  260. data: {"event": "agent_thought", "id": "67a99dc1-4f82-42d3-b354-18d4594840c8", "task_id": "9cf1ddd7-f94b-459b-b942-b77b26c59e9b", "message_id": "1fb10045-55fd-4040-99e6-d048d07cbad3", "position": 2, "thought": "I have created an image of a cute Japanese anime girl with white hair and blue eyes wearing a bunny girl suit.", "observation": "", "tool": "", "tool_input": "", "created_at": 1705639511, "message_files": [], "conversation_id": "c216c595-2d89-438c-b33c-aae5ddddd142"}
  261. 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\""}]}}
  262. data: {"event": "tts_message", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"}
  263. data: {"event": "tts_message_end", "conversation_id": "23dd85f3-1a41-4ea0-b7a9-062734ccfaf9", "message_id": "a8bdc41c-13b2-4c18-bfd9-054b9803038c", "created_at": 1721205487, "task_id": "3bf8a0bb-e73b-4690-9e66-4e429bad8ee7", "audio": ""}
  264. ```
  265. </CodeGroup>
  266. </Col>
  267. </Row>
  268. ---
  269. <Heading
  270. url='/files/upload'
  271. method='POST'
  272. title='上传文件'
  273. name='#files-upload'
  274. />
  275. <Row>
  276. <Col>
  277. 上传文件(目前仅支持图片)并在发送消息时使用,可实现图文多模态理解。
  278. 支持 png, jpg, jpeg, webp, gif 格式。
  279. <i>上传的文件仅供当前终端用户使用。</i>
  280. ### Request Body
  281. 该接口需使用 `multipart/form-data` 进行请求。
  282. <Properties>
  283. <Property name='file' type='file' key='file'>
  284. 要上传的文件。
  285. </Property>
  286. <Property name='user' type='string' key='user'>
  287. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
  288. </Property>
  289. </Properties>
  290. ### Response
  291. 成功上传后,服务器会返回文件的 ID 和相关信息。
  292. - `id` (uuid) ID
  293. - `name` (string) 文件名
  294. - `size` (int) 文件大小(byte)
  295. - `extension` (string) 文件后缀
  296. - `mime_type` (string) 文件 mime-type
  297. - `created_by` (uuid) 上传人 ID
  298. - `created_at` (timestamp) 上传时间
  299. ### Errors
  300. - 400,`no_file_uploaded`,必须提供文件
  301. - 400,`too_many_files`,目前只接受一个文件
  302. - 400,`unsupported_preview`,该文件不支持预览
  303. - 400,`unsupported_estimate`,该文件不支持估算
  304. - 413,`file_too_large`,文件太大
  305. - 415,`unsupported_file_type`,不支持的扩展名,当前只接受文档类文件
  306. - 503,`s3_connection_failed`,无法连接到 S3 服务
  307. - 503,`s3_permission_denied`,无权限上传文件到 S3
  308. - 503,`s3_file_too_large`,文件超出 S3 大小限制
  309. </Col>
  310. <Col sticky>
  311. <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'`}>
  312. ```bash {{ title: 'cURL' }}
  313. curl -X POST '${props.appDetail.api_base_url}/files/upload' \
  314. --header 'Authorization: Bearer {api_key}' \
  315. --form 'file=@"/path/to/file"'
  316. ```
  317. </CodeGroup>
  318. <CodeGroup title="Response">
  319. ```json {{ title: 'Response' }}
  320. {
  321. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  322. "name": "example.png",
  323. "size": 1024,
  324. "extension": "png",
  325. "mime_type": "image/png",
  326. "created_by": 123,
  327. "created_at": 1577836800,
  328. }
  329. ```
  330. </CodeGroup>
  331. </Col>
  332. </Row>
  333. ---
  334. <Heading
  335. url='/chat-messages/:task_id/stop'
  336. method='POST'
  337. title='停止响应'
  338. name='#Stop'
  339. />
  340. <Row>
  341. <Col>
  342. 仅支持流式模式。
  343. ### Path
  344. - `task_id` (string) 任务 ID,可在流式返回 Chunk 中获取
  345. ### Request Body
  346. - `user` (string) Required
  347. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
  348. ### Response
  349. - `result` (string) 固定返回 success
  350. </Col>
  351. <Col sticky>
  352. <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"}'`}>
  353. ```bash {{ title: 'cURL' }}
  354. curl -X POST '${props.appDetail.api_base_url}/chat-messages/:task_id/stop' \
  355. -H 'Authorization: Bearer {api_key}' \
  356. -H 'Content-Type: application/json' \
  357. --data-raw '{
  358. "user": "abc-123"
  359. }'
  360. ```
  361. </CodeGroup>
  362. <CodeGroup title="Response">
  363. ```json {{ title: 'Response' }}
  364. {
  365. "result": "success"
  366. }
  367. ```
  368. </CodeGroup>
  369. </Col>
  370. </Row>
  371. ---
  372. <Heading
  373. url='/messages/:message_id/feedbacks'
  374. method='POST'
  375. title='消息反馈(点赞)'
  376. name='#feedbacks'
  377. />
  378. <Row>
  379. <Col>
  380. 消息终端用户反馈、点赞,方便应用开发者优化输出预期。
  381. ### Path Params
  382. <Properties>
  383. <Property name='message_id' type='string' key='message_id'>
  384. 消息 ID
  385. </Property>
  386. </Properties>
  387. ### Request Body
  388. <Properties>
  389. <Property name='rating' type='string' key='rating'>
  390. 点赞 like, 点踩 dislike, 撤销点赞 null
  391. </Property>
  392. <Property name='user' type='string' key='user'>
  393. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  394. </Property>
  395. <Property name='content' type='string' key='content'>
  396. 消息反馈的具体信息。
  397. </Property>
  398. </Properties>
  399. ### Response
  400. - `result` (string) 固定返回 success
  401. </Col>
  402. <Col sticky>
  403. <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 "content": "message feedback information"\n}'`}>
  404. ```bash {{ title: 'cURL' }}
  405. curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks' \
  406. --header 'Authorization: Bearer {api_key}' \
  407. --header 'Content-Type: application/json' \
  408. --data-raw '{
  409. "rating": "like",
  410. "user": "abc-123",
  411. "content": "message feedback information"
  412. }'
  413. ```
  414. </CodeGroup>
  415. <CodeGroup title="Response">
  416. ```json {{ title: 'Response' }}
  417. {
  418. "result": "success"
  419. }
  420. ```
  421. </CodeGroup>
  422. </Col>
  423. </Row>
  424. ---
  425. <Heading
  426. url='/messages/{message_id}/suggested'
  427. method='GET'
  428. title='获取下一轮建议问题列表'
  429. name='#suggested'
  430. />
  431. <Row>
  432. <Col>
  433. 获取下一轮建议问题列表。
  434. ### Path Params
  435. <Properties>
  436. <Property name='message_id' type='string' key='message_id'>
  437. Message ID
  438. </Property>
  439. </Properties>
  440. ### Query
  441. <Properties>
  442. <Property name='user' type='string' key='user'>
  443. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  444. </Property>
  445. </Properties>
  446. </Col>
  447. <Col sticky>
  448. <CodeGroup title="Request" tag="GET" label="/messages/{message_id}/suggested" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested?user=abc-123 \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json'`}>
  449. ```bash {{ title: 'cURL' }}
  450. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  451. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  452. --header 'Content-Type: application/json' \
  453. ```
  454. </CodeGroup>
  455. <CodeGroup title="Response">
  456. ```json {{ title: 'Response' }}
  457. {
  458. "result": "success",
  459. "data": [
  460. "a",
  461. "b",
  462. "c"
  463. ]
  464. }
  465. ```
  466. </CodeGroup>
  467. </Col>
  468. </Row>
  469. ---
  470. ---
  471. <Heading
  472. url='/messages'
  473. method='GET'
  474. title='获取会话历史消息'
  475. name='#messages'
  476. />
  477. <Row>
  478. <Col>
  479. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,即:倒序返回。
  480. ### Query
  481. <Properties>
  482. <Property name='conversation_id' type='string' key='conversation_id'>
  483. 会话 ID
  484. </Property>
  485. <Property name='user' type='string' key='user'>
  486. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  487. </Property>
  488. <Property name='first_id' type='string' key='first_id'>
  489. 当前页第一条聊天记录的 ID,默认 null
  490. </Property>
  491. <Property name='limit' type='int' key='limit'>
  492. 一次请求返回多少条聊天记录,默认 20 条。
  493. </Property>
  494. </Properties>
  495. ### Response
  496. - `data` (array[object]) 消息列表
  497. - `id` (string) 消息 ID
  498. - `conversation_id` (string) 会话 ID
  499. - `inputs` (object) 用户输入参数。
  500. - `query` (string) 用户输入 / 提问内容。
  501. - `message_files` (array[object]) 消息文件
  502. - `id` (string) ID
  503. - `type` (string) 文件类型,image 图片
  504. - `url` (string) 预览图片地址
  505. - `belongs_to` (string) 文件归属方,user 或 assistant
  506. - `agent_thoughts` (array[object]) Agent思考内容(仅Agent模式下不为空)
  507. - `id` (string) agent_thought ID,每一轮Agent迭代都会有一个唯一的id
  508. - `message_id` (string) 消息唯一ID
  509. - `position` (int) agent_thought在消息中的位置,如第一轮迭代position为1
  510. - `thought` (string) agent的思考内容
  511. - `observation` (string) 工具调用的返回结果
  512. - `tool` (string) 使用的工具列表,以 ; 分割多个工具
  513. - `tool_input` (string) 工具的输入,JSON格式的字符串(object)。如:`{"dalle3": {"prompt": "a cute cat"}}`
  514. - `created_at` (int) 创建时间戳,如:1705395332
  515. - `message_files` (array[string]) 当前agent_thought 关联的文件ID
  516. - `file_id` (string) 文件ID
  517. - `conversation_id` (string) 会话ID
  518. - `answer` (string) 回答消息内容
  519. - `created_at` (timestamp) 创建时间
  520. - `feedback` (object) 反馈信息
  521. - `rating` (string) 点赞 like / 点踩 dislike
  522. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  523. - `has_more` (bool) 是否存在下一页
  524. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  525. </Col>
  526. <Col sticky>
  527. ### Request Example
  528. <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}'`}>
  529. ```bash {{ title: 'cURL' }}
  530. curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  531. --header 'Authorization: Bearer {api_key}'
  532. ```
  533. </CodeGroup>
  534. ### Response Example(基础助手)
  535. <CodeGroup title="Response">
  536. ```json {{ title: 'Response' }}
  537. {
  538. "limit": 20,
  539. "has_more": false,
  540. "data": [
  541. {
  542. "id": "a076a87f-31e5-48dc-b452-0061adbbc922",
  543. "conversation_id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  544. "inputs": {
  545. "name": "dify"
  546. },
  547. "query": "iphone 13 pro",
  548. "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.",
  549. "message_files": [],
  550. "feedback": null,
  551. "retriever_resources": [
  552. {
  553. "position": 1,
  554. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  555. "dataset_name": "iPhone",
  556. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  557. "document_name": "iPhone List",
  558. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  559. "score": 0.98457545,
  560. "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\""
  561. }
  562. ],
  563. "agent_thoughts": [],
  564. "created_at": 1705569239
  565. }
  566. ]
  567. }
  568. ```
  569. </CodeGroup>
  570. ### Response Example(智能助手)
  571. <CodeGroup title="Response">
  572. ```json {{ title: 'Response' }}
  573. {
  574. "limit": 20,
  575. "has_more": false,
  576. "data": [
  577. {
  578. "id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  579. "conversation_id": "957c068b-f258-4f89-ba10-6e8a0361c457",
  580. "inputs": {},
  581. "query": "draw a cat",
  582. "answer": "I have generated an image of a cat for you. Please check your messages to view the image.",
  583. "message_files": [
  584. {
  585. "id": "976990d2-5294-47e6-8f14-7356ba9d2d76",
  586. "type": "image",
  587. "url": "http://127.0.0.1:5001/files/tools/976990d2-5294-47e6-8f14-7356ba9d2d76.png?timestamp=1705988524&nonce=55df3f9f7311a9acd91bf074cd524092&sign=z43nMSO1L2HBvoqADLkRxr7Biz0fkjeDstnJiCK1zh8=",
  588. "belongs_to": "assistant"
  589. }
  590. ],
  591. "feedback": null,
  592. "retriever_resources": [],
  593. "created_at": 1705988187,
  594. "agent_thoughts": [
  595. {
  596. "id": "592c84cf-07ee-441c-9dcc-ffc66c033469",
  597. "chain_id": null,
  598. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  599. "position": 1,
  600. "thought": "",
  601. "tool": "dalle2",
  602. "tool_input": "{\"dalle2\": {\"prompt\": \"cat\"}}",
  603. "created_at": 1705988186,
  604. "observation": "image has been created and sent to user already, you should tell user to check it now.",
  605. "files": [
  606. "976990d2-5294-47e6-8f14-7356ba9d2d76"
  607. ]
  608. },
  609. {
  610. "id": "73ead60d-2370-4780-b5ed-532d2762b0e5",
  611. "chain_id": null,
  612. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  613. "position": 2,
  614. "thought": "I have generated an image of a cat for you. Please check your messages to view the image.",
  615. "tool": "",
  616. "tool_input": "",
  617. "created_at": 1705988199,
  618. "observation": "",
  619. "files": []
  620. }
  621. ]
  622. }
  623. ]
  624. }
  625. ```
  626. </CodeGroup>
  627. </Col>
  628. </Row>
  629. ---
  630. <Heading
  631. url='/conversations'
  632. method='GET'
  633. title='获取会话列表'
  634. name='#conversations'
  635. />
  636. <Row>
  637. <Col>
  638. 获取当前用户的会话列表,默认返回最近的 20 条。
  639. ### Query
  640. <Properties>
  641. <Property name='user' type='string' key='user'>
  642. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  643. </Property>
  644. <Property name='last_id' type='string' key='last_id'>
  645. (选填)当前页最后面一条记录的 ID,默认 null
  646. </Property>
  647. <Property name='limit' type='int' key='limit'>
  648. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  649. </Property>
  650. <Property name='sort_by' type='string' key='sort_by'>
  651. (选填)排序字段,默认 -updated_at(按更新时间倒序排列)
  652. - 可选值:created_at, -created_at, updated_at, -updated_at
  653. - 字段前面的符号代表顺序或倒序,-代表倒序
  654. </Property>
  655. </Properties>
  656. ### Response
  657. - `data` (array[object]) 会话列表
  658. - `id` (string) 会话 ID
  659. - `name` (string) 会话名称,默认为会话中用户最开始问题的截取。
  660. - `inputs` (object) 用户输入参数。
  661. - `status` (string) 会话状态
  662. - `introduction` (string) 开场白
  663. - `created_at` (timestamp) 创建时间
  664. - `updated_at` (timestamp) 更新时间
  665. - `has_more` (bool)
  666. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  667. </Col>
  668. <Col sticky>
  669. <CodeGroup title="Request" tag="GET" label="/conversations" targetCode={`curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20'\\\n--header 'Authorization: Bearer {api_key}'`}>
  670. ```bash {{ title: 'cURL' }}
  671. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  672. --header 'Authorization: Bearer {api_key}'
  673. ```
  674. </CodeGroup>
  675. <CodeGroup title="Response">
  676. ```json {{ title: 'Response' }}
  677. {
  678. "limit": 20,
  679. "has_more": false,
  680. "data": [
  681. {
  682. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  683. "name": "New chat",
  684. "inputs": {
  685. "book": "book",
  686. "myName": "Lucy"
  687. },
  688. "status": "normal",
  689. "created_at": 1679667915,
  690. "updated_at": 1679667915
  691. },
  692. {
  693. "id": "hSIhXBhNe8X1d8Et"
  694. // ...
  695. }
  696. ]
  697. }
  698. ```
  699. </CodeGroup>
  700. </Col>
  701. </Row>
  702. ---
  703. <Heading
  704. url='/conversations/:conversation_id'
  705. method='DELETE'
  706. title='删除会话'
  707. name='#delete'
  708. />
  709. <Row>
  710. <Col>
  711. 删除会话。
  712. ### Path
  713. - `conversation_id` (string) 会话 ID
  714. ### Request Body
  715. <Properties>
  716. <Property name='user' type='string' key='user'>
  717. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  718. </Property>
  719. </Properties>
  720. ### Response
  721. - `result` (string) 固定返回 success
  722. </Col>
  723. <Col sticky>
  724. <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}'`}>
  725. ```bash {{ title: 'cURL' }}
  726. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \
  727. --header 'Content-Type: application/json' \
  728. --header 'Accept: application/json' \
  729. --header 'Authorization: Bearer {api_key}' \
  730. --data '{
  731. "user": "abc-123"
  732. }'
  733. ```
  734. </CodeGroup>
  735. <CodeGroup title="Response">
  736. ```json {{ title: 'Response' }}
  737. {
  738. "result": "success"
  739. }
  740. ```
  741. </CodeGroup>
  742. </Col>
  743. </Row>
  744. ---
  745. <Heading
  746. url='/conversations/:conversation_id/name'
  747. method='POST'
  748. title='会话重命名'
  749. name='#rename'
  750. />
  751. <Row>
  752. <Col>
  753. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  754. ### Path
  755. - `conversation_id` (string) 会话 ID
  756. ### Request Body
  757. <Properties>
  758. <Property name='name' type='string' key='name'>
  759. (选填)名称,若 `auto_generate` 为 `true` 时,该参数可不传。
  760. </Property>
  761. <Property name='auto_generate' type='bool' key='auto_generate'>
  762. (选填)自动生成标题,默认 false。
  763. </Property>
  764. <Property name='user' type='string' key='user'>
  765. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  766. </Property>
  767. </Properties>
  768. ### Response
  769. - `id` (string) 会话 ID
  770. - `name` (string) 会话名称
  771. - `inputs` (object) 用户输入参数
  772. - `status` (string) 会话状态
  773. - `introduction` (string) 开场白
  774. - `created_at` (timestamp) 创建时间
  775. - `updated_at` (timestamp) 更新时间
  776. </Col>
  777. <Col sticky>
  778. <CodeGroup title="Request" tag="POST" label="/conversations/:conversation_id/name" targetCode={`curl -X POST '${props.appDetail.api_base_url}/conversations/:conversation_id/name' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "name": "", \n "auto_generate": true, \n "user": "abc-123"\n}'`}>
  779. ```bash {{ title: 'cURL' }}
  780. curl -X POST '${props.appDetail.api_base_url}/conversations/{conversation_id}/name' \
  781. --header 'Authorization: Bearer {api_key}' \
  782. --header 'Content-Type: application/json' \
  783. --data-raw '{
  784. "name": "",
  785. "auto_generate": true,
  786. "user": "abc-123"
  787. }'
  788. ```
  789. </CodeGroup>
  790. <CodeGroup title="Response">
  791. ```json {{ title: 'Response' }}
  792. {
  793. "id": "34d511d5-56de-4f16-a997-57b379508443",
  794. "name": "hello",
  795. "inputs": {},
  796. "status": "normal",
  797. "introduction": "",
  798. "created_at": 1732731141,
  799. "updated_at": 1732734510
  800. }
  801. ```
  802. </CodeGroup>
  803. </Col>
  804. </Row>
  805. ---
  806. <Heading
  807. url='/conversations/:conversation_id/variables'
  808. method='GET'
  809. title='获取对话变量'
  810. name='#conversation-variables'
  811. />
  812. <Row>
  813. <Col>
  814. 从特定对话中检索变量。此端点对于提取对话过程中捕获的结构化数据非常有用。
  815. ### 路径参数
  816. <Properties>
  817. <Property name='conversation_id' type='string' key='conversation_id'>
  818. 要从中检索变量的对话ID。
  819. </Property>
  820. </Properties>
  821. ### 查询参数
  822. <Properties>
  823. <Property name='user' type='string' key='user'>
  824. 用户标识符,由开发人员定义的规则,在应用程序内必须唯一。
  825. </Property>
  826. <Property name='last_id' type='string' key='last_id'>
  827. (选填)当前页最后面一条记录的 ID,默认 null
  828. </Property>
  829. <Property name='limit' type='int' key='limit'>
  830. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  831. </Property>
  832. </Properties>
  833. ### 响应
  834. - `limit` (int) 每页项目数
  835. - `has_more` (bool) 是否有更多项目
  836. - `data` (array[object]) 变量列表
  837. - `id` (string) 变量ID
  838. - `name` (string) 变量名称
  839. - `value_type` (string) 变量类型(字符串、数字、布尔等)
  840. - `value` (string) 变量值
  841. - `description` (string) 变量描述
  842. - `created_at` (int) 创建时间戳
  843. - `updated_at` (int) 最后更新时间戳
  844. ### 错误
  845. - 404, `conversation_not_exists`, 对话不存在
  846. </Col>
  847. <Col sticky>
  848. <CodeGroup title="Request" tag="GET" label="/conversations/:conversation_id/variables" targetCode={`curl -X GET '${props.appDetail.api_base_url}/conversations/{conversation_id}/variables?user=abc-123' \\\n--header 'Authorization: Bearer {api_key}'`}>
  849. ```bash {{ title: 'cURL' }}
  850. curl -X GET '${props.appDetail.api_base_url}/conversations/{conversation_id}/variables?user=abc-123' \
  851. --header 'Authorization: Bearer {api_key}'
  852. ```
  853. </CodeGroup>
  854. <CodeGroup title="Request with variable name filter">
  855. ```bash {{ title: 'cURL' }}
  856. curl -X GET '${props.appDetail.api_base_url}/conversations/{conversation_id}/variables?user=abc-123&variable_name=customer_name' \
  857. --header 'Authorization: Bearer {api_key}'
  858. ```
  859. </CodeGroup>
  860. <CodeGroup title="Response">
  861. ```json {{ title: 'Response' }}
  862. {
  863. "limit": 100,
  864. "has_more": false,
  865. "data": [
  866. {
  867. "id": "variable-uuid-1",
  868. "name": "customer_name",
  869. "value_type": "string",
  870. "value": "John Doe",
  871. "description": "客户名称(从对话中提取)",
  872. "created_at": 1650000000000,
  873. "updated_at": 1650000000000
  874. },
  875. {
  876. "id": "variable-uuid-2",
  877. "name": "order_details",
  878. "value_type": "json",
  879. "value": "{\"product\":\"Widget\",\"quantity\":5,\"price\":19.99}",
  880. "description": "客户的订单详情",
  881. "created_at": 1650000000000,
  882. "updated_at": 1650000000000
  883. }
  884. ]
  885. }
  886. ```
  887. </CodeGroup>
  888. </Col>
  889. </Row>
  890. ---
  891. <Heading
  892. url='/audio-to-text'
  893. method='POST'
  894. title='语音转文字'
  895. name='#audio'
  896. />
  897. <Row>
  898. <Col>
  899. ### Request Body
  900. 该接口需使用 `multipart/form-data` 进行请求。
  901. <Properties>
  902. <Property name='file' type='file' key='file'>
  903. 语音文件。
  904. 支持格式:`['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  905. 文件大小限制:15MB
  906. </Property>
  907. <Property name='user' type='string' key='user'>
  908. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  909. </Property>
  910. </Properties>
  911. ### Response
  912. - `text` (string) 输出文字
  913. </Col>
  914. <Col sticky>
  915. <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]`}>
  916. ```bash {{ title: 'cURL' }}
  917. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  918. --header 'Authorization: Bearer {api_key}' \
  919. --form 'file=@localfile;type=audio/mp3'
  920. ```
  921. </CodeGroup>
  922. <CodeGroup title="Response">
  923. ```json {{ title: 'Response' }}
  924. {
  925. "text": "hello"
  926. }
  927. ```
  928. </CodeGroup>
  929. </Col>
  930. </Row>
  931. ---
  932. <Heading
  933. url='/text-to-audio'
  934. method='POST'
  935. title='文字转语音'
  936. name='#audio'
  937. />
  938. <Row>
  939. <Col>
  940. 文字转语音。
  941. ### Request Body
  942. <Properties>
  943. <Property name='message_id' type='str' key='message_id'>
  944. Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。如果同时传 message_id 和 text,优先使用 message_id。
  945. </Property>
  946. <Property name='text' type='str' key='text'>
  947. 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
  948. </Property>
  949. <Property name='user' type='string' key='user'>
  950. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  951. </Property>
  952. </Properties>
  953. </Col>
  954. <Col sticky>
  955. <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=你好Dify;user=abc-123;message_id=5ad4cb98-f0c7-4085-b384-88c403be6290`}>
  956. ```bash {{ title: 'cURL' }}
  957. curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \
  958. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  959. --form 'file=你好Dify;user=abc-123;message_id=5ad4cb98-f0c7-4085-b384-88c403be6290'
  960. ```
  961. </CodeGroup>
  962. <CodeGroup title="headers">
  963. ```json {{ title: 'headers' }}
  964. {
  965. "Content-Type": "audio/wav"
  966. }
  967. ```
  968. </CodeGroup>
  969. </Col>
  970. </Row>
  971. ---
  972. <Heading
  973. url='/info'
  974. method='GET'
  975. title='获取应用基本信息'
  976. name='#info'
  977. />
  978. <Row>
  979. <Col>
  980. 用于获取应用的基本信息
  981. ### Response
  982. - `name` (string) 应用名称
  983. - `description` (string) 应用描述
  984. - `tags` (array[string]) 应用标签
  985. </Col>
  986. <Col>
  987. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info' \\\n-H 'Authorization: Bearer {api_key}'`}>
  988. ```bash {{ title: 'cURL' }}
  989. curl -X GET '${props.appDetail.api_base_url}/info' \
  990. -H 'Authorization: Bearer {api_key}'
  991. ```
  992. </CodeGroup>
  993. <CodeGroup title="Response">
  994. ```json {{ title: 'Response' }}
  995. {
  996. "name": "My App",
  997. "description": "This is my app.",
  998. "tags": [
  999. "tag1",
  1000. "tag2"
  1001. ]
  1002. }
  1003. ```
  1004. </CodeGroup>
  1005. </Col>
  1006. </Row>
  1007. ---
  1008. <Heading
  1009. url='/parameters'
  1010. method='GET'
  1011. title='获取应用参数'
  1012. name='#parameters'
  1013. />
  1014. <Row>
  1015. <Col>
  1016. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  1017. ### Response
  1018. - `opening_statement` (string) 开场白
  1019. - `suggested_questions` (array[string]) 开场推荐问题列表
  1020. - `suggested_questions_after_answer` (object) 启用回答后给出推荐问题。
  1021. - `enabled` (bool) 是否开启
  1022. - `speech_to_text` (object) 语音转文本
  1023. - `enabled` (bool) 是否开启
  1024. - `retriever_resource` (object) 引用和归属
  1025. - `enabled` (bool) 是否开启
  1026. - `annotation_reply` (object) 标记回复
  1027. - `enabled` (bool) 是否开启
  1028. - `user_input_form` (array[object]) 用户输入表单配置
  1029. - `text-input` (object) 文本输入控件
  1030. - `label` (string) 控件展示标签名
  1031. - `variable` (string) 控件 ID
  1032. - `required` (bool) 是否必填
  1033. - `default` (string) 默认值
  1034. - `paragraph` (object) 段落文本输入控件
  1035. - `label` (string) 控件展示标签名
  1036. - `variable` (string) 控件 ID
  1037. - `required` (bool) 是否必填
  1038. - `default` (string) 默认值
  1039. - `select` (object) 下拉控件
  1040. - `label` (string) 控件展示标签名
  1041. - `variable` (string) 控件 ID
  1042. - `required` (bool) 是否必填
  1043. - `default` (string) 默认值
  1044. - `options` (array[string]) 选项值
  1045. - `file_upload` (object) 文件上传配置
  1046. - `image` (object) 图片设置
  1047. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`
  1048. - `enabled` (bool) 是否开启
  1049. - `number_limits` (int) 图片数量限制,默认 3
  1050. - `transfer_methods` (array[string]) 传递方式列表,remote_url , local_file,必选一个
  1051. - `system_parameters` (object) 系统参数
  1052. - `file_size_limit` (int) 文档上传大小限制 (MB)
  1053. - `image_file_size_limit` (int) 图片文件上传大小限制(MB)
  1054. - `audio_file_size_limit` (int) 音频文件上传大小限制 (MB)
  1055. - `video_file_size_limit` (int) 视频文件上传大小限制 (MB)
  1056. </Col>
  1057. <Col sticky>
  1058. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer {api_key}'`}>
  1059. ```bash {{ title: 'cURL' }}
  1060. curl -X GET '${props.appDetail.api_base_url}/parameters' \
  1061. --header 'Authorization: Bearer {api_key}'
  1062. ```
  1063. </CodeGroup>
  1064. <CodeGroup title="Response">
  1065. ```json {{ title: 'Response' }}
  1066. {
  1067. "introduction": "nice to meet you",
  1068. "user_input_form": [
  1069. {
  1070. "text-input": {
  1071. "label": "a",
  1072. "variable": "a",
  1073. "required": true,
  1074. "max_length": 48,
  1075. "default": ""
  1076. }
  1077. },
  1078. {
  1079. // ...
  1080. }
  1081. ],
  1082. "file_upload": {
  1083. "image": {
  1084. "enabled": true,
  1085. "number_limits": 3,
  1086. "transfer_methods": [
  1087. "remote_url",
  1088. "local_file"
  1089. ]
  1090. }
  1091. },
  1092. "system_parameters": {
  1093. "file_size_limit": 15,
  1094. "image_file_size_limit": 10,
  1095. "audio_file_size_limit": 50,
  1096. "video_file_size_limit": 100
  1097. }
  1098. }
  1099. ```
  1100. </CodeGroup>
  1101. </Col>
  1102. </Row>
  1103. ---
  1104. <Heading
  1105. url='/meta'
  1106. method='GET'
  1107. title='获取应用Meta信息'
  1108. name='#meta'
  1109. />
  1110. <Row>
  1111. <Col>
  1112. 用于获取工具icon
  1113. ### Response
  1114. - `tool_icons`(object[string]) 工具图标
  1115. - `工具名称` (string)
  1116. - `icon` (object|string)
  1117. - (object) 图标
  1118. - `background` (string) hex格式的背景色
  1119. - `content`(string) emoji
  1120. - (string) 图标URL
  1121. </Col>
  1122. <Col>
  1123. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/meta' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1124. ```bash {{ title: 'cURL' }}
  1125. curl -X GET '${props.appDetail.api_base_url}/meta' \
  1126. -H 'Authorization: Bearer {api_key}'
  1127. ```
  1128. </CodeGroup>
  1129. <CodeGroup title="Response">
  1130. ```json {{ title: 'Response' }}
  1131. {
  1132. "tool_icons": {
  1133. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  1134. "api_tool": {
  1135. "background": "#252525",
  1136. "content": "\ud83d\ude01"
  1137. }
  1138. }
  1139. }
  1140. ```
  1141. </CodeGroup>
  1142. </Col>
  1143. </Row>