本文档介绍了 Dify 的 Completion 应用的 REST API 端点,这些应用提供文本生成能力,且无需会话持久化。Completion 应用专为单轮交互而设计,例如翻译、内容撰写、摘要以及其他文本转换任务。
对于具有会话历史的聊天应用,请参阅 Chat API 端点。对于基于工作流的应用,请参阅 Workflow API 端点。
Dify 中的 Completion 应用是无状态的文本生成服务,它们处理输入并返回生成的内容,而不维护对话上下文。与聊天应用不同,它们不支持对话会话或消息历史。
来源:web/app/components/develop/template/template.en.mdx1-670
POST /completion-messages
文本生成请求的主要端点。接收输入变量并返回生成的内容。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
inputs | 对象 | 是 | 变量值,包括必需的 query 字段 |
response_mode | 字符串 | 是 | streaming 或 blocking |
user | 字符串 | 是 | 最终用户标识符 |
files | array | 否 | 用于多模态输入的附件文件 |
请求流程
响应事件(流式传输)
message - 来自 LLM 的文本块message_end - 生成完成,包含元数据tts_message - 语音合成块(如果已启用)tts_message_end - 语音合成完成message_replace - 内容审核替换error - 错误情况ping - 心跳事件来源:web/app/components/develop/template/template.en.mdx32-140
POST /files/upload
处理用于多模态完成的文件上传。目前支持用于视觉模型的图像。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
file | file | 是 | 要上传的文件 |
user | 字符串 | 是 | 用户标识符 |
支持的格式:PNG、JPG、JPEG、WEBP、GIF
响应包含文件 ID,可在 Completion 请求中通过 upload_file_id 参数使用。
来源:web/app/components/develop/template/template.en.mdx183-253
POST /completion-messages/:task_id/stop
停止正在进行的流式文本生成。仅在流式模式下可用。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
task_id | 字符串 | 是 | 来自流式响应的任务 ID |
user | 字符串 | 是 | 用户标识符 |
来源:web/app/components/develop/template/template.en.mdx255-294
事件序列
message 事件,包含文本块tts_message 事件用于语音合成message_end 事件,包含使用情况元数据来源:web/app/components/develop/template/template.en.mdx160-198
POST /messages/:message_id/feedbacks
收集用户对生成内容的反馈,以提高质量。
| 参数 | 类型 | 描述 |
|---|---|---|
rating | 字符串 | like, dislike, 或 null |
user | 字符串 | 用户标识符 |
content | 字符串 | 反馈文本 |
GET /app/feedbacks
检索应用的反馈数据,支持分页。
| 参数 | 类型 | 描述 |
|---|---|---|
page | 字符串 | 页码(默认为 1) |
limit | 字符串 | 每页记录数(默认为 20) |
来源:web/app/components/develop/template/template.en.mdx296-419
POST /text-to-audio
将生成的文本转换为音频格式。
| 参数 | 类型 | 描述 |
|---|---|---|
message_id | 字符串 | 要转换的消息 ID |
text | 字符串 | 文本内容(如果没有 message_id) |
user | 字符串 | 用户标识符 |
GET /info - 返回应用名称、描述和标签 GET /parameters - 返回输入参数和配置 GET /site - 返回 WebApp 显示设置
GET /apps/annotations - 列出带分页的注释 POST /apps/annotations - 创建新注释 DELETE /apps/annotations/:id - 删除注释
来源:web/app/components/develop/template/template.en.mdx421-670
常见的错误响应
| 代码 | 错误 | 描述 |
|---|---|---|
| 400 | invalid_param | 无效的请求参数 |
| 400 | app_unavailable | 应用未配置 |
| 400 | provider_not_initialize | 无模型凭证 |
| 400 | provider_quota_exceeded | 已超出使用配额 |
| 400 | completion_request_error | 文本生成失败 |
| 500 | 内部服务器错误 | 系统错误 |
来源:web/app/components/develop/template/template.en.mdx131-139