跳到主要内容

[测试版]OpenID Connect (OIDC)

LiteLLM 支持使用 OpenID Connect (OIDC) 对上游服务进行身份验证。这使您可以避免在配置文件中存储敏感凭据。

信息

此功能处于测试阶段

OIDC 身份提供商 (IdP)

LiteLLM 支持以下 OIDC 身份提供商

提供商配置名称自定义受众
Google Cloud Rungoogle
CircleCI v1circleci
CircleCI v2circleci_v2
GitHub Actionsgithub
Azure Kubernetes 服务azure
文件file
环境变量env
环境路径env_path

如果您想使用不同的 OIDC 提供商,请在 GitHub 上提出一个 issue。

提示

除非您知道自己在做什么,并且确定没有其他提供商适用于您的用例,否则不要使用 fileenvenv_path 提供商。提示:其他提供商很可能适用。

OIDC 连接依赖方 (RP)

LiteLLM 支持以下 OIDC 依赖方/客户端

  • Amazon Bedrock
  • Azure OpenAI
  • (即将推出) Google Cloud Vertex AI

配置 OIDC

凡是可以使用密钥的地方,都可以直接使用 OIDC。通用格式为

oidc/config_name_here/audience_here

对于不使用 audience 参数的提供商,您可以(并且应该)省略它

oidc/config_name_here/

对于非官方的 file 提供商,您可以使用以下格式

oidc/file/home/user/dave/this_is_a_file_with_a_token.txt

对于非官方的 env,使用以下格式,其中 SECRET_TOKEN 是包含令牌的环境变量的名称

oidc/env/SECRET_TOKEN

对于非官方的 env_path,使用以下格式,其中 SECRET_TOKEN 是包含令牌文件路径的环境变量的名称

oidc/env_path/SECRET_TOKEN
提示

如果您想使用 oidc/env_path/AZURE_FEDERATED_TOKEN_FILE,请不要这样做。而是使用 oidc/azure/,这样可以确保如果 Azure 更改其 OIDC 配置和/或添加新功能时,LiteLLM 仍能继续支持。

示例

Google Cloud Run -> Amazon Bedrock

model_list:
- model_name: claude-3-haiku-20240307
litellm_params:
model: bedrock/anthropic.claude-3-haiku-20240307-v1:0
aws_region_name: us-west-2
aws_session_name: "litellm"
aws_role_name: "arn:aws:iam::YOUR_THING_HERE:role/litellm-google-demo"
aws_web_identity_token: "oidc/google/https://example.com"

CircleCI v2 -> Amazon Bedrock

model_list:
- model_name: command-r
litellm_params:
model: bedrock/cohere.command-r-v1:0
aws_region_name: us-west-2
aws_session_name: "my-test-session"
aws_role_name: "arn:aws:iam::335785316107:role/litellm-github-unit-tests-circleci"
aws_web_identity_token: "oidc/circleci_v2/"

CircleCI v2 -> Bedrock 的 Amazon IAM 角色配置

以下配置仅为示例。您应根据您的具体用例调整权限和信任关系。

权限

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": [
"arn:aws:bedrock:*::foundation-model/anthropic.claude-3-haiku-20240307-v1:0",
"arn:aws:bedrock:*::foundation-model/cohere.command-r-v1:0"
]
}
]
}

有关更多示例,请参阅 https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html

信任关系

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::335785316107:oidc-provider/oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd:aud": "c5a99188-154f-4f69-8da2-b442b1bf78dd"
},
"ForAnyValue:StringLike": {
"oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd:sub": [
"org/c5a99188-154f-4f69-8da2-b442b1bf78dd/project/*/user/*/vcs-origin/github.com/BerriAI/litellm/vcs-ref/refs/heads/main",
"org/c5a99188-154f-4f69-8da2-b442b1bf78dd/project/*/user/*/vcs-origin/github.com/BerriAI/litellm/vcs-ref/refs/heads/litellm_*"
]
}
}
}
]
}

此信任关系限制 CircleCI 仅在主分支和以 litellm_ 开头的分支上承担角色。

对于 CircleCI (v1 和 v2),您还需要在 AWS IAM 设置中添加组织的 OIDC 提供商。有关更多信息,请参阅 https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-idp_oidc.html

提示

永远不需要创建 IAM 用户。如果您创建了,说明您没有正确使用 OIDC。您应该只创建一个具有权限并与您的 OIDC 提供商具有信任关系的角色。

Google Cloud Run -> Azure OpenAI

model_list:
- model_name: gpt-4o-2024-05-13
litellm_params:
model: azure/gpt-4o-2024-05-13
azure_ad_token: "oidc/google/https://example.com"
api_version: "2024-06-01"
api_base: "https://demo-here.openai.azure.com"
model_info:
base_model: azure/gpt-4o-2024-05-13

对于 Azure OpenAI,您需要在环境中定义 AZURE_CLIENT_IDAZURE_TENANT_ID,以及可选的 AZURE_AUTHORITY_HOST

export AZURE_CLIENT_ID="91a43c21-cf21-4f34-9085-331015ea4f91" # Azure AD Application (Client) ID
export AZURE_TENANT_ID="f3b1cf79-eba8-40c3-8120-cb26aca169c2" # Will be the same across of all your Azure AD applications
export AZURE_AUTHORITY_HOST="https://login.microsoftonline.com" # 👈 Optional, defaults to "https://login.microsoftonline.com"
提示

您可以通过访问 https://login.microsoftonline.com/YOUR_DOMAIN_HERE/v2.0/.well-known/openid-configuration 并在 issuer 字段中查找 UUID 来找到 AZURE_CLIENT_ID

提示

除非您需要覆盖默认值,否则不要在环境中设置 AZURE_AUTHORITY_HOST。这样,如果默认值将来发生变化,您就不需要更新环境了。

提示

默认情况下,Azure AD 应用程序使用受众 api://AzureADTokenExchange。我们建议将受众设置为更具体地针对您的应用程序的内容。

Azure AD 应用程序配置

不幸的是,Azure 的设置比 AWS 等其他 OIDC 依赖方要复杂一些。基本上,您需要

  1. 创建一个 Azure 应用程序。
  2. 为您正在使用的 OIDC IdP(例如 Google Cloud Run)添加一个联合凭据。
  3. 将 Azure 应用程序添加到包含 Azure OpenAI 资源(s) 的资源组中。
  4. 赋予 Azure 应用程序访问 Azure OpenAI 资源(s) 所需的角色。

以下自定义角色是推荐的 Azure 应用程序访问 Azure OpenAI 资源的最低权限。您应根据您的具体用例调整权限。

{
"id": "/subscriptions/24ebb700-ec2f-417f-afad-78fe15dcc91f/providers/Microsoft.Authorization/roleDefinitions/baf42808-99ff-466d-b9da-f95bb0422c5f",
"properties": {
"roleName": "invoke-only",
"description": "",
"assignableScopes": [
"/subscriptions/24ebb700-ec2f-417f-afad-78fe15dcc91f/resourceGroups/your-openai-group-name"
],
"permissions": [
{
"actions": [],
"notActions": [],
"dataActions": [
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/audio/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/search/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/chat/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/extensions/chat/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/embeddings/action",
"Microsoft.CognitiveServices/accounts/OpenAI/images/generations/action"
],
"notDataActions": []
}
]
}
}

注意:您的 UUID 会有所不同。

如果您在设置 Azure AD 应用程序方面需要帮助,请联系我们获取付费企业支持。