You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

provider_entities.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from enum import StrEnum, auto
  2. from typing import Optional, Union
  3. from pydantic import BaseModel, ConfigDict, Field
  4. from core.entities.parameter_entities import (
  5. AppSelectorScope,
  6. CommonParameterType,
  7. ModelSelectorScope,
  8. ToolSelectorScope,
  9. )
  10. from core.model_runtime.entities.model_entities import ModelType
  11. from core.tools.entities.common_entities import I18nObject
  12. class ProviderQuotaType(StrEnum):
  13. PAID = auto()
  14. """hosted paid quota"""
  15. FREE = auto()
  16. """third-party free quota"""
  17. TRIAL = auto()
  18. """hosted trial quota"""
  19. @staticmethod
  20. def value_of(value):
  21. for member in ProviderQuotaType:
  22. if member.value == value:
  23. return member
  24. raise ValueError(f"No matching enum found for value '{value}'")
  25. class QuotaUnit(StrEnum):
  26. TIMES = auto()
  27. TOKENS = auto()
  28. CREDITS = auto()
  29. class SystemConfigurationStatus(StrEnum):
  30. """
  31. Enum class for system configuration status.
  32. """
  33. ACTIVE = auto()
  34. QUOTA_EXCEEDED = "quota-exceeded"
  35. UNSUPPORTED = auto()
  36. class RestrictModel(BaseModel):
  37. model: str
  38. base_model_name: Optional[str] = None
  39. model_type: ModelType
  40. # pydantic configs
  41. model_config = ConfigDict(protected_namespaces=())
  42. class QuotaConfiguration(BaseModel):
  43. """
  44. Model class for provider quota configuration.
  45. """
  46. quota_type: ProviderQuotaType
  47. quota_unit: QuotaUnit
  48. quota_limit: int
  49. quota_used: int
  50. is_valid: bool
  51. restrict_models: list[RestrictModel] = []
  52. class CredentialConfiguration(BaseModel):
  53. """
  54. Model class for credential configuration.
  55. """
  56. credential_id: str
  57. credential_name: str
  58. class SystemConfiguration(BaseModel):
  59. """
  60. Model class for provider system configuration.
  61. """
  62. enabled: bool
  63. current_quota_type: Optional[ProviderQuotaType] = None
  64. quota_configurations: list[QuotaConfiguration] = []
  65. credentials: Optional[dict] = None
  66. class CustomProviderConfiguration(BaseModel):
  67. """
  68. Model class for provider custom configuration.
  69. """
  70. credentials: dict
  71. current_credential_id: Optional[str] = None
  72. current_credential_name: Optional[str] = None
  73. available_credentials: list[CredentialConfiguration] = []
  74. class CustomModelConfiguration(BaseModel):
  75. """
  76. Model class for provider custom model configuration.
  77. """
  78. model: str
  79. model_type: ModelType
  80. credentials: dict | None = None
  81. current_credential_id: Optional[str] = None
  82. current_credential_name: Optional[str] = None
  83. available_model_credentials: list[CredentialConfiguration] = []
  84. unadded_to_model_list: Optional[bool] = False
  85. # pydantic configs
  86. model_config = ConfigDict(protected_namespaces=())
  87. class UnaddedModelConfiguration(BaseModel):
  88. """
  89. Model class for provider unadded model configuration.
  90. """
  91. model: str
  92. model_type: ModelType
  93. class CustomConfiguration(BaseModel):
  94. """
  95. Model class for provider custom configuration.
  96. """
  97. provider: Optional[CustomProviderConfiguration] = None
  98. models: list[CustomModelConfiguration] = []
  99. can_added_models: list[UnaddedModelConfiguration] = []
  100. class ModelLoadBalancingConfiguration(BaseModel):
  101. """
  102. Class for model load balancing configuration.
  103. """
  104. id: str
  105. name: str
  106. credentials: dict
  107. credential_source_type: str | None = None
  108. credential_id: str | None = None
  109. class ModelSettings(BaseModel):
  110. """
  111. Model class for model settings.
  112. """
  113. model: str
  114. model_type: ModelType
  115. enabled: bool = True
  116. load_balancing_enabled: bool = False
  117. load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
  118. # pydantic configs
  119. model_config = ConfigDict(protected_namespaces=())
  120. class BasicProviderConfig(BaseModel):
  121. """
  122. Base model class for common provider settings like credentials
  123. """
  124. class Type(StrEnum):
  125. SECRET_INPUT = CommonParameterType.SECRET_INPUT
  126. TEXT_INPUT = CommonParameterType.TEXT_INPUT
  127. SELECT = CommonParameterType.SELECT
  128. BOOLEAN = CommonParameterType.BOOLEAN
  129. APP_SELECTOR = CommonParameterType.APP_SELECTOR
  130. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR
  131. TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR
  132. @classmethod
  133. def value_of(cls, value: str) -> "ProviderConfig.Type":
  134. """
  135. Get value of given mode.
  136. :param value: mode value
  137. :return: mode
  138. """
  139. for mode in cls:
  140. if mode.value == value:
  141. return mode
  142. raise ValueError(f"invalid mode value {value}")
  143. type: Type = Field(..., description="The type of the credentials")
  144. name: str = Field(..., description="The name of the credentials")
  145. class ProviderConfig(BasicProviderConfig):
  146. """
  147. Model class for common provider settings like credentials
  148. """
  149. class Option(BaseModel):
  150. value: str = Field(..., description="The value of the option")
  151. label: I18nObject = Field(..., description="The label of the option")
  152. scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
  153. required: bool = False
  154. default: Optional[Union[int, str, float, bool]] = None
  155. options: Optional[list[Option]] = None
  156. label: Optional[I18nObject] = None
  157. help: Optional[I18nObject] = None
  158. url: Optional[str] = None
  159. placeholder: Optional[I18nObject] = None
  160. def to_basic_provider_config(self) -> BasicProviderConfig:
  161. return BasicProviderConfig(type=self.type, name=self.name)