skip to Main Content

I was given the task to deal with the Azure OpenAI Service and ChatGPT. In the process, when they gave me the keys, endpoint, etc., I ran into this problem:

openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

Here is code:

import os
import openai
openai.api_type = "azure"
openai.api_version = "2023-05-15" 
openai.api_base = os.getenv("EndPointHERE")
openai.api_key = os.getenv("KeyHere")

response = openai.ChatCompletion.create(
    engine="KSUAI", 
    messages=[
    {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
    {"role": "user", "content": "Who were the founders of Microsoft?"}
    ]
)

print(response)

print(response['choices'][0]['message']['content'])

2

Answers


  1. Chosen as BEST ANSWER

    The problem was solved by removing os.getenv from the code, and writing the given keys and endpoint directly into a variable


  2. I was getting same issue so I removed ‘dotenv’ stuff and directly hardcoded the keys into the code but its not ideal method.

    import openai
    
    openai.api_key = '***********************'
    openai.api_base = '******************' 
    openai.api_type = 'azure'
    openai.api_version = '2023-05-15' 
    deployment_name = 'Engine or deployment name on which model is deployed'  
    
    prompt_in = "give your prompt here + input "
    response = openai.Completion.create(
        engine=deployment_name,
        prompt=prompt_in,
        temperature=0,
        max_tokens=200,
        top_p=0.5,
        frequency_penalty=0,
        presence_penalty=0,
        best_of=1,
        stop=None)
    

    you can also use langchain for prompt and for running model.
    I hope it will help…!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search