跳至主要內容

模型存取群組

總覽

將多個模型歸組到單一名稱下,然後授權金鑰或團隊存取整個群組。可在不更新個別金鑰的情況下,將模型加入或移出群組。

使用情境:

  • 將正式環境與開發環境模型分開
  • 將昂貴模型限制給特定團隊
  • 依提供者或能力整理模型
  • 使用萬用字元控制對模型家族的存取(例如,openai/*

運作方式

重點概念: 將模型分組 → 將群組附加到金鑰 → 金鑰即可存取群組中的所有模型

步驟 1. 在 config.yaml 中指定 model, access group

config.yaml
model_list:
- model_name: gpt-4
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
model_info:
access_groups: ["beta-models"] # 👈 Model Access Group
- model_name: fireworks-llama-v3-70b-instruct
litellm_params:
model: fireworks_ai/accounts/fireworks/models/llama-v3-70b-instruct
api_key: "os.environ/FIREWORKS"
model_info:
access_groups: ["beta-models"] # 👈 Model Access Group

建立具有存取群組的金鑰

Create Key with Access Group
curl --location 'http://localhost:4000/key/generate' \
-H 'Authorization: Bearer <your-master-key>' \
-H 'Content-Type: application/json' \
-d '{"models": ["beta-models"], # 👈 Model Access Group
"max_budget": 0,}'

測試金鑰

Test Key - Allowed Access
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-<key-from-previous-step>" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello"}
]
}'

✨ 控制萬用字元模型的存取

控制對所有具有特定前綴的模型的存取(例如 openai/*)。

也可用於讓使用者存取所有模型,但排除少數您不希望他們使用的模型(例如 openai/o1-*)。

資訊

在萬用字元模型上設定模型存取群組是 Enterprise 功能。

查看價格 這裡

取得試用金鑰 這裡

  1. 設定 config.yaml
config.yaml - Wildcard Models
model_list:
- model_name: openai/*
litellm_params:
model: openai/*
api_key: os.environ/OPENAI_API_KEY
model_info:
access_groups: ["default-models"]
- model_name: openai/o1-*
litellm_params:
model: openai/o1-*
api_key: os.environ/OPENAI_API_KEY
model_info:
access_groups: ["restricted-models"]
  1. 產生可存取 default-models 的金鑰
Generate Key for Wildcard Access Group
curl -L -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"models": ["default-models"],
}'
  1. 測試金鑰
Test Wildcard Access - Allowed
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-<key-from-previous-step>" \
-d '{
"model": "openai/gpt-4",
"messages": [
{"role": "user", "content": "Hello"}
]
}'

透過 API 管理存取群組

僅限資料庫模型

存取群組管理 API 僅適用於儲存在資料庫中的模型(透過 /model/new 新增)。

定義於 config.yaml 的模型無法透過這些 API 管理,且必須直接在設定檔中設定。

使用存取群組管理端點可動態建立、更新及刪除存取群組,而無需重新啟動 proxy。

教學:完整的存取群組工作流程

本教學將示範如何建立存取群組、檢視其詳細資訊、將其附加到金鑰,以及更新群組中的模型。

必要條件:

  • 模型必須先新增至資料庫(不能只在 config.yaml 中)
  • 您需要使用 master key 進行授權

步驟 1:將模型新增至資料庫

首先,將一些模型新增至資料庫:

Add Models to Database
# Add GPT-4 to database
curl -X POST 'http://localhost:4000/model/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"model_name": "gpt-4",
"litellm_params": {
"model": "gpt-4",
"api_key": "os.environ/OPENAI_API_KEY"
}
}'

# Add Claude to database
curl -X POST 'http://localhost:4000/model/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"model_name": "claude-3-opus",
"litellm_params": {
"model": "claude-3-opus-20240229",
"api_key": "os.environ/ANTHROPIC_API_KEY"
}
}'

步驟 2:建立存取群組

建立一個包含多個模型的存取群組:

Create Access Group
curl -X POST 'http://localhost:4000/access_group/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"access_group": "production-models",
"model_names": ["gpt-4", "claude-3-opus"]
}'

回應:

Response
{
"access_group": "production-models",
"model_names": ["gpt-4", "claude-3-opus"],
"models_updated": 2
}

步驟 3:檢視存取群組資訊

檢查存取群組詳細資訊:

Get Access Group Info
curl -X GET 'http://localhost:4000/access_group/production-models/info' \
-H 'Authorization: Bearer sk-1234'

回應:

Response
{
"access_group": "production-models",
"model_names": ["gpt-4", "claude-3-opus"],
"deployment_count": 2
}

步驟 4:建立具有存取群組的金鑰

建立一個可存取群組中所有模型的 API 金鑰:

Create Key with Access Group
curl -X POST 'http://localhost:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"models": ["production-models"],
"max_budget": 100
}'

回應:

Response
{
"key": "sk-...",
"models": ["production-models"]
}

測試金鑰:

Test Key Access
# This succeeds - gpt-4 is in production-models
curl -X POST 'http://localhost:4000/v1/chat/completions' \
-H 'Authorization: Bearer sk-...' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}]
}'

# This succeeds - claude-3-opus is in production-models
curl -X POST 'http://localhost:4000/v1/chat/completions' \
-H 'Authorization: Bearer sk-...' \
-H 'Content-Type: application/json' \
-d '{
"model": "claude-3-opus",
"messages": [{"role": "user", "content": "Hello"}]
}'

步驟 5:更新存取群組

將模型新增至或從存取群組中移除:

Update Access Group
curl -X PUT 'http://localhost:4000/access_group/production-models/update' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"model_names": ["gpt-4", "claude-3-opus", "gemini-pro"]
}'

回應:

Response
{
"access_group": "production-models",
"model_names": ["gpt-4", "claude-3-opus", "gemini-pro"],
"models_updated": 3
}

第 4 步的 API 金鑰現在會自動存取 gemini-pro,而無需對金鑰本身做任何變更。

API 參考文件 - 存取群組管理

如需包含所有端點、參數與回應 schema 的完整 API 文件,請參閱 存取群組管理 API 參考文件

透過 UI 管理存取群組

您也可以透過 LiteLLM Admin UI 管理存取群組。

步驟 1:將模型新增至存取群組

將模型新增至資料庫時,請使用「Model Access Group」欄位將其指派給存取群組:

新增具有存取群組的模型

在此範例中,gpt-4 被新增至 production-models 存取群組。

步驟 2:建立具有存取群組的金鑰

建立 API 金鑰時,請在「Models」欄位中指定存取群組:

建立具有存取群組的金鑰

該金鑰將可存取 production-models 群組中的所有模型。

步驟 3:測試金鑰

使用產生的金鑰發出請求:

Test Key with Access Group
# This succeeds - gpt-4 is in production-models
curl -X POST 'http://localhost:4000/v1/chat/completions' \
-H 'Authorization: Bearer sk-...' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}]
}'

回應:

Success Response
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
]
}

如果您嘗試存取不在該存取群組中的模型,該請求將會被拒絕:

Test Rejected Request
# This fails - gpt-4o is not in production-models
curl -X POST 'http://localhost:4000/v1/chat/completions' \
-H 'Authorization: Bearer sk-...' \
-H 'Content-Type: application/json' \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello"}]
}'

回應:

Error Response
{
"error": {
"message": "Invalid model for key",
"type": "invalid_request_error"
}
}