model_entities.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. from decimal import Decimal
  2. from enum import Enum, StrEnum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, ConfigDict, model_validator
  5. from core.model_runtime.entities.common_entities import I18nObject
  6. class ModelType(Enum):
  7. """
  8. Enum class for model type.
  9. """
  10. LLM = "llm"
  11. TEXT_EMBEDDING = "text-embedding"
  12. RERANK = "rerank"
  13. SPEECH2TEXT = "speech2text"
  14. MODERATION = "moderation"
  15. TTS = "tts"
  16. @classmethod
  17. def value_of(cls, origin_model_type: str) -> "ModelType":
  18. """
  19. Get model type from origin model type.
  20. :return: model type
  21. """
  22. if origin_model_type in {"text-generation", cls.LLM.value}:
  23. return cls.LLM
  24. elif origin_model_type in {"embeddings", cls.TEXT_EMBEDDING.value}:
  25. return cls.TEXT_EMBEDDING
  26. elif origin_model_type in {"reranking", cls.RERANK.value}:
  27. return cls.RERANK
  28. elif origin_model_type in {"speech2text", cls.SPEECH2TEXT.value}:
  29. return cls.SPEECH2TEXT
  30. elif origin_model_type in {"tts", cls.TTS.value}:
  31. return cls.TTS
  32. elif origin_model_type == cls.MODERATION.value:
  33. return cls.MODERATION
  34. else:
  35. raise ValueError(f"invalid origin model type {origin_model_type}")
  36. def to_origin_model_type(self) -> str:
  37. """
  38. Get origin model type from model type.
  39. :return: origin model type
  40. """
  41. if self == self.LLM:
  42. return "text-generation"
  43. elif self == self.TEXT_EMBEDDING:
  44. return "embeddings"
  45. elif self == self.RERANK:
  46. return "reranking"
  47. elif self == self.SPEECH2TEXT:
  48. return "speech2text"
  49. elif self == self.TTS:
  50. return "tts"
  51. elif self == self.MODERATION:
  52. return "moderation"
  53. else:
  54. raise ValueError(f"invalid model type {self}")
  55. class FetchFrom(Enum):
  56. """
  57. Enum class for fetch from.
  58. """
  59. PREDEFINED_MODEL = "predefined-model"
  60. CUSTOMIZABLE_MODEL = "customizable-model"
  61. class ModelFeature(Enum):
  62. """
  63. Enum class for llm feature.
  64. """
  65. TOOL_CALL = "tool-call"
  66. MULTI_TOOL_CALL = "multi-tool-call"
  67. AGENT_THOUGHT = "agent-thought"
  68. VISION = "vision"
  69. STREAM_TOOL_CALL = "stream-tool-call"
  70. DOCUMENT = "document"
  71. VIDEO = "video"
  72. AUDIO = "audio"
  73. STRUCTURED_OUTPUT = "structured-output"
  74. class DefaultParameterName(StrEnum):
  75. """
  76. Enum class for parameter template variable.
  77. """
  78. TEMPERATURE = "temperature"
  79. TOP_P = "top_p"
  80. TOP_K = "top_k"
  81. PRESENCE_PENALTY = "presence_penalty"
  82. FREQUENCY_PENALTY = "frequency_penalty"
  83. MAX_TOKENS = "max_tokens"
  84. RESPONSE_FORMAT = "response_format"
  85. JSON_SCHEMA = "json_schema"
  86. @classmethod
  87. def value_of(cls, value: Any) -> "DefaultParameterName":
  88. """
  89. Get parameter name from value.
  90. :param value: parameter value
  91. :return: parameter name
  92. """
  93. for name in cls:
  94. if name.value == value:
  95. return name
  96. raise ValueError(f"invalid parameter name {value}")
  97. class ParameterType(Enum):
  98. """
  99. Enum class for parameter type.
  100. """
  101. FLOAT = "float"
  102. INT = "int"
  103. STRING = "string"
  104. BOOLEAN = "boolean"
  105. TEXT = "text"
  106. class ModelPropertyKey(Enum):
  107. """
  108. Enum class for model property key.
  109. """
  110. MODE = "mode"
  111. CONTEXT_SIZE = "context_size"
  112. MAX_CHUNKS = "max_chunks"
  113. FILE_UPLOAD_LIMIT = "file_upload_limit"
  114. SUPPORTED_FILE_EXTENSIONS = "supported_file_extensions"
  115. MAX_CHARACTERS_PER_CHUNK = "max_characters_per_chunk"
  116. DEFAULT_VOICE = "default_voice"
  117. VOICES = "voices"
  118. WORD_LIMIT = "word_limit"
  119. AUDIO_TYPE = "audio_type"
  120. MAX_WORKERS = "max_workers"
  121. class ProviderModel(BaseModel):
  122. """
  123. Model class for provider model.
  124. """
  125. model: str
  126. label: I18nObject
  127. model_type: ModelType
  128. features: Optional[list[ModelFeature]] = None
  129. fetch_from: FetchFrom
  130. model_properties: dict[ModelPropertyKey, Any]
  131. deprecated: bool = False
  132. model_config = ConfigDict(protected_namespaces=())
  133. @property
  134. def support_structure_output(self) -> bool:
  135. return self.features is not None and ModelFeature.STRUCTURED_OUTPUT in self.features
  136. class ParameterRule(BaseModel):
  137. """
  138. Model class for parameter rule.
  139. """
  140. name: str
  141. use_template: Optional[str] = None
  142. label: I18nObject
  143. type: ParameterType
  144. help: Optional[I18nObject] = None
  145. required: bool = False
  146. default: Optional[Any] = None
  147. min: Optional[float] = None
  148. max: Optional[float] = None
  149. precision: Optional[int] = None
  150. options: list[str] = []
  151. class PriceConfig(BaseModel):
  152. """
  153. Model class for pricing info.
  154. """
  155. input: Decimal
  156. output: Optional[Decimal] = None
  157. unit: Decimal
  158. currency: str
  159. class AIModelEntity(ProviderModel):
  160. """
  161. Model class for AI model.
  162. """
  163. parameter_rules: list[ParameterRule] = []
  164. pricing: Optional[PriceConfig] = None
  165. @model_validator(mode="after")
  166. def validate_model(self):
  167. supported_schema_keys = ["json_schema"]
  168. schema_key = next((rule.name for rule in self.parameter_rules if rule.name in supported_schema_keys), None)
  169. if not schema_key:
  170. return self
  171. if self.features is None:
  172. self.features = [ModelFeature.STRUCTURED_OUTPUT]
  173. else:
  174. if ModelFeature.STRUCTURED_OUTPUT not in self.features:
  175. self.features.append(ModelFeature.STRUCTURED_OUTPUT)
  176. return self
  177. class ModelUsage(BaseModel):
  178. pass
  179. class PriceType(Enum):
  180. """
  181. Enum class for price type.
  182. """
  183. INPUT = "input"
  184. OUTPUT = "output"
  185. class PriceInfo(BaseModel):
  186. """
  187. Model class for price info.
  188. """
  189. unit_price: Decimal
  190. unit: Decimal
  191. total_amount: Decimal
  192. currency: str