Skip to content

Commit

Permalink
Check for OpenAI base_url before API key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarku committed Feb 14, 2024
1 parent 939653d commit 822bc78
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion autogen/oai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

LEGACY_DEFAULT_CACHE_SEED = 41
LEGACY_CACHE_DIR = ".cache"
OPEN_API_BASE_URL_PREFIX = "https://api.openai.com"


class ModelClient(Protocol):
Expand Down Expand Up @@ -111,7 +112,11 @@ class OpenAIClient:

def __init__(self, client: Union[OpenAI, AzureOpenAI]):
self._oai_client = client
if not isinstance(client, openai.AzureOpenAI) and not is_valid_api_key(self._oai_client.api_key):
if (
not isinstance(client, openai.AzureOpenAI)
and str(client.base_url).startswith(OPEN_API_BASE_URL_PREFIX)
and not is_valid_api_key(self._oai_client.api_key)
):
logger.warning(
"The API key specified is not a valid OpenAI format; it won't work with the OpenAI-hosted model."
)
Expand Down

0 comments on commit 822bc78

Please sign in to comment.