Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Set API_KEY #143

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def set_api_key(request):
data = await request.post()
api_key = data.get("api_key")
if api_key:
if not validate_api_key(api_key, override=True):
if not validate_api_key(api_key):
error_msg = "Wrong API key provided, please refer to cloud.siliconflow.cn to get the key"
print("set_api_key:", error_msg)
return web.Response(
Expand Down
15 changes: 11 additions & 4 deletions src/bizyair/common/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ def validate_api_key(api_key: str = None) -> bool:
url = "https://api.siliconflow.cn/v1/user/info"
headers = {"accept": "application/json", "authorization": f"Bearer {api_key}"}

response_data = send_request(method="GET", url=url, headers=headers, callback=None)
if "message" not in response_data or response_data["message"] != "Ok":
try:
response_data = send_request(
method="GET", url=url, headers=headers, callback=None
)
if "message" not in response_data or response_data["message"] != "Ok":
api_key_state.is_valid = False
print(f"\033[91mAPI key validation failed. API Key: {api_key}\033[0m")
else:
api_key_state.is_valid = True
except Exception as e:
api_key_state.is_valid = False
else:
api_key_state.is_valid = True
print(f"\033[91mError validating API key: {api_key}, error: {e}\033[0m")
return api_key_state.is_valid


Expand Down
2 changes: 1 addition & 1 deletion src/bizyair/common/env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def load_api_key():
has_key = api_key.startswith("sk-")
return has_key, api_key
else:
return False, "Key not found, please refer to cloud.siliconflow.cn"
return False, None


def create_api_key_file(api_key):
Expand Down
Loading