跳至主要內容

RAGFlow

Litellm 支援 Ragflow 的 chat completions API

支援的功能

  • ✅ 聊天 completions
  • ✅ 串流回應
  • ✅ 聊天與 agent 端點皆支援
  • ✅ 多種認證來源(params、env vars、litellm_params)
  • ✅ 相容 OpenAI 的 API 格式

API 金鑰

# env variable
os.environ['RAGFLOW_API_KEY']

API 基底網址

# env variable
os.environ['RAGFLOW_API_BASE']

概觀

RAGFlow 提供相容 OpenAI 的 API,且具有包含 chat 與 agent ID 的獨特路徑結構:

  • 聊天端點/api/v1/chats_openai/{chat_id}/chat/completions
  • Agent 端點/api/v1/agents_openai/{agent_id}/chat/completions

模型名稱格式會內嵌端點類型與 ID:

  • Chat:ragflow/chat/{chat_id}/{model_name}
  • Agent:ragflow/agent/{agent_id}/{model_name}

範例用法 - 聊天端點

from litellm import completion
import os

os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380" # or your hosted URL

response = completion(
model="ragflow/chat/my-chat-id/gpt-4o-mini",
messages=[{"role": "user", "content": "How does the deep doc understanding work?"}]
)
print(response)

範例用法 - Agent 端點

from litellm import completion
import os

os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380" # or your hosted URL

response = completion(
model="ragflow/agent/my-agent-id/gpt-4o-mini",
messages=[{"role": "user", "content": "What are the key features?"}]
)
print(response)

範例用法 - 使用參數

您也可以直接將 api_keyapi_base 作為參數傳入:

from litellm import completion

response = completion(
model="ragflow/chat/my-chat-id/gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
api_key="your-ragflow-api-key",
api_base="http://localhost:9380"
)
print(response)

範例用法 - 串流

from litellm import completion
import os

os.environ['RAGFLOW_API_KEY'] = "your-ragflow-api-key"
os.environ['RAGFLOW_API_BASE'] = "http://localhost:9380"

response = completion(
model="ragflow/agent/my-agent-id/gpt-4o-mini",
messages=[{"role": "user", "content": "Explain RAGFlow"}],
stream=True
)

for chunk in response:
print(chunk)

模型名稱格式

模型名稱必須符合以下其中一種格式:

聊天端點

ragflow/chat/{chat_id}/{model_name}

範例:ragflow/chat/my-chat-id/gpt-4o-mini

Agent 端點

ragflow/agent/{agent_id}/{model_name}

範例:ragflow/agent/my-agent-id/gpt-4o-mini

其中:

  • {chat_id}{agent_id} 是您在 RAGFlow 中的 chat 或 agent ID
  • {model_name} 是實際的模型名稱(例如 gpt-4o-minigpt-4o 等)

設定來源

LiteLLM 支援多種提供認證資訊的方式,並依照以下順序檢查:

  1. 函式參數api_key="..."api_base="..."
  2. litellm_paramslitellm_params={"api_key": "...", "api_base": "..."}
  3. 環境變數RAGFLOW_API_KEYRAGFLOW_API_BASE
  4. 全域 litellm 設定litellm.api_keylitellm.api_base

用法 - LiteLLM Proxy Server

1. 將金鑰儲存在您的環境中

export RAGFLOW_API_KEY="your-ragflow-api-key"
export RAGFLOW_API_BASE="http://localhost:9380"

2. 啟動 proxy

model_list:
- model_name: ragflow-chat-gpt4
litellm_params:
model: ragflow/chat/my-chat-id/gpt-4o-mini
api_key: os.environ/RAGFLOW_API_KEY
api_base: os.environ/RAGFLOW_API_BASE
- model_name: ragflow-agent-gpt4
litellm_params:
model: ragflow/agent/my-agent-id/gpt-4o-mini
api_key: os.environ/RAGFLOW_API_KEY
api_base: os.environ/RAGFLOW_API_BASE

3. 測試

curl http://0.0.0.0:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "ragflow-chat-gpt4",
"messages": [
{"role": "user", "content": "How does RAGFlow work?"}
]
}'

API 基底網址處理

api_base 參數可帶有或不帶有 /v1 後綴。LiteLLM 會自動處理:

  • http://localhost:9380http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions
  • http://localhost:9380/v1http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions
  • http://localhost:9380/api/v1http://localhost:9380/api/v1/chats_openai/{chat_id}/chat/completions

這三種格式都可正常運作。

錯誤處理

如果您遇到錯誤:

  1. 無效的模型格式:請確認您的模型名稱符合 ragflow/{chat|agent}/{id}/{model_name} 格式
  2. 缺少 api_base:透過參數、環境變數或 litellm_params 提供 api_base
  3. 連線錯誤:驗證您的 RAGFlow 伺服器正在執行,且可透過提供的 api_base 存取
資訊

如需更多關於傳入 provider-specific 參數的資訊,請前往此處