I want to add openai_api_key to my mac

I am trying to add openai api key and with same section checking my api key setup or not.
but it is returning False, Is there I doing wrong?

import openai
import os

from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
openapi_api_key = os.getenv(‘OPENAI_API_KEY’)

openai.api_key = ‘OPENAI_API_KEY’

def is_api_key_valid():
try:
respose = openai.Completion.create(
engine = “davinci”,
prompt=“this is a test”,
amx_tokens=5
)
except:
return False
else:
return True

print(is_api_key_valid())

ouput : False

1 Like

Welcome to the dev forum @RameshChandRebari

I strongly recommend reading the API reference as it’s clear from the code that there are discrepancies in your understanding of the API.

Also, to check the validity of the API key, you can simply make a list models call instead of trying to generate the text completion.

2 Likes