model_entities.py 5.0 KB

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