Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Azure Provider #314

Open
bakes82 opened this issue Dec 18, 2024 · 0 comments
Open

Custom Azure Provider #314

bakes82 opened this issue Dec 18, 2024 · 0 comments

Comments

@bakes82
Copy link

bakes82 commented Dec 18, 2024

Can you point me to where the Azure Provider code is or can the current version be updated. The MS preferred way is to epically in enterprise organizations is to use it behind an APIM with application regenerations. It's basically just proxying the auth beforehand and then obscuring the "keys" to the load balanced instances in the APIM so that way people don't have full access to the instances.

https://github.com/microsoft/AzureOpenAI-with-APIM

Ex for app registration you get a token and pass it as the bearer token to the APIM for auth instead of a subscription key.


import requests
import os

###############################
# Variables you will need
# Load sensitive information from environment variables, such as client id/secret
# Can source as you see fit, but you will need these
# #############################

client_id = "customId"
client_secret = "topSecret"
tenant_id = "123-123-4124-124"
token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'


###############################
# example function
# #############################

def get_azure_openai_api_token(client_id: str, client_secret: str, token_url: str):
    """
    Obtain an access token from Azure AD using app registration client credentials.

    Args:
        client_id (str): The client ID of the Azure app registration.
        client_secret (str): The client secret of the Azure app registration.
        token_url (str): The URL to obtain the OAuth2 token from Azure AD.
        
    Returns:
        str: The access token if the request is successful, None otherwise.

    Raises:
        requests.exceptions.RequestException: If there is an issue with the 
        HTTP request.
    """

    token_data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': 'https://cognitiveservices.azure.com/.default'
    }

    try:
        token_r = requests.post(token_url, data=token_data)
        token_r.raise_for_status()
        return token_r.json().get('access_token')
    except requests.exceptions.RequestException as e:
        print(f"Error obtaining access token: {e}")
        return None

###############################
# Example usage
# #############################

token = get_azure_openai_api_token(client_id, client_secret, token_url)      
print(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant