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 57KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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='/app/feedbacks'
  427. method='GET'
  428. title='获取APP的消息点赞和反馈'
  429. name='#app-feedbacks'
  430. />
  431. <Row>
  432. <Col>
  433. 获取应用的终端用户反馈、点赞。
  434. ### Query
  435. <Properties>
  436. <Property name='page' type='string' key='page'>
  437. (选填)分页,默认值:1
  438. </Property>
  439. </Properties>
  440. <Properties>
  441. <Property name='limit' type='string' key='limit'>
  442. (选填)每页数量,默认值:20
  443. </Property>
  444. </Properties>
  445. ### Response
  446. - `data` (List) 返回该APP的点赞、反馈列表。
  447. </Col>
  448. <Col sticky>
  449. <CodeGroup title="Request" tag="GET" label="/app/feedbacks" targetCode={`curl -X GET '${props.appDetail.api_base_url}/app/feedbacks?page=1&limit=20'`}>
  450. ```bash {{ title: 'cURL' }}
  451. curl -X GET '${props.appDetail.api_base_url}/app/feedbacks?page=1&limit=20' \
  452. --header 'Authorization: Bearer {api_key}' \
  453. --header 'Content-Type: application/json'
  454. ```
  455. </CodeGroup>
  456. <CodeGroup title="Response">
  457. ```json {{ title: 'Response' }}
  458. {
  459. "data": [
  460. {
  461. "id": "8c0fbed8-e2f9-49ff-9f0e-15a35bdd0e25",
  462. "app_id": "f252d396-fe48-450e-94ec-e184218e7346",
  463. "conversation_id": "2397604b-9deb-430e-b285-4726e51fd62d",
  464. "message_id": "709c0b0f-0a96-4a4e-91a4-ec0889937b11",
  465. "rating": "like",
  466. "content": "message feedback information-3",
  467. "from_source": "user",
  468. "from_end_user_id": "74286412-9a1a-42c1-929c-01edb1d381d5",
  469. "from_account_id": null,
  470. "created_at": "2025-04-24T09:24:38",
  471. "updated_at": "2025-04-24T09:24:38"
  472. }
  473. ]
  474. }
  475. ```
  476. </CodeGroup>
  477. </Col>
  478. </Row>
  479. ---
  480. <Heading
  481. url='/messages/{message_id}/suggested'
  482. method='GET'
  483. title='获取下一轮建议问题列表'
  484. name='#suggested'
  485. />
  486. <Row>
  487. <Col>
  488. 获取下一轮建议问题列表。
  489. ### Path Params
  490. <Properties>
  491. <Property name='message_id' type='string' key='message_id'>
  492. Message ID
  493. </Property>
  494. </Properties>
  495. ### Query
  496. <Properties>
  497. <Property name='user' type='string' key='user'>
  498. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  499. </Property>
  500. </Properties>
  501. </Col>
  502. <Col sticky>
  503. <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'`}>
  504. ```bash {{ title: 'cURL' }}
  505. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  506. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  507. --header 'Content-Type: application/json' \
  508. ```
  509. </CodeGroup>
  510. <CodeGroup title="Response">
  511. ```json {{ title: 'Response' }}
  512. {
  513. "result": "success",
  514. "data": [
  515. "a",
  516. "b",
  517. "c"
  518. ]
  519. }
  520. ```
  521. </CodeGroup>
  522. </Col>
  523. </Row>
  524. ---
  525. ---
  526. <Heading
  527. url='/messages'
  528. method='GET'
  529. title='获取会话历史消息'
  530. name='#messages'
  531. />
  532. <Row>
  533. <Col>
  534. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,即:倒序返回。
  535. ### Query
  536. <Properties>
  537. <Property name='conversation_id' type='string' key='conversation_id'>
  538. 会话 ID
  539. </Property>
  540. <Property name='user' type='string' key='user'>
  541. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  542. </Property>
  543. <Property name='first_id' type='string' key='first_id'>
  544. 当前页第一条聊天记录的 ID,默认 null
  545. </Property>
  546. <Property name='limit' type='int' key='limit'>
  547. 一次请求返回多少条聊天记录,默认 20 条。
  548. </Property>
  549. </Properties>
  550. ### Response
  551. - `data` (array[object]) 消息列表
  552. - `id` (string) 消息 ID
  553. - `conversation_id` (string) 会话 ID
  554. - `inputs` (object) 用户输入参数。
  555. - `query` (string) 用户输入 / 提问内容。
  556. - `message_files` (array[object]) 消息文件
  557. - `id` (string) ID
  558. - `type` (string) 文件类型,image 图片
  559. - `url` (string) 预览图片地址
  560. - `belongs_to` (string) 文件归属方,user 或 assistant
  561. - `agent_thoughts` (array[object]) Agent思考内容(仅Agent模式下不为空)
  562. - `id` (string) agent_thought ID,每一轮Agent迭代都会有一个唯一的id
  563. - `message_id` (string) 消息唯一ID
  564. - `position` (int) agent_thought在消息中的位置,如第一轮迭代position为1
  565. - `thought` (string) agent的思考内容
  566. - `observation` (string) 工具调用的返回结果
  567. - `tool` (string) 使用的工具列表,以 ; 分割多个工具
  568. - `tool_input` (string) 工具的输入,JSON格式的字符串(object)。如:`{"dalle3": {"prompt": "a cute cat"}}`
  569. - `created_at` (int) 创建时间戳,如:1705395332
  570. - `message_files` (array[string]) 当前agent_thought 关联的文件ID
  571. - `file_id` (string) 文件ID
  572. - `conversation_id` (string) 会话ID
  573. - `answer` (string) 回答消息内容
  574. - `created_at` (timestamp) 创建时间
  575. - `feedback` (object) 反馈信息
  576. - `rating` (string) 点赞 like / 点踩 dislike
  577. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  578. - `has_more` (bool) 是否存在下一页
  579. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  580. </Col>
  581. <Col sticky>
  582. ### Request Example
  583. <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}'`}>
  584. ```bash {{ title: 'cURL' }}
  585. curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  586. --header 'Authorization: Bearer {api_key}'
  587. ```
  588. </CodeGroup>
  589. ### Response Example(基础助手)
  590. <CodeGroup title="Response">
  591. ```json {{ title: 'Response' }}
  592. {
  593. "limit": 20,
  594. "has_more": false,
  595. "data": [
  596. {
  597. "id": "a076a87f-31e5-48dc-b452-0061adbbc922",
  598. "conversation_id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  599. "inputs": {
  600. "name": "dify"
  601. },
  602. "query": "iphone 13 pro",
  603. "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.",
  604. "message_files": [],
  605. "feedback": null,
  606. "retriever_resources": [
  607. {
  608. "position": 1,
  609. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  610. "dataset_name": "iPhone",
  611. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  612. "document_name": "iPhone List",
  613. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  614. "score": 0.98457545,
  615. "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\""
  616. }
  617. ],
  618. "agent_thoughts": [],
  619. "created_at": 1705569239
  620. }
  621. ]
  622. }
  623. ```
  624. </CodeGroup>
  625. ### Response Example(智能助手)
  626. <CodeGroup title="Response">
  627. ```json {{ title: 'Response' }}
  628. {
  629. "limit": 20,
  630. "has_more": false,
  631. "data": [
  632. {
  633. "id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  634. "conversation_id": "957c068b-f258-4f89-ba10-6e8a0361c457",
  635. "inputs": {},
  636. "query": "draw a cat",
  637. "answer": "I have generated an image of a cat for you. Please check your messages to view the image.",
  638. "message_files": [
  639. {
  640. "id": "976990d2-5294-47e6-8f14-7356ba9d2d76",
  641. "type": "image",
  642. "url": "http://127.0.0.1:5001/files/tools/976990d2-5294-47e6-8f14-7356ba9d2d76.png?timestamp=1705988524&nonce=55df3f9f7311a9acd91bf074cd524092&sign=z43nMSO1L2HBvoqADLkRxr7Biz0fkjeDstnJiCK1zh8=",
  643. "belongs_to": "assistant"
  644. }
  645. ],
  646. "feedback": null,
  647. "retriever_resources": [],
  648. "created_at": 1705988187,
  649. "agent_thoughts": [
  650. {
  651. "id": "592c84cf-07ee-441c-9dcc-ffc66c033469",
  652. "chain_id": null,
  653. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  654. "position": 1,
  655. "thought": "",
  656. "tool": "dalle2",
  657. "tool_input": "{\"dalle2\": {\"prompt\": \"cat\"}}",
  658. "created_at": 1705988186,
  659. "observation": "image has been created and sent to user already, you should tell user to check it now.",
  660. "files": [
  661. "976990d2-5294-47e6-8f14-7356ba9d2d76"
  662. ]
  663. },
  664. {
  665. "id": "73ead60d-2370-4780-b5ed-532d2762b0e5",
  666. "chain_id": null,
  667. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  668. "position": 2,
  669. "thought": "I have generated an image of a cat for you. Please check your messages to view the image.",
  670. "tool": "",
  671. "tool_input": "",
  672. "created_at": 1705988199,
  673. "observation": "",
  674. "files": []
  675. }
  676. ]
  677. }
  678. ]
  679. }
  680. ```
  681. </CodeGroup>
  682. </Col>
  683. </Row>
  684. ---
  685. <Heading
  686. url='/conversations'
  687. method='GET'
  688. title='获取会话列表'
  689. name='#conversations'
  690. />
  691. <Row>
  692. <Col>
  693. 获取当前用户的会话列表,默认返回最近的 20 条。
  694. ### Query
  695. <Properties>
  696. <Property name='user' type='string' key='user'>
  697. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  698. </Property>
  699. <Property name='last_id' type='string' key='last_id'>
  700. (选填)当前页最后面一条记录的 ID,默认 null
  701. </Property>
  702. <Property name='limit' type='int' key='limit'>
  703. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  704. </Property>
  705. <Property name='sort_by' type='string' key='sort_by'>
  706. (选填)排序字段,默认 -updated_at(按更新时间倒序排列)
  707. - 可选值:created_at, -created_at, updated_at, -updated_at
  708. - 字段前面的符号代表顺序或倒序,-代表倒序
  709. </Property>
  710. </Properties>
  711. ### Response
  712. - `data` (array[object]) 会话列表
  713. - `id` (string) 会话 ID
  714. - `name` (string) 会话名称,默认为会话中用户最开始问题的截取。
  715. - `inputs` (object) 用户输入参数。
  716. - `status` (string) 会话状态
  717. - `introduction` (string) 开场白
  718. - `created_at` (timestamp) 创建时间
  719. - `updated_at` (timestamp) 更新时间
  720. - `has_more` (bool)
  721. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  722. </Col>
  723. <Col sticky>
  724. <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}'`}>
  725. ```bash {{ title: 'cURL' }}
  726. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  727. --header 'Authorization: Bearer {api_key}'
  728. ```
  729. </CodeGroup>
  730. <CodeGroup title="Response">
  731. ```json {{ title: 'Response' }}
  732. {
  733. "limit": 20,
  734. "has_more": false,
  735. "data": [
  736. {
  737. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  738. "name": "New chat",
  739. "inputs": {
  740. "book": "book",
  741. "myName": "Lucy"
  742. },
  743. "status": "normal",
  744. "created_at": 1679667915,
  745. "updated_at": 1679667915
  746. },
  747. {
  748. "id": "hSIhXBhNe8X1d8Et"
  749. // ...
  750. }
  751. ]
  752. }
  753. ```
  754. </CodeGroup>
  755. </Col>
  756. </Row>
  757. ---
  758. <Heading
  759. url='/conversations/:conversation_id'
  760. method='DELETE'
  761. title='删除会话'
  762. name='#delete'
  763. />
  764. <Row>
  765. <Col>
  766. 删除会话。
  767. ### Path
  768. - `conversation_id` (string) 会话 ID
  769. ### Request Body
  770. <Properties>
  771. <Property name='user' type='string' key='user'>
  772. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  773. </Property>
  774. </Properties>
  775. ### Response
  776. - `result` (string) 固定返回 success
  777. </Col>
  778. <Col sticky>
  779. <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}'`}>
  780. ```bash {{ title: 'cURL' }}
  781. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \
  782. --header 'Content-Type: application/json' \
  783. --header 'Accept: application/json' \
  784. --header 'Authorization: Bearer {api_key}' \
  785. --data '{
  786. "user": "abc-123"
  787. }'
  788. ```
  789. </CodeGroup>
  790. <CodeGroup title="Response">
  791. ```text {{ title: 'Response' }}
  792. 204 No Content
  793. ```
  794. </CodeGroup>
  795. </Col>
  796. </Row>
  797. ---
  798. <Heading
  799. url='/conversations/:conversation_id/name'
  800. method='POST'
  801. title='会话重命名'
  802. name='#rename'
  803. />
  804. <Row>
  805. <Col>
  806. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  807. ### Path
  808. - `conversation_id` (string) 会话 ID
  809. ### Request Body
  810. <Properties>
  811. <Property name='name' type='string' key='name'>
  812. (选填)名称,若 `auto_generate` 为 `true` 时,该参数可不传。
  813. </Property>
  814. <Property name='auto_generate' type='bool' key='auto_generate'>
  815. (选填)自动生成标题,默认 false。
  816. </Property>
  817. <Property name='user' type='string' key='user'>
  818. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  819. </Property>
  820. </Properties>
  821. ### Response
  822. - `id` (string) 会话 ID
  823. - `name` (string) 会话名称
  824. - `inputs` (object) 用户输入参数
  825. - `status` (string) 会话状态
  826. - `introduction` (string) 开场白
  827. - `created_at` (timestamp) 创建时间
  828. - `updated_at` (timestamp) 更新时间
  829. </Col>
  830. <Col sticky>
  831. <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}'`}>
  832. ```bash {{ title: 'cURL' }}
  833. curl -X POST '${props.appDetail.api_base_url}/conversations/{conversation_id}/name' \
  834. --header 'Authorization: Bearer {api_key}' \
  835. --header 'Content-Type: application/json' \
  836. --data-raw '{
  837. "name": "",
  838. "auto_generate": true,
  839. "user": "abc-123"
  840. }'
  841. ```
  842. </CodeGroup>
  843. <CodeGroup title="Response">
  844. ```json {{ title: 'Response' }}
  845. {
  846. "id": "34d511d5-56de-4f16-a997-57b379508443",
  847. "name": "hello",
  848. "inputs": {},
  849. "status": "normal",
  850. "introduction": "",
  851. "created_at": 1732731141,
  852. "updated_at": 1732734510
  853. }
  854. ```
  855. </CodeGroup>
  856. </Col>
  857. </Row>
  858. ---
  859. <Heading
  860. url='/conversations/:conversation_id/variables'
  861. method='GET'
  862. title='获取对话变量'
  863. name='#conversation-variables'
  864. />
  865. <Row>
  866. <Col>
  867. 从特定对话中检索变量。此端点对于提取对话过程中捕获的结构化数据非常有用。
  868. ### 路径参数
  869. <Properties>
  870. <Property name='conversation_id' type='string' key='conversation_id'>
  871. 要从中检索变量的对话ID。
  872. </Property>
  873. </Properties>
  874. ### 查询参数
  875. <Properties>
  876. <Property name='user' type='string' key='user'>
  877. 用户标识符,由开发人员定义的规则,在应用程序内必须唯一。
  878. </Property>
  879. <Property name='last_id' type='string' key='last_id'>
  880. (选填)当前页最后面一条记录的 ID,默认 null
  881. </Property>
  882. <Property name='limit' type='int' key='limit'>
  883. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  884. </Property>
  885. </Properties>
  886. ### 响应
  887. - `limit` (int) 每页项目数
  888. - `has_more` (bool) 是否有更多项目
  889. - `data` (array[object]) 变量列表
  890. - `id` (string) 变量 ID
  891. - `name` (string) 变量名称
  892. - `value_type` (string) 变量类型(字符串、数字、布尔等)
  893. - `value` (string) 变量值
  894. - `description` (string) 变量描述
  895. - `created_at` (int) 创建时间戳
  896. - `updated_at` (int) 最后更新时间戳
  897. ### 错误
  898. - 404, `conversation_not_exists`, 对话不存在
  899. </Col>
  900. <Col sticky>
  901. <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}'`}>
  902. ```bash {{ title: 'cURL' }}
  903. curl -X GET '${props.appDetail.api_base_url}/conversations/{conversation_id}/variables?user=abc-123' \
  904. --header 'Authorization: Bearer {api_key}'
  905. ```
  906. </CodeGroup>
  907. <CodeGroup title="Request with variable name filter">
  908. ```bash {{ title: 'cURL' }}
  909. curl -X GET '${props.appDetail.api_base_url}/conversations/{conversation_id}/variables?user=abc-123&variable_name=customer_name' \
  910. --header 'Authorization: Bearer {api_key}'
  911. ```
  912. </CodeGroup>
  913. <CodeGroup title="Response">
  914. ```json {{ title: 'Response' }}
  915. {
  916. "limit": 100,
  917. "has_more": false,
  918. "data": [
  919. {
  920. "id": "variable-uuid-1",
  921. "name": "customer_name",
  922. "value_type": "string",
  923. "value": "John Doe",
  924. "description": "客户名称(从对话中提取)",
  925. "created_at": 1650000000000,
  926. "updated_at": 1650000000000
  927. },
  928. {
  929. "id": "variable-uuid-2",
  930. "name": "order_details",
  931. "value_type": "json",
  932. "value": "{\"product\":\"Widget\",\"quantity\":5,\"price\":19.99}",
  933. "description": "客户的订单详情",
  934. "created_at": 1650000000000,
  935. "updated_at": 1650000000000
  936. }
  937. ]
  938. }
  939. ```
  940. </CodeGroup>
  941. </Col>
  942. </Row>
  943. ---
  944. <Heading
  945. url='/audio-to-text'
  946. method='POST'
  947. title='语音转文字'
  948. name='#audio'
  949. />
  950. <Row>
  951. <Col>
  952. ### Request Body
  953. 该接口需使用 `multipart/form-data` 进行请求。
  954. <Properties>
  955. <Property name='file' type='file' key='file'>
  956. 语音文件。
  957. 支持格式:`['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  958. 文件大小限制:15MB
  959. </Property>
  960. <Property name='user' type='string' key='user'>
  961. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  962. </Property>
  963. </Properties>
  964. ### Response
  965. - `text` (string) 输出文字
  966. </Col>
  967. <Col sticky>
  968. <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]`}>
  969. ```bash {{ title: 'cURL' }}
  970. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  971. --header 'Authorization: Bearer {api_key}' \
  972. --form 'file=@localfile;type=audio/mp3'
  973. ```
  974. </CodeGroup>
  975. <CodeGroup title="Response">
  976. ```json {{ title: 'Response' }}
  977. {
  978. "text": "hello"
  979. }
  980. ```
  981. </CodeGroup>
  982. </Col>
  983. </Row>
  984. ---
  985. <Heading
  986. url='/text-to-audio'
  987. method='POST'
  988. title='文字转语音'
  989. name='#audio'
  990. />
  991. <Row>
  992. <Col>
  993. 文字转语音。
  994. ### Request Body
  995. <Properties>
  996. <Property name='message_id' type='str' key='message_id'>
  997. Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。如果同时传 message_id 和 text,优先使用 message_id。
  998. </Property>
  999. <Property name='text' type='str' key='text'>
  1000. 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
  1001. </Property>
  1002. <Property name='user' type='string' key='user'>
  1003. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  1004. </Property>
  1005. </Properties>
  1006. </Col>
  1007. <Col sticky>
  1008. <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`}>
  1009. ```bash {{ title: 'cURL' }}
  1010. curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \
  1011. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  1012. --form 'file=你好Dify;user=abc-123;message_id=5ad4cb98-f0c7-4085-b384-88c403be6290'
  1013. ```
  1014. </CodeGroup>
  1015. <CodeGroup title="headers">
  1016. ```json {{ title: 'headers' }}
  1017. {
  1018. "Content-Type": "audio/wav"
  1019. }
  1020. ```
  1021. </CodeGroup>
  1022. </Col>
  1023. </Row>
  1024. ---
  1025. <Heading
  1026. url='/info'
  1027. method='GET'
  1028. title='获取应用基本信息'
  1029. name='#info'
  1030. />
  1031. <Row>
  1032. <Col>
  1033. 用于获取应用的基本信息
  1034. ### Response
  1035. - `name` (string) 应用名称
  1036. - `description` (string) 应用描述
  1037. - `tags` (array[string]) 应用标签
  1038. - `mode` (string) 应用模式
  1039. - 'author_name' (string) 作者名称
  1040. </Col>
  1041. <Col>
  1042. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1043. ```bash {{ title: 'cURL' }}
  1044. curl -X GET '${props.appDetail.api_base_url}/info' \
  1045. -H 'Authorization: Bearer {api_key}'
  1046. ```
  1047. </CodeGroup>
  1048. <CodeGroup title="Response">
  1049. ```json {{ title: 'Response' }}
  1050. {
  1051. "name": "My App",
  1052. "description": "This is my app.",
  1053. "tags": [
  1054. "tag1",
  1055. "tag2"
  1056. ],
  1057. "mode": "chat",
  1058. "author_name": "Dify"
  1059. }
  1060. ```
  1061. </CodeGroup>
  1062. </Col>
  1063. </Row>
  1064. ---
  1065. <Heading
  1066. url='/parameters'
  1067. method='GET'
  1068. title='获取应用参数'
  1069. name='#parameters'
  1070. />
  1071. <Row>
  1072. <Col>
  1073. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  1074. ### Response
  1075. - `opening_statement` (string) 开场白
  1076. - `suggested_questions` (array[string]) 开场推荐问题列表
  1077. - `suggested_questions_after_answer` (object) 启用回答后给出推荐问题。
  1078. - `enabled` (bool) 是否开启
  1079. - `speech_to_text` (object) 语音转文本
  1080. - `enabled` (bool) 是否开启
  1081. - `text_to_speech` (object) 文本转语音
  1082. - `enabled` (bool) 是否开启
  1083. - `voice` (string) 语音类型
  1084. - `language` (string) 语言
  1085. - `autoPlay` (string) 自动播放
  1086. - `enabled` 开启
  1087. - `disabled` 关闭
  1088. - `retriever_resource` (object) 引用和归属
  1089. - `enabled` (bool) 是否开启
  1090. - `annotation_reply` (object) 标记回复
  1091. - `enabled` (bool) 是否开启
  1092. - `user_input_form` (array[object]) 用户输入表单配置
  1093. - `text-input` (object) 文本输入控件
  1094. - `label` (string) 控件展示标签名
  1095. - `variable` (string) 控件 ID
  1096. - `required` (bool) 是否必填
  1097. - `default` (string) 默认值
  1098. - `paragraph` (object) 段落文本输入控件
  1099. - `label` (string) 控件展示标签名
  1100. - `variable` (string) 控件 ID
  1101. - `required` (bool) 是否必填
  1102. - `default` (string) 默认值
  1103. - `select` (object) 下拉控件
  1104. - `label` (string) 控件展示标签名
  1105. - `variable` (string) 控件 ID
  1106. - `required` (bool) 是否必填
  1107. - `default` (string) 默认值
  1108. - `options` (array[string]) 选项值
  1109. - `file_upload` (object) 文件上传配置
  1110. - `image` (object) 图片设置
  1111. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`
  1112. - `enabled` (bool) 是否开启
  1113. - `number_limits` (int) 图片数量限制,默认 3
  1114. - `transfer_methods` (array[string]) 传递方式列表,remote_url , local_file,必选一个
  1115. - `system_parameters` (object) 系统参数
  1116. - `file_size_limit` (int) 文档上传大小限制 (MB)
  1117. - `image_file_size_limit` (int) 图片文件上传大小限制(MB)
  1118. - `audio_file_size_limit` (int) 音频文件上传大小限制 (MB)
  1119. - `video_file_size_limit` (int) 视频文件上传大小限制 (MB)
  1120. </Col>
  1121. <Col sticky>
  1122. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer {api_key}'`}>
  1123. ```bash {{ title: 'cURL' }}
  1124. curl -X GET '${props.appDetail.api_base_url}/parameters' \
  1125. --header 'Authorization: Bearer {api_key}'
  1126. ```
  1127. </CodeGroup>
  1128. <CodeGroup title="Response">
  1129. ```json {{ title: 'Response' }}
  1130. {
  1131. "introduction": "nice to meet you",
  1132. "user_input_form": [
  1133. {
  1134. "text-input": {
  1135. "label": "a",
  1136. "variable": "a",
  1137. "required": true,
  1138. "max_length": 48,
  1139. "default": ""
  1140. }
  1141. },
  1142. {
  1143. // ...
  1144. }
  1145. ],
  1146. "file_upload": {
  1147. "image": {
  1148. "enabled": true,
  1149. "number_limits": 3,
  1150. "transfer_methods": [
  1151. "remote_url",
  1152. "local_file"
  1153. ]
  1154. }
  1155. },
  1156. "system_parameters": {
  1157. "file_size_limit": 15,
  1158. "image_file_size_limit": 10,
  1159. "audio_file_size_limit": 50,
  1160. "video_file_size_limit": 100
  1161. }
  1162. }
  1163. ```
  1164. </CodeGroup>
  1165. </Col>
  1166. </Row>
  1167. ---
  1168. <Heading
  1169. url='/meta'
  1170. method='GET'
  1171. title='获取应用Meta信息'
  1172. name='#meta'
  1173. />
  1174. <Row>
  1175. <Col>
  1176. 用于获取工具 icon
  1177. ### Response
  1178. - `tool_icons`(object[string]) 工具图标
  1179. - `工具名称` (string)
  1180. - `icon` (object|string)
  1181. - (object) 图标
  1182. - `background` (string) hex 格式的背景色
  1183. - `content`(string) emoji
  1184. - (string) 图标 URL
  1185. </Col>
  1186. <Col>
  1187. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/meta' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1188. ```bash {{ title: 'cURL' }}
  1189. curl -X GET '${props.appDetail.api_base_url}/meta' \
  1190. -H 'Authorization: Bearer {api_key}'
  1191. ```
  1192. </CodeGroup>
  1193. <CodeGroup title="Response">
  1194. ```json {{ title: 'Response' }}
  1195. {
  1196. "tool_icons": {
  1197. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  1198. "api_tool": {
  1199. "background": "#252525",
  1200. "content": "\ud83d\ude01"
  1201. }
  1202. }
  1203. }
  1204. ```
  1205. </CodeGroup>
  1206. </Col>
  1207. </Row>
  1208. ---
  1209. <Heading
  1210. url='/site'
  1211. method='GET'
  1212. title='获取应用 WebApp 设置'
  1213. name='#site'
  1214. />
  1215. <Row>
  1216. <Col>
  1217. 用于获取应用的 WebApp 设置
  1218. ### Response
  1219. - `title` (string) WebApp 名称
  1220. - `chat_color_theme` (string) 聊天颜色主题,hex 格式
  1221. - `chat_color_theme_inverted` (bool) 聊天颜色主题是否反转
  1222. - `icon_type` (string) 图标类型,`emoji`-表情,`image`-图片
  1223. - `icon` (string) 图标,如果是 `emoji` 类型,则是 emoji 表情符号,如果是 `image` 类型,则是图片 URL
  1224. - `icon_background` (string) hex 格式的背景色
  1225. - `icon_url` (string) 图标 URL
  1226. - `description` (string) 描述
  1227. - `copyright` (string) 版权信息
  1228. - `privacy_policy` (string) 隐私政策链接
  1229. - `custom_disclaimer` (string) 自定义免责声明
  1230. - `default_language` (string) 默认语言
  1231. - `show_workflow_steps` (bool) 是否显示工作流详情
  1232. - `use_icon_as_answer_icon` (bool) 是否使用 WebApp 图标替换聊天中的 🤖
  1233. </Col>
  1234. <Col>
  1235. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/site' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1236. ```bash {{ title: 'cURL' }}
  1237. curl -X GET '${props.appDetail.api_base_url}/site' \
  1238. -H 'Authorization: Bearer {api_key}'
  1239. ```
  1240. </CodeGroup>
  1241. <CodeGroup title="Response">
  1242. ```json {{ title: 'Response' }}
  1243. {
  1244. "title": "My App",
  1245. "chat_color_theme": "#ff4a4a",
  1246. "chat_color_theme_inverted": false,
  1247. "icon_type": "emoji",
  1248. "icon": "😄",
  1249. "icon_background": "#FFEAD5",
  1250. "icon_url": null,
  1251. "description": "This is my app.",
  1252. "copyright": "all rights reserved",
  1253. "privacy_policy": "",
  1254. "custom_disclaimer": "All generated by AI",
  1255. "default_language": "en-US",
  1256. "show_workflow_steps": false,
  1257. "use_icon_as_answer_icon": false,
  1258. }
  1259. ```
  1260. </CodeGroup>
  1261. </Col>
  1262. </Row>
  1263. ___