From 34861013f8cd3dcb91929a32aafc1580137f6012 Mon Sep 17 00:00:00 2001 From: Yuhong Sun Date: Sat, 24 Jun 2023 00:00:43 -0700 Subject: [PATCH] DAN-142 OpenAI key once a day (#113) --- backend/danswer/configs/app_configs.py | 2 +- backend/danswer/server/manage.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/danswer/configs/app_configs.py b/backend/danswer/configs/app_configs.py index 68513f2e8..991d27ad2 100644 --- a/backend/danswer/configs/app_configs.py +++ b/backend/danswer/configs/app_configs.py @@ -11,7 +11,7 @@ APP_PORT = 8080 # User Facing Features Configs ##### BLURB_LENGTH = 200 # Characters. Blurbs will be truncated at the first punctuation after this many characters. - +GENERATIVE_MODEL_ACCESS_CHECK_FREQ = 86400 # 1 day ##### # Web Configs diff --git a/backend/danswer/server/manage.py b/backend/danswer/server/manage.py index 4defeafc0..6e8bffdcc 100644 --- a/backend/danswer/server/manage.py +++ b/backend/danswer/server/manage.py @@ -1,8 +1,11 @@ +from datetime import datetime +from datetime import timedelta from typing import cast from danswer.auth.schemas import UserRole from danswer.auth.users import current_admin_user from danswer.auth.users import current_user +from danswer.configs.app_configs import GENERATIVE_MODEL_ACCESS_CHECK_FREQ from danswer.configs.app_configs import MASK_CREDENTIAL_PREFIX from danswer.configs.constants import OPENAI_API_KEY_STORAGE_KEY from danswer.connectors.file.utils import write_temp_files @@ -290,6 +293,20 @@ def connector_run_once( def validate_existing_openai_api_key( _: User = Depends(current_admin_user), ) -> None: + check_key_time = "openai_api_key_last_check_time" + kv_store = get_dynamic_config_store() + curr_time = datetime.now() + try: + last_check = datetime.fromtimestamp(cast(float, kv_store.load(check_key_time))) + check_freq_sec = timedelta(seconds=GENERATIVE_MODEL_ACCESS_CHECK_FREQ) + if curr_time - last_check < check_freq_sec: + return + except ConfigNotFoundError: + # First time checking the key, nothing unusual + pass + + get_dynamic_config_store().store(check_key_time, curr_time.timestamp()) + try: openai_api_key = get_openai_api_key() is_valid = check_openai_api_key_is_valid(openai_api_key)