model_provider_entities.py 4.9 KB

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