model_entities.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from decimal import Decimal
  2. from enum import Enum
  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 == 'text-generation' or origin_model_type == cls.LLM.value:
  24. return cls.LLM
  25. elif origin_model_type == 'embeddings' or origin_model_type == cls.TEXT_EMBEDDING.value:
  26. return cls.TEXT_EMBEDDING
  27. elif origin_model_type == 'reranking' or origin_model_type == cls.RERANK.value:
  28. return cls.RERANK
  29. elif origin_model_type == 'speech2text' or origin_model_type == cls.SPEECH2TEXT.value:
  30. return cls.SPEECH2TEXT
  31. elif origin_model_type == 'tts' or origin_model_type == cls.TTS.value:
  32. return cls.TTS
  33. elif origin_model_type == 'text2img' or origin_model_type == 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. class DefaultParameterName(Enum):
  76. """
  77. Enum class for parameter template variable.
  78. """
  79. TEMPERATURE = "temperature"
  80. TOP_P = "top_p"
  81. PRESENCE_PENALTY = "presence_penalty"
  82. FREQUENCY_PENALTY = "frequency_penalty"
  83. MAX_TOKENS = "max_tokens"
  84. RESPONSE_FORMAT = "response_format"
  85. @classmethod
  86. def value_of(cls, value: Any) -> 'DefaultParameterName':
  87. """
  88. Get parameter name from value.
  89. :param value: parameter value
  90. :return: parameter name
  91. """
  92. for name in cls:
  93. if name.value == value:
  94. return name
  95. raise ValueError(f'invalid parameter name {value}')
  96. class ParameterType(Enum):
  97. """
  98. Enum class for parameter type.
  99. """
  100. FLOAT = "float"
  101. INT = "int"
  102. STRING = "string"
  103. BOOLEAN = "boolean"
  104. class ModelPropertyKey(Enum):
  105. """
  106. Enum class for model property key.
  107. """
  108. MODE = "mode"
  109. CONTEXT_SIZE = "context_size"
  110. MAX_CHUNKS = "max_chunks"
  111. FILE_UPLOAD_LIMIT = "file_upload_limit"
  112. SUPPORTED_FILE_EXTENSIONS = "supported_file_extensions"
  113. MAX_CHARACTERS_PER_CHUNK = "max_characters_per_chunk"
  114. DEFAULT_VOICE = "default_voice"
  115. VOICES = "voices"
  116. WORD_LIMIT = "word_limit"
  117. AUDIO_TYPE = "audio_type"
  118. MAX_WORKERS = "max_workers"
  119. class ProviderModel(BaseModel):
  120. """
  121. Model class for provider model.
  122. """
  123. model: str
  124. label: I18nObject
  125. model_type: ModelType
  126. features: Optional[list[ModelFeature]] = None
  127. fetch_from: FetchFrom
  128. model_properties: dict[ModelPropertyKey, Any]
  129. deprecated: bool = False
  130. model_config = ConfigDict(protected_namespaces=())
  131. class ParameterRule(BaseModel):
  132. """
  133. Model class for parameter rule.
  134. """
  135. name: str
  136. use_template: Optional[str] = None
  137. label: I18nObject
  138. type: ParameterType
  139. help: Optional[I18nObject] = None
  140. required: bool = False
  141. default: Optional[Any] = None
  142. min: Optional[float] = None
  143. max: Optional[float] = None
  144. precision: Optional[int] = None
  145. options: list[str] = []
  146. class PriceConfig(BaseModel):
  147. """
  148. Model class for pricing info.
  149. """
  150. input: Decimal
  151. output: Optional[Decimal] = None
  152. unit: Decimal
  153. currency: str
  154. class AIModelEntity(ProviderModel):
  155. """
  156. Model class for AI model.
  157. """
  158. parameter_rules: list[ParameterRule] = []
  159. pricing: Optional[PriceConfig] = None
  160. class ModelUsage(BaseModel):
  161. pass
  162. class PriceType(Enum):
  163. """
  164. Enum class for price type.
  165. """
  166. INPUT = "input"
  167. OUTPUT = "output"
  168. class PriceInfo(BaseModel):
  169. """
  170. Model class for price info.
  171. """
  172. unit_price: Decimal
  173. unit: Decimal
  174. total_amount: Decimal
  175. currency: str