model_entities.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. 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. class ParameterRule(BaseModel):
  134. """
  135. Model class for parameter rule.
  136. """
  137. name: str
  138. use_template: Optional[str] = None
  139. label: I18nObject
  140. type: ParameterType
  141. help: Optional[I18nObject] = None
  142. required: bool = False
  143. default: Optional[Any] = None
  144. min: Optional[float] = None
  145. max: Optional[float] = None
  146. precision: Optional[int] = None
  147. options: list[str] = []
  148. class PriceConfig(BaseModel):
  149. """
  150. Model class for pricing info.
  151. """
  152. input: Decimal
  153. output: Optional[Decimal] = None
  154. unit: Decimal
  155. currency: str
  156. class AIModelEntity(ProviderModel):
  157. """
  158. Model class for AI model.
  159. """
  160. parameter_rules: list[ParameterRule] = []
  161. pricing: Optional[PriceConfig] = None
  162. class ModelUsage(BaseModel):
  163. pass
  164. class PriceType(Enum):
  165. """
  166. Enum class for price type.
  167. """
  168. INPUT = "input"
  169. OUTPUT = "output"
  170. class PriceInfo(BaseModel):
  171. """
  172. Model class for price info.
  173. """
  174. unit_price: Decimal
  175. unit: Decimal
  176. total_amount: Decimal
  177. currency: str