template_chat.zh.mdx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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. <Property name='content' type='string' key='content'>
  391. 消息反馈的具体信息。
  392. </Property>
  393. </Properties>
  394. ### Response
  395. - `result` (string) 固定返回 success
  396. </Col>
  397. <Col sticky>
  398. <CodeGroup title="Request" tag="POST" label="/messages/:message_id/feedbacks" targetCode={`curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "rating": "like",\n "user": "abc-123",\n "content": "message feedback information"\n}'`}>
  399. ```bash {{ title: 'cURL' }}
  400. curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks' \
  401. --header 'Authorization: Bearer {api_key}' \
  402. --header 'Content-Type: application/json' \
  403. --data-raw '{
  404. "rating": "like",
  405. "user": "abc-123",
  406. "content": "message feedback information"
  407. }'
  408. ```
  409. </CodeGroup>
  410. <CodeGroup title="Response">
  411. ```json {{ title: 'Response' }}
  412. {
  413. "result": "success"
  414. }
  415. ```
  416. </CodeGroup>
  417. </Col>
  418. </Row>
  419. ---
  420. <Heading
  421. url='/messages/{message_id}/suggested'
  422. method='GET'
  423. title='获取下一轮建议问题列表'
  424. name='#suggested'
  425. />
  426. <Row>
  427. <Col>
  428. 获取下一轮建议问题列表。
  429. ### Path Params
  430. <Properties>
  431. <Property name='message_id' type='string' key='message_id'>
  432. Message ID
  433. </Property>
  434. </Properties>
  435. ### Query
  436. <Properties>
  437. <Property name='user' type='string' key='user'>
  438. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  439. </Property>
  440. </Properties>
  441. </Col>
  442. <Col sticky>
  443. <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'`}>
  444. ```bash {{ title: 'cURL' }}
  445. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  446. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  447. --header 'Content-Type: application/json' \
  448. ```
  449. </CodeGroup>
  450. <CodeGroup title="Response">
  451. ```json {{ title: 'Response' }}
  452. {
  453. "result": "success",
  454. "data": [
  455. "a",
  456. "b",
  457. "c"
  458. ]
  459. }
  460. ```
  461. </CodeGroup>
  462. </Col>
  463. </Row>
  464. ---
  465. ---
  466. <Heading
  467. url='/messages'
  468. method='GET'
  469. title='获取会话历史消息'
  470. name='#messages'
  471. />
  472. <Row>
  473. <Col>
  474. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,即:倒序返回。
  475. ### Query
  476. <Properties>
  477. <Property name='conversation_id' type='string' key='conversation_id'>
  478. 会话 ID
  479. </Property>
  480. <Property name='user' type='string' key='user'>
  481. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  482. </Property>
  483. <Property name='first_id' type='string' key='first_id'>
  484. 当前页第一条聊天记录的 ID,默认 null
  485. </Property>
  486. <Property name='limit' type='int' key='limit'>
  487. 一次请求返回多少条聊天记录,默认 20 条。
  488. </Property>
  489. </Properties>
  490. ### Response
  491. - `data` (array[object]) 消息列表
  492. - `id` (string) 消息 ID
  493. - `conversation_id` (string) 会话 ID
  494. - `inputs` (object) 用户输入参数。
  495. - `query` (string) 用户输入 / 提问内容。
  496. - `message_files` (array[object]) 消息文件
  497. - `id` (string) ID
  498. - `type` (string) 文件类型,image 图片
  499. - `url` (string) 预览图片地址
  500. - `belongs_to` (string) 文件归属方,user 或 assistant
  501. - `agent_thoughts` (array[object]) Agent思考内容(仅Agent模式下不为空)
  502. - `id` (string) agent_thought ID,每一轮Agent迭代都会有一个唯一的id
  503. - `message_id` (string) 消息唯一ID
  504. - `position` (int) agent_thought在消息中的位置,如第一轮迭代position为1
  505. - `thought` (string) agent的思考内容
  506. - `observation` (string) 工具调用的返回结果
  507. - `tool` (string) 使用的工具列表,以 ; 分割多个工具
  508. - `tool_input` (string) 工具的输入,JSON格式的字符串(object)。如:`{"dalle3": {"prompt": "a cute cat"}}`
  509. - `created_at` (int) 创建时间戳,如:1705395332
  510. - `message_files` (array[string]) 当前agent_thought 关联的文件ID
  511. - `file_id` (string) 文件ID
  512. - `conversation_id` (string) 会话ID
  513. - `answer` (string) 回答消息内容
  514. - `created_at` (timestamp) 创建时间
  515. - `feedback` (object) 反馈信息
  516. - `rating` (string) 点赞 like / 点踩 dislike
  517. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  518. - `has_more` (bool) 是否存在下一页
  519. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  520. </Col>
  521. <Col sticky>
  522. ### Request Example
  523. <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}'`}>
  524. ```bash {{ title: 'cURL' }}
  525. curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  526. --header 'Authorization: Bearer {api_key}'
  527. ```
  528. </CodeGroup>
  529. ### Response Example(基础助手)
  530. <CodeGroup title="Response">
  531. ```json {{ title: 'Response' }}
  532. {
  533. "limit": 20,
  534. "has_more": false,
  535. "data": [
  536. {
  537. "id": "a076a87f-31e5-48dc-b452-0061adbbc922",
  538. "conversation_id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  539. "inputs": {
  540. "name": "dify"
  541. },
  542. "query": "iphone 13 pro",
  543. "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.",
  544. "message_files": [],
  545. "feedback": null,
  546. "retriever_resources": [
  547. {
  548. "position": 1,
  549. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  550. "dataset_name": "iPhone",
  551. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  552. "document_name": "iPhone List",
  553. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  554. "score": 0.98457545,
  555. "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\""
  556. }
  557. ],
  558. "agent_thoughts": [],
  559. "created_at": 1705569239
  560. }
  561. ]
  562. }
  563. ```
  564. </CodeGroup>
  565. ### Response Example(智能助手)
  566. <CodeGroup title="Response">
  567. ```json {{ title: 'Response' }}
  568. {
  569. "limit": 20,
  570. "has_more": false,
  571. "data": [
  572. {
  573. "id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  574. "conversation_id": "957c068b-f258-4f89-ba10-6e8a0361c457",
  575. "inputs": {},
  576. "query": "draw a cat",
  577. "answer": "I have generated an image of a cat for you. Please check your messages to view the image.",
  578. "message_files": [
  579. {
  580. "id": "976990d2-5294-47e6-8f14-7356ba9d2d76",
  581. "type": "image",
  582. "url": "http://127.0.0.1:5001/files/tools/976990d2-5294-47e6-8f14-7356ba9d2d76.png?timestamp=1705988524&nonce=55df3f9f7311a9acd91bf074cd524092&sign=z43nMSO1L2HBvoqADLkRxr7Biz0fkjeDstnJiCK1zh8=",
  583. "belongs_to": "assistant"
  584. }
  585. ],
  586. "feedback": null,
  587. "retriever_resources": [],
  588. "created_at": 1705988187,
  589. "agent_thoughts": [
  590. {
  591. "id": "592c84cf-07ee-441c-9dcc-ffc66c033469",
  592. "chain_id": null,
  593. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  594. "position": 1,
  595. "thought": "",
  596. "tool": "dalle2",
  597. "tool_input": "{\"dalle2\": {\"prompt\": \"cat\"}}",
  598. "created_at": 1705988186,
  599. "observation": "image has been created and sent to user already, you should tell user to check it now.",
  600. "message_files": [
  601. "976990d2-5294-47e6-8f14-7356ba9d2d76"
  602. ]
  603. },
  604. {
  605. "id": "73ead60d-2370-4780-b5ed-532d2762b0e5",
  606. "chain_id": null,
  607. "message_id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  608. "position": 2,
  609. "thought": "I have generated an image of a cat for you. Please check your messages to view the image.",
  610. "tool": "",
  611. "tool_input": "",
  612. "created_at": 1705988199,
  613. "observation": "",
  614. "message_files": []
  615. }
  616. ]
  617. }
  618. ]
  619. }
  620. ```
  621. </CodeGroup>
  622. </Col>
  623. </Row>
  624. ---
  625. <Heading
  626. url='/conversations'
  627. method='GET'
  628. title='获取会话列表'
  629. name='#conversations'
  630. />
  631. <Row>
  632. <Col>
  633. 获取当前用户的会话列表,默认返回最近的 20 条。
  634. ### Query
  635. <Properties>
  636. <Property name='user' type='string' key='user'>
  637. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  638. </Property>
  639. <Property name='last_id' type='string' key='last_id'>
  640. (选填)当前页最后面一条记录的 ID,默认 null
  641. </Property>
  642. <Property name='limit' type='int' key='limit'>
  643. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  644. </Property>
  645. <Property name='sort_by' type='string' key='sort_by'>
  646. (选填)排序字段,默认 -updated_at(按更新时间倒序排列)
  647. - 可选值:created_at, -created_at, updated_at, -updated_at
  648. - 字段前面的符号代表顺序或倒序,-代表倒序
  649. </Property>
  650. </Properties>
  651. ### Response
  652. - `data` (array[object]) 会话列表
  653. - `id` (string) 会话 ID
  654. - `name` (string) 会话名称,默认为会话中用户最开始问题的截取。
  655. - `inputs` (object) 用户输入参数。
  656. - `status` (string) 会话状态
  657. - `introduction` (string) 开场白
  658. - `created_at` (timestamp) 创建时间
  659. - `updated_at` (timestamp) 更新时间
  660. - `has_more` (bool)
  661. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  662. </Col>
  663. <Col sticky>
  664. <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}'`}>
  665. ```bash {{ title: 'cURL' }}
  666. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  667. --header 'Authorization: Bearer {api_key}'
  668. ```
  669. </CodeGroup>
  670. <CodeGroup title="Response">
  671. ```json {{ title: 'Response' }}
  672. {
  673. "limit": 20,
  674. "has_more": false,
  675. "data": [
  676. {
  677. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  678. "name": "New chat",
  679. "inputs": {
  680. "book": "book",
  681. "myName": "Lucy"
  682. },
  683. "status": "normal",
  684. "created_at": 1679667915,
  685. "updated_at": 1679667915
  686. },
  687. {
  688. "id": "hSIhXBhNe8X1d8Et"
  689. // ...
  690. }
  691. ]
  692. }
  693. ```
  694. </CodeGroup>
  695. </Col>
  696. </Row>
  697. ---
  698. <Heading
  699. url='/conversations/:conversation_id'
  700. method='DELETE'
  701. title='删除会话'
  702. name='#delete'
  703. />
  704. <Row>
  705. <Col>
  706. 删除会话。
  707. ### Path
  708. - `conversation_id` (string) 会话 ID
  709. ### Request Body
  710. <Properties>
  711. <Property name='user' type='string' key='user'>
  712. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  713. </Property>
  714. </Properties>
  715. ### Response
  716. - `result` (string) 固定返回 success
  717. </Col>
  718. <Col sticky>
  719. <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}'`}>
  720. ```bash {{ title: 'cURL' }}
  721. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \
  722. --header 'Content-Type: application/json' \
  723. --header 'Accept: application/json' \
  724. --header 'Authorization: Bearer {api_key}' \
  725. --data '{
  726. "user": "abc-123"
  727. }'
  728. ```
  729. </CodeGroup>
  730. <CodeGroup title="Response">
  731. ```json {{ title: 'Response' }}
  732. {
  733. "result": "success"
  734. }
  735. ```
  736. </CodeGroup>
  737. </Col>
  738. </Row>
  739. ---
  740. <Heading
  741. url='/conversations/:conversation_id/name'
  742. method='POST'
  743. title='会话重命名'
  744. name='#rename'
  745. />
  746. <Row>
  747. <Col>
  748. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  749. ### Path
  750. - `conversation_id` (string) 会话 ID
  751. ### Request Body
  752. <Properties>
  753. <Property name='name' type='string' key='name'>
  754. (选填)名称,若 `auto_generate` 为 `true` 时,该参数可不传。
  755. </Property>
  756. <Property name='auto_generate' type='bool' key='auto_generate'>
  757. (选填)自动生成标题,默认 false。
  758. </Property>
  759. <Property name='user' type='string' key='user'>
  760. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  761. </Property>
  762. </Properties>
  763. ### Response
  764. - `id` (string) 会话 ID
  765. - `name` (string) 会话名称
  766. - `inputs` (object) 用户输入参数
  767. - `status` (string) 会话状态
  768. - `introduction` (string) 开场白
  769. - `created_at` (timestamp) 创建时间
  770. - `updated_at` (timestamp) 更新时间
  771. </Col>
  772. <Col sticky>
  773. <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}'`}>
  774. ```bash {{ title: 'cURL' }}
  775. curl -X POST '${props.appDetail.api_base_url}/conversations/{conversation_id}/name' \
  776. --header 'Authorization: Bearer {api_key}' \
  777. --header 'Content-Type: application/json' \
  778. --data-raw '{
  779. "name": "",
  780. "auto_generate": true,
  781. "user": "abc-123"
  782. }'
  783. ```
  784. </CodeGroup>
  785. <CodeGroup title="Response">
  786. ```json {{ title: 'Response' }}
  787. {
  788. "id": "34d511d5-56de-4f16-a997-57b379508443",
  789. "name": "hello",
  790. "inputs": {},
  791. "status": "normal",
  792. "introduction": "",
  793. "created_at": 1732731141,
  794. "updated_at": 1732734510
  795. }
  796. ```
  797. </CodeGroup>
  798. </Col>
  799. </Row>
  800. ---
  801. <Heading
  802. url='/audio-to-text'
  803. method='POST'
  804. title='语音转文字'
  805. name='#audio'
  806. />
  807. <Row>
  808. <Col>
  809. ### Request Body
  810. 该接口需使用 `multipart/form-data` 进行请求。
  811. <Properties>
  812. <Property name='file' type='file' key='file'>
  813. 语音文件。
  814. 支持格式:`['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  815. 文件大小限制:15MB
  816. </Property>
  817. <Property name='user' type='string' key='user'>
  818. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  819. </Property>
  820. </Properties>
  821. ### Response
  822. - `text` (string) 输出文字
  823. </Col>
  824. <Col sticky>
  825. <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]`}>
  826. ```bash {{ title: 'cURL' }}
  827. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  828. --header 'Authorization: Bearer {api_key}' \
  829. --form 'file=@localfile;type=audio/mp3'
  830. ```
  831. </CodeGroup>
  832. <CodeGroup title="Response">
  833. ```json {{ title: 'Response' }}
  834. {
  835. "text": "hello"
  836. }
  837. ```
  838. </CodeGroup>
  839. </Col>
  840. </Row>
  841. ---
  842. <Heading
  843. url='/text-to-audio'
  844. method='POST'
  845. title='文字转语音'
  846. name='#audio'
  847. />
  848. <Row>
  849. <Col>
  850. 文字转语音。
  851. ### Request Body
  852. <Properties>
  853. <Property name='message_id' type='str' key='message_id'>
  854. Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。如果同时传 message_id 和 text,优先使用 message_id。
  855. </Property>
  856. <Property name='text' type='str' key='text'>
  857. 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
  858. </Property>
  859. <Property name='user' type='string' key='user'>
  860. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  861. </Property>
  862. </Properties>
  863. </Col>
  864. <Col sticky>
  865. <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`}>
  866. ```bash {{ title: 'cURL' }}
  867. curl --location --request POST '${props.appDetail.api_base_url}/text-to-audio' \
  868. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  869. --form 'file=你好Dify;user=abc-123;message_id=5ad4cb98-f0c7-4085-b384-88c403be6290'
  870. ```
  871. </CodeGroup>
  872. <CodeGroup title="headers">
  873. ```json {{ title: 'headers' }}
  874. {
  875. "Content-Type": "audio/wav"
  876. }
  877. ```
  878. </CodeGroup>
  879. </Col>
  880. </Row>
  881. ---
  882. <Heading
  883. url='/info'
  884. method='GET'
  885. title='获取应用基本信息'
  886. name='#info'
  887. />
  888. <Row>
  889. <Col>
  890. 用于获取应用的基本信息
  891. ### Response
  892. - `name` (string) 应用名称
  893. - `description` (string) 应用描述
  894. - `tags` (array[string]) 应用标签
  895. </Col>
  896. <Col>
  897. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info' \\\n-H 'Authorization: Bearer {api_key}'`}>
  898. ```bash {{ title: 'cURL' }}
  899. curl -X GET '${props.appDetail.api_base_url}/info' \
  900. -H 'Authorization: Bearer {api_key}'
  901. ```
  902. </CodeGroup>
  903. <CodeGroup title="Response">
  904. ```json {{ title: 'Response' }}
  905. {
  906. "name": "My App",
  907. "description": "This is my app.",
  908. "tags": [
  909. "tag1",
  910. "tag2"
  911. ]
  912. }
  913. ```
  914. </CodeGroup>
  915. </Col>
  916. </Row>
  917. ---
  918. <Heading
  919. url='/parameters'
  920. method='GET'
  921. title='获取应用参数'
  922. name='#parameters'
  923. />
  924. <Row>
  925. <Col>
  926. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  927. ### Response
  928. - `opening_statement` (string) 开场白
  929. - `suggested_questions` (array[string]) 开场推荐问题列表
  930. - `suggested_questions_after_answer` (object) 启用回答后给出推荐问题。
  931. - `enabled` (bool) 是否开启
  932. - `speech_to_text` (object) 语音转文本
  933. - `enabled` (bool) 是否开启
  934. - `retriever_resource` (object) 引用和归属
  935. - `enabled` (bool) 是否开启
  936. - `annotation_reply` (object) 标记回复
  937. - `enabled` (bool) 是否开启
  938. - `user_input_form` (array[object]) 用户输入表单配置
  939. - `text-input` (object) 文本输入控件
  940. - `label` (string) 控件展示标签名
  941. - `variable` (string) 控件 ID
  942. - `required` (bool) 是否必填
  943. - `default` (string) 默认值
  944. - `paragraph` (object) 段落文本输入控件
  945. - `label` (string) 控件展示标签名
  946. - `variable` (string) 控件 ID
  947. - `required` (bool) 是否必填
  948. - `default` (string) 默认值
  949. - `select` (object) 下拉控件
  950. - `label` (string) 控件展示标签名
  951. - `variable` (string) 控件 ID
  952. - `required` (bool) 是否必填
  953. - `default` (string) 默认值
  954. - `options` (array[string]) 选项值
  955. - `file_upload` (object) 文件上传配置
  956. - `image` (object) 图片设置
  957. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`
  958. - `enabled` (bool) 是否开启
  959. - `number_limits` (int) 图片数量限制,默认 3
  960. - `transfer_methods` (array[string]) 传递方式列表,remote_url , local_file,必选一个
  961. - `system_parameters` (object) 系统参数
  962. - `file_size_limit` (int) 文档上传大小限制 (MB)
  963. - `image_file_size_limit` (int) 图片文件上传大小限制(MB)
  964. - `audio_file_size_limit` (int) 音频文件上传大小限制 (MB)
  965. - `video_file_size_limit` (int) 视频文件上传大小限制 (MB)
  966. </Col>
  967. <Col sticky>
  968. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer {api_key}'`}>
  969. ```bash {{ title: 'cURL' }}
  970. curl -X GET '${props.appDetail.api_base_url}/parameters' \
  971. --header 'Authorization: Bearer {api_key}'
  972. ```
  973. </CodeGroup>
  974. <CodeGroup title="Response">
  975. ```json {{ title: 'Response' }}
  976. {
  977. "introduction": "nice to meet you",
  978. "user_input_form": [
  979. {
  980. "text-input": {
  981. "label": "a",
  982. "variable": "a",
  983. "required": true,
  984. "max_length": 48,
  985. "default": ""
  986. }
  987. },
  988. {
  989. // ...
  990. }
  991. ],
  992. "file_upload": {
  993. "image": {
  994. "enabled": true,
  995. "number_limits": 3,
  996. "transfer_methods": [
  997. "remote_url",
  998. "local_file"
  999. ]
  1000. }
  1001. },
  1002. "system_parameters": {
  1003. "file_size_limit": 15,
  1004. "image_file_size_limit": 10,
  1005. "audio_file_size_limit": 50,
  1006. "video_file_size_limit": 100
  1007. }
  1008. }
  1009. ```
  1010. </CodeGroup>
  1011. </Col>
  1012. </Row>
  1013. ---
  1014. <Heading
  1015. url='/meta'
  1016. method='GET'
  1017. title='获取应用Meta信息'
  1018. name='#meta'
  1019. />
  1020. <Row>
  1021. <Col>
  1022. 用于获取工具icon
  1023. ### Response
  1024. - `tool_icons`(object[string]) 工具图标
  1025. - `工具名称` (string)
  1026. - `icon` (object|string)
  1027. - (object) 图标
  1028. - `background` (string) hex格式的背景色
  1029. - `content`(string) emoji
  1030. - (string) 图标URL
  1031. </Col>
  1032. <Col>
  1033. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/meta' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1034. ```bash {{ title: 'cURL' }}
  1035. curl -X GET '${props.appDetail.api_base_url}/meta' \
  1036. -H 'Authorization: Bearer {api_key}'
  1037. ```
  1038. </CodeGroup>
  1039. <CodeGroup title="Response">
  1040. ```json {{ title: 'Response' }}
  1041. {
  1042. "tool_icons": {
  1043. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  1044. "api_tool": {
  1045. "background": "#252525",
  1046. "content": "\ud83d\ude01"
  1047. }
  1048. }
  1049. }
  1050. ```
  1051. </CodeGroup>
  1052. </Col>
  1053. </Row>