您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

model_entities.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from collections.abc import Sequence
  2. from enum import Enum
  3. from typing import Optional
  4. from pydantic import BaseModel, ConfigDict
  5. from core.model_runtime.entities.common_entities import I18nObject
  6. from core.model_runtime.entities.model_entities import ModelType, ProviderModel
  7. from core.model_runtime.entities.provider_entities import ProviderEntity
  8. class ModelStatus(Enum):
  9. """
  10. Enum class for model status.
  11. """
  12. ACTIVE = "active"
  13. NO_CONFIGURE = "no-configure"
  14. QUOTA_EXCEEDED = "quota-exceeded"
  15. NO_PERMISSION = "no-permission"
  16. DISABLED = "disabled"
  17. class SimpleModelProviderEntity(BaseModel):
  18. """
  19. Simple provider.
  20. """
  21. provider: str
  22. label: I18nObject
  23. icon_small: Optional[I18nObject] = None
  24. icon_large: Optional[I18nObject] = None
  25. supported_model_types: list[ModelType]
  26. def __init__(self, provider_entity: ProviderEntity) -> None:
  27. """
  28. Init simple provider.
  29. :param provider_entity: provider entity
  30. """
  31. super().__init__(
  32. provider=provider_entity.provider,
  33. label=provider_entity.label,
  34. icon_small=provider_entity.icon_small,
  35. icon_large=provider_entity.icon_large,
  36. supported_model_types=provider_entity.supported_model_types,
  37. )
  38. class ProviderModelWithStatusEntity(ProviderModel):
  39. """
  40. Model class for model response.
  41. """
  42. status: ModelStatus
  43. load_balancing_enabled: bool = False
  44. def raise_for_status(self) -> None:
  45. """
  46. Check model status and raise ValueError if not active.
  47. :raises ValueError: When model status is not active, with a descriptive message
  48. """
  49. if self.status == ModelStatus.ACTIVE:
  50. return
  51. error_messages = {
  52. ModelStatus.NO_CONFIGURE: "Model is not configured",
  53. ModelStatus.QUOTA_EXCEEDED: "Model quota has been exceeded",
  54. ModelStatus.NO_PERMISSION: "No permission to use this model",
  55. ModelStatus.DISABLED: "Model is disabled",
  56. }
  57. if self.status in error_messages:
  58. raise ValueError(error_messages[self.status])
  59. class ModelWithProviderEntity(ProviderModelWithStatusEntity):
  60. """
  61. Model with provider entity.
  62. """
  63. provider: SimpleModelProviderEntity
  64. class DefaultModelProviderEntity(BaseModel):
  65. """
  66. Default model provider entity.
  67. """
  68. provider: str
  69. label: I18nObject
  70. icon_small: Optional[I18nObject] = None
  71. icon_large: Optional[I18nObject] = None
  72. supported_model_types: Sequence[ModelType] = []
  73. class DefaultModelEntity(BaseModel):
  74. """
  75. Default model entity.
  76. """
  77. model: str
  78. model_type: ModelType
  79. provider: DefaultModelProviderEntity
  80. # pydantic configs
  81. model_config = ConfigDict(protected_namespaces=())