pythonazureazure-active-directoryopenai-apichatgpt-api

OpenAI remove key access while using AAD authentication


I am calling Azure OpenAI API in my python code. To set it up, we need to provide a few parameters, one of which is openai.api_key. There are 2 options to get this value -

  1. Static key
  2. AAD Token

I am using AAD token (refer this - https://github.com/openai/openai-python#microsoft-azure-active-directory-authentication) but still I can access it using the static key. I want to remove the static key access.

Is there any way to disable the static key and only authenticate using AAD authentication?


Solution

  • Is there any way to disable the static key and only authenticate using AAD authentication?

    If you need to authenticate using only AAD authentication without using api.key you can use the below code with request method using Python.

    First, create an application and assign Cognitive Services User role to your application.

    Portal:

    enter image description here

    Code:

    import requests
    import json
    
    
    tenantid="xxxxx"
    clientid="xxxxx"
    clientsecret="xxxxxx"
    
    auth_url = f'https://login.microsoftonline.com/{tenantid}/oauth2/v2.0/token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': clientid,
        'client_secret': clientsecret,
        'scope': 'https://cognitiveservices.azure.com/.default'
    }
    response = requests.post(auth_url, data=data).json()
    access_token=response['access_token']
    
    url="https://YOUR_RESOURCE_NAME.openai.azure.com/openai/deployments/YOUR_DEPLOYMENT_NAME/completions?api-version=2023-05-15"
    
    h1 = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type":"application/json"
    }
    
    body={
        "prompt":"MS Dhoni is greatest captain",
        "max_tokens": 100
    }
    response = requests.post(url,headers=h1,data=json.dumps(body))
    s=json.loads(response.text)
    d=json.dumps(s,indent=3)
    print(d)
    

    Output:

      {
       "id": "cmpl-880wqMQxxxxxxxPl",
       "object": "text_completion",
       "created": 1696xxxx0,
       "model": "text-davinci-002",
       "choices": [
          {
             "text": " India has had, he is motivation: Ravichandran AshwinR Ashwin tweeted saying MS Dhoni has given all he had to India cricket for over 15 years and has been a motivation for everybody. Michael holding reveals his best captain story and it involves Kapil and Viv. The Indians were taken by surprise and after the initial shock it took off from there.New Zealand lost by 15 runs. It is a catch that has a contenders for the most iconic cricketing pictures.\n\nIndia",
             "index": 0,
             "finish_reason": "length",
             "logprobs": null
          }
       ],
       "usage": {
          "completion_tokens": 100,
          "prompt_tokens": 6,
          "total_tokens": 106
       }
    }
    

    enter image description here

    Reference:

    How to configure Azure OpenAI Service with managed identities - Azure OpenAI | Microsoft Learn