|
| 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 | + |
0 commit comments