model_entities.py 4.7 KB

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