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

template_chat.zh.mdx 49KB

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