provider_manager.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. import json
  2. from collections import defaultdict
  3. from json import JSONDecodeError
  4. from typing import Optional
  5. from sqlalchemy.exc import IntegrityError
  6. from core.entities.model_entities import DefaultModelEntity, DefaultModelProviderEntity
  7. from core.entities.provider_configuration import ProviderConfigurations, ProviderConfiguration, ProviderModelBundle
  8. from core.entities.provider_entities import CustomConfiguration, CustomProviderConfiguration, CustomModelConfiguration, \
  9. SystemConfiguration, QuotaConfiguration
  10. from core.helper import encrypter
  11. from core.helper.model_provider_cache import ProviderCredentialsCache, ProviderCredentialsCacheType
  12. from core.model_runtime.entities.model_entities import ModelType
  13. from core.model_runtime.entities.provider_entities import ProviderEntity, CredentialFormSchema, FormType
  14. from core.model_runtime.model_providers import model_provider_factory
  15. from extensions import ext_hosting_provider
  16. from extensions.ext_database import db
  17. from models.provider import TenantDefaultModel, Provider, ProviderModel, ProviderQuotaType, ProviderType, \
  18. TenantPreferredModelProvider
  19. class ProviderManager:
  20. """
  21. ProviderManager is a class that manages the model providers includes Hosting and Customize Model Providers.
  22. """
  23. def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
  24. """
  25. Get model provider configurations.
  26. Construct ProviderConfiguration objects for each provider
  27. Including:
  28. 1. Basic information of the provider
  29. 2. Hosting configuration information, including:
  30. (1. Whether to enable (support) hosting type, if enabled, the following information exists
  31. (2. List of hosting type provider configurations
  32. (including quota type, quota limit, current remaining quota, etc.)
  33. (3. The current hosting type in use (whether there is a quota or not)
  34. paid quotas > provider free quotas > hosting trial quotas
  35. (4. Unified credentials for hosting providers
  36. 3. Custom configuration information, including:
  37. (1. Whether to enable (support) custom type, if enabled, the following information exists
  38. (2. Custom provider configuration (including credentials)
  39. (3. List of custom provider model configurations (including credentials)
  40. 4. Hosting/custom preferred provider type.
  41. Provide methods:
  42. - Get the current configuration (including credentials)
  43. - Get the availability and status of the hosting configuration: active available,
  44. quota_exceeded insufficient quota, unsupported hosting
  45. - Get the availability of custom configuration
  46. Custom provider available conditions:
  47. (1. custom provider credentials available
  48. (2. at least one custom model credentials available
  49. - Verify, update, and delete custom provider configuration
  50. - Verify, update, and delete custom provider model configuration
  51. - Get the list of available models (optional provider filtering, model type filtering)
  52. Append custom provider models to the list
  53. - Get provider instance
  54. - Switch selection priority
  55. :param tenant_id:
  56. :return:
  57. """
  58. # Get all provider records of the workspace
  59. provider_name_to_provider_records_dict = self._get_all_providers(tenant_id)
  60. # Initialize trial provider records if not exist
  61. provider_name_to_provider_records_dict = self._init_trial_provider_records(
  62. tenant_id,
  63. provider_name_to_provider_records_dict
  64. )
  65. # Get all provider model records of the workspace
  66. provider_name_to_provider_model_records_dict = self._get_all_provider_models(tenant_id)
  67. # Get all provider entities
  68. provider_entities = model_provider_factory.get_providers()
  69. # Get All preferred provider types of the workspace
  70. provider_name_to_preferred_model_provider_records_dict = self._get_all_preferred_model_providers(tenant_id)
  71. provider_configurations = ProviderConfigurations(
  72. tenant_id=tenant_id
  73. )
  74. # Construct ProviderConfiguration objects for each provider
  75. for provider_entity in provider_entities:
  76. provider_name = provider_entity.provider
  77. provider_records = provider_name_to_provider_records_dict.get(provider_entity.provider)
  78. if not provider_records:
  79. provider_records = []
  80. provider_model_records = provider_name_to_provider_model_records_dict.get(provider_entity.provider)
  81. if not provider_model_records:
  82. provider_model_records = []
  83. # Convert to custom configuration
  84. custom_configuration = self._to_custom_configuration(
  85. tenant_id,
  86. provider_entity,
  87. provider_records,
  88. provider_model_records
  89. )
  90. # Convert to system configuration
  91. system_configuration = self._to_system_configuration(
  92. tenant_id,
  93. provider_entity,
  94. provider_records
  95. )
  96. # Get preferred provider type
  97. preferred_provider_type_record = provider_name_to_preferred_model_provider_records_dict.get(provider_name)
  98. if preferred_provider_type_record:
  99. preferred_provider_type = ProviderType.value_of(preferred_provider_type_record.preferred_provider_type)
  100. else:
  101. if custom_configuration.provider or custom_configuration.models:
  102. preferred_provider_type = ProviderType.CUSTOM
  103. elif system_configuration.enabled:
  104. preferred_provider_type = ProviderType.SYSTEM
  105. else:
  106. preferred_provider_type = ProviderType.CUSTOM
  107. using_provider_type = preferred_provider_type
  108. if preferred_provider_type == ProviderType.SYSTEM:
  109. if not system_configuration.enabled:
  110. using_provider_type = ProviderType.CUSTOM
  111. has_valid_quota = False
  112. for quota_configuration in system_configuration.quota_configurations:
  113. if quota_configuration.is_valid:
  114. has_valid_quota = True
  115. break
  116. if not has_valid_quota:
  117. using_provider_type = ProviderType.CUSTOM
  118. else:
  119. if not custom_configuration.provider and not custom_configuration.models:
  120. if system_configuration.enabled:
  121. has_valid_quota = False
  122. for quota_configuration in system_configuration.quota_configurations:
  123. if quota_configuration.is_valid:
  124. has_valid_quota = True
  125. break
  126. if has_valid_quota:
  127. using_provider_type = ProviderType.SYSTEM
  128. provider_configuration = ProviderConfiguration(
  129. tenant_id=tenant_id,
  130. provider=provider_entity,
  131. preferred_provider_type=preferred_provider_type,
  132. using_provider_type=using_provider_type,
  133. system_configuration=system_configuration,
  134. custom_configuration=custom_configuration
  135. )
  136. provider_configurations[provider_name] = provider_configuration
  137. # Return the encapsulated object
  138. return provider_configurations
  139. def get_provider_model_bundle(self, tenant_id: str, provider: str, model_type: ModelType) -> ProviderModelBundle:
  140. """
  141. Get provider model bundle.
  142. :param tenant_id: workspace id
  143. :param provider: provider name
  144. :param model_type: model type
  145. :return:
  146. """
  147. provider_configurations = self.get_configurations(tenant_id)
  148. # get provider instance
  149. provider_configuration = provider_configurations.get(provider)
  150. if not provider_configuration:
  151. raise ValueError(f"Provider {provider} does not exist.")
  152. provider_instance = provider_configuration.get_provider_instance()
  153. model_type_instance = provider_instance.get_model_instance(model_type)
  154. return ProviderModelBundle(
  155. configuration=provider_configuration,
  156. provider_instance=provider_instance,
  157. model_type_instance=model_type_instance
  158. )
  159. def get_default_model(self, tenant_id: str, model_type: ModelType) -> Optional[DefaultModelEntity]:
  160. """
  161. Get default model.
  162. :param tenant_id: workspace id
  163. :param model_type: model type
  164. :return:
  165. """
  166. # Get the corresponding TenantDefaultModel record
  167. default_model = db.session.query(TenantDefaultModel) \
  168. .filter(
  169. TenantDefaultModel.tenant_id == tenant_id,
  170. TenantDefaultModel.model_type == model_type.to_origin_model_type()
  171. ).first()
  172. # If it does not exist, get the first available provider model from get_configurations
  173. # and update the TenantDefaultModel record
  174. if not default_model:
  175. # Get provider configurations
  176. provider_configurations = self.get_configurations(tenant_id)
  177. # get available models from provider_configurations
  178. available_models = provider_configurations.get_models(
  179. model_type=model_type,
  180. only_active=True
  181. )
  182. if available_models:
  183. available_model = available_models[0]
  184. default_model = TenantDefaultModel(
  185. tenant_id=tenant_id,
  186. model_type=model_type.to_origin_model_type(),
  187. provider_name=available_model.provider.provider,
  188. model_name=available_model.model
  189. )
  190. db.session.add(default_model)
  191. db.session.commit()
  192. if not default_model:
  193. return None
  194. provider_instance = model_provider_factory.get_provider_instance(default_model.provider_name)
  195. return DefaultModelEntity(
  196. model=default_model.model_name,
  197. model_type=model_type,
  198. provider=DefaultModelProviderEntity(**provider_instance.get_provider_schema().to_simple_provider().dict())
  199. )
  200. def update_default_model_record(self, tenant_id: str, model_type: ModelType, provider: str, model: str) \
  201. -> TenantDefaultModel:
  202. """
  203. Update default model record.
  204. :param tenant_id: workspace id
  205. :param model_type: model type
  206. :param provider: provider name
  207. :param model: model name
  208. :return:
  209. """
  210. provider_configurations = self.get_configurations(tenant_id)
  211. if provider not in provider_configurations:
  212. raise ValueError(f"Provider {provider} does not exist.")
  213. # get available models from provider_configurations
  214. available_models = provider_configurations.get_models(
  215. model_type=model_type,
  216. only_active=True
  217. )
  218. # check if the model is exist in available models
  219. model_names = [model.model for model in available_models]
  220. if model not in model_names:
  221. raise ValueError(f"Model {model} does not exist.")
  222. # Get the list of available models from get_configurations and check if it is LLM
  223. default_model = db.session.query(TenantDefaultModel) \
  224. .filter(
  225. TenantDefaultModel.tenant_id == tenant_id,
  226. TenantDefaultModel.model_type == model_type.to_origin_model_type()
  227. ).first()
  228. # create or update TenantDefaultModel record
  229. if default_model:
  230. # update default model
  231. default_model.provider_name = provider
  232. default_model.model_name = model
  233. db.session.commit()
  234. else:
  235. # create default model
  236. default_model = TenantDefaultModel(
  237. tenant_id=tenant_id,
  238. model_type=model_type.value,
  239. provider_name=provider,
  240. model_name=model,
  241. )
  242. db.session.add(default_model)
  243. db.session.commit()
  244. return default_model
  245. def _get_all_providers(self, tenant_id: str) -> dict[str, list[Provider]]:
  246. """
  247. Get all provider records of the workspace.
  248. :param tenant_id: workspace id
  249. :return:
  250. """
  251. providers = db.session.query(Provider) \
  252. .filter(
  253. Provider.tenant_id == tenant_id,
  254. Provider.is_valid == True
  255. ).all()
  256. provider_name_to_provider_records_dict = defaultdict(list)
  257. for provider in providers:
  258. provider_name_to_provider_records_dict[provider.provider_name].append(provider)
  259. return provider_name_to_provider_records_dict
  260. def _get_all_provider_models(self, tenant_id: str) -> dict[str, list[ProviderModel]]:
  261. """
  262. Get all provider model records of the workspace.
  263. :param tenant_id: workspace id
  264. :return:
  265. """
  266. # Get all provider model records of the workspace
  267. provider_models = db.session.query(ProviderModel) \
  268. .filter(
  269. ProviderModel.tenant_id == tenant_id,
  270. ProviderModel.is_valid == True
  271. ).all()
  272. provider_name_to_provider_model_records_dict = defaultdict(list)
  273. for provider_model in provider_models:
  274. provider_name_to_provider_model_records_dict[provider_model.provider_name].append(provider_model)
  275. return provider_name_to_provider_model_records_dict
  276. def _get_all_preferred_model_providers(self, tenant_id: str) -> dict[str, TenantPreferredModelProvider]:
  277. """
  278. Get All preferred provider types of the workspace.
  279. :param tenant_id:
  280. :return:
  281. """
  282. preferred_provider_types = db.session.query(TenantPreferredModelProvider) \
  283. .filter(
  284. TenantPreferredModelProvider.tenant_id == tenant_id
  285. ).all()
  286. provider_name_to_preferred_provider_type_records_dict = {
  287. preferred_provider_type.provider_name: preferred_provider_type
  288. for preferred_provider_type in preferred_provider_types
  289. }
  290. return provider_name_to_preferred_provider_type_records_dict
  291. def _init_trial_provider_records(self, tenant_id: str,
  292. provider_name_to_provider_records_dict: dict[str, list]) -> dict[str, list]:
  293. """
  294. Initialize trial provider records if not exists.
  295. :param tenant_id: workspace id
  296. :param provider_name_to_provider_records_dict: provider name to provider records dict
  297. :return:
  298. """
  299. # Get hosting configuration
  300. hosting_configuration = ext_hosting_provider.hosting_configuration
  301. for provider_name, configuration in hosting_configuration.provider_map.items():
  302. if not configuration.enabled:
  303. continue
  304. provider_records = provider_name_to_provider_records_dict.get(provider_name)
  305. if not provider_records:
  306. provider_records = []
  307. provider_quota_to_provider_record_dict = dict()
  308. for provider_record in provider_records:
  309. if provider_record.provider_type != ProviderType.SYSTEM.value:
  310. continue
  311. provider_quota_to_provider_record_dict[ProviderQuotaType.value_of(provider_record.quota_type)] \
  312. = provider_record
  313. for quota in configuration.quotas:
  314. if quota.quota_type == ProviderQuotaType.TRIAL:
  315. # Init trial provider records if not exists
  316. if ProviderQuotaType.TRIAL not in provider_quota_to_provider_record_dict:
  317. try:
  318. provider_record = Provider(
  319. tenant_id=tenant_id,
  320. provider_name=provider_name,
  321. provider_type=ProviderType.SYSTEM.value,
  322. quota_type=ProviderQuotaType.TRIAL.value,
  323. quota_limit=quota.quota_limit,
  324. quota_used=0,
  325. is_valid=True
  326. )
  327. db.session.add(provider_record)
  328. db.session.commit()
  329. except IntegrityError:
  330. db.session.rollback()
  331. provider_record = db.session.query(Provider) \
  332. .filter(
  333. Provider.tenant_id == tenant_id,
  334. Provider.provider_name == provider_name,
  335. Provider.provider_type == ProviderType.SYSTEM.value,
  336. Provider.quota_type == ProviderQuotaType.TRIAL.value
  337. ).first()
  338. if provider_record and not provider_record.is_valid:
  339. provider_record.is_valid = True
  340. db.session.commit()
  341. provider_name_to_provider_records_dict[provider_name].append(provider_record)
  342. return provider_name_to_provider_records_dict
  343. def _to_custom_configuration(self,
  344. tenant_id: str,
  345. provider_entity: ProviderEntity,
  346. provider_records: list[Provider],
  347. provider_model_records: list[ProviderModel]) -> CustomConfiguration:
  348. """
  349. Convert to custom configuration.
  350. :param tenant_id: workspace id
  351. :param provider_entity: provider entity
  352. :param provider_records: provider records
  353. :param provider_model_records: provider model records
  354. :return:
  355. """
  356. # Get provider credential secret variables
  357. provider_credential_secret_variables = self._extract_secret_variables(
  358. provider_entity.provider_credential_schema.credential_form_schemas
  359. if provider_entity.provider_credential_schema else []
  360. )
  361. # Get custom provider record
  362. custom_provider_record = None
  363. for provider_record in provider_records:
  364. if provider_record.provider_type == ProviderType.SYSTEM.value:
  365. continue
  366. if not provider_record.encrypted_config:
  367. continue
  368. custom_provider_record = provider_record
  369. # Get custom provider credentials
  370. custom_provider_configuration = None
  371. if custom_provider_record:
  372. provider_credentials_cache = ProviderCredentialsCache(
  373. tenant_id=tenant_id,
  374. identity_id=custom_provider_record.id,
  375. cache_type=ProviderCredentialsCacheType.PROVIDER
  376. )
  377. # Get cached provider credentials
  378. cached_provider_credentials = provider_credentials_cache.get()
  379. if not cached_provider_credentials:
  380. try:
  381. # fix origin data
  382. if (custom_provider_record.encrypted_config
  383. and not custom_provider_record.encrypted_config.startswith("{")):
  384. provider_credentials = {
  385. "openai_api_key": custom_provider_record.encrypted_config
  386. }
  387. else:
  388. provider_credentials = json.loads(custom_provider_record.encrypted_config)
  389. except JSONDecodeError:
  390. provider_credentials = {}
  391. # Get decoding rsa key and cipher for decrypting credentials
  392. decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
  393. for variable in provider_credential_secret_variables:
  394. if variable in provider_credentials:
  395. try:
  396. provider_credentials[variable] = encrypter.decrypt_token_with_decoding(
  397. provider_credentials.get(variable),
  398. decoding_rsa_key,
  399. decoding_cipher_rsa
  400. )
  401. except ValueError:
  402. pass
  403. # cache provider credentials
  404. provider_credentials_cache.set(
  405. credentials=provider_credentials
  406. )
  407. else:
  408. provider_credentials = cached_provider_credentials
  409. custom_provider_configuration = CustomProviderConfiguration(
  410. credentials=provider_credentials
  411. )
  412. # Get provider model credential secret variables
  413. model_credential_secret_variables = self._extract_secret_variables(
  414. provider_entity.model_credential_schema.credential_form_schemas
  415. if provider_entity.model_credential_schema else []
  416. )
  417. # Get custom provider model credentials
  418. custom_model_configurations = []
  419. for provider_model_record in provider_model_records:
  420. if not provider_model_record.encrypted_config:
  421. continue
  422. provider_model_credentials_cache = ProviderCredentialsCache(
  423. tenant_id=tenant_id,
  424. identity_id=provider_model_record.id,
  425. cache_type=ProviderCredentialsCacheType.MODEL
  426. )
  427. # Get cached provider model credentials
  428. cached_provider_model_credentials = provider_model_credentials_cache.get()
  429. if not cached_provider_model_credentials:
  430. try:
  431. provider_model_credentials = json.loads(provider_model_record.encrypted_config)
  432. except JSONDecodeError:
  433. continue
  434. # Get decoding rsa key and cipher for decrypting credentials
  435. decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
  436. for variable in model_credential_secret_variables:
  437. if variable in provider_model_credentials:
  438. try:
  439. provider_model_credentials[variable] = encrypter.decrypt_token_with_decoding(
  440. provider_model_credentials.get(variable),
  441. decoding_rsa_key,
  442. decoding_cipher_rsa
  443. )
  444. except ValueError:
  445. pass
  446. # cache provider model credentials
  447. provider_model_credentials_cache.set(
  448. credentials=provider_model_credentials
  449. )
  450. else:
  451. provider_model_credentials = cached_provider_model_credentials
  452. custom_model_configurations.append(
  453. CustomModelConfiguration(
  454. model=provider_model_record.model_name,
  455. model_type=ModelType.value_of(provider_model_record.model_type),
  456. credentials=provider_model_credentials
  457. )
  458. )
  459. return CustomConfiguration(
  460. provider=custom_provider_configuration,
  461. models=custom_model_configurations
  462. )
  463. def _to_system_configuration(self,
  464. tenant_id: str,
  465. provider_entity: ProviderEntity,
  466. provider_records: list[Provider]) -> SystemConfiguration:
  467. """
  468. Convert to system configuration.
  469. :param tenant_id: workspace id
  470. :param provider_entity: provider entity
  471. :param provider_records: provider records
  472. :return:
  473. """
  474. # Get hosting configuration
  475. hosting_configuration = ext_hosting_provider.hosting_configuration
  476. if provider_entity.provider not in hosting_configuration.provider_map \
  477. or not hosting_configuration.provider_map.get(provider_entity.provider).enabled:
  478. return SystemConfiguration(
  479. enabled=False
  480. )
  481. provider_hosting_configuration = hosting_configuration.provider_map.get(provider_entity.provider)
  482. # Convert provider_records to dict
  483. quota_type_to_provider_records_dict = dict()
  484. for provider_record in provider_records:
  485. if provider_record.provider_type != ProviderType.SYSTEM.value:
  486. continue
  487. quota_type_to_provider_records_dict[ProviderQuotaType.value_of(provider_record.quota_type)] \
  488. = provider_record
  489. quota_configurations = []
  490. for provider_quota in provider_hosting_configuration.quotas:
  491. if provider_quota.quota_type not in quota_type_to_provider_records_dict:
  492. continue
  493. provider_record = quota_type_to_provider_records_dict[provider_quota.quota_type]
  494. quota_configuration = QuotaConfiguration(
  495. quota_type=provider_quota.quota_type,
  496. quota_unit=provider_hosting_configuration.quota_unit,
  497. quota_used=provider_record.quota_used,
  498. quota_limit=provider_record.quota_limit,
  499. is_valid=provider_record.quota_limit > provider_record.quota_used or provider_record.quota_limit == -1,
  500. restrict_llms=provider_quota.restrict_llms
  501. )
  502. quota_configurations.append(quota_configuration)
  503. if len(quota_configurations) == 0:
  504. return SystemConfiguration(
  505. enabled=False
  506. )
  507. current_quota_type = self._choice_current_using_quota_type(quota_configurations)
  508. current_using_credentials = provider_hosting_configuration.credentials
  509. if current_quota_type == ProviderQuotaType.FREE:
  510. provider_record = quota_type_to_provider_records_dict.get(current_quota_type)
  511. if provider_record:
  512. provider_credentials_cache = ProviderCredentialsCache(
  513. tenant_id=tenant_id,
  514. identity_id=provider_record.id,
  515. cache_type=ProviderCredentialsCacheType.PROVIDER
  516. )
  517. # Get cached provider credentials
  518. cached_provider_credentials = provider_credentials_cache.get()
  519. if not cached_provider_credentials:
  520. try:
  521. provider_credentials = json.loads(provider_record.encrypted_config)
  522. except JSONDecodeError:
  523. provider_credentials = {}
  524. # Get provider credential secret variables
  525. provider_credential_secret_variables = self._extract_secret_variables(
  526. provider_entity.provider_credential_schema.credential_form_schemas
  527. if provider_entity.provider_credential_schema else []
  528. )
  529. # Get decoding rsa key and cipher for decrypting credentials
  530. decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
  531. for variable in provider_credential_secret_variables:
  532. if variable in provider_credentials:
  533. try:
  534. provider_credentials[variable] = encrypter.decrypt_token_with_decoding(
  535. provider_credentials.get(variable),
  536. decoding_rsa_key,
  537. decoding_cipher_rsa
  538. )
  539. except ValueError:
  540. pass
  541. current_using_credentials = provider_credentials
  542. # cache provider credentials
  543. provider_credentials_cache.set(
  544. credentials=current_using_credentials
  545. )
  546. else:
  547. current_using_credentials = cached_provider_credentials
  548. else:
  549. current_using_credentials = {}
  550. return SystemConfiguration(
  551. enabled=True,
  552. current_quota_type=current_quota_type,
  553. quota_configurations=quota_configurations,
  554. credentials=current_using_credentials
  555. )
  556. def _choice_current_using_quota_type(self, quota_configurations: list[QuotaConfiguration]) -> ProviderQuotaType:
  557. """
  558. Choice current using quota type.
  559. paid quotas > provider free quotas > hosting trial quotas
  560. If there is still quota for the corresponding quota type according to the sorting,
  561. :param quota_configurations:
  562. :return:
  563. """
  564. # convert to dict
  565. quota_type_to_quota_configuration_dict = {
  566. quota_configuration.quota_type: quota_configuration
  567. for quota_configuration in quota_configurations
  568. }
  569. last_quota_configuration = None
  570. for quota_type in [ProviderQuotaType.PAID, ProviderQuotaType.FREE, ProviderQuotaType.TRIAL]:
  571. if quota_type in quota_type_to_quota_configuration_dict:
  572. last_quota_configuration = quota_type_to_quota_configuration_dict[quota_type]
  573. if last_quota_configuration.is_valid:
  574. return quota_type
  575. if last_quota_configuration:
  576. return last_quota_configuration.quota_type
  577. raise ValueError('No quota type available')
  578. def _extract_secret_variables(self, credential_form_schemas: list[CredentialFormSchema]) -> list[str]:
  579. """
  580. Extract secret input form variables.
  581. :param credential_form_schemas:
  582. :return:
  583. """
  584. secret_input_form_variables = []
  585. for credential_form_schema in credential_form_schemas:
  586. if credential_form_schema.type == FormType.SECRET_INPUT:
  587. secret_input_form_variables.append(credential_form_schema.variable)
  588. return secret_input_form_variables