provider_entities.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from enum import Enum
  2. from typing import Optional, Union
  3. from pydantic import BaseModel, ConfigDict, Field
  4. from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelConfigScope
  5. from core.model_runtime.entities.common_entities import I18nObject
  6. from core.model_runtime.entities.model_entities import ModelType
  7. from models.provider import ProviderQuotaType
  8. class QuotaUnit(Enum):
  9. TIMES = 'times'
  10. TOKENS = 'tokens'
  11. CREDITS = 'credits'
  12. class SystemConfigurationStatus(Enum):
  13. """
  14. Enum class for system configuration status.
  15. """
  16. ACTIVE = 'active'
  17. QUOTA_EXCEEDED = 'quota-exceeded'
  18. UNSUPPORTED = 'unsupported'
  19. class RestrictModel(BaseModel):
  20. model: str
  21. base_model_name: Optional[str] = None
  22. model_type: ModelType
  23. # pydantic configs
  24. model_config = ConfigDict(protected_namespaces=())
  25. class QuotaConfiguration(BaseModel):
  26. """
  27. Model class for provider quota configuration.
  28. """
  29. quota_type: ProviderQuotaType
  30. quota_unit: QuotaUnit
  31. quota_limit: int
  32. quota_used: int
  33. is_valid: bool
  34. restrict_models: list[RestrictModel] = []
  35. class SystemConfiguration(BaseModel):
  36. """
  37. Model class for provider system configuration.
  38. """
  39. enabled: bool
  40. current_quota_type: Optional[ProviderQuotaType] = None
  41. quota_configurations: list[QuotaConfiguration] = []
  42. credentials: Optional[dict] = None
  43. class CustomProviderConfiguration(BaseModel):
  44. """
  45. Model class for provider custom configuration.
  46. """
  47. credentials: dict
  48. class CustomModelConfiguration(BaseModel):
  49. """
  50. Model class for provider custom model configuration.
  51. """
  52. model: str
  53. model_type: ModelType
  54. credentials: dict
  55. # pydantic configs
  56. model_config = ConfigDict(protected_namespaces=())
  57. class CustomConfiguration(BaseModel):
  58. """
  59. Model class for provider custom configuration.
  60. """
  61. provider: Optional[CustomProviderConfiguration] = None
  62. models: list[CustomModelConfiguration] = []
  63. class ModelLoadBalancingConfiguration(BaseModel):
  64. """
  65. Class for model load balancing configuration.
  66. """
  67. id: str
  68. name: str
  69. credentials: dict
  70. class ModelSettings(BaseModel):
  71. """
  72. Model class for model settings.
  73. """
  74. model: str
  75. model_type: ModelType
  76. enabled: bool = True
  77. load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
  78. # pydantic configs
  79. model_config = ConfigDict(protected_namespaces=())
  80. class BasicProviderConfig(BaseModel):
  81. """
  82. Base model class for common provider settings like credentials
  83. """
  84. class Type(Enum):
  85. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  86. TEXT_INPUT = CommonParameterType.TEXT_INPUT.value
  87. SELECT = CommonParameterType.SELECT.value
  88. BOOLEAN = CommonParameterType.BOOLEAN.value
  89. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  90. MODEL_CONFIG = CommonParameterType.MODEL_CONFIG.value
  91. @classmethod
  92. def value_of(cls, value: str) -> "ProviderConfig.Type":
  93. """
  94. Get value of given mode.
  95. :param value: mode value
  96. :return: mode
  97. """
  98. for mode in cls:
  99. if mode.value == value:
  100. return mode
  101. raise ValueError(f'invalid mode value {value}')
  102. @staticmethod
  103. def default(value: str) -> str:
  104. return ""
  105. type: Type = Field(..., description="The type of the credentials")
  106. name: str = Field(..., description="The name of the credentials")
  107. class ProviderConfig(BasicProviderConfig):
  108. """
  109. Model class for common provider settings like credentials
  110. """
  111. class Option(BaseModel):
  112. value: str = Field(..., description="The value of the option")
  113. label: I18nObject = Field(..., description="The label of the option")
  114. scope: AppSelectorScope | ModelConfigScope | None
  115. required: bool = False
  116. default: Optional[Union[int, str]] = None
  117. options: Optional[list[Option]] = None
  118. label: Optional[I18nObject] = None
  119. help: Optional[I18nObject] = None
  120. url: Optional[str] = None
  121. placeholder: Optional[I18nObject] = None