/azure-openai-keyless-python

Provision an OpenAI account with GPT deployment and RBAC role for your user account for keyless access

Primary LanguageBicepMIT LicenseMIT

Azure OpenAI Keyless Deployment

The purpose of this repository is to provision an Azure OpenAI account with an RBAC role permission for your user account to access, so that you can use the OpenAI API SDKs with keyless (Entra) authentication. By default, the account will include a gpt-3.5 model, but you can modify infra/main.bicep to deploy other models instead.

Prerequisites

  1. Sign up for a free Azure account and create an Azure Subscription.
  2. Request access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access and awaiting approval.
  3. Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)

Provisioning

  1. Login to Azure:

    azd auth login
  2. Provision the OpenAI account:

    azd provision

    It will prompt you to provide an azd environment name (like "chat-app"), select a subscription from your Azure account, and select a location where the OpenAI model is available (like "canadaeast"). Then it will provision the resources in your account and deploy the latest code. If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the OpenAI resource. To change the location run:

    azd env set AZURE_LOCATION "yournewlocationname"
  3. When azd has finished, you should have an OpenAI account you can use locally when logged into your Azure account. You can output the necessary environment variables into an .env file by running a script:

    For Mac OS X / Linux:

    ./write_dot_env.sh

    For Windows:

    pwsh ./write_dot_env.ps1
  4. Then you can run the example code in this repository.

Running the Python example

  1. If you're not already running in a Codespace or Dev Container, create a Python virtual environment.

  2. Install the requirements:

    python -m pip install -r requirements.txt
  3. Run the example:

    python example.py

    This will use the OpenAI API SDK to make a request to the OpenAI API and print the response.