Skip to main content

基于标签的路由

根据标签路由请求。这对于

  • 为用户实现免费/付费层级
  • 控制每个团队的模型访问,例如团队A可以访问gpt-4部署A,团队B可以访问gpt-4部署B(团队LLM访问控制)

快速开始

1. 在config.yaml中定义标签

  • 带有 tags=["free"] 的请求将被路由到 openai/fake
  • 带有 tags=["paid"] 的请求将被路由到 openai/gpt-4o
model_list:
- model_name: gpt-4
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
tags: ["free"] # 👈 关键更改
- model_name: gpt-4
litellm_params:
model: openai/gpt-4o
api_key: os.environ/OPENAI_API_KEY
tags: ["paid"] # 👈 关键更改
- model_name: gpt-4
litellm_params:
model: openai/gpt-4o
api_key: os.environ/OPENAI_API_KEY
api_base: https://exampleopenaiendpoint-production.up.railway.app/
tags: ["default"] # 可选 - 所有未标记的请求将被路由到此处


router_settings:
enable_tag_filtering: True # 👈 关键更改
general_settings:
master_key: sk-1234

2. 使用 tags=["free"] 发起请求

此请求包含 "tags": ["free"],将被路由到 openai/fake

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello, Claude gm!"}
],
"tags": ["free"]
}'

预期响应

当此操作成功时,预期会看到以下响应头

x-litellm-model-api-base: https://exampleopenaiendpoint-production.up.railway.app/

响应

{
"id": "chatcmpl-33c534e3d70148218e2d62496b81270b",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "\n\nHello there, how may I assist you today?",
"role": "assistant",
"tool_calls": null,
"function_call": null
}
}
],
"created": 1677652288,
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion",
"system_fingerprint": "fp_44709d6fcb",
"usage": {
"completion_tokens": 12,
"prompt_tokens": 9,
"total_tokens": 21
}
}

3. 使用 tags=["paid"] 发起请求

此请求包含 "tags": ["paid"],将被路由到 openai/gpt-4

curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello, Claude gm!"}
],
"tags": ["paid"]
}'

预期响应

当此操作成功时,预期会看到以下响应头

x-litellm-model-api-base: https://api.openai.com

响应

{
"id": "chatcmpl-9maCcqQYTqdJrtvfakIawMOIUbEZx",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "Good morning! How can I assist you today?",
"role": "assistant",
"tool_calls": null,
"function_call": null
}
}
],
"created": 1721365934,
"model": "gpt-4o-2024-05-13",
"object": "chat.completion",
"system_fingerprint": "fp_c4e5b6fa31",
"usage": {
"completion_tokens": 10,
"prompt_tokens": 12,
"total_tokens": 22
}
}

设置默认标签

如果你想将所有未标记的请求路由到特定部署,可以使用此功能

  1. 在yaml中设置默认标签
  model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
tags: ["default"] # 👈 关键更改 - 所有未标记的请求将被路由到此处
model_info:
id: "default-model" # 用于在响应头中识别模型
  1. 启动代理
$ litellm --config /path/to/config.yaml
  1. 发起无标签的请求
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "fake-openai-endpoint",
"messages": [
{"role": "user", "content": "Hello, Claude gm!"}
]
}'

当此操作成功时,预期会看到以下响应头

x-litellm-model-id: default-model

✨ 基于团队的标签路由(企业版)

LiteLLM代理支持基于团队的标签路由,允许你将特定标签与团队关联并相应地路由请求。例如 团队A可以访问gpt-4部署A,团队B可以访问gpt-4部署B(团队LLM访问控制)

info

以下是如何使用curl命令设置和使用基于团队的标签路由:

  1. 在代理配置中启用标签过滤: 在你的 proxy_config.yaml 中,确保你有以下设置:

    model_list:
    - model_name: fake-openai-endpoint
    litellm_params:
    model: openai/fake
    api_key: fake-key
    api_base: https://exampleopenaiendpoint-production.up.railway.app/
    tags: ["teamA"] # 👈 关键更改
    model_info:
    id: "team-a-model" # 用于在响应头中识别模型
    - model_name: fake-openai-endpoint
    litellm_params:
    model: openai/fake
    api_key: fake-key
    api_base: https://exampleopenaiendpoint-production.up.railway.app/
    tags: ["teamB"] # 👈 关键更改
    model_info:
    id: "team-b-model" # 用于在响应头中识别模型
    - model_name: fake-openai-endpoint
    litellm_params:
    model: openai/fake
    api_key: fake-key
    api_base: https://exampleopenaiendpoint-production.up.railway.app/
    tags: ["default"] # 可选 - 所有未标记的请求将路由到此

    router_settings:
    enable_tag_filtering: True # 👈 关键更改

    general_settings:
    master_key: sk-1234
  2. 创建带有标签的团队:

    使用 /team/new 端点创建带有特定标签的团队:

    # 创建 Team A
    curl -X POST http://0.0.0.0:4000/team/new \
    -H "Authorization: Bearer sk-1234" \
    -H "Content-Type: application/json" \
    -d '{"tags": ["teamA"]}'
    # 创建 Team B
    curl -X POST http://0.0.0.0:4000/team/new \
    -H "Authorization: Bearer sk-1234" \
    -H "Content-Type: application/json" \
    -d '{"tags": ["teamB"]}'

    这些命令将返回包含每个团队的 team_id 的 JSON 响应。

  3. 为团队成员生成密钥:

    使用 /key/generate 端点创建与特定团队关联的密钥:

    # 为 Team A 生成密钥
    curl -X POST http://0.0.0.0:4000/key/generate \
    -H "Authorization: Bearer sk-1234" \
    -H "Content-Type: application/json" \
    -d '{"team_id": "team_a_id_here"}'
    # 为 Team B 生成密钥
    curl -X POST http://0.0.0.0:4000/key/generate \
    -H "Authorization: Bearer sk-1234" \
    -H "Content-Type: application/json" \
    -d '{"team_id": "team_b_id_here"}'

    team_a_id_hereteam_b_id_here 替换为从步骤 2 中收到的实际团队 ID。

  4. 验证路由:

    检查响应中的 x-litellm-model-id 头,确认请求是否根据团队的标签路由到正确的模型。你可以使用 curl 的 -i 标志包含响应头:

    使用 Team A 的密钥发出的请求(包括头)

    curl -i -X POST http://0.0.0.0:4000/chat/completions \
    -H "Authorization: Bearer team_a_key_here" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "fake-openai-endpoint",
    "messages": [
    {"role": "user", "content": "Hello!"}
    ]
    }'

    在响应头中,你应该看到:

    x-litellm-model-id: team-a-model

    同样地,当使用 Team B 的密钥时,你应该看到:

    x-litellm-model-id: team-b-model

通过遵循这些步骤并使用这些 curl 命令,你可以在 LiteLLM Proxy 设置中实现并测试基于团队的标签路由,确保不同的团队根据其分配的标签路由到适当的模型或部署。

其他基于标签的功能

优云智算