跳至主要内容

控制公共和私有路由

信息

需要 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 的默认公共路由。查看源代码

测试

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 请求头即可工作。

进阶:通配符模式

使用通配符模式同时匹配多个路由。

语法

模式描述示例
/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