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_advanced_chat.zh.mdx 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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. 如果变量是文件类型,请指定一个包含以下 `files` 中所述键的对象。
  40. 默认 `{}`
  41. </Property>
  42. <Property name='response_mode' type='string' key='response_mode'>
  43. - `streaming` 流式模式(推荐)。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现类似打字机输出方式的流式返回。
  44. - `blocking` 阻塞模式,等待执行完毕后返回结果。(请求若流程较长可能会被中断)。
  45. <i>由于 Cloudflare 限制,请求会在 100 秒超时无返回后中断。</i>
  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. 文件列表,适用于传入文件结合文本理解并回答问题,仅当模型支持 Vision 能力时可用。
  56. - `type` (string) 支持类型:
  57. - `document` 具体类型包含:'TXT', 'MD', 'MARKDOWN', 'PDF', 'HTML', 'XLSX', 'XLS', 'DOCX', 'CSV', 'EML', 'MSG', 'PPTX', 'PPT', 'XML', 'EPUB'
  58. - `image` 具体类型包含:'JPG', 'JPEG', 'PNG', 'GIF', 'WEBP', 'SVG'
  59. - `audio` 具体类型包含:'MP3', 'M4A', 'WAV', 'WEBM', 'AMR'
  60. - `video` 具体类型包含:'MP4', 'MOV', 'MPEG', 'MPGA'
  61. - `custom` 具体类型包含:其他文件类型
  62. - `transfer_method` (string) 传递方式:
  63. - `remote_url`: 图片地址。
  64. - `local_file`: 上传文件。
  65. - `url` 图片地址。(仅当传递方式为 `remote_url` 时)。
  66. - `upload_file_id` 上传文件 ID。(仅当传递方式为 `local_file `时)。
  67. </Property>
  68. <Property name='auto_generate_name' type='bool' key='auto_generate_name'>
  69. (选填)自动生成标题,默认 `true`。 若设置为 `false`,则可通过调用会话重命名接口并设置 `auto_generate` 为 `true` 实现异步生成标题。
  70. </Property>
  71. </Properties>
  72. ### Response
  73. <Properties>
  74. 当 `response_mode` 为 `blocking` 时,返回 ChatCompletionResponse object。
  75. 当 `response_mode` 为 `streaming`时,返回 ChunkChatCompletionResponse object 流式序列。
  76. ### ChatCompletionResponse
  77. 返回完整的 App 结果,`Content-Type` 为 `application/json`。
  78. - `event` (string) 事件类型,固定为 `message`
  79. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  80. - `id` (string) 唯一ID
  81. - `message_id` (string) 消息唯一 ID
  82. - `conversation_id` (string) 会话 ID
  83. - `mode` (string) App 模式,固定为 chat
  84. - `answer` (string) 完整回复内容
  85. - `metadata` (object) 元数据
  86. - `usage` (Usage) 模型用量信息
  87. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  88. - `created_at` (int) 消息创建时间戳,如:1705395332
  89. ### ChunkChatCompletionResponse
  90. 返回 App 输出的流式块,`Content-Type` 为 `text/event-stream`。
  91. 每个流式块均为 data: 开头,块之间以 \n\n 即两个换行符分隔,如下所示:
  92. <CodeGroup>
  93. ```streaming {{ title: 'Response' }}
  94. data: {"event": "message", "task_id": "900bbd43-dc0b-4383-a372-aa6e6c414227", "id": "663c5084-a254-4040-8ad3-51f2a3c1a77c", "answer": "Hi", "created_at": 1705398420}\n\n
  95. ```
  96. </CodeGroup>
  97. 流式块中根据 event 不同,结构也不同:
  98. - `event: message` LLM 返回文本块事件,即:完整的文本以分块的方式输出。
  99. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  100. - `message_id` (string) 消息唯一 ID
  101. - `conversation_id` (string) 会话 ID
  102. - `answer` (string) LLM 返回文本块内容
  103. - `created_at` (int) 创建时间戳,如:1705395332
  104. - `event: message_file` 文件事件,表示有新文件需要展示
  105. - `id` (string) 文件唯一ID
  106. - `type` (string) 文件类型,目前仅为image
  107. - `belongs_to` (string) 文件归属,user或assistant,该接口返回仅为 `assistant`
  108. - `url` (string) 文件访问地址
  109. - `conversation_id` (string) 会话ID
  110. - `event: message_end` 消息结束事件,收到此事件则代表流式返回结束。
  111. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  112. - `message_id` (string) 消息唯一 ID
  113. - `conversation_id` (string) 会话 ID
  114. - `metadata` (object) 元数据
  115. - `usage` (Usage) 模型用量信息
  116. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  117. - `event: tts_message` TTS 音频流事件,即:语音合成输出。内容是Mp3格式的音频块,使用 base64 编码后的字符串,播放的时候直接解码即可。(开启自动播放才有此消息)
  118. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  119. - `message_id` (string) 消息唯一 ID
  120. - `audio` (string) 语音合成之后的音频块使用 Base64 编码之后的文本内容,播放的时候直接 base64 解码送入播放器即可
  121. - `created_at` (int) 创建时间戳,如:1705395332
  122. - `event: tts_message_end` TTS 音频流结束事件,收到这个事件表示音频流返回结束。
  123. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  124. - `message_id` (string) 消息唯一 ID
  125. - `audio` (string) 结束事件是没有音频的,所以这里是空字符串
  126. - `created_at` (int) 创建时间戳,如:1705395332
  127. - `event: message_replace` 消息内容替换事件。
  128. 开启内容审查和审查输出内容时,若命中了审查条件,则会通过此事件替换消息内容为预设回复。
  129. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  130. - `message_id` (string) 消息唯一 ID
  131. - `conversation_id` (string) 会话 ID
  132. - `answer` (string) 替换内容(直接替换 LLM 所有回复文本)
  133. - `created_at` (int) 创建时间戳,如:1705395332
  134. - `event: workflow_started` workflow 开始执行
  135. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  136. - `workflow_run_id` (string) workflow 执行 ID
  137. - `event` (string) 固定为 `workflow_started`
  138. - `data` (object) 详细内容
  139. - `id` (string) workflow 执行 ID
  140. - `workflow_id` (string) 关联 Workflow ID
  141. - `sequence_number` (int) 自增序号,App 内自增,从 1 开始
  142. - `created_at` (timestamp) 开始时间
  143. - `event: node_started` node 开始执行
  144. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  145. - `workflow_run_id` (string) workflow 执行 ID
  146. - `event` (string) 固定为 `node_started`
  147. - `data` (object) 详细内容
  148. - `id` (string) workflow 执行 ID
  149. - `node_id` (string) 节点 ID
  150. - `node_type` (string) 节点类型
  151. - `title` (string) 节点名称
  152. - `index` (int) 执行序号,用于展示 Tracing Node 顺序
  153. - `predecessor_node_id` (string) 前置节点 ID,用于画布展示执行路径
  154. - `inputs` (object) 节点中所有使用到的前置节点变量内容
  155. - `created_at` (timestamp) 开始时间
  156. - `event: node_finished` node 执行结束,成功失败同一事件中不同状态
  157. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  158. - `workflow_run_id` (string) workflow 执行 ID
  159. - `event` (string) 固定为 `node_finished`
  160. - `data` (object) 详细内容
  161. - `id` (string) node 执行 ID
  162. - `node_id` (string) 节点 ID
  163. - `index` (int) 执行序号,用于展示 Tracing Node 顺序
  164. - `predecessor_node_id` (string) optional 前置节点 ID,用于画布展示执行路径
  165. - `inputs` (object) 节点中所有使用到的前置节点变量内容
  166. - `process_data` (json) Optional 节点过程数据
  167. - `outputs` (json) Optional 输出内容
  168. - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped`
  169. - `error` (string) Optional 错误原因
  170. - `elapsed_time` (float) Optional 耗时(s)
  171. - `execution_metadata` (json) 元数据
  172. - `total_tokens` (int) optional 总使用 tokens
  173. - `total_price` (decimal) optional 总费用
  174. - `currency` (string) optional 货币,如 `USD` / `RMB`
  175. - `created_at` (timestamp) 开始时间
  176. - `event: workflow_finished` workflow 执行结束,成功失败同一事件中不同状态
  177. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  178. - `workflow_run_id` (string) workflow 执行 ID
  179. - `event` (string) 固定为 `workflow_finished`
  180. - `data` (object) 详细内容
  181. - `id` (string) workflow 执行 ID
  182. - `workflow_id` (string) 关联 Workflow ID
  183. - `status` (string) 执行状态 `running` / `succeeded` / `failed` / `stopped`
  184. - `outputs` (json) Optional 输出内容
  185. - `error` (string) Optional 错误原因
  186. - `elapsed_time` (float) Optional 耗时(s)
  187. - `total_tokens` (int) Optional 总使用 tokens
  188. - `total_steps` (int) 总步数(冗余),默认 0
  189. - `created_at` (timestamp) 开始时间
  190. - `finished_at` (timestamp) 结束时间
  191. - `event: error`
  192. 流式输出过程中出现的异常会以 stream event 形式输出,收到异常事件后即结束。
  193. - `task_id` (string) 任务 ID,用于请求跟踪和下方的停止响应接口
  194. - `message_id` (string) 消息唯一 ID
  195. - `status` (int) HTTP 状态码
  196. - `code` (string) 错误码
  197. - `message` (string) 错误消息
  198. - `event: ping` 每 10s 一次的 ping 事件,保持连接存活。
  199. ### Errors
  200. - 404,对话不存在
  201. - 400,`invalid_param`,传入参数异常
  202. - 400,`app_unavailable`,App 配置不可用
  203. - 400,`provider_not_initialize`,无可用模型凭据配置
  204. - 400,`provider_quota_exceeded`,模型调用额度不足
  205. - 400,`model_currently_not_support`,当前模型不可用
  206. - 400,`completion_request_error`,文本生成失败
  207. - 500,服务内部异常
  208. </Properties>
  209. </Col>
  210. <Col sticky>
  211. <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}'`}>
  212. ```bash {{ title: 'cURL' }}
  213. curl -X POST '${props.appDetail.api_base_url}/chat-messages' \
  214. -H 'Authorization: Bearer {api_key}' \
  215. -H 'Content-Type: application/json' \
  216. --data-raw '{
  217. "inputs": {
  218. "name": "dify"
  219. },
  220. "query": "What are the specs of the iPhone 13 Pro Max?",
  221. "conversation_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  222. "response_mode": "streaming",
  223. "user": "abc-123",
  224. "files": [
  225. {
  226. "type": "image",
  227. "transfer_method": "remote_url",
  228. "url": "https://cloud.dify.ai/logo/logo-site.png"
  229. }
  230. ]
  231. }'
  232. ```
  233. </CodeGroup>
  234. ### 阻塞模式
  235. <CodeGroup title="Response">
  236. ```json {{ title: 'Response' }}
  237. {
  238. "event": "message",
  239. "task_id": "c3800678-a077-43df-a102-53f23ed20b88",
  240. "id": "9da23599-e713-473b-982c-4328d4f5c78a",
  241. "message_id": "9da23599-e713-473b-982c-4328d4f5c78a",
  242. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  243. "mode": "chat",
  244. "answer": "iPhone 13 Pro Max specs are listed here:...",
  245. "metadata": {
  246. "usage": {
  247. "prompt_tokens": 1033,
  248. "prompt_unit_price": "0.001",
  249. "prompt_price_unit": "0.001",
  250. "prompt_price": "0.0010330",
  251. "completion_tokens": 128,
  252. "completion_unit_price": "0.002",
  253. "completion_price_unit": "0.001",
  254. "completion_price": "0.0002560",
  255. "total_tokens": 1161,
  256. "total_price": "0.0012890",
  257. "currency": "USD",
  258. "latency": 0.7682376249867957
  259. },
  260. "retriever_resources": [
  261. {
  262. "position": 1,
  263. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  264. "dataset_name": "iPhone",
  265. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  266. "document_name": "iPhone List",
  267. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  268. "score": 0.98457545,
  269. "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\""
  270. }
  271. ]
  272. },
  273. "created_at": 1705407629
  274. }
  275. ```
  276. </CodeGroup>
  277. ### 流式模式
  278. <CodeGroup title="Response">
  279. ```streaming {{ title: 'Response' }}
  280. data: {"event": "workflow_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "sequence_number": 1, "created_at": 1679586595}}
  281. data: {"event": "node_started", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "created_at": 1679586595}}
  282. data: {"event": "node_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "node_id": "dfjasklfjdslag", "node_type": "start", "title": "Start", "index": 0, "predecessor_node_id": "fdljewklfklgejlglsd", "inputs": {}, "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "execution_metadata": {"total_tokens": 63127864, "total_price": 2.378, "currency": "USD"}, "created_at": 1679586595}}
  283. data: {"event": "workflow_finished", "task_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "workflow_run_id": "5ad498-f0c7-4085-b384-88cbe6290", "data": {"id": "5ad498-f0c7-4085-b384-88cbe6290", "workflow_id": "dfjasklfjdslag", "outputs": {}, "status": "succeeded", "elapsed_time": 0.324, "total_tokens": 63127864, "total_steps": "1", "created_at": 1679586595, "finished_at": 1679976595}}
  284. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " I", "created_at": 1679586595}
  285. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": "'m", "created_at": 1679586595}
  286. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " glad", "created_at": 1679586595}
  287. data: {"event": "message", "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " to", "created_at": 1679586595}
  288. data: {"event": "message", "message_id" : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " meet", "created_at": 1679586595}
  289. data: {"event": "message", "message_id" : "5ad4cb98-f0c7-4085-b384-88c403be6290", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "answer": " you", "created_at": 1679586595}
  290. 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\""}]}}
  291. 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"}
  292. 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": ""}
  293. ```
  294. </CodeGroup>
  295. </Col>
  296. </Row>
  297. ---
  298. <Heading
  299. url='/files/upload'
  300. method='POST'
  301. title='上传文件'
  302. name='#files-upload'
  303. />
  304. <Row>
  305. <Col>
  306. 上传文件并在发送消息时使用,可实现图文多模态理解。
  307. 支持您的应用程序所支持的所有格式。
  308. <i>上传的文件仅供当前终端用户使用。</i>
  309. ### Request Body
  310. 该接口需使用 `multipart/form-data` 进行请求。
  311. <Properties>
  312. <Property name='file' type='file' key='file'>
  313. 要上传的文件。
  314. </Property>
  315. <Property name='user' type='string' key='user'>
  316. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
  317. </Property>
  318. </Properties>
  319. ### Response
  320. 成功上传后,服务器会返回文件的 ID 和相关信息。
  321. - `id` (uuid) ID
  322. - `name` (string) 文件名
  323. - `size` (int) 文件大小(byte)
  324. - `extension` (string) 文件后缀
  325. - `mime_type` (string) 文件 mime-type
  326. - `created_by` (uuid) 上传人 ID
  327. - `created_at` (timestamp) 上传时间
  328. ### Errors
  329. - 400,`no_file_uploaded`,必须提供文件
  330. - 400,`too_many_files`,目前只接受一个文件
  331. - 400,`unsupported_preview`,该文件不支持预览
  332. - 400,`unsupported_estimate`,该文件不支持估算
  333. - 413,`file_too_large`,文件太大
  334. - 415,`unsupported_file_type`,不支持的扩展名,当前只接受文档类文件
  335. - 503,`s3_connection_failed`,无法连接到 S3 服务
  336. - 503,`s3_permission_denied`,无权限上传文件到 S3
  337. - 503,`s3_file_too_large`,文件超出 S3 大小限制
  338. </Col>
  339. <Col sticky>
  340. <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'`}>
  341. ```bash {{ title: 'cURL' }}
  342. curl -X POST '${props.appDetail.api_base_url}/files/upload' \
  343. --header 'Authorization: Bearer {api_key}' \
  344. --form 'file=@"/path/to/file"'
  345. ```
  346. </CodeGroup>
  347. <CodeGroup title="Response">
  348. ```json {{ title: 'Response' }}
  349. {
  350. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  351. "name": "example.png",
  352. "size": 1024,
  353. "extension": "png",
  354. "mime_type": "image/png",
  355. "created_by": 123,
  356. "created_at": 1577836800,
  357. }
  358. ```
  359. </CodeGroup>
  360. </Col>
  361. </Row>
  362. ---
  363. <Heading
  364. url='/chat-messages/:task_id/stop'
  365. method='POST'
  366. title='停止响应'
  367. name='#Stop'
  368. />
  369. <Row>
  370. <Col>
  371. 仅支持流式模式。
  372. ### Path
  373. - `task_id` (string) 任务 ID,可在流式返回 Chunk 中获取
  374. ### Request Body
  375. - `user` (string) Required
  376. 用户标识,用于定义终端用户的身份,必须和发送消息接口传入 user 保持一致。
  377. ### Response
  378. - `result` (string) 固定返回 success
  379. </Col>
  380. <Col sticky>
  381. <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"}'`}>
  382. ```bash {{ title: 'cURL' }}
  383. curl -X POST '${props.appDetail.api_base_url}/chat-messages/:task_id/stop' \
  384. -H 'Authorization: Bearer {api_key}' \
  385. -H 'Content-Type: application/json' \
  386. --data-raw '{
  387. "user": "abc-123"
  388. }'
  389. ```
  390. </CodeGroup>
  391. <CodeGroup title="Response">
  392. ```json {{ title: 'Response' }}
  393. {
  394. "result": "success"
  395. }
  396. ```
  397. </CodeGroup>
  398. </Col>
  399. </Row>
  400. ---
  401. <Heading
  402. url='/messages/:message_id/feedbacks'
  403. method='POST'
  404. title='消息反馈(点赞)'
  405. name='#feedbacks'
  406. />
  407. <Row>
  408. <Col>
  409. 消息终端用户反馈、点赞,方便应用开发者优化输出预期。
  410. ### Path Params
  411. <Properties>
  412. <Property name='message_id' type='string' key='message_id'>
  413. 消息 ID
  414. </Property>
  415. </Properties>
  416. ### Request Body
  417. <Properties>
  418. <Property name='rating' type='string' key='rating'>
  419. 点赞 like, 点踩 dislike, 撤销点赞 null
  420. </Property>
  421. <Property name='user' type='string' key='user'>
  422. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  423. </Property>
  424. <Property name='content' type='string' key='content'>
  425. 消息反馈的具体信息。
  426. </Property>
  427. </Properties>
  428. ### Response
  429. - `result` (string) 固定返回 success
  430. </Col>
  431. <Col sticky>
  432. <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}'`}>
  433. ```bash {{ title: 'cURL' }}
  434. curl -X POST '${props.appDetail.api_base_url}/messages/:message_id/feedbacks' \
  435. --header 'Authorization: Bearer {api_key}' \
  436. --header 'Content-Type: application/json' \
  437. --data-raw '{
  438. "rating": "like",
  439. "user": "abc-123",
  440. "content": "message feedback information"
  441. }'
  442. ```
  443. </CodeGroup>
  444. <CodeGroup title="Response">
  445. ```json {{ title: 'Response' }}
  446. {
  447. "result": "success"
  448. }
  449. ```
  450. </CodeGroup>
  451. </Col>
  452. </Row>
  453. ---
  454. <Heading
  455. url='/messages/{message_id}/suggested'
  456. method='GET'
  457. title='获取下一轮建议问题列表'
  458. name='#suggested'
  459. />
  460. <Row>
  461. <Col>
  462. 获取下一轮建议问题列表。
  463. ### Path Params
  464. <Properties>
  465. <Property name='message_id' type='string' key='message_id'>
  466. Message ID
  467. </Property>
  468. </Properties>
  469. ### Query
  470. <Properties>
  471. <Property name='user' type='string' key='user'>
  472. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  473. </Property>
  474. </Properties>
  475. </Col>
  476. <Col sticky>
  477. <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'`}>
  478. ```bash {{ title: 'cURL' }}
  479. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested'?user=abc-123 \
  480. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  481. --header 'Content-Type: application/json' \
  482. ```
  483. </CodeGroup>
  484. <CodeGroup title="Response">
  485. ```json {{ title: 'Response' }}
  486. {
  487. "result": "success",
  488. "data": [
  489. "a",
  490. "b",
  491. "c"
  492. ]
  493. }
  494. ```
  495. </CodeGroup>
  496. </Col>
  497. </Row>
  498. ---
  499. ---
  500. <Heading
  501. url='/messages'
  502. method='GET'
  503. title='获取会话历史消息'
  504. name='#messages'
  505. />
  506. <Row>
  507. <Col>
  508. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,即:倒序返回。
  509. ### Query
  510. <Properties>
  511. <Property name='conversation_id' type='string' key='conversation_id'>
  512. 会话 ID
  513. </Property>
  514. <Property name='user' type='string' key='user'>
  515. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  516. </Property>
  517. <Property name='first_id' type='string' key='first_id'>
  518. 当前页第一条聊天记录的 ID,默认 null
  519. </Property>
  520. <Property name='limit' type='int' key='limit'>
  521. 一次请求返回多少条聊天记录,默认 20 条。
  522. </Property>
  523. </Properties>
  524. ### Response
  525. - `data` (array[object]) 消息列表
  526. - `id` (string) 消息 ID
  527. - `conversation_id` (string) 会话 ID
  528. - `inputs` (object) 用户输入参数。
  529. - `query` (string) 用户输入 / 提问内容。
  530. - `message_files` (array[object]) 消息文件
  531. - `id` (string) ID
  532. - `type` (string) 文件类型,image 图片
  533. - `url` (string) 预览图片地址
  534. - `belongs_to` (string) 文件归属方,user 或 assistant
  535. - `answer` (string) 回答消息内容
  536. - `created_at` (timestamp) 创建时间
  537. - `feedback` (object) 反馈信息
  538. - `rating` (string) 点赞 like / 点踩 dislike
  539. - `retriever_resources` (array[RetrieverResource]) 引用和归属分段列表
  540. - `has_more` (bool) 是否存在下一页
  541. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  542. </Col>
  543. <Col sticky>
  544. ### Request Example
  545. <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}'`}>
  546. ```bash {{ title: 'cURL' }}
  547. curl -X GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  548. --header 'Authorization: Bearer {api_key}'
  549. ```
  550. </CodeGroup>
  551. ### Response Example
  552. <CodeGroup title="Response">
  553. ```json {{ title: 'Response' }}
  554. {
  555. "limit": 20,
  556. "has_more": false,
  557. "data": [
  558. {
  559. "id": "a076a87f-31e5-48dc-b452-0061adbbc922",
  560. "conversation_id": "cd78daf6-f9e4-4463-9ff2-54257230a0ce",
  561. "inputs": {
  562. "name": "dify"
  563. },
  564. "query": "iphone 13 pro",
  565. "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.",
  566. "message_files": [],
  567. "feedback": null,
  568. "retriever_resources": [
  569. {
  570. "position": 1,
  571. "dataset_id": "101b4c97-fc2e-463c-90b1-5261a4cdcafb",
  572. "dataset_name": "iPhone",
  573. "document_id": "8dd1ad74-0b5f-4175-b735-7d98bbbb4e00",
  574. "document_name": "iPhone List",
  575. "segment_id": "ed599c7f-2766-4294-9d1d-e5235a61270a",
  576. "score": 0.98457545,
  577. "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\""
  578. }
  579. ],
  580. "created_at": 1705569239
  581. }
  582. ]
  583. }
  584. ```
  585. </CodeGroup>
  586. ### Response Example(智能助手)
  587. <CodeGroup title="Response">
  588. ```json {{ title: 'Response' }}
  589. {
  590. "limit": 20,
  591. "has_more": false,
  592. "data": [
  593. {
  594. "id": "d35e006c-7c4d-458f-9142-be4930abdf94",
  595. "conversation_id": "957c068b-f258-4f89-ba10-6e8a0361c457",
  596. "inputs": {},
  597. "query": "draw a cat",
  598. "answer": "I have generated an image of a cat for you. Please check your messages to view the image.",
  599. "message_files": [
  600. {
  601. "id": "976990d2-5294-47e6-8f14-7356ba9d2d76",
  602. "type": "image",
  603. "url": "http://127.0.0.1:5001/files/tools/976990d2-5294-47e6-8f14-7356ba9d2d76.png?timestamp=1705988524&nonce=55df3f9f7311a9acd91bf074cd524092&sign=z43nMSO1L2HBvoqADLkRxr7Biz0fkjeDstnJiCK1zh8=",
  604. "belongs_to": "assistant"
  605. }
  606. ],
  607. "feedback": null,
  608. "retriever_resources": [],
  609. "created_at": 1705988187
  610. }
  611. ]
  612. }
  613. ```
  614. </CodeGroup>
  615. </Col>
  616. </Row>
  617. ---
  618. <Heading
  619. url='/conversations'
  620. method='GET'
  621. title='获取会话列表'
  622. name='#conversations'
  623. />
  624. <Row>
  625. <Col>
  626. 获取当前用户的会话列表,默认返回最近的 20 条。
  627. ### Query
  628. <Properties>
  629. <Property name='user' type='string' key='user'>
  630. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  631. </Property>
  632. <Property name='last_id' type='string' key='last_id'>
  633. (选填)当前页最后面一条记录的 ID,默认 null
  634. </Property>
  635. <Property name='limit' type='int' key='limit'>
  636. (选填)一次请求返回多少条记录,默认 20 条,最大 100 条,最小 1 条。
  637. </Property>
  638. <Property name='sort_by' type='string' key='sort_by'>
  639. (选填)排序字段,默认 -updated_at(按更新时间倒序排列)
  640. - 可选值:created_at, -created_at, updated_at, -updated_at
  641. - 字段前面的符号代表顺序或倒序,-代表倒序
  642. </Property>
  643. </Properties>
  644. ### Response
  645. - `data` (array[object]) 会话列表
  646. - `id` (string) 会话 ID
  647. - `name` (string) 会话名称,默认由大语言模型生成。
  648. - `inputs` (object) 用户输入参数。
  649. - `status` (string) 会话状态
  650. - `introduction` (string) 开场白
  651. - `created_at` (timestamp) 创建时间
  652. - `updated_at` (timestamp) 更新时间
  653. - `has_more` (bool)
  654. - `limit` (int) 返回条数,若传入超过系统限制,返回系统限制数量
  655. </Col>
  656. <Col sticky>
  657. <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}'`}>
  658. ```bash {{ title: 'cURL' }}
  659. curl -X GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  660. --header 'Authorization: Bearer {api_key}'
  661. ```
  662. </CodeGroup>
  663. <CodeGroup title="Response">
  664. ```json {{ title: 'Response' }}
  665. {
  666. "limit": 20,
  667. "has_more": false,
  668. "data": [
  669. {
  670. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  671. "name": "New chat",
  672. "inputs": {
  673. "book": "book",
  674. "myName": "Lucy"
  675. },
  676. "status": "normal",
  677. "created_at": 1679667915,
  678. "updated_at": 1679667915
  679. },
  680. {
  681. "id": "hSIhXBhNe8X1d8Et"
  682. // ...
  683. }
  684. ]
  685. }
  686. ```
  687. </CodeGroup>
  688. </Col>
  689. </Row>
  690. ---
  691. <Heading
  692. url='/conversations/:conversation_id'
  693. method='DELETE'
  694. title='删除会话'
  695. name='#delete'
  696. />
  697. <Row>
  698. <Col>
  699. 删除会话。
  700. ### Path
  701. - `conversation_id` (string) 会话 ID
  702. ### Request Body
  703. <Properties>
  704. <Property name='user' type='string' key='user'>
  705. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  706. </Property>
  707. </Properties>
  708. ### Response
  709. - `result` (string) 固定返回 success
  710. </Col>
  711. <Col sticky>
  712. <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}'`}>
  713. ```bash {{ title: 'cURL' }}
  714. curl -X DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \
  715. --header 'Content-Type: application/json' \
  716. --header 'Accept: application/json' \
  717. --header 'Authorization: Bearer {api_key}' \
  718. --data '{
  719. "user": "abc-123"
  720. }'
  721. ```
  722. </CodeGroup>
  723. <CodeGroup title="Response">
  724. ```json {{ title: 'Response' }}
  725. {
  726. "result": "success"
  727. }
  728. ```
  729. </CodeGroup>
  730. </Col>
  731. </Row>
  732. ---
  733. <Heading
  734. url='/conversations/:conversation_id/name'
  735. method='POST'
  736. title='会话重命名'
  737. name='#rename'
  738. />
  739. <Row>
  740. <Col>
  741. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  742. ### Path
  743. - `conversation_id` (string) 会话 ID
  744. ### Request Body
  745. <Properties>
  746. <Property name='name' type='string' key='name'>
  747. (选填)名称,若 `auto_generate` 为 `true` 时,该参数可不传。
  748. </Property>
  749. <Property name='auto_generate' type='bool' key='auto_generate'>
  750. (选填)自动生成标题,默认 false。
  751. </Property>
  752. <Property name='user' type='string' key='user'>
  753. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  754. </Property>
  755. </Properties>
  756. ### Response
  757. - `id` (string) 会话 ID
  758. - `name` (string) 会话名称
  759. - `inputs` (object) 用户输入参数
  760. - `status` (string) 会话状态
  761. - `introduction` (string) 开场白
  762. - `created_at` (timestamp) 创建时间
  763. - `updated_at` (timestamp) 更新时间
  764. </Col>
  765. <Col sticky>
  766. <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}'`}>
  767. ```bash {{ title: 'cURL' }}
  768. curl -X POST '${props.appDetail.api_base_url}/conversations/{conversation_id}/name' \
  769. --header 'Authorization: Bearer {api_key}' \
  770. --header 'Content-Type: application/json' \
  771. --data-raw '{
  772. "name": "",
  773. "auto_generate": true,
  774. "user": "abc-123"
  775. }'
  776. ```
  777. </CodeGroup>
  778. <CodeGroup title="Response">
  779. ```json {{ title: 'Response' }}
  780. {
  781. "id": "34d511d5-56de-4f16-a997-57b379508443",
  782. "name": "hello",
  783. "inputs": {},
  784. "status": "normal",
  785. "introduction": "",
  786. "created_at": 1732731141,
  787. "updated_at": 1732734510
  788. }
  789. ```
  790. </CodeGroup>
  791. </Col>
  792. </Row>
  793. ---
  794. <Heading
  795. url='/audio-to-text'
  796. method='POST'
  797. title='语音转文字'
  798. name='#audio'
  799. />
  800. <Row>
  801. <Col>
  802. ### Request Body
  803. 该接口需使用 `multipart/form-data` 进行请求。
  804. <Properties>
  805. <Property name='file' type='file' key='file'>
  806. 语音文件。
  807. 支持格式:`['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']`
  808. 文件大小限制:15MB
  809. </Property>
  810. <Property name='user' type='string' key='user'>
  811. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  812. </Property>
  813. </Properties>
  814. ### Response
  815. - `text` (string) 输出文字
  816. </Col>
  817. <Col sticky>
  818. <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]`}>
  819. ```bash {{ title: 'cURL' }}
  820. curl -X POST '${props.appDetail.api_base_url}/conversations/name' \
  821. --header 'Authorization: Bearer {api_key}' \
  822. --form 'file=@localfile;type=audio/mp3'
  823. ```
  824. </CodeGroup>
  825. <CodeGroup title="Response">
  826. ```json {{ title: 'Response' }}
  827. {
  828. "text": "hello"
  829. }
  830. ```
  831. </CodeGroup>
  832. </Col>
  833. </Row>
  834. ---
  835. <Heading
  836. url='/text-to-audio'
  837. method='POST'
  838. title='文字转语音'
  839. name='#audio'
  840. />
  841. <Row>
  842. <Col>
  843. 文字转语音。
  844. ### Request Body
  845. <Properties>
  846. <Property name='message_id' type='str' key='message_id'>
  847. Dify 生成的文本消息,那么直接传递生成的message-id 即可,后台会通过 message_id 查找相应的内容直接合成语音信息。如果同时传 message_id 和 text,优先使用 message_id。
  848. </Property>
  849. <Property name='text' type='str' key='text'>
  850. 语音生成内容。如果没有传 message-id的话,则会使用这个字段的内容
  851. </Property>
  852. <Property name='user' type='string' key='user'>
  853. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  854. </Property>
  855. </Properties>
  856. </Col>
  857. <Col sticky>
  858. <CodeGroup title="Request" tag="POST" label="/text-to-audio" targetCode={`curl -o text-to-audio.mp3 -X POST '${props.appDetail.api_base_url}/text-to-audio' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290",\n "text": "你好Dify",\n "user": "abc-123"\n}'`}>
  859. ```bash {{ title: 'cURL' }}
  860. curl -o text-to-audio.mp3 -X POST '${props.appDetail.api_base_url}/text-to-audio' \
  861. --header 'Authorization: Bearer {api_key}' \
  862. --header 'Content-Type: application/json' \
  863. --data-raw '{
  864. "message_id": "5ad4cb98-f0c7-4085-b384-88c403be6290",
  865. "text": "你好Dify",
  866. "user": "abc-123"
  867. }'
  868. ```
  869. </CodeGroup>
  870. <CodeGroup title="headers">
  871. ```json {{ title: 'headers' }}
  872. {
  873. "Content-Type": "audio/wav"
  874. }
  875. ```
  876. </CodeGroup>
  877. </Col>
  878. </Row>
  879. ---
  880. <Heading
  881. url='/info'
  882. method='GET'
  883. title='获取应用基本信息'
  884. name='#info'
  885. />
  886. <Row>
  887. <Col>
  888. 用于获取应用的基本信息
  889. ### Response
  890. - `name` (string) 应用名称
  891. - `description` (string) 应用描述
  892. - `tags` (array[string]) 应用标签
  893. </Col>
  894. <Col>
  895. <CodeGroup title="Request" tag="GET" label="/info" targetCode={`curl -X GET '${props.appDetail.api_base_url}/info' \\\n-H 'Authorization: Bearer {api_key}'`}>
  896. ```bash {{ title: 'cURL' }}
  897. curl -X GET '${props.appDetail.api_base_url}/info' \
  898. -H 'Authorization: Bearer {api_key}'
  899. ```
  900. </CodeGroup>
  901. <CodeGroup title="Response">
  902. ```json {{ title: 'Response' }}
  903. {
  904. "name": "My App",
  905. "description": "This is my app.",
  906. "tags": [
  907. "tag1",
  908. "tag2"
  909. ]
  910. }
  911. ```
  912. </CodeGroup>
  913. </Col>
  914. </Row>
  915. ---
  916. <Heading
  917. url='/parameters'
  918. method='GET'
  919. title='获取应用参数'
  920. name='#parameters'
  921. />
  922. <Row>
  923. <Col>
  924. 用于进入页面一开始,获取功能开关、输入参数名称、类型及默认值等使用。
  925. ### Response
  926. - `opening_statement` (string) 开场白
  927. - `suggested_questions` (array[string]) 开场推荐问题列表
  928. - `suggested_questions_after_answer` (object) 启用回答后给出推荐问题。
  929. - `enabled` (bool) 是否开启
  930. - `speech_to_text` (object) 语音转文本
  931. - `enabled` (bool) 是否开启
  932. - `retriever_resource` (object) 引用和归属
  933. - `enabled` (bool) 是否开启
  934. - `annotation_reply` (object) 标记回复
  935. - `enabled` (bool) 是否开启
  936. - `user_input_form` (array[object]) 用户输入表单配置
  937. - `text-input` (object) 文本输入控件
  938. - `label` (string) 控件展示标签名
  939. - `variable` (string) 控件 ID
  940. - `required` (bool) 是否必填
  941. - `default` (string) 默认值
  942. - `paragraph` (object) 段落文本输入控件
  943. - `label` (string) 控件展示标签名
  944. - `variable` (string) 控件 ID
  945. - `required` (bool) 是否必填
  946. - `default` (string) 默认值
  947. - `select` (object) 下拉控件
  948. - `label` (string) 控件展示标签名
  949. - `variable` (string) 控件 ID
  950. - `required` (bool) 是否必填
  951. - `default` (string) 默认值
  952. - `options` (array[string]) 选项值
  953. - `file_upload` (object) 文件上传配置
  954. - `image` (object) 图片设置
  955. 当前仅支持图片类型:`png`, `jpg`, `jpeg`, `webp`, `gif`
  956. - `enabled` (bool) 是否开启
  957. - `number_limits` (int) 图片数量限制,默认 3
  958. - `transfer_methods` (array[string]) 传递方式列表,remote_url , local_file,必选一个
  959. - `system_parameters` (object) 系统参数
  960. - `file_size_limit` (int) Document upload size limit (MB)
  961. - `image_file_size_limit` (int) Image file upload size limit (MB)
  962. - `audio_file_size_limit` (int) Audio file upload size limit (MB)
  963. - `video_file_size_limit` (int) Video file upload size limit (MB)
  964. </Col>
  965. <Col sticky>
  966. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl -X GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer {api_key}'`}>
  967. ```bash {{ title: 'cURL' }}
  968. curl -X GET '${props.appDetail.api_base_url}/parameters' \
  969. --header 'Authorization: Bearer {api_key}'
  970. ```
  971. </CodeGroup>
  972. <CodeGroup title="Response">
  973. ```json {{ title: 'Response' }}
  974. {
  975. "introduction": "nice to meet you",
  976. "user_input_form": [
  977. {
  978. "text-input": {
  979. "label": "a",
  980. "variable": "a",
  981. "required": true,
  982. "max_length": 48,
  983. "default": ""
  984. }
  985. },
  986. {
  987. // ...
  988. }
  989. ],
  990. "file_upload": {
  991. "image": {
  992. "enabled": true,
  993. "number_limits": 3,
  994. "transfer_methods": [
  995. "remote_url",
  996. "local_file"
  997. ]
  998. }
  999. },
  1000. "system_parameters": {
  1001. "file_size_limit": 15,
  1002. "image_file_size_limit": 10,
  1003. "audio_file_size_limit": 50,
  1004. "video_file_size_limit": 100
  1005. }
  1006. }
  1007. ```
  1008. </CodeGroup>
  1009. </Col>
  1010. </Row>
  1011. ---
  1012. <Heading
  1013. url='/meta'
  1014. method='GET'
  1015. title='获取应用Meta信息'
  1016. name='#meta'
  1017. />
  1018. <Row>
  1019. <Col>
  1020. 用于获取工具icon
  1021. ### Response
  1022. - `tool_icons`(object[string]) 工具图标
  1023. - `工具名称` (string)
  1024. - `icon` (object|string)
  1025. - (object) 图标
  1026. - `background` (string) hex格式的背景色
  1027. - `content`(string) emoji
  1028. - (string) 图标URL
  1029. </Col>
  1030. <Col>
  1031. <CodeGroup title="Request" tag="POST" label="/meta" targetCode={`curl -X GET '${props.appDetail.api_base_url}/meta' \\\n-H 'Authorization: Bearer {api_key}'`}>
  1032. ```bash {{ title: 'cURL' }}
  1033. curl -X GET '${props.appDetail.api_base_url}/meta' \
  1034. -H 'Authorization: Bearer {api_key}'
  1035. ```
  1036. </CodeGroup>
  1037. <CodeGroup title="Response">
  1038. ```json {{ title: 'Response' }}
  1039. {
  1040. "tool_icons": {
  1041. "dalle2": "https://cloud.dify.ai/console/api/workspaces/current/tool-provider/builtin/dalle/icon",
  1042. "api_tool": {
  1043. "background": "#252525",
  1044. "content": "\ud83d\ude01"
  1045. }
  1046. }
  1047. }
  1048. ```
  1049. </CodeGroup>
  1050. </Col>
  1051. </Row>
  1052. ---
  1053. ---
  1054. <Heading
  1055. url='/apps/annotations'
  1056. method='GET'
  1057. title='获取标注列表'
  1058. name='#annotation_list'
  1059. />
  1060. <Row>
  1061. <Col>
  1062. ### Query
  1063. <Properties>
  1064. <Property name='page' type='string' key='page'>
  1065. 页码
  1066. </Property>
  1067. <Property name='limit' type='string' key='limit'>
  1068. 每页数量
  1069. </Property>
  1070. </Properties>
  1071. </Col>
  1072. <Col sticky>
  1073. <CodeGroup
  1074. title="Request"
  1075. tag="GET"
  1076. label="/apps/annotations"
  1077. targetCode={`curl --location --request GET '${props.apiBaseUrl}/apps/annotations?page=1&limit=20' \\\n--header 'Authorization: Bearer {api_key}'`}
  1078. >
  1079. ```bash {{ title: 'cURL' }}
  1080. curl --location --request GET '${props.apiBaseUrl}/apps/annotations?page=1&limit=20' \
  1081. --header 'Authorization: Bearer {api_key}'
  1082. ```
  1083. </CodeGroup>
  1084. <CodeGroup title="Response">
  1085. ```json {{ title: 'Response' }}
  1086. {
  1087. "data": [
  1088. {
  1089. "id": "69d48372-ad81-4c75-9c46-2ce197b4d402",
  1090. "question": "What is your name?",
  1091. "answer": "I am Dify.",
  1092. "hit_count": 0,
  1093. "created_at": 1735625869
  1094. }
  1095. ],
  1096. "has_more": false,
  1097. "limit": 20,
  1098. "total": 1,
  1099. "page": 1
  1100. }
  1101. ```
  1102. </CodeGroup>
  1103. </Col>
  1104. </Row>
  1105. ---
  1106. <Heading
  1107. url='/apps/annotations'
  1108. method='POST'
  1109. title='创建标注'
  1110. name='#create_annotation'
  1111. />
  1112. <Row>
  1113. <Col>
  1114. ### Query
  1115. <Properties>
  1116. <Property name='question' type='string' key='question'>
  1117. 问题
  1118. </Property>
  1119. <Property name='answer' type='string' key='answer'>
  1120. 答案内容
  1121. </Property>
  1122. </Properties>
  1123. </Col>
  1124. <Col sticky>
  1125. <CodeGroup
  1126. title="Request"
  1127. tag="POST"
  1128. label="/apps/annotations"
  1129. targetCode={`curl --location --request POST '${props.apiBaseUrl}/apps/annotations' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{"question": "What is your name?","answer": "I am Dify."}'`}
  1130. >
  1131. ```bash {{ title: 'cURL' }}
  1132. curl --location --request POST '${props.apiBaseUrl}/apps/annotations' \
  1133. --header 'Authorization: Bearer {api_key}' \
  1134. --header 'Content-Type: application/json' \
  1135. --data-raw '{
  1136. "question": "What is your name?",
  1137. "answer": "I am Dify."
  1138. }'
  1139. ```
  1140. </CodeGroup>
  1141. <CodeGroup title="Response">
  1142. ```json {{ title: 'Response' }}
  1143. {
  1144. {
  1145. "id": "69d48372-ad81-4c75-9c46-2ce197b4d402",
  1146. "question": "What is your name?",
  1147. "answer": "I am Dify.",
  1148. "hit_count": 0,
  1149. "created_at": 1735625869
  1150. }
  1151. }
  1152. ```
  1153. </CodeGroup>
  1154. </Col>
  1155. </Row>
  1156. ---
  1157. <Heading
  1158. url='/apps/annotations/{annotation_id}'
  1159. method='PUT'
  1160. title='更新标注'
  1161. name='#update_annotation'
  1162. />
  1163. <Row>
  1164. <Col>
  1165. ### Query
  1166. <Properties>
  1167. <Property name='annotation_id' type='string' key='annotation_id'>
  1168. 标注 ID
  1169. </Property>
  1170. <Property name='question' type='string' key='question'>
  1171. 问题
  1172. </Property>
  1173. <Property name='answer' type='string' key='answer'>
  1174. 答案内容
  1175. </Property>
  1176. </Properties>
  1177. </Col>
  1178. <Col sticky>
  1179. <CodeGroup
  1180. title="Request"
  1181. tag="PUT"
  1182. label="/apps/annotations/{annotation_id}"
  1183. targetCode={`curl --location --request POST '${props.apiBaseUrl}/apps/annotations/{annotation_id}' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{"question": "What is your name?","answer": "I am Dify."}'`}
  1184. >
  1185. ```bash {{ title: 'cURL' }}
  1186. curl --location --request POST '${props.apiBaseUrl}/apps/annotations/{annotation_id}' \
  1187. --header 'Authorization: Bearer {api_key}' \
  1188. --header 'Content-Type: application/json' \
  1189. --data-raw '{
  1190. "question": "What is your name?",
  1191. "answer": "I am Dify."
  1192. }'
  1193. ```
  1194. </CodeGroup>
  1195. <CodeGroup title="Response">
  1196. ```json {{ title: 'Response' }}
  1197. {
  1198. {
  1199. "id": "69d48372-ad81-4c75-9c46-2ce197b4d402",
  1200. "question": "What is your name?",
  1201. "answer": "I am Dify.",
  1202. "hit_count": 0,
  1203. "created_at": 1735625869
  1204. }
  1205. }
  1206. ```
  1207. </CodeGroup>
  1208. </Col>
  1209. </Row>
  1210. ---
  1211. <Heading
  1212. url='/apps/annotations/{annotation_id}'
  1213. method='DELETE'
  1214. title='删除标注'
  1215. name='#delete_annotation'
  1216. />
  1217. <Row>
  1218. <Col>
  1219. ### Query
  1220. <Properties>
  1221. <Property name='annotation_id' type='string' key='annotation_id'>
  1222. 标注 ID
  1223. </Property>
  1224. </Properties>
  1225. </Col>
  1226. <Col sticky>
  1227. <CodeGroup
  1228. title="Request"
  1229. tag="PUT"
  1230. label="/apps/annotations/{annotation_id}"
  1231. targetCode={`curl --location --request DELETE '${props.apiBaseUrl}/apps/annotations/{annotation_id}' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json'`}
  1232. >
  1233. ```bash {{ title: 'cURL' }}
  1234. curl --location --request DELETE '${props.apiBaseUrl}/apps/annotations/{annotation_id}' \
  1235. --header 'Authorization: Bearer {api_key}'
  1236. ```
  1237. </CodeGroup>
  1238. <CodeGroup title="Response">
  1239. ```json {{ title: 'Response' }}
  1240. {"result": "success"}
  1241. ```
  1242. </CodeGroup>
  1243. </Col>
  1244. </Row>
  1245. ---
  1246. <Heading
  1247. url='/apps/annotation-reply/{action}'
  1248. method='POST'
  1249. title='标注回复初始设置'
  1250. name='#initial_annotation_reply_settings'
  1251. />
  1252. <Row>
  1253. <Col>
  1254. ### Query
  1255. <Properties>
  1256. <Property name='action' type='string' key='action'>
  1257. 动作,只能是 'enable' 或 'disable'
  1258. </Property>
  1259. <Property name='embedding_model_provider' type='string' key='embedding_model_provider'>
  1260. 指定的嵌入模型提供商, 必须先在系统内设定好接入的模型,对应的是provider字段
  1261. </Property>
  1262. <Property name='embedding_model' type='string' key='embedding_model'>
  1263. 指定的嵌入模型,对应的是model字段
  1264. </Property>
  1265. <Property name='score_threshold' type='number' key='score_threshold'>
  1266. 相似度阈值,当相似度大于该阈值时,系统会自动回复,否则不回复
  1267. </Property>
  1268. </Properties>
  1269. </Col>
  1270. <Col sticky>
  1271. 嵌入模型的提供商和模型名称可以通过以下接口获取:v1/workspaces/current/models/model-types/text-embedding, 具体见:通过 API 维护知识库。 使用的Authorization是Dataset的API Token。
  1272. <CodeGroup
  1273. title="Request"
  1274. tag="POST"
  1275. label="/apps/annotation-reply/{action}"
  1276. targetCode={`curl --location --request POST '${props.apiBaseUrl}/apps/annotation-reply/{action}' \\\n--header 'Authorization: Bearer {api_key}' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{"score_threshold": 0.9, "embedding_provider_name": "zhipu", "embedding_model_name": "embedding_3"}'`}
  1277. >
  1278. ```bash {{ title: 'cURL' }}
  1279. curl --location --request POST 'https://api.dify.ai/v1/apps/annotation-reply/{action}' \
  1280. --header 'Authorization: Bearer {api_key}' \
  1281. --header 'Content-Type: application/json' \
  1282. --data-raw '{
  1283. "score_threshold": 0.9,
  1284. "embedding_provider_name": "zhipu",
  1285. "embedding_model_name": "embedding_3"
  1286. }'
  1287. ```
  1288. </CodeGroup>
  1289. <CodeGroup title="Response">
  1290. ```json {{ title: 'Response' }}
  1291. {
  1292. "job_id": "b15c8f68-1cf4-4877-bf21-ed7cf2011802",
  1293. "job_status": "waiting"
  1294. }
  1295. ```
  1296. </CodeGroup>
  1297. 该接口是异步执行,所以会返回一个job_id,通过查询job状态接口可以获取到最终的执行结果。
  1298. </Col>
  1299. </Row>
  1300. ---
  1301. <Heading
  1302. url='/apps/annotation-reply/{action}/status/{job_id}'
  1303. method='GET'
  1304. title='查询标注回复初始设置任务状态'
  1305. name='#initial_annotation_reply_settings_task_status'
  1306. />
  1307. <Row>
  1308. <Col>
  1309. ### Query
  1310. <Properties>
  1311. <Property name='action' type='string' key='action'>
  1312. 动作,只能是 'enable' 或 'disable',并且必须和标注回复初始设置接口的动作一致
  1313. </Property>
  1314. <Property name='job_id' type='string' key='job_id'>
  1315. 任务 ID,从标注回复初始设置接口返回的 job_id
  1316. </Property>
  1317. </Properties>
  1318. </Col>
  1319. <Col sticky>
  1320. <CodeGroup
  1321. title="Request"
  1322. tag="GET"
  1323. label="/apps/annotations"
  1324. targetCode={`curl --location --request GET '${props.apiBaseUrl}/apps/annotation-reply/{action}/status/{job_id}' \\\n--header 'Authorization: Bearer {api_key}'`}
  1325. >
  1326. ```bash {{ title: 'cURL' }}
  1327. curl --location --request GET '${props.apiBaseUrl}/apps/annotation-reply/{action}/status/{job_id}' \
  1328. --header 'Authorization: Bearer {api_key}'
  1329. ```
  1330. </CodeGroup>
  1331. <CodeGroup title="Response">
  1332. ```json {{ title: 'Response' }}
  1333. {
  1334. "job_id": "b15c8f68-1cf4-4877-bf21-ed7cf2011802",
  1335. "job_status": "waiting",
  1336. "error_msg": ""
  1337. }
  1338. ```
  1339. </CodeGroup>
  1340. </Col>
  1341. </Row>