diff --git a/autogen/oai/client.py b/autogen/oai/client.py index 8f6e3f185b6a..fcf2df24b3f1 100644 --- a/autogen/oai/client.py +++ b/autogen/oai/client.py @@ -12,7 +12,7 @@ from autogen.cache import Cache from autogen.io.base import IOStream from autogen.logger.logger_utils import get_current_ts -from autogen.oai.openai_utils import OAI_PRICE1K, get_key, is_valid_api_key +from autogen.oai.openai_utils import OAI_PRICE1K, get_key from autogen.runtime_logging import log_chat_completion, log_new_client, log_new_wrapper, logging_enabled from autogen.token_count_utils import count_token @@ -163,11 +163,7 @@ class OpenAIClient: def __init__(self, client: Union[OpenAI, AzureOpenAI]): self._oai_client = client - 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) - ): + if not isinstance(client, openai.AzureOpenAI) and str(client.base_url).startswith(OPEN_API_BASE_URL_PREFIX): logger.warning( "The API key specified is not a valid OpenAI format; it won't work with the OpenAI-hosted model." ) diff --git a/autogen/oai/openai_utils.py b/autogen/oai/openai_utils.py index 41b94324118a..3844795c24f5 100644 --- a/autogen/oai/openai_utils.py +++ b/autogen/oai/openai_utils.py @@ -99,19 +99,6 @@ def get_key(config: Dict[str, Any]) -> str: return json.dumps(config, sort_keys=True) -def is_valid_api_key(api_key: str) -> bool: - """Determine if input is valid OpenAI API key. - - Args: - api_key (str): An input string to be validated. - - Returns: - bool: A boolean that indicates if input is valid OpenAI API key. - """ - api_key_re = re.compile(r"^sk-([A-Za-z0-9]+(-+[A-Za-z0-9]+)*-)?[A-Za-z0-9]{32,}$") - return bool(re.fullmatch(api_key_re, api_key)) - - def get_config_list( api_keys: List[str], base_urls: Optional[List[str]] = None, diff --git a/test/oai/test_utils.py b/test/oai/test_utils.py index fd81d3f9f548..b7aeaa5c2549 100755 --- a/test/oai/test_utils.py +++ b/test/oai/test_utils.py @@ -12,7 +12,7 @@ from conftest import MOCK_OPEN_AI_API_KEY import autogen # noqa: E402 -from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION, filter_config, is_valid_api_key +from autogen.oai.openai_utils import DEFAULT_AZURE_API_VERSION, filter_config # Example environment variables ENV_VARS = { @@ -414,24 +414,5 @@ def test_tags(): assert len(list_5) == 0 -def test_is_valid_api_key(): - assert not is_valid_api_key("") - assert not is_valid_api_key("sk-") - assert not is_valid_api_key("SK-") - assert not is_valid_api_key("sk-asajsdjsd2") - assert not is_valid_api_key("FooBar") - assert not is_valid_api_key("sk-asajsdjsd22372%23kjdfdfdf2329ffUUDSDS") - assert is_valid_api_key("sk-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS") - assert is_valid_api_key("sk-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS1212121221212sssXX") - assert is_valid_api_key("sk-proj-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert is_valid_api_key("sk-0-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert is_valid_api_key("sk-aut0gen-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert is_valid_api_key("sk-aut0-gen-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert is_valid_api_key("sk-aut0--gen-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert not is_valid_api_key("sk-aut0-gen--asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert not is_valid_api_key("sk--aut0-gen-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212") - assert is_valid_api_key(MOCK_OPEN_AI_API_KEY) - - if __name__ == "__main__": pytest.main()