HTTP API
完整的RAGFlow RESTful API参考文档。在继续之前,请确保您已准备好RAGFlow的API密钥用于身份验证。
错误代码
状态码 | 消息 | 描述 |
---|---|---|
400 | 请求错误 | 请求参数无效 |
401 | 未授权 | 访问未授权 |
403 | 禁止访问 | 访问被拒绝 |
404 | 资源未找到 | 资源不存在 |
500 | 服务器内部错误 | 服务器内部错误 |
1001 | 无效Chunk ID | Chunk ID无效 |
1002 | Chunk更新失败 | Chunk更新失败 |
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 parameter)string
,必填
用于生成回复的模型。服务器会自动解析此字段,因此您可以暂时将其设置为任何值。 -
messages
(Body parameter)list[object]
,必填
用于生成回复的历史聊天消息列表。此参数必须包含至少一条具有user
角色的消息。 -
stream
(Body parameter)boolean
是否以流式接收响应。如果您希望一次性接收完整响应而不是分块传输,请显式设置为false
。
创建代理完成
响应
流式:
data:{
"id": "chatcmpl-3b0397f277f511f0b47f729e3aa55728",
"choices": [
{
"delta": {
"content": "Hello! It seems like you're just greeting me. If you have a specific",
"role": "assistant",
"function_call": null,
"tool_calls": null,
"reasoning_content": null
},
"finish_reason": null,
"index": 0,
"logprobs": null
}
],
"created": 1755084508,
"model": "model",
"object": "chat.completion.chunk",
"system_fingerprint": "",
"usage": null
}
data:{"id": "chatcmpl-3b0397f277f511f0b47f729e3aa55728", "choices": [{"delta": {"content": " question or need information, feel free to ask, and I'll do my best", "role": "assistant", "function_call": null, "tool_calls": null, "reasoning_content": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1755084508, "model": "model", "object": "chat.completion.chunk", "system_fingerprint": "", "usage": null}
data:{"id": "chatcmpl-3b0397f277f511f0b47f729e3aa55728", "choices": [{"delta": {"content": " to assist you based on the knowledge base provided.", "role": "assistant", "function_call": null, "tool_calls": null, "reasoning_content": null}, "finish_reason": null, "index": 0, "logprobs": null}], "created": 1755084508, "model": "model", "object": "chat.completion.chunk", "system_fingerprint": "", "usage": null}
data:{"id": "chatcmpl-3b0397f277f511f0b47f729e3aa55728", "choices": [{"delta": {"content": null, "role": "assistant", "function_call": null, "tool_calls": null, "reasoning_content": null}, "finish_reason": "stop", "index": 0, "logprobs": null}], "created": 1755084508, "model": "model", "object": "chat.completion.chunk", "system_fingerprint": "", "usage": {"prompt_tokens": 5, "completion_tokens": 188, "total_tokens": 193}}
data:[DONE]
非流式:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "Hello! I'm your smart assistant. What can I do for you?",
"role": "assistant"
}
}
],
"created": 1755084403,
"id": "chatcmpl-3b0397f277f511f0b47f729e3aa55728",
"model": "model",
"object": "chat.completion",
"usage": {
"completion_tokens": 55,
"completion_tokens_details": {
"accepted_prediction_tokens": 55,
"reasoning_tokens": 5,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 5,
"total_tokens": 60
}
}
失败:
{
"code": 102,
"message": "The last content of this conversation is not from user."
}
创建代理完成
POST /api/v1/agents_openai/{agent_id}/chat/completions
为给定的聊天对话生成模型响应。
此API遵循与OpenAI API相同请求和响应格式。您可以像使用OpenAI的API一样与模型交互。
请求
- 方法:POST
- URL:
/api/v1/agents_openai/{agent_id}/chat/completions
- 头部:
'content-Type: application/json'
'Authorization: Bearer <YOUR_API_KEY>'
- 正文:
"model"
:string
,必需"messages"
:object 列表
,必需"stream"
:boolean
请求示例
curl --request POST \
--url http://{address}/api/v1/agents_openai/{agent_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 parameter)string
,必需
用于生成响应的模型。服务器会自动解析此值,因此您可以将其设置为 任何值。 -
"messages"
(Body parameter)object 列表
,必需
包含消息内容和角色的对象列表。 -
"stream"
(Body parameter)boolean
,可选
是否启用流式响应。默认为false
。
响应
流式输出:
data: {
"id": "5fa65c94-e316-4954-800a-06dfd5827052",
"object": "chat.completion.chunk",
"model": "99ee29d6783511f09c921a6272e682d8",
"choices": [
{
"delta": {
"content": "Hello"
},
"finish_reason": null,
"index": 0
}
]
}
data: {"id": "518022d9-545b-4100-89ed-ecd9e46fa753", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": "!"}, "finish_reason": null, "index": 0}]}
data: {"id": "f37c4af0-8187-4c86-8186-048c3c6ffe4e", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " How"}, "finish_reason": null, "index": 0}]}
data: {"id": "3ebc0fcb-0f85-4024-b4a5-3b03234a16df", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " can"}, "finish_reason": null, "index": 0}]}
data: {"id": "efa1f3cf-7bc4-47a4-8e53-cd696f290587", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " I"}, "finish_reason": null, "index": 0}]}
data: {"id": "2eb6f741-50a3-4d3d-8418-88be27895611", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " assist"}, "finish_reason": null, "index": 0}]}
data: {"id": "f1227e4f-bf8b-462c-8632-8f5269492ce9", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " you"}, "finish_reason": null, "index": 0}]}
data: {"id": "35b669d0-b2be-4c0c-88d8-17ff98592b21", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": " today"}, "finish_reason": null, "index": 0}]}
data: {"id": "f00d8a39-af60-4f32-924f-d64106a7fdf1", "object": "chat.completion.chunk", "model": "99ee29d6783511f09c921a6272e682d8", "choices": [{"delta": {"content": "?"}, "finish_reason": null, "index": 0}]}
data: [DONE]
非流式输出:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "Hello! How can I assist you today?",
"role": "assistant"
}
}
],
"created": null,
"id": "17aa4ec5-6d36-40c6-9a96-1b069c216d59",
"model": "99ee29d6783511f09c921a6272e682d8",
"object": "chat.completion",
"param": null,
"usage": {
"completion_tokens": 9,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 1,
"total_tokens": 10
}
}
失败情况:
{
"code": 102,
"message": "The last content of this conversation is empty."
}