template_chat.zh.mdx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. import { CodeGroup } from '../code.tsx'
  2. import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx'
  3. # 对话型应用 API
  4. 可用于大部分场景的对话型应用,采用一问一答模式与用户持续对话。要开始一个对话请调用 chat-messages 接口,通过继续传入返回的 conversation_id 可持续保持该会话。**[开始前请阅读 !! 什么是 Bearer Token ?](https://swagger.io/docs/specification/authentication/bearer-authentication/)**
  5. <div>
  6. ### 鉴权
  7. Dify Service API 使用 `API-Key` 进行鉴权。
  8. 建议开发者把 `API-Key` 放在后端存储,而非分享或者放在客户端存储,以免 `API-Key` 泄露,导致财产损失。
  9. 所有 API 请求都应在 **`Authorization`** HTTP Header 中包含您的 `API-Key`,如下所示:
  10. <CodeGroup title="Code">
  11. ```javascript
  12. Authorization: Bearer {API_KEY}
  13. ```
  14. </CodeGroup>
  15. </div>
  16. ---
  17. <Heading
  18. url='/chat-messages'
  19. method='POST'
  20. title='发送对话消息'
  21. name='#Create-Chat-Message'
  22. />
  23. <Row>
  24. <Col>
  25. 创建会话消息,或基于此前的对话继续发送消息。
  26. ### Request Body
  27. <Properties>
  28. <Property name='inputs' type='object' key='inputs'>
  29. (选填)以键值对方式提供用户输入字段,与提示词编排中的变量对应。Key 为变量名称,Value 是参数值。如果字段类型为 Select,传入的 Value 需为预设选项之一。
  30. <ul>
  31. {!!props.variables.length && props.variables.map(
  32. val => (
  33. <SubProperty name={val.key} type={val.type} key={val.key}>
  34. {val.name ? `${val.name}` : ''}
  35. </SubProperty>
  36. )
  37. )}
  38. </ul>
  39. </Property>
  40. <Property name='query' type='string' key='query'>
  41. 用户输入/提问内容
  42. </Property>
  43. <Property name='response_mode' type='string' key='response_mode'>
  44. - blocking 阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断)
  45. - streaming 流式返回。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现流式返回。
  46. </Property>
  47. <Property name='conversation_id' type='string' key='conversation_id'>
  48. (必填)<strong>‼️ 会话标识符,首次对话为 conversation_id: "" ‼️</strong>,如果要继续对话请传入上下文返回的 conversation_id
  49. </Property>
  50. <Property name='user' type='string' key='user'>
  51. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  52. </Property>
  53. <Property name='files' type='array[object]' key='files'>
  54. 上传的文件。
  55. - `type` 文件类型:目前仅支持 `image`。
  56. - `transfer_method` 传递方式:
  57. - `remote_url`: 图片地址。
  58. - `local_file`: 上传文件。
  59. - `upload_file_id` 上传文件 ID。`transfer_method` 为 `local_file` 时必填。
  60. - `url` 图片地址。`transfer_method` 为 `remote_url` 时必填。
  61. </Property>
  62. </Properties>
  63. </Col>
  64. <Col sticky>
  65. <CodeGroup title="Request" tag="POST" label="/chat-messages" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "inputs": ${JSON.stringify(props.inputs)},\n "query": "eh",\n "response_mode": "streaming",\n "conversation_id": "",\n "user": "abc-123"\n}'\n`}>
  66. ```bash {{ title: 'cURL' }}
  67. curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \
  68. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  69. --header 'Content-Type: application/json' \
  70. --data-raw '{
  71. "inputs": {},
  72. "query": "eh",
  73. "response_mode": "streaming",
  74. "conversation_id": "",
  75. "user": "abc-123"
  76. }'
  77. ```
  78. </CodeGroup>
  79. ### blocking
  80. <CodeGroup title="Response">
  81. ```json {{ title: 'Response' }}
  82. {
  83. "answer": "Hi, is there anything I can help you?",
  84. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  85. "created_at": 1679587005,
  86. "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
  87. }
  88. ```
  89. </CodeGroup>
  90. ### streaming
  91. <CodeGroup title="Response">
  92. ```streaming {{ title: 'Response' }}
  93. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  94. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  95. ```
  96. </CodeGroup>
  97. </Col>
  98. </Row>
  99. ---
  100. <Heading
  101. url='/messages/{message_id}/feedbacks'
  102. method='POST'
  103. title='消息反馈(点赞)'
  104. name='#feedbacks'
  105. />
  106. <Row>
  107. <Col>
  108. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  109. ### Path Params
  110. <Properties>
  111. <Property name='message_id' type='string' key='message_id'>
  112. 消息 ID
  113. </Property>
  114. </Properties>
  115. ### Request Body
  116. <Properties>
  117. <Property name='rating' type='string' key='rating'>
  118. like 或 dislike, 空值为撤销
  119. </Property>
  120. <Property name='user' type='string' key='user'>
  121. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  122. </Property>
  123. </Properties>
  124. </Col>
  125. <Col sticky>
  126. <CodeGroup title="Request" tag="POST" label="/messages/{message_id}/feedbacks" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n "rating": "like",\n "user": "abc-123"\n}'`}>
  127. ```bash {{ title: 'cURL' }}
  128. curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
  129. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  130. --header 'Content-Type: application/json' \
  131. --data-raw '{
  132. "rating": "like",
  133. "user": "abc-123"
  134. }'
  135. ```
  136. </CodeGroup>
  137. <CodeGroup title="Response">
  138. ```json {{ title: 'Response' }}
  139. {
  140. "has_more": false,
  141. "data": [
  142. {
  143. "id": "WAz8eIbvDR60rouK",
  144. "username": "FrankMcCallister",
  145. "phone_number": "1-800-759-3000",
  146. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  147. "display_name": null,
  148. "conversation_id": "xgQQXg3hrtjh7AvZ",
  149. "created_at": 692233200
  150. },
  151. {
  152. "id": "hSIhXBhNe8X1d8Et"
  153. // ...
  154. }
  155. ]
  156. }
  157. ```
  158. </CodeGroup>
  159. </Col>
  160. </Row>
  161. ---
  162. <Heading
  163. url='/messages/{message_id}/suggested'
  164. method='GET'
  165. title='消息下一步问题建议'
  166. name='#suggested'
  167. />
  168. <Row>
  169. <Col>
  170. 获取针对当前问题的下一步问题建议
  171. ### Path Params
  172. <Properties>
  173. <Property name='message_id' type='string' key='message_id'>
  174. 消息 ID
  175. </Property>
  176. </Properties>
  177. </Col>
  178. <Col sticky>
  179. <CodeGroup title="Request" tag="GET" label="/messages/{message_id}/suggested" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json'`}>
  180. ```bash {{ title: 'cURL' }}
  181. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
  182. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  183. --header 'Content-Type: application/json' \
  184. ```
  185. </CodeGroup>
  186. <CodeGroup title="Response">
  187. ```json {{ title: 'Response' }}
  188. {
  189. "result": "success",
  190. "data": [
  191. "a",
  192. "b",
  193. "c"
  194. ]
  195. ]
  196. }
  197. ```
  198. </CodeGroup>
  199. </Col>
  200. </Row>
  201. ---
  202. <Heading
  203. url='/messages'
  204. method='GET'
  205. title='获取会话历史消息'
  206. name='#messages'
  207. />
  208. <Row>
  209. <Col>
  210. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,加载更多时,返回 `first_id` 之前的 `limit` 条。
  211. ### Query
  212. <Properties>
  213. <Property name='conversation_id' type='string' key='conversation_id'>
  214. 会话 ID
  215. </Property>
  216. <Property name='first_id' type='string' key='first_id'>
  217. 当前页第一条聊天记录的 ID,默认 none
  218. </Property>
  219. <Property name='limit' type='int' key='limit'>
  220. 一次请求返回多少条聊天记录
  221. </Property>
  222. <Property name='user' type='string' key='user'>
  223. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  224. </Property>
  225. </Properties>
  226. </Col>
  227. <Col sticky>
  228. <CodeGroup title="Request" tag="GET" label="/messages" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id=' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  229. ```bash {{ title: 'cURL' }}
  230. curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  231. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  232. ```
  233. </CodeGroup>
  234. <CodeGroup title="Response">
  235. ```json {{ title: 'Response' }}
  236. {
  237. "has_more": false,
  238. "data": [
  239. {
  240. "id": "WAz8eIbvDR60rouK",
  241. "conversation_id": "xgQQXg3hrtjh7AvZ",
  242. "inputs": {},
  243. "query": "...",
  244. "answer": "...",
  245. "feedback": "like",
  246. "created_at": 692233200
  247. },
  248. {
  249. "id": "hSIhXBhNe8X1d8Et"
  250. // ...
  251. }
  252. ]
  253. }
  254. ```
  255. </CodeGroup>
  256. </Col>
  257. </Row>
  258. ---
  259. <Heading
  260. url='/conversations'
  261. method='GET'
  262. title='获取会话列表'
  263. name='#conversations'
  264. />
  265. <Row>
  266. <Col>
  267. 获取当前用户的会话列表,默认返回最近的 20 条。
  268. ### Query
  269. <Properties>
  270. <Property name='last_id' type='string' key='last_id'>
  271. 当前页最后面一条记录的 ID,默认 none
  272. </Property>
  273. <Property name='limit' type='int' key='limit'>
  274. 一次请求返回多少条记录
  275. </Property>
  276. <Property name='user' type='string' key='user'>
  277. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  278. </Property>
  279. </Properties>
  280. </Col>
  281. <Col sticky>
  282. <CodeGroup title="Request" tag="GET" label="/conversations" targetCode={`curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20'\\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  283. ```bash {{ title: 'cURL' }}
  284. curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  285. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  286. ```
  287. </CodeGroup>
  288. <CodeGroup title="Response">
  289. ```json {{ title: 'Response' }}
  290. {
  291. "limit": 20,
  292. "has_more": false,
  293. "data": [
  294. {
  295. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  296. "name": "New chat",
  297. "inputs": {
  298. "book": "book",
  299. "myName": "Lucy"
  300. },
  301. "status": "normal",
  302. "created_at": 1679667915
  303. },
  304. {
  305. "id": "hSIhXBhNe8X1d8Et"
  306. // ...
  307. }
  308. ]
  309. }
  310. ```
  311. </CodeGroup>
  312. </Col>
  313. </Row>
  314. ---
  315. <Heading
  316. url='/conversations/{converation_id}/name'
  317. method='POST'
  318. title='会话重命名'
  319. name='#rename'
  320. />
  321. <Row>
  322. <Col>
  323. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  324. ### Request Body
  325. <Properties>
  326. <Property name='name' type='string' key='name'>
  327. 新的名称
  328. </Property>
  329. <Property name='user' type='string' key='user'>
  330. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  331. </Property>
  332. </Properties>
  333. </Col>
  334. <Col sticky>
  335. <CodeGroup title="Request" tag="POST" label="/conversations/{converation_id}/name" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "name": "", \n "user": "abc-123"\n}'`}>
  336. ```bash {{ title: 'cURL' }}
  337. curl --location --request POST '${props.appDetail.api_base_url}/conversations/{converation_id}/name' \
  338. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  339. --header 'Content-Type: application/json' \
  340. --data-raw '{
  341. "name": "",
  342. "user": "abc-123"
  343. }'
  344. ```
  345. </CodeGroup>
  346. <CodeGroup title="Response">
  347. ```json {{ title: 'Response' }}
  348. {
  349. "result": "success"
  350. }
  351. ```
  352. </CodeGroup>
  353. </Col>
  354. </Row>
  355. ---
  356. <Heading
  357. url='/conversations/{converation_id}'
  358. method='DELETE'
  359. title='删除会话'
  360. name='#delete'
  361. />
  362. <Row>
  363. <Col>
  364. 删除会话。
  365. ### Request Body
  366. <Properties>
  367. <Property name='user' type='string' key='user'>
  368. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  369. </Property>
  370. </Properties>
  371. </Col>
  372. <Col sticky>
  373. <CodeGroup title="Request" tag="DELETE" label="/conversations/{converation_id}" targetCode={`curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{conversation_id}' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{ \n "user": "abc-123"\n}'`}>
  374. ```bash {{ title: 'cURL' }}
  375. curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \
  376. --header 'Content-Type: application/json' \
  377. --header 'Accept: application/json' \
  378. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  379. --data '{
  380. "user": "abc-123"
  381. }'
  382. ```
  383. </CodeGroup>
  384. <CodeGroup title="Response">
  385. ```json {{ title: 'Response' }}
  386. {
  387. "result": "success"
  388. }
  389. ```
  390. </CodeGroup>
  391. </Col>
  392. </Row>
  393. ---
  394. <Heading
  395. url='/files/upload'
  396. method='POST'
  397. title='上传文件'
  398. name='#files-upload'
  399. />
  400. <Row>
  401. <Col>
  402. 上传文件到服务器,供文本生成、对话使用。上传的文件仅供当前终端用户使用。
  403. ### Request Body
  404. 请求方式为 `multipart/form-data`。
  405. <Properties>
  406. <Property name='file' type='file' key='file'>
  407. 要上传的文件。目前支持 png, jpg, jpeg, webp, gif 格式文件。
  408. </Property>
  409. <Property name='user' type='string' key='user'>
  410. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  411. </Property>
  412. </Properties>
  413. </Col>
  414. <Col sticky>
  415. <CodeGroup title="Request" tag="POST" label="/files/upload" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'file=@localfile;type=image/[png|jpeg|jpg|webp|gif] \\\n--form 'user=abc-123'`}>
  416. ```bash {{ title: 'cURL' }}
  417. curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \
  418. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  419. --form 'file=@"/path/to/file"'
  420. ```
  421. </CodeGroup>
  422. <CodeGroup title="Response">
  423. ```json {{ title: 'Response' }}
  424. {
  425. "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
  426. "name": "example.png",
  427. "size": 1024,
  428. "extension": "png",
  429. "mime_type": "image/png",
  430. "created_by": 123,
  431. "created_at": 1577836800,
  432. }
  433. ```
  434. </CodeGroup>
  435. </Col>
  436. </Row>
  437. ---
  438. <Heading
  439. url='/audio-to-text'
  440. method='POST'
  441. title='语音转文字'
  442. name='#audio'
  443. />
  444. <Row>
  445. <Col>
  446. 语音转文字,仅支持 openai 模型。
  447. ### Request Body
  448. <Properties>
  449. <Property name='file' type='file' key='file'>
  450. 语音文件。
  451. 文件上传当前限制为 15 MB,并且支持以下输入文件类型:mp3、mp4、mpeg、mpga、m4a、wav 和 webm。
  452. </Property>
  453. </Properties>
  454. </Col>
  455. <Col sticky>
  456. <CodeGroup title="Request" tag="POST" label="/audio-to-text" targetCode={`curl --location --request POST '${props.appDetail.api_base_url}/audio-to-text' \\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \\\n--form 'file=@localfile;type=audio/[mp3|mp4|mpeg|mpga|m4a|wav|webm]`}>
  457. ```bash {{ title: 'cURL' }}
  458. curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \
  459. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  460. --form 'file=@localfile;type=audio/mp3'
  461. ```
  462. </CodeGroup>
  463. <CodeGroup title="Response">
  464. ```json {{ title: 'Response' }}
  465. {
  466. "text": ""
  467. }
  468. ```
  469. </CodeGroup>
  470. </Col>
  471. </Row>
  472. ---
  473. <Heading
  474. url='/parameters'
  475. method='GET'
  476. title='获取应用配置信息'
  477. name='#rename'
  478. />
  479. <Row>
  480. <Col>
  481. 内容包括:
  482. 1. 已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  483. 2. 上传图片的配置,包括是否开启上传图片,上传图片的数量和上传方式。注意:这个配置只有使用支持多模态的模型时才会生效。
  484. ### Query
  485. <Properties>
  486. <Property name='user' type='string' key='user'>
  487. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  488. </Property>
  489. </Properties>
  490. </Col>
  491. <Col sticky>
  492. <CodeGroup title="Request" tag="GET" label="/parameters" targetCode={` curl --location --request GET '${props.appDetail.api_base_url}/parameters'\\\n--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'`}>
  493. ```bash {{ title: 'cURL' }}
  494. curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  495. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  496. ```
  497. </CodeGroup>
  498. <CodeGroup title="Response">
  499. ```json {{ title: 'Response' }}
  500. {
  501. "introduction": "nice to meet you",
  502. "user_input_form": [
  503. {
  504. "text-input": {
  505. "label": "a",
  506. "variable": "a",
  507. "required": true,
  508. "max_length": 48,
  509. "default": ""
  510. }
  511. }
  512. {
  513. // ...
  514. }
  515. ],
  516. "file_upload": {
  517. "image": {
  518. "enabled": true,
  519. "number_limits": 3,
  520. "transfer_methods": [
  521. "remote_url",
  522. "local_file"
  523. ]
  524. }
  525. }
  526. }
  527. ```
  528. </CodeGroup>
  529. </Col>
  530. </Row>