控制公共和私有路由
信息
需要 LiteLLM 企业版许可证。获取免费试用。
控制哪些路由需要身份验证,哪些路由是公开可访问的。
路由类型
| 路由类型 | 需要身份验证 | 描述 |
|---|---|---|
public_routes | 否 | 无需任何身份验证即可访问的路由 |
admin_only_routes | 是(仅限管理员) | 仅能由 代理管理员 访问的路由 |
allowed_routes | 是 | 代理上暴露的路由。如果未设置,则暴露所有路由 |
快速入门
使路由公开
允许在无需身份验证的情况下访问特定路由
general_settings:
master_key: sk-1234
public_routes: ["LiteLLMRoutes.public_routes", "/spend/calculate"]
将路由限制为仅限管理员访问
限制某些路由仅能由代理管理员访问
general_settings:
master_key: sk-1234
admin_only_routes: ["/key/generate", "/key/delete"]
限制可用路由
仅在代理上暴露特定路由
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 是一个枚举(ENUM),对应 LiteLLM 的默认公共路由。查看源代码。
测试
- 测试 public_routes
- 测试 admin_only_routes
- 测试 allowed_routes
curl --request POST \
--url 'https://:4000/spend/calculate' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hey, how'\''s it going?"}]
}'
此端点无需 Authorization 请求头即可工作。
成功请求(管理员)
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data '{}'
失败请求(非管理员)
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <virtual-key-from-non-admin>' \
--header 'Content-Type: application/json' \
--data '{"user_role": "internal_user"}'
预期响应
{
"error": {
"message": "user not allowed to access this route. Route=/key/generate is an admin only route",
"type": "auth_error",
"param": "None",
"code": "403"
}
}
成功请求
curl https://:4000/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "fake-openai-endpoint",
"messages": [
{"role": "user", "content": "Hello, Claude"}
]
}'
失败请求(路由不被允许)
curl --location 'http://0.0.0.0:4000/embeddings' \
--header 'Content-Type: application/json' \
-H "Authorization: Bearer sk-1234" \
--data '{
"model": "text-embedding-ada-002",
"input": ["write a litellm poem"]
}'
预期响应
{
"error": {
"message": "Route /embeddings not allowed",
"type": "auth_error",
"param": "None",
"code": "403"
}
}
进阶:通配符模式
使用通配符模式同时匹配多个路由。
语法
| 模式 | 描述 | 示例 |
|---|---|---|
/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 https://:4000/public/status
# This also works without auth (matches /public/*)
curl https://:4000/public/health/detailed
# This requires auth (doesn't match /public/*)
curl https://:4000/private/data