template_chat.zh.mdx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. </Properties>
  54. </Col>
  55. <Col sticky>
  56. <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`}>
  57. ```bash {{ title: 'cURL' }}
  58. curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \
  59. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  60. --header 'Content-Type: application/json' \
  61. --data-raw '{
  62. "inputs": {},
  63. "query": "eh",
  64. "response_mode": "streaming",
  65. "conversation_id": "",
  66. "user": "abc-123"
  67. }'
  68. ```
  69. </CodeGroup>
  70. ### blocking
  71. <CodeGroup title="Response">
  72. ```json {{ title: 'Response' }}
  73. {
  74. "answer": "Hi, is there anything I can help you?",
  75. "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
  76. "created_at": 1679587005,
  77. "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
  78. }
  79. ```
  80. </CodeGroup>
  81. ### streaming
  82. <CodeGroup title="Response">
  83. ```streaming {{ title: 'Response' }}
  84. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  85. data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
  86. ```
  87. </CodeGroup>
  88. </Col>
  89. </Row>
  90. ---
  91. <Heading
  92. url='/messages/{message_id}/feedbacks'
  93. method='POST'
  94. title='消息反馈(点赞)'
  95. name='#feedbacks'
  96. />
  97. <Row>
  98. <Col>
  99. 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。
  100. ### Path Params
  101. <Properties>
  102. <Property name='message_id' type='string' key='message_id'>
  103. 消息 ID
  104. </Property>
  105. </Properties>
  106. ### Request Body
  107. <Properties>
  108. <Property name='rating' type='string' key='rating'>
  109. like 或 dislike, 空值为撤销
  110. </Property>
  111. <Property name='user' type='string' key='user'>
  112. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  113. </Property>
  114. </Properties>
  115. </Col>
  116. <Col sticky>
  117. <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}'`}>
  118. ```bash {{ title: 'cURL' }}
  119. curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
  120. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  121. --header 'Content-Type: application/json' \
  122. --data-raw '{
  123. "rating": "like",
  124. "user": "abc-123"
  125. }'
  126. ```
  127. </CodeGroup>
  128. <CodeGroup title="Response">
  129. ```json {{ title: 'Response' }}
  130. {
  131. "has_more": false,
  132. "data": [
  133. {
  134. "id": "WAz8eIbvDR60rouK",
  135. "username": "FrankMcCallister",
  136. "phone_number": "1-800-759-3000",
  137. "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
  138. "display_name": null,
  139. "conversation_id": "xgQQXg3hrtjh7AvZ",
  140. "created_at": 692233200
  141. },
  142. {
  143. "id": "hSIhXBhNe8X1d8Et"
  144. // ...
  145. }
  146. ]
  147. }
  148. ```
  149. </CodeGroup>
  150. </Col>
  151. </Row>
  152. ---
  153. <Heading
  154. url='/messages/{message_id}/suggested'
  155. method='GET'
  156. title='消息下一步问题建议'
  157. name='#suggested'
  158. />
  159. <Row>
  160. <Col>
  161. 获取针对当前问题的下一步问题建议
  162. ### Path Params
  163. <Properties>
  164. <Property name='message_id' type='string' key='message_id'>
  165. 消息 ID
  166. </Property>
  167. </Properties>
  168. </Col>
  169. <Col sticky>
  170. <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'`}>
  171. ```bash {{ title: 'cURL' }}
  172. curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggeste' \
  173. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  174. --header 'Content-Type: application/json' \
  175. ```
  176. </CodeGroup>
  177. <CodeGroup title="Response">
  178. ```json {{ title: 'Response' }}
  179. {
  180. "result": "success",
  181. "data": [
  182. "a",
  183. "b",
  184. "c"
  185. ]
  186. ]
  187. }
  188. ```
  189. </CodeGroup>
  190. </Col>
  191. </Row>
  192. ---
  193. <Heading
  194. url='/messages'
  195. method='GET'
  196. title='获取会话历史消息'
  197. name='#messages'
  198. />
  199. <Row>
  200. <Col>
  201. 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,加载更多时,返回 `first_id` 之前的 `limit` 条。
  202. ### Query
  203. <Properties>
  204. <Property name='conversation_id' type='string' key='conversation_id'>
  205. 会话 ID
  206. </Property>
  207. <Property name='first_id' type='string' key='first_id'>
  208. 当前页第一条聊天记录的 ID,默认 none
  209. </Property>
  210. <Property name='limit' type='int' key='limit'>
  211. 一次请求返回多少条聊天记录
  212. </Property>
  213. <Property name='user' type='string' key='user'>
  214. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  215. </Property>
  216. </Properties>
  217. </Col>
  218. <Col sticky>
  219. <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'`}>
  220. ```bash {{ title: 'cURL' }}
  221. curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
  222. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  223. ```
  224. </CodeGroup>
  225. <CodeGroup title="Response">
  226. ```json {{ title: 'Response' }}
  227. {
  228. "has_more": false,
  229. "data": [
  230. {
  231. "id": "WAz8eIbvDR60rouK",
  232. "conversation_id": "xgQQXg3hrtjh7AvZ",
  233. "inputs": {},
  234. "query": "...",
  235. "answer": "...",
  236. "feedback": "like",
  237. "created_at": 692233200
  238. },
  239. {
  240. "id": "hSIhXBhNe8X1d8Et"
  241. // ...
  242. }
  243. ]
  244. }
  245. ```
  246. </CodeGroup>
  247. </Col>
  248. </Row>
  249. ---
  250. <Heading
  251. url='/conversations'
  252. method='GET'
  253. title='获取会话列表'
  254. name='#conversations'
  255. />
  256. <Row>
  257. <Col>
  258. 获取当前用户的会话列表,默认返回最近的 20 条。
  259. ### Query
  260. <Properties>
  261. <Property name='last_id' type='string' key='last_id'>
  262. 当前页最后面一条记录的 ID,默认 none
  263. </Property>
  264. <Property name='limit' type='int' key='limit'>
  265. 一次请求返回多少条记录
  266. </Property>
  267. <Property name='user' type='string' key='user'>
  268. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  269. </Property>
  270. </Properties>
  271. </Col>
  272. <Col sticky>
  273. <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'`}>
  274. ```bash {{ title: 'cURL' }}
  275. curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
  276. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  277. ```
  278. </CodeGroup>
  279. <CodeGroup title="Response">
  280. ```json {{ title: 'Response' }}
  281. {
  282. "limit": 20,
  283. "has_more": false,
  284. "data": [
  285. {
  286. "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
  287. "name": "New chat",
  288. "inputs": {
  289. "book": "book",
  290. "myName": "Lucy"
  291. },
  292. "status": "normal",
  293. "created_at": 1679667915
  294. },
  295. {
  296. "id": "hSIhXBhNe8X1d8Et"
  297. // ...
  298. }
  299. ]
  300. }
  301. ```
  302. </CodeGroup>
  303. </Col>
  304. </Row>
  305. ---
  306. <Heading
  307. url='/conversations/{converation_id}/name'
  308. method='POST'
  309. title='会话重命名'
  310. name='#rename'
  311. />
  312. <Row>
  313. <Col>
  314. 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。
  315. ### Request Body
  316. <Properties>
  317. <Property name='name' type='string' key='name'>
  318. 新的名称
  319. </Property>
  320. <Property name='user' type='string' key='user'>
  321. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  322. </Property>
  323. </Properties>
  324. </Col>
  325. <Col sticky>
  326. <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}'`}>
  327. ```bash {{ title: 'cURL' }}
  328. curl --location --request POST '${props.appDetail.api_base_url}/conversations/{converation_id}/name' \
  329. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  330. --header 'Content-Type: application/json' \
  331. --data-raw '{
  332. "name": "",
  333. "user": "abc-123"
  334. }'
  335. ```
  336. </CodeGroup>
  337. <CodeGroup title="Response">
  338. ```json {{ title: 'Response' }}
  339. {
  340. "result": "success"
  341. }
  342. ```
  343. </CodeGroup>
  344. </Col>
  345. </Row>
  346. ---
  347. <Heading
  348. url='/conversations/{converation_id}'
  349. method='DELETE'
  350. title='删除会话'
  351. name='#delete'
  352. />
  353. <Row>
  354. <Col>
  355. 删除会话。
  356. ### Request Body
  357. <Properties>
  358. <Property name='user' type='string' key='user'>
  359. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  360. </Property>
  361. </Properties>
  362. </Col>
  363. <Col sticky>
  364. <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}'`}>
  365. ```bash {{ title: 'cURL' }}
  366. curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \
  367. --header 'Content-Type: application/json' \
  368. --header 'Accept: application/json' \
  369. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  370. --data '{
  371. "user": "abc-123"
  372. }'
  373. ```
  374. </CodeGroup>
  375. <CodeGroup title="Response">
  376. ```json {{ title: 'Response' }}
  377. {
  378. "result": "success"
  379. }
  380. ```
  381. </CodeGroup>
  382. </Col>
  383. </Row>
  384. ---
  385. <Heading
  386. url='/audio-to-text'
  387. method='POST'
  388. title='语音转文字'
  389. name='#audio'
  390. />
  391. <Row>
  392. <Col>
  393. 语音转文字,仅支持 openai 模型。
  394. ### Request Body
  395. <Properties>
  396. <Property name='file' type='file' key='file'>
  397. 语音文件。
  398. 文件上传当前限制为 15 MB,并且支持以下输入文件类型:mp3、mp4、mpeg、mpga、m4a、wav 和 webm。
  399. </Property>
  400. </Properties>
  401. </Col>
  402. <Col sticky>
  403. <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]`}>
  404. ```bash {{ title: 'cURL' }}
  405. curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \
  406. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
  407. --form 'file=@localfile;type=audio/mp3'
  408. ```
  409. </CodeGroup>
  410. <CodeGroup title="Response">
  411. ```json {{ title: 'Response' }}
  412. {
  413. "text": ""
  414. }
  415. ```
  416. </CodeGroup>
  417. </Col>
  418. </Row>
  419. ---
  420. <Heading
  421. url='/parameters'
  422. method='GET'
  423. title='获取应用配置信息'
  424. name='#rename'
  425. />
  426. <Row>
  427. <Col>
  428. 获取已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。
  429. ### Query
  430. <Properties>
  431. <Property name='user' type='string' key='user'>
  432. 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。
  433. </Property>
  434. </Properties>
  435. </Col>
  436. <Col sticky>
  437. <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'`}>
  438. ```bash {{ title: 'cURL' }}
  439. curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
  440. --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
  441. ```
  442. </CodeGroup>
  443. <CodeGroup title="Response">
  444. ```json {{ title: 'Response' }}
  445. {
  446. "introduction": "nice to meet you",
  447. "variables": [
  448. {
  449. "key": "book",
  450. "name": "book",
  451. "description": null,
  452. "type": "string",
  453. "default": null,
  454. "options": null
  455. },
  456. {
  457. // ...
  458. }
  459. ]
  460. }
  461. ```
  462. </CodeGroup>
  463. </Col>
  464. </Row>