Skip to content

Commit

Permalink
Remove api key validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgerrits committed Sep 25, 2024
1 parent 4dfe2fb commit ed2bec7
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 39 deletions.
8 changes: 2 additions & 6 deletions autogen/oai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."
)
Expand Down
13 changes: 0 additions & 13 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 1 addition & 20 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()

0 comments on commit ed2bec7

Please sign in to comment.