Skip to main content

基于区域的路由

将特定客户路由到仅限欧盟的模型。

通过为某个客户指定“allowed_model_region”,LiteLLM 将过滤掉模型组中不在允许区域(即“eu”)内的任何模型。

查看代码

1. 创建带有区域指定的客户

为此使用 litellm 的“最终用户”对象。

最终用户可以通过在 openai 聊天完成/嵌入调用中传递“用户”参数来跟踪/识别。

curl -X POST --location 'http://0.0.0.0:4000/end_user/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"user_id" : "ishaan-jaff-45",
"allowed_model_region": "eu", # 👈 指定允许的区域为'eu'
}'

2. 将欧盟模型添加到模型组

将欧盟模型添加到模型组中。对于 Azure 模型,LiteLLM 可以自动推断区域(无需设置)。

model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/gpt-35-turbo-eu # 👈 欧盟 Azure 模型
api_base: https://my-endpoint-europe-berri-992.openai.azure.com/
api_key: os.environ/AZURE_EUROPE_API_KEY
- model_name: gpt-3.5-turbo
litellm_params:
model: azure/chatgpt-v-2
api_base: https://openai-gpt-4-test-v-1.openai.azure.com/
api_version: "2023-05-15"
api_key: os.environ/AZURE_API_KEY

router_settings:
enable_pre_call_checks: true # 👈 重要

启动代理

litellm --config /path/to/config.yaml

3. 测试它!

向代理发起一个简单的聊天完成调用。在响应头中,你应该会看到返回的 API 基础地址。

curl -X POST --location 'http://localhost:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "宇宙的意义是什么?1234"
}],
"user": "ishaan-jaff-45" # 👈 用户 ID
}
'

预期的响应头中的 API 基础地址

x-litellm-api-base: "https://my-endpoint-europe-berri-992.openai.azure.com/"

FAQ

如果该区域没有可用的模型会发生什么?

由于路由器会过滤掉不在指定区域内的模型,如果该区域内没有可用模型,它将向用户返回一个错误。

优云智算