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
The problem was solved by removing os.getenv from the code, and writing the given keys and endpoint directly into a variable
I was getting same issue so I removed ‘dotenv’ stuff and directly hardcoded the keys into the code but its not ideal method.
you can also use langchain for prompt and for running model.
I hope it will help…!