model_provider_entities.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 ProviderQuotaType, 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 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. tenant_id: str
  44. provider: str
  45. label: I18nObject
  46. description: Optional[I18nObject] = None
  47. icon_small: Optional[I18nObject] = None
  48. icon_large: Optional[I18nObject] = None
  49. background: Optional[str] = None
  50. help: Optional[ProviderHelpEntity] = None
  51. supported_model_types: list[ModelType]
  52. configurate_methods: list[ConfigurateMethod]
  53. provider_credential_schema: Optional[ProviderCredentialSchema] = None
  54. model_credential_schema: Optional[ModelCredentialSchema] = None
  55. preferred_provider_type: ProviderType
  56. custom_configuration: CustomConfigurationResponse
  57. system_configuration: SystemConfigurationResponse
  58. # pydantic configs
  59. model_config = ConfigDict(protected_namespaces=())
  60. def __init__(self, **data) -> None:
  61. super().__init__(**data)
  62. url_prefix = (
  63. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  64. )
  65. if self.icon_small is not None:
  66. self.icon_small = I18nObject(
  67. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  68. )
  69. if self.icon_large is not None:
  70. self.icon_large = I18nObject(
  71. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  72. )
  73. class ProviderWithModelsResponse(BaseModel):
  74. """
  75. Model class for provider with models response.
  76. """
  77. tenant_id: str
  78. provider: str
  79. label: I18nObject
  80. icon_small: Optional[I18nObject] = None
  81. icon_large: Optional[I18nObject] = None
  82. status: CustomConfigurationStatus
  83. models: list[ProviderModelWithStatusEntity]
  84. def __init__(self, **data) -> None:
  85. super().__init__(**data)
  86. url_prefix = (
  87. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  88. )
  89. if self.icon_small is not None:
  90. self.icon_small = I18nObject(
  91. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  92. )
  93. if self.icon_large is not None:
  94. self.icon_large = I18nObject(
  95. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  96. )
  97. class SimpleProviderEntityResponse(SimpleProviderEntity):
  98. """
  99. Simple provider entity response.
  100. """
  101. tenant_id: str
  102. def __init__(self, **data) -> None:
  103. super().__init__(**data)
  104. url_prefix = (
  105. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  106. )
  107. if self.icon_small is not None:
  108. self.icon_small = I18nObject(
  109. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  110. )
  111. if self.icon_large is not None:
  112. self.icon_large = I18nObject(
  113. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  114. )
  115. class DefaultModelResponse(BaseModel):
  116. """
  117. Default model entity.
  118. """
  119. model: str
  120. model_type: ModelType
  121. provider: SimpleProviderEntityResponse
  122. # pydantic configs
  123. model_config = ConfigDict(protected_namespaces=())
  124. class ModelWithProviderEntityResponse(ModelWithProviderEntity):
  125. """
  126. Model with provider entity.
  127. """
  128. provider: SimpleModelProviderEntity
  129. def __init__(self, tenant_id: str, model: ModelWithProviderEntity) -> None:
  130. dump_model = model.model_dump()
  131. dump_model["provider"]["tenant_id"] = tenant_id
  132. super().__init__(**dump_model)