Skip to content

Commit 52090c3

Browse files
authored
Merge pull request #17350 from BerriAI/litellm_rag_chat_completion_api
Add ragflow support for chat completions API
2 parents 8edcc4e + 831ad45 commit 52090c3

File tree

13 files changed

+946
-0
lines changed

13 files changed

+946
-0
lines changed
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import Tabs from '@theme/Tabs';
2+
import TabItem from '@theme/TabItem';
3+
4+
# RAGFlow
5+
6+
Litellm supports Ragflow's chat completions APIs
7+
8+
## Supported Features
9+
10+
- ✅ Chat completions
11+
- ✅ Streaming responses
12+
- ✅ Both chat and agent endpoints
13+
- ✅ Multiple credential sources (params, env vars, litellm_params)
14+
- ✅ OpenAI-compatible API format
15+
16+
17+
## API Key
18+
19+
```python
20+
# env variable
21+
os.environ['RAGFLOW_API_KEY']
22+
```
23+
24+
## API Base
25+
26+
```python
27+
# env variable
28+
os.environ['RAGFLOW_API_BASE']
29+
```
30+
31+
## Overview
32+
33+
RAGFlow provides OpenAI-compatible APIs with unique path structures that include chat and agent IDs:
34+
35+
- **Chat endpoint**: `/api/v1/chats_openai/{chat_id}/chat/completions`
36+
- **Agent endpoint**: `/api/v1/agents_openai/{agent_id}/chat/completions`
37+
38+
The model name format embeds the endpoint type and ID:
39+
- Chat: `ragflow/chat/{chat_id}/{model_name}`
40+
- Agent: `ragflow/agent/{agent_id}/{model_name}`
41+
42+
43+
## Sample Usage - Chat Endpoint
44+
45+
```python
46+
from litellm import completion
47+
import os
48+
49+
os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
50+
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380" # or your hosted URL
51+
52+
response = completion(
53+
model="ragflow/chat/my-chat-id/gpt-4o-mini",
54+
messages=[{"role": "user", "content": "How does the deep doc understanding work?"}]
55+
)
56+
print(response)
57+
```
58+
59+
## Sample Usage - Agent Endpoint
60+
61+
```python
62+
from litellm import completion
63+
import os
64+
65+
os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
66+
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380" # or your hosted URL
67+
68+
response = completion(
69+
model="ragflow/agent/my-agent-id/gpt-4o-mini",
70+
messages=[{"role": "user", "content": "What are the key features?"}]
71+
)
72+
print(response)
73+
```
74+
75+
## Sample Usage - With Parameters
76+
77+
You can also pass `api_key` and `api_base` directly as parameters:
78+
79+
```python
80+
from litellm import completion
81+
82+
response = completion(
83+
model="ragflow/chat/my-chat-id/gpt-4o-mini",
84+
messages=[{"role": "user", "content": "Hello!"}],
85+
api_key="your-ragflow-api-key",
86+
api_base="http://localhost:9380"
87+
)
88+
print(response)
89+
```
90+
91+
## Sample Usage - Streaming
92+
93+
```python
94+
from litellm import completion
95+
import os
96+
97+
os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
98+
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380"
99+
100+
response = completion(
101+
model="ragflow/agent/my-agent-id/gpt-4o-mini",
102+
messages=[{"role": "user", "content": "Explain RAGFlow"}],
103+
stream=True
104+
)
105+
106+
for chunk in response:
107+
print(chunk)
108+
```
109+
110+
## Model Name Format
111+
112+
The model name must follow one of these formats:
113+
114+
### Chat Endpoint
115+
```
116+
ragflow/chat/{chat_id}/{model_name}
117+
```
118+
119+
Example: `ragflow/chat/my-chat-id/gpt-4o-mini`
120+
121+
### Agent Endpoint
122+
```
123+
ragflow/agent/{agent_id}/{model_name}
124+
```
125+
126+
Example: `ragflow/agent/my-agent-id/gpt-4o-mini`
127+
128+
Where:
129+
- `{chat_id}` or `{agent_id}` is the ID of your chat or agent in RAGFlow
130+
- `{model_name}` is the actual model name (e.g., `gpt-4o-mini`, `gpt-4o`, etc.)
131+
132+
## Configuration Sources
133+
134+
LiteLLM supports multiple ways to provide credentials, checked in this order:
135+
136+
1. **Function parameters**: `api_key="..."`, `api_base="..."`
137+
2. **litellm_params**: `litellm_params={"api_key": "...", "api_base": "..."}`
138+
3. **Environment variables**: `RAGFLOW_API_KEY`, `RAGFLOW_API_BASE`
139+
4. **Global litellm settings**: `litellm.api_key`, `litellm.api_base`
140+
141+
## Usage - LiteLLM Proxy Server
142+
143+
### 1. Save key in your environment
144+
145+
```bash
146+
export RAGFLOW_API_KEY="your-ragflow-api-key"
147+
export RAGFLOW_API_BASE="http://localhost:9380"
148+
```
149+
150+
### 2. Start the proxy
151+
152+
<Tabs>
153+
<TabItem value="config" label="config.yaml">
154+
155+
```yaml
156+
model_list:
157+
- model_name: ragflow-chat-gpt4
158+
litellm_params:
159+
model: ragflow/chat/my-chat-id/gpt-4o-mini
160+
api_key: os.environ/RAGFLOW_API_KEY
161+
api_base: os.environ/RAGFLOW_API_BASE
162+
- model_name: ragflow-agent-gpt4
163+
litellm_params:
164+
model: ragflow/agent/my-agent-id/gpt-4o-mini
165+
api_key: os.environ/RAGFLOW_API_KEY
166+
api_base: os.environ/RAGFLOW_API_BASE
167+
```
168+
169+
</TabItem>
170+
<TabItem value="cli" label="CLI">
171+
172+
```bash
173+
$ litellm --config /path/to/config.yaml
174+
175+
# Server running on http://0.0.0.0:4000
176+
```
177+
178+
</TabItem>
179+
</Tabs>
180+
181+
### 3. Test it
182+
183+
<Tabs>
184+
<TabItem value="Curl" label="Curl Request">
185+
186+
```bash
187+
curl http://0.0.0.0:4000/v1/chat/completions \
188+
-H "Content-Type: application/json" \
189+
-H "Authorization: Bearer sk-1234" \
190+
-d '{
191+
"model": "ragflow-chat-gpt4",
192+
"messages": [
193+
{"role": "user", "content": "How does RAGFlow work?"}
194+
]
195+
}'
196+
```
197+
198+
</TabItem>
199+
<TabItem value="Python" label="Python SDK">
200+
201+
```python
202+
from openai import OpenAI
203+
204+
client = OpenAI(
205+
api_key="sk-1234", # Your LiteLLM proxy key
206+
base_url="http://0.0.0.0:4000"
207+
)
208+
209+
response = client.chat.completions.create(
210+
model="ragflow-chat-gpt4",
211+
messages=[
212+
{"role": "user", "content": "How does RAGFlow work?"}
213+
]
214+
)
215+
print(response)
216+
```
217+
218+
</TabItem>
219+
</Tabs>
220+
221+
## API Base URL Handling
222+
223+
The `api_base` parameter can be provided with or without `/v1` suffix. LiteLLM will automatically handle it:
224+
225+
- `http://localhost:9380``http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions`
226+
- `http://localhost:9380/v1``http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions`
227+
- `http://localhost:9380/api/v1``http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions`
228+
229+
All three formats will work correctly.
230+
231+
## Error Handling
232+
233+
If you encounter errors:
234+
235+
1. **Invalid model format**: Ensure your model name follows `ragflow/{chat|agent}/{id}/{model_name}` format
236+
2. **Missing api_base**: Provide `api_base` via parameter, environment variable, or litellm_params
237+
3. **Connection errors**: Verify your RAGFlow server is running and accessible at the provided `api_base`
238+
239+
:::info
240+
241+
For more information about passing provider-specific parameters, [go here](../completion/provider_specific_params.md)
242+
243+
:::
244+

docs/my-website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ const sidebars = {
627627
"providers/petals",
628628
"providers/publicai",
629629
"providers/predibase",
630+
"providers/ragflow",
630631
"providers/recraft",
631632
"providers/replicate",
632633
{

litellm/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ def add_known_models():
13871387
from .llms.v0.chat.transformation import V0ChatConfig
13881388
from .llms.oci.chat.transformation import OCIChatConfig
13891389
from .llms.morph.chat.transformation import MorphChatConfig
1390+
from .llms.ragflow.chat.transformation import RAGFlowConfig
13901391
from .llms.lambda_ai.chat.transformation import LambdaAIChatConfig
13911392
from .llms.hyperbolic.chat.transformation import HyperbolicChatConfig
13921393
from .llms.vercel_ai_gateway.chat.transformation import VercelAIGatewayConfig

litellm/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@
601601
"cometapi",
602602
"clarifai",
603603
"docker_model_runner",
604+
"ragflow",
604605
]
605606
openai_text_completion_compatible_providers: List = (
606607
[ # providers that support `/v1/completions`

litellm/litellm_core_utils/get_llm_provider_logic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,16 @@ def _get_openai_compatible_provider_info( # noqa: PLR0915
840840
) = litellm.ClarifaiConfig()._get_openai_compatible_provider_info(
841841
api_base, api_key
842842
)
843+
elif custom_llm_provider == "ragflow":
844+
full_model = f"ragflow/{model}"
845+
(
846+
api_base,
847+
dynamic_api_key,
848+
_,
849+
) = litellm.RAGFlowConfig()._get_openai_compatible_provider_info(
850+
full_model, api_base, api_key, "ragflow"
851+
)
852+
model = full_model
843853

844854
if api_base is not None and not isinstance(api_base, str):
845855
raise Exception("api base needs to be a string. api_base={}".format(api_base))

litellm/llms/ragflow/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
RAGFlow provider for LiteLLM.
3+
4+
RAGFlow provides OpenAI-compatible APIs with unique path structures:
5+
- Chat endpoint: /api/v1/chats_openai/{chat_id}/chat/completions
6+
- Agent endpoint: /api/v1/agents_openai/{agent_id}/chat/completions
7+
"""
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
RAGFlow chat completion configuration.
3+
"""
4+

0 commit comments

Comments
 (0)