import { CodeGroup } from '../code.tsx' import { Row, Col, Properties, Property, Heading, SubProperty } from '../md.tsx' # 对话型应用 API 可用于大部分场景的对话型应用,采用一问一答模式与用户持续对话。要开始一个对话请调用 chat-messages 接口,通过继续传入返回的 conversation_id 可持续保持该会话。**[开始前请阅读 !! 什么是 Bearer Token ?](https://swagger.io/docs/specification/authentication/bearer-authentication/)**
### 鉴权 Dify Service API 使用 `API-Key` 进行鉴权。 建议开发者把 `API-Key` 放在后端存储,而非分享或者放在客户端存储,以免 `API-Key` 泄露,导致财产损失。 所有 API 请求都应在 **`Authorization`** HTTP Header 中包含您的 `API-Key`,如下所示: ```javascript Authorization: Bearer {API_KEY} ```
--- 创建会话消息,或基于此前的对话继续发送消息。 ### Request Body (选填)以键值对方式提供用户输入字段,与提示词编排中的变量对应。Key 为变量名称,Value 是参数值。如果字段类型为 Select,传入的 Value 需为预设选项之一。
    {!!props.variables.length && props.variables.map( val => ( {val.name ? `${val.name}` : ''} ) )}
用户输入/提问内容 - blocking 阻塞型,等待执行完毕后返回结果。(请求若流程较长可能会被中断) - streaming 流式返回。基于 SSE(**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**)实现流式返回。 (必填)‼️ 会话标识符,首次对话为 conversation_id: "" ‼️,如果要继续对话请传入上下文返回的 conversation_id 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 上传的文件。 - `type` 文件类型:目前仅支持 `image`。 - `transfer_method` 传递方式: - `remote_url`: 图片地址。 - `local_file`: 上传文件。 - `upload_file_id` 上传文件 ID。`transfer_method` 为 `local_file` 时必填。 - `url` 图片地址。`transfer_method` 为 `remote_url` 时必填。
```bash {{ title: 'cURL' }} curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --header 'Content-Type: application/json' \ --data-raw '{ "inputs": {}, "query": "eh", "response_mode": "streaming", "conversation_id": "", "user": "abc-123" }' ``` ### blocking ```json {{ title: 'Response' }} { "answer": "Hi, is there anything I can help you?", "conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2", "created_at": 1679587005, "id": "059f87d9-15c0-473a-870c-fde95cdcc1e4" } ``` ### streaming ```streaming {{ title: 'Response' }} data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595} data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595} ```
--- 代表最终用户对返回消息进行评价,可以点赞与点踩,该数据将在“日志与标注”页中可见,并用于后续的模型微调。 ### Path Params 消息 ID ### Request Body like 或 dislike, 空值为撤销 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --header 'Content-Type: application/json' \ --data-raw '{ "rating": "like", "user": "abc-123" }' ``` ```json {{ title: 'Response' }} { "has_more": false, "data": [ { "id": "WAz8eIbvDR60rouK", "username": "FrankMcCallister", "phone_number": "1-800-759-3000", "avatar_url": "https://assets.protocol.chat/avatars/frank.jpg", "display_name": null, "conversation_id": "xgQQXg3hrtjh7AvZ", "created_at": 692233200 }, { "id": "hSIhXBhNe8X1d8Et" // ... } ] } ``` --- 获取针对当前问题的下一步问题建议 ### Path Params 消息 ID ```bash {{ title: 'cURL' }} curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --header 'Content-Type: application/json' \ ``` ```json {{ title: 'Response' }} { "result": "success", "data": [ "a", "b", "c" ] ] } ``` --- 滚动加载形式返回历史聊天记录,第一页返回最新 `limit` 条,加载更多时,返回 `first_id` 之前的 `limit` 条。 ### Query 会话 ID 当前页第一条聊天记录的 ID,默认 none 一次请求返回多少条聊天记录 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id=' --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' ``` ```json {{ title: 'Response' }} { "has_more": false, "data": [ { "id": "WAz8eIbvDR60rouK", "conversation_id": "xgQQXg3hrtjh7AvZ", "inputs": {}, "query": "...", "answer": "...", "feedback": "like", "created_at": 692233200 }, { "id": "hSIhXBhNe8X1d8Et" // ... } ] } ``` --- 获取当前用户的会话列表,默认返回最近的 20 条。 ### Query 当前页最后面一条记录的 ID,默认 none 一次请求返回多少条记录 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' ``` ```json {{ title: 'Response' }} { "limit": 20, "has_more": false, "data": [ { "id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54", "name": "New chat", "inputs": { "book": "book", "myName": "Lucy" }, "status": "normal", "created_at": 1679667915 }, { "id": "hSIhXBhNe8X1d8Et" // ... } ] } ``` --- 对会话进行重命名,会话名称用于显示在支持多会话的客户端上。 ### Request Body 新的名称 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request POST '${props.appDetail.api_base_url}/conversations/{converation_id}/name' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --header 'Content-Type: application/json' \ --data-raw '{ "name": "", "user": "abc-123" }' ``` ```json {{ title: 'Response' }} { "result": "success" } ``` --- 删除会话。 ### Request Body 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \ --header 'Content-Type: application/json' \ --header 'Accept: application/json' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --data '{ "user": "abc-123" }' ``` ```json {{ title: 'Response' }} { "result": "success" } ``` --- 上传文件到服务器,供文本生成、对话使用。上传的文件仅供当前终端用户使用。 ### Request Body 请求方式为 `multipart/form-data`。 要上传的文件。目前支持 png, jpg, jpeg, webp, gif 格式文件。 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --form 'file=@"/path/to/file"' ``` ```json {{ title: 'Response' }} { "id": "72fa9618-8f89-4a37-9b33-7e1178a24a67", "name": "example.png", "size": 1024, "extension": "png", "mime_type": "image/png", "created_by": 123, "created_at": 1577836800, } ``` --- 语音转文字,仅支持 openai 模型。 ### Request Body 语音文件。 文件上传当前限制为 15 MB,并且支持以下输入文件类型:mp3、mp4、mpeg、mpga、m4a、wav 和 webm。 ```bash {{ title: 'cURL' }} curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \ --form 'file=@localfile;type=audio/mp3' ``` ```json {{ title: 'Response' }} { "text": "" } ``` --- 内容包括: 1. 已配置的 Input 参数,包括变量名、字段名称、类型与默认值。通常用于客户端加载后显示这些字段的表单或填入默认值。 2. 上传图片的配置,包括是否开启上传图片,上传图片的数量和上传方式。注意:这个配置只有使用支持多模态的模型时才会生效。 ### Query 用户标识,由开发者定义规则,需保证用户标识在应用内唯一。 ```bash {{ title: 'cURL' }} curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \ --header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' ``` ```json {{ title: 'Response' }} { "introduction": "nice to meet you", "user_input_form": [ { "text-input": { "label": "a", "variable": "a", "required": true, "max_length": 48, "default": "" } } { // ... } ], "file_upload": { "image": { "enabled": true, "number_limits": 3, "transfer_methods": [ "remote_url", "local_file" ] } } } ```