ragflow/api/apps
Yongteng Lei 5c6a7cb4b8
Added OpenAI-like completion api (#5351)
### What problem does this PR solve?

Added OpenAI-like completion api, related to #4672, #4705 

This function allows users to interact with a model to get responses
based on a series of messages.
If `stream` is set to True, the response will be streamed in chunks,
mimicking the OpenAI-style API.

#### Example usage:

```bash
curl -X POST https://ragflow_address.com/api/v1/chats_openai/<chat_id>/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $RAGFLOW_API_KEY" \
    -d '{
        "model": "model",
        "messages": [{"role": "user", "content": "Say this is a test!"}],
        "stream": true
    }'
```

Alternatively, you can use Python's `OpenAI` client:

```python
from openai import OpenAI

model = "model"
client = OpenAI(api_key="ragflow-api-key", base_url=f"http://ragflow_address/api/v1/chats_openai/<chat_id>")

completion = client.chat.completions.create(
    model=model,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who you are?"},
        {"role": "assistant", "content": "I am an AI assistant named..."},
        {"role": "user", "content": "Can you tell me how to install neovim"},
    ],
    stream=True
)

stream = True
if stream:
    for chunk in completion:
        print(chunk)
else:
    print(completion.choices[0].message.content)
```
### Type of change
- [x] New Feature (non-breaking change which adds functionality)

### Related Issues
Related to #4672, #4705
2025-02-26 11:37:29 +08:00
..
sdk Added OpenAI-like completion api (#5351) 2025-02-26 11:37:29 +08:00
__init__.py Fix: Starting the source code on Windows, the 'HTTP API' returns 404 (#5042) 2025-02-17 19:33:49 +08:00
api_app.py Tagging (#4426) 2025-01-09 17:07:21 +08:00
canvas_app.py Update error message for agent name conflict (#4299) 2024-12-31 14:36:23 +08:00
chunk_app.py Fix: 'Chunk not found!' error in team-sharing knowledge base. (#5361) 2025-02-26 10:24:35 +08:00
conversation_app.py Light GraphRAG (#4585) 2025-01-22 19:43:14 +08:00
dialog_app.py Support chat solo. (#5218) 2025-02-21 12:24:02 +08:00
document_app.py Check meta data format in json map (#4461) 2025-01-13 17:34:50 +08:00
file2document_app.py Added static check at PR CI (#3921) 2024-12-08 21:23:51 +08:00
file_app.py Added static check at PR CI (#3921) 2024-12-08 21:23:51 +08:00
kb_app.py Ignore exceptions when no index ahead. (#5047) 2025-02-18 09:09:22 +08:00
llm_app.py Refine error message for re-rank model. (#5278) 2025-02-24 13:01:34 +08:00
system_app.py Compatible with former API keys. (#4055) 2024-12-17 13:58:26 +08:00
tenant_app.py Update team invite message (#4085) 2024-12-18 14:20:09 +08:00
user_app.py Added static check at PR CI (#3921) 2024-12-08 21:23:51 +08:00