Skip to content

Commit

Permalink
OpenAI API key format validation (microsoft#44)
Browse files Browse the repository at this point in the history
* Updated to expand acceptable OpenAI API key format

* Update to ChromaDB version required for RetrieveChatTest
  • Loading branch information
marklysze authored Sep 28, 2024
1 parent 3a683fc commit cd64eb3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions autogen/oai/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ def get_key(config: Dict[str, Any]) -> str:


def is_valid_api_key(api_key: str) -> bool:
"""Determine if input is valid OpenAI API key.
"""Determine if input is valid OpenAI API key. As of 2024-09-24 there's no official definition of the key structure
so we will allow anything starting with "sk-" and having at least 48 alphanumeric (plus underscore and dash) characters.
Keys are known to start with "sk-", "sk-proj", "sk-None", and "sk-svcaat"
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,}$")
api_key_re = re.compile(r"^sk-[A-Za-z0-9_-]{48,}$")
return bool(re.fullmatch(api_key_re, api_key))


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

retrieve_chat = [
"protobuf==4.25.3",
"chromadb",
"chromadb==0.5.3",
"sentence_transformers",
"pypdf",
"ipython",
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
skip_redis = False
skip_docker = False
reason = "requested to skip"
MOCK_OPEN_AI_API_KEY = "sk-mockopenaiAPIkeyinexpectedformatfortestingonly"
MOCK_OPEN_AI_API_KEY = "sk-mockopenaiAPIkeysinexpectedformatsfortestingonly"


# Registers command-line options like '--skip-openai' and '--skip-redis' via pytest hook.
Expand Down
13 changes: 10 additions & 3 deletions test/oai/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,22 @@ def test_is_valid_api_key():
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-1b7n_YEvn7mmiUqkipH-JXk7DvqYoacDoe6Ae8v212T3BlbkFJWFRrGd3ZHN9GpZWX9Zx13gLolZ3NqcMFvb7ov5tzeA"
) # correct format, not real
assert is_valid_api_key(
"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"
) # correct format, not real
assert is_valid_api_key("sk-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS1212121221212sssXX")
assert is_valid_api_key("sk-proj-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212")
assert is_valid_api_key("sk-None-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212")
assert is_valid_api_key("sk-svcaat-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("sk-aut0-gen--asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212")
assert is_valid_api_key("sk--aut0-gen-asajsdjsd22372X23kjdfdfdf2329ffUUDSDS12121212212")
assert is_valid_api_key(MOCK_OPEN_AI_API_KEY)


Expand Down

0 comments on commit cd64eb3

Please sign in to comment.