Last active
October 7, 2024 23:24
-
-
Save cedricvidal/f834f1ab7dc538f6eda5eef4af6723c0 to your computer and use it in GitHub Desktop.
LiteLLM Monkey Patched to support Azure Keyless authentication using `DefaultAzureCredential` and `get_bearer_token_provider`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3.12 | |
# -*- coding: utf-8 -*- | |
import re | |
import sys | |
# LiteLLM doesn't support DefaultAzureCredential and get_bearer_token_provider | |
# There has been a discussion on the subject but no clear path forward: | |
# See https://github.com/BerriAI/litellm/issues/4417 | |
# This monkey patches LiteLLM to force it to use the DefaultAzureCredential and get_bearer_token_provider | |
# This requires the `tenant_id` to be set on `litellm_params` | |
# - litellm_params: | |
# api_base: https://aoai-3d5caufuiotbg.openai.azure.com/ | |
# model: azure/openai-gpt-4o | |
# tpm: 8000 | |
# tenant_id: "HACK" | |
# model_name: gpt-4o | |
from litellm.router_utils import client_initalization_utils | |
from typing import TYPE_CHECKING, Any, Callable, Optional | |
from litellm._logging import verbose_router_logger | |
def get_azure_ad_token_from_entrata_id( | |
tenant_id: str, client_id: str, client_secret: str | |
) -> Callable[[], str]: | |
from azure.identity import ( | |
ClientSecretCredential, | |
DefaultAzureCredential, | |
get_bearer_token_provider, | |
) | |
verbose_router_logger.debug("Getting Azure AD Token from Entrata ID") | |
credential = DefaultAzureCredential() | |
verbose_router_logger.debug("credential %s", credential) | |
token_provider = get_bearer_token_provider( | |
credential, "https://cognitiveservices.azure.com/.default" | |
) | |
verbose_router_logger.debug("token_provider %s", token_provider) | |
return token_provider | |
client_initalization_utils.get_azure_ad_token_from_entrata_id = get_azure_ad_token_from_entrata_id | |
from litellm import run_server | |
if __name__ == '__main__': | |
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) | |
sys.exit(run_server()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment