跳至主要內容

控制公開與私人路由

資訊

需要 LiteLLM Enterprise 授權。 取得免費試用

控制哪些路由需要驗證,以及哪些路由可公開存取。

路由類型

路由類型需要驗證說明
public_routes無需任何驗證即可存取的路由
admin_only_routes是(僅限管理員)僅能由 Proxy 管理員 存取的路由
allowed_routes在 proxy 上公開的路由。若未設定,則所有路由都會公開

快速開始

將路由設為公開

允許特定路由在未經驗證的情況下存取:

general_settings:
master_key: sk-1234
public_routes: ["LiteLLMRoutes.public_routes", "/spend/calculate"]

將路由限制為僅限管理員

將某些路由限制為僅能由 Proxy 管理員存取:

general_settings:
master_key: sk-1234
admin_only_routes: ["/key/generate", "/key/delete"]

限制可用路由

只在 proxy 上公開特定路由:

general_settings:
master_key: sk-1234
allowed_routes: ["/chat/completions", "/embeddings", "LiteLLMRoutes.public_routes"]

使用範例

定義公開、僅限管理員與允許的路由

general_settings:
master_key: sk-1234
public_routes: ["LiteLLMRoutes.public_routes", "/spend/calculate"]
admin_only_routes: ["/key/generate"]
allowed_routes: ["/chat/completions", "/spend/calculate", "LiteLLMRoutes.public_routes"]

LiteLLMRoutes.public_routes 是對應 LiteLLM 預設公開路由的 ENUM。 檢視原始碼

測試

curl --request POST \
--url 'http://localhost:4000/spend/calculate' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hey, how'\''s it going?"}]
}'

此端點可在沒有 Authorization 標頭的情況下運作。

進階:萬用字元模式

使用萬用字元模式一次比對多個路由。

語法

模式說明範例
/path/*比對任何以 /path/ 開頭的路由/api/* 比對 /api/users/api/users/123

範例

將某一路徑下的所有路由設為公開

general_settings:
master_key: sk-1234
public_routes:
- "LiteLLMRoutes.public_routes"
- "/api/v1/*" # All routes under /api/v1/
- "/health/*" # All health check routes

使用萬用字元限制管理員路由

general_settings:
master_key: sk-1234
admin_only_routes:
- "/admin/*" # All admin routes
- "/internal/*" # All internal routes

測試萬用字元路由

設定:

general_settings:
master_key: sk-1234
public_routes:
- "/public/*"

測試:

# This works without auth (matches /public/*)
curl http://localhost:4000/public/status

# This also works without auth (matches /public/*)
curl http://localhost:4000/public/health/detailed

# This requires auth (doesn't match /public/*)
curl http://localhost:4000/private/data