Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

provider_entities.py 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. # pydantic configs
  85. model_config = ConfigDict(protected_namespaces=())
  86. class CustomConfiguration(BaseModel):
  87. """
  88. Model class for provider custom configuration.
  89. """
  90. provider: Optional[CustomProviderConfiguration] = None
  91. models: list[CustomModelConfiguration] = []
  92. class ModelLoadBalancingConfiguration(BaseModel):
  93. """
  94. Class for model load balancing configuration.
  95. """
  96. id: str
  97. name: str
  98. credentials: dict
  99. credential_source_type: str | None = None
  100. class ModelSettings(BaseModel):
  101. """
  102. Model class for model settings.
  103. """
  104. model: str
  105. model_type: ModelType
  106. enabled: bool = True
  107. load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
  108. # pydantic configs
  109. model_config = ConfigDict(protected_namespaces=())
  110. class BasicProviderConfig(BaseModel):
  111. """
  112. Base model class for common provider settings like credentials
  113. """
  114. class Type(Enum):
  115. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  116. TEXT_INPUT = CommonParameterType.TEXT_INPUT.value
  117. SELECT = CommonParameterType.SELECT.value
  118. BOOLEAN = CommonParameterType.BOOLEAN.value
  119. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  120. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
  121. TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR.value
  122. @classmethod
  123. def value_of(cls, value: str) -> "ProviderConfig.Type":
  124. """
  125. Get value of given mode.
  126. :param value: mode value
  127. :return: mode
  128. """
  129. for mode in cls:
  130. if mode.value == value:
  131. return mode
  132. raise ValueError(f"invalid mode value {value}")
  133. type: Type = Field(..., description="The type of the credentials")
  134. name: str = Field(..., description="The name of the credentials")
  135. class ProviderConfig(BasicProviderConfig):
  136. """
  137. Model class for common provider settings like credentials
  138. """
  139. class Option(BaseModel):
  140. value: str = Field(..., description="The value of the option")
  141. label: I18nObject = Field(..., description="The label of the option")
  142. scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
  143. required: bool = False
  144. default: Optional[Union[int, str, float, bool]] = None
  145. options: Optional[list[Option]] = None
  146. label: Optional[I18nObject] = None
  147. help: Optional[I18nObject] = None
  148. url: Optional[str] = None
  149. placeholder: Optional[I18nObject] = None
  150. def to_basic_provider_config(self) -> BasicProviderConfig:
  151. return BasicProviderConfig(type=self.type, name=self.name)