provider_entities.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 SystemConfiguration(BaseModel):
  53. """
  54. Model class for provider system configuration.
  55. """
  56. enabled: bool
  57. current_quota_type: Optional[ProviderQuotaType] = None
  58. quota_configurations: list[QuotaConfiguration] = []
  59. credentials: Optional[dict] = None
  60. class CustomProviderConfiguration(BaseModel):
  61. """
  62. Model class for provider custom configuration.
  63. """
  64. credentials: dict
  65. class CustomModelConfiguration(BaseModel):
  66. """
  67. Model class for provider custom model configuration.
  68. """
  69. model: str
  70. model_type: ModelType
  71. credentials: dict
  72. # pydantic configs
  73. model_config = ConfigDict(protected_namespaces=())
  74. class CustomConfiguration(BaseModel):
  75. """
  76. Model class for provider custom configuration.
  77. """
  78. provider: Optional[CustomProviderConfiguration] = None
  79. models: list[CustomModelConfiguration] = []
  80. class ModelLoadBalancingConfiguration(BaseModel):
  81. """
  82. Class for model load balancing configuration.
  83. """
  84. id: str
  85. name: str
  86. credentials: dict
  87. class ModelSettings(BaseModel):
  88. """
  89. Model class for model settings.
  90. """
  91. model: str
  92. model_type: ModelType
  93. enabled: bool = True
  94. load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
  95. # pydantic configs
  96. model_config = ConfigDict(protected_namespaces=())
  97. class BasicProviderConfig(BaseModel):
  98. """
  99. Base model class for common provider settings like credentials
  100. """
  101. class Type(Enum):
  102. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  103. TEXT_INPUT = CommonParameterType.TEXT_INPUT.value
  104. SELECT = CommonParameterType.SELECT.value
  105. BOOLEAN = CommonParameterType.BOOLEAN.value
  106. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  107. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
  108. @classmethod
  109. def value_of(cls, value: str) -> "ProviderConfig.Type":
  110. """
  111. Get value of given mode.
  112. :param value: mode value
  113. :return: mode
  114. """
  115. for mode in cls:
  116. if mode.value == value:
  117. return mode
  118. raise ValueError(f"invalid mode value {value}")
  119. type: Type = Field(..., description="The type of the credentials")
  120. name: str = Field(..., description="The name of the credentials")
  121. class ProviderConfig(BasicProviderConfig):
  122. """
  123. Model class for common provider settings like credentials
  124. """
  125. class Option(BaseModel):
  126. value: str = Field(..., description="The value of the option")
  127. label: I18nObject = Field(..., description="The label of the option")
  128. scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
  129. required: bool = False
  130. default: Optional[Union[int, str]] = None
  131. options: Optional[list[Option]] = None
  132. label: Optional[I18nObject] = None
  133. help: Optional[I18nObject] = None
  134. url: Optional[str] = None
  135. placeholder: Optional[I18nObject] = None
  136. def to_basic_provider_config(self) -> BasicProviderConfig:
  137. return BasicProviderConfig(type=self.type, name=self.name)