model_entities.py 5.3 KB

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