mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-25 17:42:24 +08:00
Refa: restore openai-compatible chat completions api (#14380)
### What problem does this PR solve? restore openai-compatible chat completions api ### Type of change - [x] Refactoring
This commit is contained in:
@@ -33,7 +33,7 @@ A complete reference for RAGFlow's RESTful API. Before proceeding, please ensure
|
||||
|
||||
### Create chat completion
|
||||
|
||||
**POST** `/api/v1/chats_openai/{chat_id}/chat/completions`
|
||||
**POST** `/api/v1/openai/{chat_id}/chat/completions`
|
||||
|
||||
Creates a model response for a given chat conversation.
|
||||
|
||||
@@ -42,7 +42,7 @@ This API follows the same request and response format as OpenAI's API. It allows
|
||||
#### Request
|
||||
|
||||
- Method: POST
|
||||
- URL: `/api/v1/chats_openai/{chat_id}/chat/completions`
|
||||
- URL: `/api/v1/openai/{chat_id}/chat/completions`
|
||||
- Headers:
|
||||
- `'content-Type: application/json'`
|
||||
- `'Authorization: Bearer <YOUR_API_KEY>'`
|
||||
@@ -56,11 +56,11 @@ This API follows the same request and response format as OpenAI's API. It allows
|
||||
|
||||
```bash
|
||||
curl --request POST \
|
||||
--url http://{address}/api/v1/chats_openai/{chat_id}/chat/completions \
|
||||
--url http://{address}/api/v1/openai/{chat_id}/chat/completions \
|
||||
--header 'Content-Type: application/json' \
|
||||
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
||||
--data '{
|
||||
"model": "model",
|
||||
"model": "glm-4-flash@ZHIPU-AI",
|
||||
"messages": [{"role": "user", "content": "Say this is a test!"}],
|
||||
"stream": true,
|
||||
"extra_body": {
|
||||
@@ -85,8 +85,11 @@ curl --request POST \
|
||||
|
||||
##### Request Parameters
|
||||
|
||||
- `chat_id` (*Path parameter*) `string`, *Required*
|
||||
Existing chat assistant ID. The request will use that chat assistant's knowledge and settings.
|
||||
|
||||
- `model` (*Body parameter*) `string`, *Required*
|
||||
The model used to generate the response. The server will parse this automatically, so you can set it to any value for now.
|
||||
The model used to generate the response. When `chat_id` is provided, you may also use the legacy placeholder value `"model"` to keep using the chat assistant's configured model.
|
||||
|
||||
- `messages` (*Body parameter*) `list[object]`, *Required*
|
||||
A list of historical chat messages used to generate the response. This must contain at least one message with the `user` role.
|
||||
|
||||
@@ -46,9 +46,13 @@ Creates a model response for the given historical chat conversation via OpenAI's
|
||||
|
||||
#### Parameters
|
||||
|
||||
##### chat_id: `string`, *Required*
|
||||
|
||||
Existing chat assistant ID. This value is part of the request path: `/api/v1/openai/<chat_id>/chat/completions`.
|
||||
|
||||
##### model: `string`, *Required*
|
||||
|
||||
The model used to generate the response. The server will parse this automatically, so you can set it to any value for now.
|
||||
The model used to generate the response. You may also use the legacy placeholder value `"model"` to keep using the chat assistant's configured model.
|
||||
|
||||
##### messages: `list[object]`, *Required*
|
||||
|
||||
@@ -65,20 +69,12 @@ Whether to receive the response as a stream. Set this to `false` explicitly if y
|
||||
|
||||
#### Examples
|
||||
|
||||
> **Note**
|
||||
> Streaming via `client.chat.completions.create(stream=True, ...)` does not
|
||||
> return `reference` currently because `reference` is only exposed in the
|
||||
> non-stream response payload. The only way to return `reference` is non-stream
|
||||
> mode with `with_raw_response`.
|
||||
:::caution NOTE
|
||||
Streaming via `client.chat.completions.create(stream=True, ...)` does not return `reference` because it is *only* included in the raw response payload in non-stream mode. To return `reference`, set `stream=False`.
|
||||
:::
|
||||
```python
|
||||
from openai import OpenAI
|
||||
import json
|
||||
|
||||
model = "model"
|
||||
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")
|
||||
model = "glm-4-flash@ZHIPU-AI"
|
||||
client = OpenAI(api_key="ragflow-api-key", base_url="http://ragflow_address/api/v1/openai/<chat_id>/chat")
|
||||
|
||||
stream = True
|
||||
reference = True
|
||||
@@ -92,13 +88,11 @@ request_kwargs = dict(
|
||||
{"role": "user", "content": "Can you tell me how to install neovim"},
|
||||
],
|
||||
extra_body={
|
||||
"extra_body": {
|
||||
"reference": reference,
|
||||
"reference_metadata": {
|
||||
"include": True,
|
||||
"fields": ["author", "year", "source"],
|
||||
},
|
||||
}
|
||||
"reference": reference,
|
||||
"reference_metadata": {
|
||||
"include": True,
|
||||
"fields": ["author", "year", "source"],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -119,6 +113,8 @@ else:
|
||||
print("reference:", data["choices"][0]["message"].get("reference"))
|
||||
```
|
||||
|
||||
When `extra_body.reference` is `true`, the streamed final chunk may include `choices[0].delta.reference`, and the non-stream response may include `choices[0].message.reference`.
|
||||
|
||||
When `extra_body.reference_metadata.include` is `true`, each reference chunk may include a `document_metadata` object in both streaming and non-streaming responses.
|
||||
|
||||
## DATASET MANAGEMENT
|
||||
|
||||
Reference in New Issue
Block a user