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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 (
  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(Enum):
  13. PAID = "paid"
  14. """hosted paid quota"""
  15. FREE = "free"
  16. """third-party free quota"""
  17. TRIAL = "trial"
  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(Enum):
  26. TIMES = "times"
  27. TOKENS = "tokens"
  28. CREDITS = "credits"
  29. class SystemConfigurationStatus(Enum):
  30. """
  31. Enum class for system configuration status.
  32. """
  33. ACTIVE = "active"
  34. QUOTA_EXCEEDED = "quota-exceeded"
  35. UNSUPPORTED = "unsupported"
  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
  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. class ModelSettings(BaseModel):
  109. """
  110. Model class for model settings.
  111. """
  112. model: str
  113. model_type: ModelType
  114. enabled: bool = True
  115. load_balancing_enabled: bool = False
  116. load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
  117. # pydantic configs
  118. model_config = ConfigDict(protected_namespaces=())
  119. class BasicProviderConfig(BaseModel):
  120. """
  121. Base model class for common provider settings like credentials
  122. """
  123. class Type(Enum):
  124. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  125. TEXT_INPUT = CommonParameterType.TEXT_INPUT.value
  126. SELECT = CommonParameterType.SELECT.value
  127. BOOLEAN = CommonParameterType.BOOLEAN.value
  128. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  129. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
  130. TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR.value
  131. @classmethod
  132. def value_of(cls, value: str) -> "ProviderConfig.Type":
  133. """
  134. Get value of given mode.
  135. :param value: mode value
  136. :return: mode
  137. """
  138. for mode in cls:
  139. if mode.value == value:
  140. return mode
  141. raise ValueError(f"invalid mode value {value}")
  142. type: Type = Field(..., description="The type of the credentials")
  143. name: str = Field(..., description="The name of the credentials")
  144. class ProviderConfig(BasicProviderConfig):
  145. """
  146. Model class for common provider settings like credentials
  147. """
  148. class Option(BaseModel):
  149. value: str = Field(..., description="The value of the option")
  150. label: I18nObject = Field(..., description="The label of the option")
  151. scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
  152. required: bool = False
  153. default: Optional[Union[int, str, float, bool]] = None
  154. options: Optional[list[Option]] = None
  155. label: Optional[I18nObject] = None
  156. help: Optional[I18nObject] = None
  157. url: Optional[str] = None
  158. placeholder: Optional[I18nObject] = None
  159. def to_basic_provider_config(self) -> BasicProviderConfig:
  160. return BasicProviderConfig(type=self.type, name=self.name)