55from json import JSONDecodeError
66from typing import Any , cast
77
8+ import sqlalchemy .orm as sa_orm
89from sqlalchemy import select
910from sqlalchemy .exc import IntegrityError
1011from sqlalchemy .orm import Session
@@ -100,8 +101,9 @@ def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
100101 :param tenant_id:
101102 :return:
102103 """
104+ session = db .session
103105 # Get all provider records of the workspace
104- provider_name_to_provider_records_dict = self ._get_all_providers (tenant_id )
106+ provider_name_to_provider_records_dict = self ._get_all_providers (session , tenant_id )
105107
106108 # Initialize trial provider records if not exist
107109 provider_name_to_provider_records_dict = self ._init_trial_provider_records (
@@ -118,7 +120,7 @@ def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
118120 ]
119121
120122 # Get all provider model records of the workspace
121- provider_name_to_provider_model_records_dict = self ._get_all_provider_models (tenant_id )
123+ provider_name_to_provider_model_records_dict = self ._get_all_provider_models (session , tenant_id )
122124 for provider_name in list (provider_name_to_provider_model_records_dict .keys ()):
123125 provider_id = ModelProviderID (provider_name )
124126 if str (provider_id ) not in provider_name_to_provider_model_records_dict :
@@ -131,7 +133,9 @@ def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
131133 provider_entities = model_provider_factory .get_providers ()
132134
133135 # Get All preferred provider types of the workspace
134- provider_name_to_preferred_model_provider_records_dict = self ._get_all_preferred_model_providers (tenant_id )
136+ provider_name_to_preferred_model_provider_records_dict = self ._get_all_preferred_model_providers (
137+ session , tenant_id
138+ )
135139 # Ensure that both the original provider name and its ModelProviderID string representation
136140 # are present in the dictionary to handle cases where either form might be used
137141 for provider_name in list (provider_name_to_preferred_model_provider_records_dict .keys ()):
@@ -143,15 +147,15 @@ def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
143147 )
144148
145149 # Get All provider model settings
146- provider_name_to_provider_model_settings_dict = self ._get_all_provider_model_settings (tenant_id )
150+ provider_name_to_provider_model_settings_dict = self ._get_all_provider_model_settings (session , tenant_id )
147151
148152 # Get All load balancing configs
149153 provider_name_to_provider_load_balancing_model_configs_dict = self ._get_all_provider_load_balancing_configs (
150- tenant_id
154+ session , tenant_id
151155 )
152156
153157 # Get All provider model credentials
154- provider_name_to_provider_model_credentials_dict = self ._get_all_provider_model_credentials (tenant_id )
158+ provider_name_to_provider_model_credentials_dict = self ._get_all_provider_model_credentials (session , tenant_id )
155159
156160 provider_configurations = ProviderConfigurations (tenant_id = tenant_id )
157161
@@ -403,88 +407,91 @@ def update_default_model_record(
403407 return default_model
404408
405409 @staticmethod
406- def _get_all_providers (tenant_id : str ) -> dict [str , list [Provider ]]:
410+ def _get_all_providers (session : sa_orm . scoped_session [ Any ], tenant_id : str ) -> dict [str , list [Provider ]]:
407411 provider_name_to_provider_records_dict = defaultdict (list )
408- with Session (db .engine , expire_on_commit = False ) as session :
409- stmt = select (Provider ).where (Provider .tenant_id == tenant_id , Provider .is_valid == True )
410- providers = session .scalars (stmt )
411- for provider in providers :
412- # Use provider name with prefix after the data migration
413- provider_name_to_provider_records_dict [str (ModelProviderID (provider .provider_name ))].append (provider )
412+ stmt = select (Provider ).where (Provider .tenant_id == tenant_id , Provider .is_valid == True )
413+ providers = session .scalars (stmt )
414+ for provider in providers :
415+ # Use provider name with prefix after the data migration
416+ provider_name_to_provider_records_dict [str (ModelProviderID (provider .provider_name ))].append (provider )
414417 return provider_name_to_provider_records_dict
415418
416419 @staticmethod
417- def _get_all_provider_models (tenant_id : str ) -> dict [str , list [ProviderModel ]]:
420+ def _get_all_provider_models (
421+ session : sa_orm .scoped_session [Any ], tenant_id : str ) -> dict [str , list [ProviderModel ]]:
418422 """
419423 Get all provider model records of the workspace.
420424
421425 :param tenant_id: workspace id
422426 :return:
423427 """
424428 provider_name_to_provider_model_records_dict = defaultdict (list )
425- with Session (db .engine , expire_on_commit = False ) as session :
426- stmt = select (ProviderModel ).where (ProviderModel .tenant_id == tenant_id , ProviderModel .is_valid == True )
427- provider_models = session .scalars (stmt )
428- for provider_model in provider_models :
429- provider_name_to_provider_model_records_dict [provider_model .provider_name ].append (provider_model )
429+ stmt = select (ProviderModel ).where (ProviderModel .tenant_id == tenant_id , ProviderModel .is_valid == True )
430+ provider_models = session .scalars (stmt )
431+ for provider_model in provider_models :
432+ provider_name_to_provider_model_records_dict [provider_model .provider_name ].append (provider_model )
430433 return provider_name_to_provider_model_records_dict
431434
432435 @staticmethod
433- def _get_all_preferred_model_providers (tenant_id : str ) -> dict [str , TenantPreferredModelProvider ]:
436+ def _get_all_preferred_model_providers (
437+ session : sa_orm .scoped_session [Any ], tenant_id : str ) -> dict [str , TenantPreferredModelProvider ]:
434438 """
435439 Get All preferred provider types of the workspace.
436440
437441 :param tenant_id: workspace id
438442 :return:
439443 """
440444 provider_name_to_preferred_provider_type_records_dict = {}
441- with Session (db .engine , expire_on_commit = False ) as session :
442- stmt = select (TenantPreferredModelProvider ).where (TenantPreferredModelProvider .tenant_id == tenant_id )
443- preferred_provider_types = session .scalars (stmt )
444- provider_name_to_preferred_provider_type_records_dict = {
445- preferred_provider_type .provider_name : preferred_provider_type
446- for preferred_provider_type in preferred_provider_types
447- }
445+ stmt = select (TenantPreferredModelProvider ).where (TenantPreferredModelProvider .tenant_id == tenant_id )
446+ preferred_provider_types = session .scalars (stmt )
447+ provider_name_to_preferred_provider_type_records_dict = {
448+ preferred_provider_type .provider_name : preferred_provider_type
449+ for preferred_provider_type in preferred_provider_types
450+ }
448451 return provider_name_to_preferred_provider_type_records_dict
449452
450453 @staticmethod
451- def _get_all_provider_model_settings (tenant_id : str ) -> dict [str , list [ProviderModelSetting ]]:
454+ def _get_all_provider_model_settings (
455+ session : sa_orm .scoped_session [Any ], tenant_id : str ) -> dict [str , list [ProviderModelSetting ]]:
452456 """
453457 Get All provider model settings of the workspace.
454458
455459 :param tenant_id: workspace id
456460 :return:
457461 """
458462 provider_name_to_provider_model_settings_dict = defaultdict (list )
459- with Session ( db . engine , expire_on_commit = False ) as session :
460- stmt = select ( ProviderModelSetting ). where ( ProviderModelSetting . tenant_id == tenant_id )
461- provider_model_settings = session . scalars ( stmt )
462- for provider_model_setting in provider_model_settings :
463- provider_name_to_provider_model_settings_dict [ provider_model_setting . provider_name ]. append (
464- provider_model_setting
465- )
463+ stmt = select ( ProviderModelSetting ). where ( ProviderModelSetting . tenant_id == tenant_id )
464+ provider_model_settings = session . scalars ( stmt )
465+ for provider_model_setting in provider_model_settings :
466+ provider_name_to_provider_model_settings_dict [ provider_model_setting . provider_name ]. append (
467+ provider_model_setting
468+ )
469+
466470 return provider_name_to_provider_model_settings_dict
467471
468472 @staticmethod
469- def _get_all_provider_model_credentials (tenant_id : str ) -> dict [str , list [ProviderModelCredential ]]:
473+ def _get_all_provider_model_credentials (
474+ session : sa_orm .scoped_session [Any ], tenant_id : str
475+ ) -> dict [str , list [ProviderModelCredential ]]:
470476 """
471477 Get All provider model credentials of the workspace.
472478
473479 :param tenant_id: workspace id
474480 :return:
475481 """
476482 provider_name_to_provider_model_credentials_dict = defaultdict (list )
477- with Session (db .engine , expire_on_commit = False ) as session :
478- stmt = select (ProviderModelCredential ).where (ProviderModelCredential .tenant_id == tenant_id )
479- provider_model_credentials = session .scalars (stmt )
480- for provider_model_credential in provider_model_credentials :
481- provider_name_to_provider_model_credentials_dict [provider_model_credential .provider_name ].append (
482- provider_model_credential
483- )
483+ stmt = select (ProviderModelCredential ).where (ProviderModelCredential .tenant_id == tenant_id )
484+ provider_model_credentials = session .scalars (stmt )
485+ for provider_model_credential in provider_model_credentials :
486+ provider_name_to_provider_model_credentials_dict [provider_model_credential .provider_name ].append (
487+ provider_model_credential
488+ )
484489 return provider_name_to_provider_model_credentials_dict
485490
486491 @staticmethod
487- def _get_all_provider_load_balancing_configs (tenant_id : str ) -> dict [str , list [LoadBalancingModelConfig ]]:
492+ def _get_all_provider_load_balancing_configs (
493+ session : sa_orm .scoped_session [Any ], tenant_id : str
494+ ) -> dict [str , list [LoadBalancingModelConfig ]]:
488495 """
489496 Get All provider load balancing configs of the workspace.
490497
@@ -504,13 +511,12 @@ def _get_all_provider_load_balancing_configs(tenant_id: str) -> dict[str, list[L
504511 return {}
505512
506513 provider_name_to_provider_load_balancing_model_configs_dict = defaultdict (list )
507- with Session (db .engine , expire_on_commit = False ) as session :
508- stmt = select (LoadBalancingModelConfig ).where (LoadBalancingModelConfig .tenant_id == tenant_id )
509- provider_load_balancing_configs = session .scalars (stmt )
510- for provider_load_balancing_config in provider_load_balancing_configs :
511- provider_name_to_provider_load_balancing_model_configs_dict [
512- provider_load_balancing_config .provider_name
513- ].append (provider_load_balancing_config )
514+ stmt = select (LoadBalancingModelConfig ).where (LoadBalancingModelConfig .tenant_id == tenant_id )
515+ provider_load_balancing_configs = session .scalars (stmt )
516+ for provider_load_balancing_config in provider_load_balancing_configs :
517+ provider_name_to_provider_load_balancing_model_configs_dict [
518+ provider_load_balancing_config .provider_name
519+ ].append (provider_load_balancing_config )
514520
515521 return provider_name_to_provider_load_balancing_model_configs_dict
516522
0 commit comments