template_chat.zh.mdx 48 KB

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