You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

model_provider_entities.py 5.2KB

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