37
37
OpenAIAPIConfigurationError ,
38
38
OpenAIAPIValueError ,
39
39
)
40
- from pydantic import Field , SecretStr , ValidationInfo , field_validator
40
+ from pydantic import Field , SecretStr , ValidationError , ValidationInfo , field_validator
41
41
from pydantic_settings import BaseSettings
42
42
43
43
@@ -217,7 +217,7 @@ class Settings(BaseSettings):
217
217
_initialized : bool = False
218
218
219
219
# pylint: disable=too-many-branches,too-many-statements
220
- def __init__ (self , ** data : Any ):
220
+ def __init__ (self , ** data : Any ): # noqa: C901
221
221
super ().__init__ (** data )
222
222
if not Services .enabled (Services .AWS_CLI ):
223
223
self ._initialized = True
@@ -240,14 +240,12 @@ def __init__(self, **data: Any):
240
240
logger .info ("running inside GitHub Actions" )
241
241
aws_access_key_id = os .environ .get ("AWS_ACCESS_KEY_ID" , None )
242
242
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 :
244
244
raise OpenAIAPIConfigurationError (
245
245
"required environment variable(s) AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY not set"
246
246
)
247
- self ._aws_access_key_id_source = "environ"
248
- self ._aws_secret_access_key_source = "environ"
249
247
region_name = os .environ .get ("AWS_REGION" , None )
250
- if not region_name :
248
+ if not region_name and not self . aws_profile :
251
249
raise OpenAIAPIConfigurationError ("required environment variable AWS_REGION not set" )
252
250
try :
253
251
self ._aws_session = boto3 .Session (
@@ -259,11 +257,17 @@ def __init__(self, **data: Any):
259
257
self ._aws_access_key_id_source = "environ"
260
258
self ._aws_secret_access_key_source = "environ"
261
259
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 )
263
263
264
- if self .aws_profile and self . _aws_access_key_id_source == "unset" :
264
+ if self .aws_profile :
265
265
self ._aws_access_key_id_source = "aws_profile"
266
266
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
+
267
271
self ._initialized = True
268
272
269
273
if not self .initialized :
@@ -392,6 +396,7 @@ def aws_auth(self) -> dict:
392
396
}
393
397
if self .init_info :
394
398
retval ["init_info" ] = self .init_info
399
+ return retval
395
400
396
401
@property
397
402
def aws_session (self ):
@@ -702,4 +707,25 @@ def check_pinecone_api_key(cls, v) -> SecretStr:
702
707
return v
703
708
704
709
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