template_chat.zh.mdx 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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` (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. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  640. </Property>
  641. <Property name='sort_by' type='string' key='sort_by'>
  642. (选填)排序字段,默认 -updated_at(按更新时间倒序排列)
  643. - 可选值:created_at, -created_at, updated_at, -updated_at
  644. - 字段前面的符号代表顺序或倒序,-代表倒序
  645. </Property>
  646. </Properties>
  647. ### Response
  648. - `data` (array[object]) 会话列表
  649. - `id` (string) 会话 ID
  650. - `name` (string) 会话名称,默认为会话中用户最开始问题的截取。
  651. - `inputs` (object) 用户输入参数。
  652. - `status` (string) 会话状态
  653. - `introduction` (string) 开场白
  654. - `created_at` (timestamp) 创建时间
  655. - `updated_at` (timestamp) 更新时间
  656. - `has_more` (bool)
  657. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  658. </Col>
  659. <Col sticky>
  660. <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}'`}>
  661. ```bash {{ title: 'cURL' }}
  662. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  663. --header 'Authorization: Bearer {api_key}'
  664. ```
  665. </CodeGroup>
  666. <CodeGroup title="Response">
  667. ```json {{ title: 'Response' }}
  668. {
  669. "limit": 20,
  670. "has_more": false,
  671. "data": [
  672. {
  673. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  674. "name": "New chat",
  675. "inputs": {
  676. "book": "book",
  677. "myName": "Lucy"
  678. },
  679. "status": "normal",
  680. "created_at": 1679667915,
  681. "updated_at": 1679667915
  682. },
  683. {
  684. "id": "hSIhXBhNe8X1d8Et"
  685. // ...
  686. }
  687. ]
  688. }
  689. ```
  690. </CodeGroup>
  691. </Col>
  692. </Row>
  693. ---
  694. <Heading
  695. url='/conversations/:conversation_id'
  696. method='DELETE'
  697. title='删除会话'
  698. name='#delete'
  699. />
  700. <Row>
  701. <Col>
  702. 删除会话。
  703. ### Path
  704. - `conversation_id` (string) 会话 ID
  705. ### Request Body
  706. <Properties>
  707. <Property name='user' type='string' key='user'>
  708. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  709. </Property>
  710. </Properties>
  711. ### Response
  712. - `result` (string) 固定返回 success
  713. </Col>
  714. <Col sticky>
  715. <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}'`}>
  716. ```bash {{ title: 'cURL' }}
  717. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \
  718. --header 'Content-Type: application/json' \
  719. --header 'Accept: application/json' \
  720. --header 'Authorization: Bearer {api_key}' \
  721. --data '{
  722. "user": "abc-123"
  723. }'
  724. ```
  725. </CodeGroup>
  726. <CodeGroup title="Response">
  727. ```json {{ title: 'Response' }}
  728. {
  729. "result": "success"
  730. }
  731. ```
  732. </CodeGroup>
  733. </Col>
  734. </Row>
  735. ---
  736. <Heading
  737. url='/conversations/:conversation_id/name'
  738. method='POST'
  739. title='会话重命名'
  740. name='#rename'
  741. />
  742. <Row>
  743. <Col>
  744. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  745. ### Path
  746. - `conversation_id` (string) 会话 ID
  747. ### Request Body
  748. <Properties>
  749. <Property name='name' type='string' key='name'>
  750. (选填)名称,若 `auto_generate` 为 `true` 时,该参数可不传。
  751. </Property>
  752. <Property name='auto_generate' type='bool' key='auto_generate'>
  753. (选填)自动生成标题,默认 false。
  754. </Property>
  755. <Property name='user' type='string' key='user'>
  756. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  757. </Property>
  758. </Properties>
  759. ### Response
  760. - `id` (string) 会话 ID
  761. - `name` (string) 会话名称
  762. - `inputs` (object) 用户输入参数
  763. - `status` (string) 会话状态
  764. - `introduction` (string) 开场白
  765. - `created_at` (timestamp) 创建时间
  766. - `updated_at` (timestamp) 更新时间
  767. </Col>
  768. <Col sticky>
  769. <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}'`}>
  770. ```bash {{ title: 'cURL' }}
  771. curl -X POST '${props.appDetail.api_base_url}/conversations/{conversation_id}/name' \
  772. --header 'Authorization: Bearer {api_key}' \
  773. --header 'Content-Type: application/json' \
  774. --data-raw '{
  775. "name": "",
  776. "auto_generate": true,
  777. "user": "abc-123"
  778. }'
  779. ```
  780. </CodeGroup>
  781. <CodeGroup title="Response">
  782. ```json {{ title: 'Response' }}
  783. {
  784. "id": "34d511d5-56de-4f16-a997-57b379508443",
  785. "name": "hello",
  786. "inputs": {},
  787. "status": "normal",
  788. "introduction": "",
  789. "created_at": 1732731141,
  790. "updated_at": 1732734510
  791. }
  792. ```
  793. </CodeGroup>
  794. </Col>
  795. </Row>
  796. ---
  797. <Heading
  798. url='/audio-to-text'
  799. method='POST'
  800. title='语音转文字'
  801. name='#audio'
  802. />
  803. <Row>
  804. <Col>
  805. ### Request Body
  806. 该接口需使用 `multipart/form-data` 进行请求。
  807. <Properties>
  808. <Property name='file' type='file' key='file'>
  809. 语音文件。
  810. 支持格式:`['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  811. 文件大小限制:15MB
  812. </Property>
  813. <Property name='user' type='string' key='user'>
  814. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  815. </Property>
  816. </Properties>
  817. ### Response
  818. - `text` (string) 输出文字
  819. </Col>
  820. <Col sticky>
  821. <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]`}>
  822. ```bash {{ title: 'cURL' }}
  823. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  824. --header 'Authorization: Bearer {api_key}' \
  825. --form 'file=@localfile;type=audio/mp3'
  826. ```
  827. </CodeGroup>
  828. <CodeGroup title="Response">
  829. ```json {{ title: 'Response' }}
  830. {
  831. "text": "hello"
  832. }
  833. ```
  834. </CodeGroup>
  835. </Col>
  836. </Row>
  837. ---
  838. <Heading
  839. url='/text-to-audio'
  840. method='POST'
  841. title='文字转语音'
  842. name='#audio'
  843. />
  844. <Row>
  845. <Col>
  846. 文字转语音。
  847. ### Request Body
  848. <Properties>
  849. <Property name='message_id' type='str' key='text'>
  850. Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。如果同时传 message_id 和 text,优先使用 message_id。
  851. </Property>
  852. <Property name='text' type='str' key='text'>
  853. 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
  854. </Property>
  855. <Property name='user' type='string' key='user'>
  856. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  857. </Property>
  858. </Properties>
  859. </Col>
  860. <Col sticky>
  861. <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`}>
  862. ```bash {{ title: 'cURL' }}
  863. curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \
  864. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  865. --form 'file=你好Dify;user=abc-123;message_id=5ad4cb98-f0c7-4085-b384-88c403be6290'
  866. ```
  867. </CodeGroup>
  868. <CodeGroup title="headers">
  869. ```json {{ title: 'headers' }}
  870. {
  871. "Content-Type": "audio/wav"
  872. }
  873. ```
  874. </CodeGroup>
  875. </Col>
  876. </Row>
  877. ---
  878. <Heading
  879. url='/info'
  880. method='GET'
  881. title='获取应用基本信息'
  882. name='#info'
  883. />
  884. <Row>
  885. <Col>
  886. 用于获取应用的基本信息
  887. ### Query
  888. <Properties>
  889. <Property name='user' type='string' key='user'>
  890. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  891. </Property>
  892. </Properties>
  893. ### Response
  894. - `name` (string) 应用名称
  895. - `description` (string) 应用描述
  896. - `tags` (array[string]) 应用标签
  897. </Col>
  898. <Col>
  899. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info?user=abc-123' \\\n-H 'Authorization: Bearer {api_key}'`}>
  900. ```bash {{ title: 'cURL' }}
  901. curl -X GET '${props.appDetail.api_base_url}/info?user=abc-123' \
  902. -H 'Authorization: Bearer {api_key}'
  903. ```
  904. </CodeGroup>
  905. <CodeGroup title="Response">
  906. ```json {{ title: 'Response' }}
  907. {
  908. "name": "My App",
  909. "description": "This is my app.",
  910. "tags": [
  911. "tag1",
  912. "tag2"
  913. ]
  914. }
  915. ```
  916. </CodeGroup>
  917. </Col>
  918. </Row>
  919. ---
  920. <Heading
  921. url='/parameters'
  922. method='GET'
  923. title='获取应用参数'
  924. name='#parameters'
  925. />
  926. <Row>
  927. <Col>
  928. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  929. ### Query
  930. <Properties>
  931. <Property name='user' type='string' key='user'>
  932. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  933. </Property>
  934. </Properties>
  935. ### Response
  936. - `opening_statement` (string) 开场白
  937. - `suggested_questions` (array[string]) 开场推荐问题列表
  938. - `suggested_questions_after_answer` (object) 启用回答后给出推荐问题。
  939. - `enabled` (bool) 是否开启
  940. - `speech_to_text` (object) 语音转文本
  941. - `enabled` (bool) 是否开启
  942. - `retriever_resource` (object) 引用和归属
  943. - `enabled` (bool) 是否开启
  944. - `annotation_reply` (object) 标记回复
  945. - `enabled` (bool) 是否开启
  946. - `user_input_form` (array[object]) 用户输入表单配置
  947. - `text-input` (object) 文本输入控件
  948. - `label` (string) 控件展示标签名
  949. - `variable` (string) 控件 ID
  950. - `required` (bool) 是否必填
  951. - `default` (string) 默认值
  952. - `paragraph` (object) 段落文本输入控件
  953. - `label` (string) 控件展示标签名
  954. - `variable` (string) 控件 ID
  955. - `required` (bool) 是否必填
  956. - `default` (string) 默认值
  957. - `select` (object) 下拉控件
  958. - `label` (string) 控件展示标签名
  959. - `variable` (string) 控件 ID
  960. - `required` (bool) 是否必填
  961. - `default` (string) 默认值
  962. - `options` (array[string]) 选项值
  963. - `file_upload` (object) 文件上传配置
  964. - `image` (object) 图片设置
  965. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`
  966. - `enabled` (bool) 是否开启
  967. - `number_limits` (int) 图片数量限制,默认 3
  968. - `transfer_methods` (array[string]) 传递方式列表,remote_url , local_file,必选一个
  969. - `system_parameters` (object) 系统参数
  970. - `file_size_limit` (int) 文档上传大小限制 (MB)
  971. - `image_file_size_limit` (int) 图片文件上传大小限制(MB)
  972. - `audio_file_size_limit` (int) 音频文件上传大小限制 (MB)
  973. - `video_file_size_limit` (int) 视频文件上传大小限制 (MB)
  974. </Col>
  975. <Col sticky>
  976. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer {api_key}'`}>
  977. ```bash {{ title: 'cURL' }}
  978. curl -X GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  979. --header 'Authorization: Bearer {api_key}'
  980. ```
  981. </CodeGroup>
  982. <CodeGroup title="Response">
  983. ```json {{ title: 'Response' }}
  984. {
  985. "introduction": "nice to meet you",
  986. "user_input_form": [
  987. {
  988. "text-input": {
  989. "label": "a",
  990. "variable": "a",
  991. "required": true,
  992. "max_length": 48,
  993. "default": ""
  994. }
  995. },
  996. {
  997. // ...
  998. }
  999. ],
  1000. "file_upload": {
  1001. "image": {
  1002. "enabled": true,
  1003. "number_limits": 3,
  1004. "transfer_methods": [
  1005. "remote_url",
  1006. "local_file"
  1007. ]
  1008. }
  1009. },
  1010. "system_parameters": {
  1011. "file_size_limit": 15,
  1012. "image_file_size_limit": 10,
  1013. "audio_file_size_limit": 50,
  1014. "video_file_size_limit": 100
  1015. }
  1016. }
  1017. ```
  1018. </CodeGroup>
  1019. </Col>
  1020. </Row>
  1021. ---
  1022. <Heading
  1023. url='/meta'
  1024. method='GET'
  1025. title='获取应用Meta信息'
  1026. name='#meta'
  1027. />
  1028. <Row>
  1029. <Col>
  1030. 用于获取工具icon
  1031. ### Query
  1032. <Properties>
  1033. <Property name='user' type='string' key='user'>
  1034. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  1035. </Property>
  1036. </Properties>
  1037. ### Response
  1038. - `tool_icons`(object[string]) 工具图标
  1039. - `工具名称` (string)
  1040. - `icon` (object|string)
  1041. - (object) 图标
  1042. - `background` (string) hex格式的背景色
  1043. - `content`(string) emoji
  1044. - (string) 图标URL
  1045. </Col>
  1046. <Col>
  1047. <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}'`}>
  1048. ```bash {{ title: 'cURL' }}
  1049. curl -X GET '${props.appDetail.api_base_url}/meta?user=abc-123' \
  1050. -H 'Authorization: Bearer {api_key}'
  1051. ```
  1052. </CodeGroup>
  1053. <CodeGroup title="Response">
  1054. ```json {{ title: 'Response' }}
  1055. {
  1056. "tool_icons": {
  1057. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  1058. "api_tool": {
  1059. "background": "#252525",
  1060. "content": "\ud83d\ude01"
  1061. }
  1062. }
  1063. }
  1064. ```
  1065. </CodeGroup>
  1066. </Col>
  1067. </Row>