Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active April 20, 2024 12:11
Show Gist options
  • Save primaryobjects/523577860628974501ffd3c52cd73525 to your computer and use it in GitHub Desktop.
Save primaryobjects/523577860628974501ffd3c52cd73525 to your computer and use it in GitHub Desktop.
How to Configure AutoGPT with Azure OpenAI Active Directory Managed Identity

How to Configure AutoGPT with Azure OpenAI Active Directory Managed Identity

AutoGPT is an extension of ChatGPT to automatically run an agent to complete a solution without human intervention.

Normally, an OpenAI API key is used.

For Azure OpenAI, you must use either an API token or an Azure Active Directory account.

Loading an API Key with Azure Managed Identity

  1. Download and install Azure CLI.

    pip install azure-cli azure-functions azure-identity
  2. Login to Azure.

    az login
  3. Obtain an API key or modify your run.sh script to automate.

    export accessToken=$(az account get-access-token --resource https://cognitiveservices.azure.com | jq -r .accessToken)
    echo $accessToken
  4. Copy and paste the accessToken contents into your AutoGPT env file.

    OPENAI_API_KEY=<accessToken contents go here>
    USE_AZURE=True
    
  5. Create a file named azure.yaml with the following contents, adjust for your Azure account:

    azure_api_type: azure_ad
    azure_api_base: https://your-domain.openai.azure.com
    azure_api_version: 2023-03-01-preview
    azure_model_map:
        fast_llm_model_deployment_id: gpt-4
        smart_llm_model_deployment_id: gpt-4
        embedding_model_deployment_id: text-embedding-ada-002
    

Automatic Login to Azure

You can setup run.sh to automatically extract the accessToken each time you run AutoGPT by editing the script as follows:

#!/bin/bash
python3 scripts/check_requirements.py requirements.txt
if [ $? -eq 1 ]
then
    echo Installing missing packages...
    pip install -r requirements.txt
fi

export accessToken=$(az account get-access-token --resource https://cognitiveservices.azure.com | jq -r .accessToken)
sed -i "s/OPENAI_API_KEY=.*/OPENAI_API_KEY=$accessToken/" ./.env

python3 -m autogpt $@
read -p "Press any key to continue..."

Specifically, note the lines to export the token and run sed.

Additional

@altsang
Copy link

altsang commented May 8, 2023

FYI - for anyone running into the same problem - if you're using sed on a Mac or other FreeBSD based OS - you need to tell it to destructively overwrite the file with the -i flag

sed -i "" "s/OPENAI_API_KEY=.*/OPENAI_API_KEY=$accessToken/" ./.env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment