Created
March 26, 2024 07:40
-
-
Save abhijitmamarde/ed7986d31f163b158dd2a9c3f08871ee to your computer and use it in GitHub Desktop.
Verify if the given openai API key is valid or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
import openai.types | |
def check_openai_api_key(api_key): | |
openai.api_key = api_key | |
try: | |
openai.models.list() | |
except openai.AuthenticationError as e: | |
return False | |
else: | |
return True | |
if __name__ == "__main__": | |
import sys | |
api_key = sys.argv[1] | |
is_valid = check_openai_api_key(api_key) | |
if is_valid: | |
print("Valid OpenAI API key.") | |
else: | |
print("Invalid OpenAI API key.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment