model_provider_entities.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from enum import Enum
  2. from typing import Optional
  3. from pydantic import BaseModel, ConfigDict
  4. from configs import dify_config
  5. from core.entities.model_entities import (
  6. ModelWithProviderEntity,
  7. ProviderModelWithStatusEntity,
  8. SimpleModelProviderEntity,
  9. )
  10. from core.entities.provider_entities import QuotaConfiguration
  11. from core.model_runtime.entities.common_entities import I18nObject
  12. from core.model_runtime.entities.model_entities import ModelType
  13. from core.model_runtime.entities.provider_entities import (
  14. ConfigurateMethod,
  15. ModelCredentialSchema,
  16. ProviderCredentialSchema,
  17. ProviderHelpEntity,
  18. SimpleProviderEntity,
  19. )
  20. from models.provider import ProviderQuotaType, ProviderType
  21. class CustomConfigurationStatus(Enum):
  22. """
  23. Enum class for custom configuration status.
  24. """
  25. ACTIVE = "active"
  26. NO_CONFIGURE = "no-configure"
  27. class CustomConfigurationResponse(BaseModel):
  28. """
  29. Model class for provider custom configuration response.
  30. """
  31. status: CustomConfigurationStatus
  32. class SystemConfigurationResponse(BaseModel):
  33. """
  34. Model class for provider system configuration response.
  35. """
  36. enabled: bool
  37. current_quota_type: Optional[ProviderQuotaType] = None
  38. quota_configurations: list[QuotaConfiguration] = []
  39. class ProviderResponse(BaseModel):
  40. """
  41. Model class for provider response.
  42. """
  43. provider: str
  44. label: I18nObject
  45. description: Optional[I18nObject] = None
  46. icon_small: Optional[I18nObject] = None
  47. icon_large: Optional[I18nObject] = None
  48. background: Optional[str] = None
  49. help: Optional[ProviderHelpEntity] = None
  50. supported_model_types: list[ModelType]
  51. configurate_methods: list[ConfigurateMethod]
  52. provider_credential_schema: Optional[ProviderCredentialSchema] = None
  53. model_credential_schema: Optional[ModelCredentialSchema] = None
  54. preferred_provider_type: ProviderType
  55. custom_configuration: CustomConfigurationResponse
  56. system_configuration: SystemConfigurationResponse
  57. # pydantic configs
  58. model_config = ConfigDict(protected_namespaces=())
  59. def __init__(self, **data) -> None:
  60. super().__init__(**data)
  61. url_prefix = dify_config.CONSOLE_API_URL + f"/console/api/workspaces/current/model-providers/{self.provider}"
  62. if self.icon_small is not None:
  63. self.icon_small = I18nObject(
  64. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  65. )
  66. if self.icon_large is not None:
  67. self.icon_large = I18nObject(
  68. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  69. )
  70. class ProviderWithModelsResponse(BaseModel):
  71. """
  72. Model class for provider with models response.
  73. """
  74. provider: str
  75. label: I18nObject
  76. icon_small: Optional[I18nObject] = None
  77. icon_large: Optional[I18nObject] = None
  78. status: CustomConfigurationStatus
  79. models: list[ProviderModelWithStatusEntity]
  80. def __init__(self, **data) -> None:
  81. super().__init__(**data)
  82. url_prefix = dify_config.CONSOLE_API_URL + f"/console/api/workspaces/current/model-providers/{self.provider}"
  83. if self.icon_small is not None:
  84. self.icon_small = I18nObject(
  85. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  86. )
  87. if self.icon_large is not None:
  88. self.icon_large = I18nObject(
  89. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  90. )
  91. class SimpleProviderEntityResponse(SimpleProviderEntity):
  92. """
  93. Simple provider entity response.
  94. """
  95. def __init__(self, **data) -> None:
  96. super().__init__(**data)
  97. url_prefix = dify_config.CONSOLE_API_URL + f"/console/api/workspaces/current/model-providers/{self.provider}"
  98. if self.icon_small is not None:
  99. self.icon_small = I18nObject(
  100. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  101. )
  102. if self.icon_large is not None:
  103. self.icon_large = I18nObject(
  104. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  105. )
  106. class DefaultModelResponse(BaseModel):
  107. """
  108. Default model entity.
  109. """
  110. model: str
  111. model_type: ModelType
  112. provider: SimpleProviderEntityResponse
  113. # pydantic configs
  114. model_config = ConfigDict(protected_namespaces=())
  115. class ModelWithProviderEntityResponse(ModelWithProviderEntity):
  116. """
  117. Model with provider entity.
  118. """
  119. provider: SimpleModelProviderEntity
  120. def __init__(self, model: ModelWithProviderEntity) -> None:
  121. super().__init__(**model.model_dump())