Skip to content

Instantly share code, notes, and snippets.

@UtsavChokshiCNU
Created December 18, 2023 13:27
Show Gist options
  • Save UtsavChokshiCNU/f0ceb168d624867bd50d57c6bb3d3adb to your computer and use it in GitHub Desktop.
Save UtsavChokshiCNU/f0ceb168d624867bd50d57c6bb3d3adb to your computer and use it in GitHub Desktop.
Validate open ai creds as well as check for required model access
import openai
# OpenAI Models used
OPENAI_GPT_35_MODEL = "gpt-3.5-turbo-16k"
OPENAI_GPT_4_MODEL = "gpt-4"
def is_valid_openai_creds(
openai_api_key: str, openai_organization_id: str
) -> Tuple[bool, str]:
"""Checks if OpenAI API Key and Organization ID is valid as well as have access to required OpenAI models"""
try:
openai.api_key = openai_api_key
openai.organization = openai_organization_id
available_models = [item.id for item in openai.Model.list().data]
required_models = [OPENAI_GPT_35_MODEL, OPENAI_GPT_4_MODEL]
if len(set(required_models) - set(available_models)) != 0:
error_message = f"Provided OpenAI Credentials does not have access to all {required_models=}"
return (False, error_message)
except openai.error.AuthenticationError:
error_message = "Invalid OpenAI API Key and/or Organization ID"
return (False, error_message)
else:
return (True, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment