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

@infinitelyloopy-bt
Copy link

Thank you so much for this. It still doesn't work because I get this error:

Requirement already satisfied: pycparser in /Users/bakertamory/anaconda3/envs/auto-gpt/lib/python3.10/site-packages (from cffi>=1.12->cryptography>=1.3.4->urllib3>=1.21.1->pinecone-client==2.2.1->-r requirements.txt (line 15)) (2.21) ./run.sh: line 9: jq: command not found Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> BrokenPipeError: [Errno 32] Broken pipe sed: 1: "./.env": invalid command code . Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/__main__.py", line 5, in <module> autogpt.cli.main() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/core.py", line 1130, in __call__ return self.main(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) ^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/core.py", line 1635, in invoke rv = super().invoke(ctx) ^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func return f(get_current_context(), *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/cli.py", line 87, in main from autogpt.main import run_auto_gpt File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/main.py", line 8, in <module> from autogpt.agent.agent import Agent File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/agent/__init__.py", line 1, in <module> from autogpt.agent.agent import Agent File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/agent/agent.py", line 3, in <module> from autogpt.app import execute_command, get_command File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/app.py", line 5, in <module> from autogpt.agent.agent_manager import AgentManager File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/agent/agent_manager.py", line 6, in <module> from autogpt.config.config import Config File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/config/__init__.py", line 4, in <module> from autogpt.config.ai_config import AIConfig File "/Users/bakertamory/Documents/App Dev/Auto-GPT-Latest/Auto-GPT/autogpt/config/ai_config.py", line 12, in <module> import distro ModuleNotFoundError: No module named 'distro' Press any key to continue...

@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