model_provider_entities.py 4.9 KB

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