HTTP API
RAGFlow RESTful API 的完整参考。在继续之前,请确保您已准备好 RAGFlow API 密钥进行身份验证。
错误代码
| 代码 | 消息 | 描述 |
|---|---|---|
| 400 | Bad Request | 无效的请求参数 |
| 401 | Unauthorized | 未授权访问 |
| 403 | Forbidden | 访问被拒绝 |
| 404 | Not Found | 资源未找到 |
| 500 | Internal Server Error | 服务器内部错误 |
| 1001 | Invalid Chunk ID | 无效的块 ID |
| 1002 | Chunk Update Failed | 块更新失败 |
OpenAI 兼容 API
创建聊天完成
POST /api/v1/chats_openai/{chat_id}/chat/completions
为给定的聊天对话创建模型响应。
此 API 遵循与 OpenAI API 相同的请求和响应格式。它允许您以类似 于使用 OpenAI API 的方式与模型交互。
请求
- 方法:POST
- URL:
/api/v1/chats_openai/{chat_id}/chat/completions - 请求头:
'content-Type: application/json''Authorization: Bearer <YOUR_API_KEY>'
- 请求体:
"model":string"messages":object list"stream":boolean
请求示例
curl --request POST \
--url http://{address}/api/v1/chats_openai/{chat_id}/chat/completions \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--data '{
"model": "model",
"messages": [{"role": "user", "content": "Say this is a test!"}],
"stream": true
}'
请求参数
-
model(Body 参数)string,必填
用于生成响应的模型。服务器将自动解析此参数,因此您现在可以将其设置为任何值。 -
messages(Body 参数)list[object],必填
用于生成响应的历史聊天消息列表。这必须包含至少一条具有user角色的消息。 -
stream(Body 参数)boolean
是 否以流的形式接收响应。如果您希望一次性接收完整响应而不是流,请明确设置为false。
响应
流式:
{
"id": "chatcmpl-3a9c3572f29311efa69751e139332ced",
"choices": [
{
"delta": {
"content": "This is a test. If you have any specific questions or need information, feel",
"role": "assistant",
"function_call": null,
"tool_calls": null
},
"finish_reason": null,
"index": 0,
"logprobs": null
}
],
"created": 1740543996,
"model": "model",
"object": "chat.completion.chunk",
"system_fingerprint": "",
"usage": null
}
// omit duplicated information
{"choices":[{"delta":{"content":" free to ask, and I will do my best to provide an answer based on","role":"assistant"}}]}
{"choices":[{"delta":{"content":" the knowledge I have. If your question is unrelated to the provided knowledge base,","role":"assistant"}}]}
{"choices":[{"delta":{"content":" I will let you know.","role":"assistant"}}]}
// the last chunk
{
"id": "chatcmpl-3a9c3572f29311efa69751e139332ced",
"choices": [
{
"delta": {
"content": null,
"role": "assistant",
"function_call": null,
"tool_calls": null
},
"finish_reason": "stop",
"index": 0,
"logprobs": null
}
],
"created": 1740543996,
"model": "model",
"object": "chat.completion.chunk",
"system_fingerprint": "",
"usage": {
"prompt_tokens": 18,
"completion_tokens": 225,
"total_tokens": 243
}
}
非流式:
{
"choices":[
{
"finish_reason":"stop",
"index":0,
"logprobs":null,
"message":{
"content":"This is a test. If you have any specific questions or need information, feel free to ask, and I will do my best to provide an answer based on the knowledge I have. If your question is unrelated to the provided knowledge base, I will let you know.",
"role":"assistant"
}
}
],
"created":1740543499,
"id":"chatcmpl-3a9c3572f29311efa69751e139332ced",
"model":"model",
"object":"chat.completion",
"usage":{
"completion_tokens":246,
"completion_tokens_details":{
"accepted_prediction_tokens":246,
"reasoning_tokens":18,
"rejected_prediction_tokens":0
},
"prompt_tokens":18,
"total_tokens":264
}
}
失败:
{
"code": 102,
"message": "The last content of this conversation is not from user."
}