3737 OpenAIAPIConfigurationError ,
3838 OpenAIAPIValueError ,
3939)
40- from pydantic import Field , SecretStr , ValidationInfo , field_validator
40+ from pydantic import Field , SecretStr , ValidationError , ValidationInfo , field_validator
4141from pydantic_settings import BaseSettings
4242
4343
@@ -217,7 +217,7 @@ class Settings(BaseSettings):
217217 _initialized : bool = False
218218
219219 # pylint: disable=too-many-branches,too-many-statements
220- def __init__ (self , ** data : Any ):
220+ def __init__ (self , ** data : Any ): # noqa: C901
221221 super ().__init__ (** data )
222222 if not Services .enabled (Services .AWS_CLI ):
223223 self ._initialized = True
@@ -240,14 +240,12 @@ def __init__(self, **data: Any):
240240 logger .info ("running inside GitHub Actions" )
241241 aws_access_key_id = os .environ .get ("AWS_ACCESS_KEY_ID" , None )
242242 aws_secret_access_key = os .environ .get ("AWS_SECRET_ACCESS_KEY" , None )
243- if not aws_access_key_id or not aws_secret_access_key :
243+ if not aws_access_key_id or not aws_secret_access_key and not self . aws_profile :
244244 raise OpenAIAPIConfigurationError (
245245 "required environment variable(s) AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY not set"
246246 )
247- self ._aws_access_key_id_source = "environ"
248- self ._aws_secret_access_key_source = "environ"
249247 region_name = os .environ .get ("AWS_REGION" , None )
250- if not region_name :
248+ if not region_name and not self . aws_profile :
251249 raise OpenAIAPIConfigurationError ("required environment variable AWS_REGION not set" )
252250 try :
253251 self ._aws_session = boto3 .Session (
@@ -259,11 +257,17 @@ def __init__(self, **data: Any):
259257 self ._aws_access_key_id_source = "environ"
260258 self ._aws_secret_access_key_source = "environ"
261259 except ProfileNotFound :
262- logger .warning ("aws_profile %s not found" , self .aws_profile )
260+ # only log this if the aws_profile is set
261+ if self .aws_profile :
262+ logger .warning ("aws_profile %s not found" , self .aws_profile )
263263
264- if self .aws_profile and self . _aws_access_key_id_source == "unset" :
264+ if self .aws_profile :
265265 self ._aws_access_key_id_source = "aws_profile"
266266 self ._aws_secret_access_key_source = "aws_profile"
267+ else :
268+ self ._aws_access_key_id_source = "environ"
269+ self ._aws_secret_access_key_source = "environ"
270+
267271 self ._initialized = True
268272
269273 if not self .initialized :
@@ -392,6 +396,7 @@ def aws_auth(self) -> dict:
392396 }
393397 if self .init_info :
394398 retval ["init_info" ] = self .init_info
399+ return retval
395400
396401 @property
397402 def aws_session (self ):
@@ -702,4 +707,25 @@ def check_pinecone_api_key(cls, v) -> SecretStr:
702707 return v
703708
704709
705- settings = Settings ()
710+ class SingletonSettings :
711+ """Singleton for Settings"""
712+
713+ _instance = None
714+
715+ def __new__ (cls ):
716+ """Create a new instance of Settings"""
717+ if cls ._instance is None :
718+ cls ._instance = super (SingletonSettings , cls ).__new__ (cls )
719+ try :
720+ cls ._instance ._settings = Settings ()
721+ except ValidationError as e :
722+ raise OpenAIAPIConfigurationError ("Invalid configuration: " + str (e )) from e
723+ return cls ._instance
724+
725+ @property
726+ def settings (self ):
727+ """Return the settings"""
728+ return self ._settings
729+
730+
731+ settings = SingletonSettings ().settings
0 commit comments