How to resolve "no api key provided" error in Azure OpenAI Service code generation

Leabriah Spencer 45 Reputation points
2023-10-27T15:52:07.5233333+00:00

In the "Generate and improve code with Azure OpenAI Service" tutorial found in https://microsoftlearning.github.io/mslearn-openai/Instructions/Labs/04-code-generation.html#set-up-an-application-in-cloud-shell, I keep encountering a "no api key provided error" despite providing my API key in both the .env file and the program file. I have tried generating new keys, following instructions from OpenAI, and setting the API key directly in the code or as an environment variable, but none of these methods have resolved the issue. I have also consulted Q&A Assist, but the provided answer was not helpful. Can someone provide guidance to resolve this error?

Attached are images of the error message I am receiving.

apikeyerror2

apikeyerror3

apikeyerror

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
{count} votes

1 answer

Sort by: Most helpful
  1. Saurabh Sharma 23,866 Reputation points Microsoft Employee Moderator
    2023-10-31T17:31:37.7333333+00:00

    @Leabriah Spencer Were you able to test the if the api_key is getting set properly.

    If you are not getting the correct values when using echo cmdlet in command prompt, then the respective environment variable is not configured correctly. Please validate the environment variables by checking "System Properties" > "Environment Variables". (See screenshot below)

    User's image

    If you are using environment variables you need to use the code as below -

    # Get configuration settings 
    load_dotenv()
    azure_oai_endpoint =("https://{resourcename}.openai.azure.com/")
    azure_oai_key = os.getenv("AOAIKey")
    azure_oai_model = "gpt35turbo"
    
    # Set OpenAI configuration settings
    openai.api_type = "azure"
    openai.api_base = azure_oai_endpoint
    openai.api_version = "2023-05-15"
    openai.api_key = azure_oai_key
    

    Also, try hardcoding like this and see if it works.

    openai.api_type = "azure"
    openai.api_base = "https://{resource}.openai.azure.com/"
    openai.api_version = "2023-05-15"
    openai.api_key = "{key}"
    

    Thanks

    Saurabh


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.