model_entities.py 5.2 KB

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