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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from enum import Enum
  2. from pydantic import BaseModel, ConfigDict
  3. from configs import dify_config
  4. from core.entities.model_entities import (
  5. ModelWithProviderEntity,
  6. ProviderModelWithStatusEntity,
  7. )
  8. from core.entities.provider_entities import (
  9. CredentialConfiguration,
  10. CustomModelConfiguration,
  11. ProviderQuotaType,
  12. QuotaConfiguration,
  13. UnaddedModelConfiguration,
  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: str | None = None
  37. current_credential_name: str | None = None
  38. available_credentials: list[CredentialConfiguration] | None = None
  39. custom_models: list[CustomModelConfiguration] | None = None
  40. can_added_models: list[UnaddedModelConfiguration] | None = None
  41. class SystemConfigurationResponse(BaseModel):
  42. """
  43. Model class for provider system configuration response.
  44. """
  45. enabled: bool
  46. current_quota_type: ProviderQuotaType | None = None
  47. quota_configurations: list[QuotaConfiguration] = []
  48. class ProviderResponse(BaseModel):
  49. """
  50. Model class for provider response.
  51. """
  52. tenant_id: str
  53. provider: str
  54. label: I18nObject
  55. description: I18nObject | None = None
  56. icon_small: I18nObject | None = None
  57. icon_large: I18nObject | None = None
  58. background: str | None = None
  59. help: ProviderHelpEntity | None = None
  60. supported_model_types: list[ModelType]
  61. configurate_methods: list[ConfigurateMethod]
  62. provider_credential_schema: ProviderCredentialSchema | None = None
  63. model_credential_schema: ModelCredentialSchema | None = None
  64. preferred_provider_type: ProviderType
  65. custom_configuration: CustomConfigurationResponse
  66. system_configuration: SystemConfigurationResponse
  67. # pydantic configs
  68. model_config = ConfigDict(protected_namespaces=())
  69. def __init__(self, **data):
  70. super().__init__(**data)
  71. url_prefix = (
  72. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  73. )
  74. if self.icon_small is not None:
  75. self.icon_small = I18nObject(
  76. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  77. )
  78. if self.icon_large is not None:
  79. self.icon_large = I18nObject(
  80. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  81. )
  82. class ProviderWithModelsResponse(BaseModel):
  83. """
  84. Model class for provider with models response.
  85. """
  86. tenant_id: str
  87. provider: str
  88. label: I18nObject
  89. icon_small: I18nObject | None = None
  90. icon_large: I18nObject | None = None
  91. status: CustomConfigurationStatus
  92. models: list[ProviderModelWithStatusEntity]
  93. def __init__(self, **data):
  94. super().__init__(**data)
  95. url_prefix = (
  96. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  97. )
  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 SimpleProviderEntityResponse(SimpleProviderEntity):
  107. """
  108. Simple provider entity response.
  109. """
  110. tenant_id: str
  111. def __init__(self, **data):
  112. super().__init__(**data)
  113. url_prefix = (
  114. dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
  115. )
  116. if self.icon_small is not None:
  117. self.icon_small = I18nObject(
  118. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  119. )
  120. if self.icon_large is not None:
  121. self.icon_large = I18nObject(
  122. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  123. )
  124. class DefaultModelResponse(BaseModel):
  125. """
  126. Default model entity.
  127. """
  128. model: str
  129. model_type: ModelType
  130. provider: SimpleProviderEntityResponse
  131. # pydantic configs
  132. model_config = ConfigDict(protected_namespaces=())
  133. class ModelWithProviderEntityResponse(ProviderModelWithStatusEntity):
  134. """
  135. Model with provider entity.
  136. """
  137. provider: SimpleProviderEntityResponse
  138. def __init__(self, tenant_id: str, model: ModelWithProviderEntity):
  139. dump_model = model.model_dump()
  140. dump_model["provider"]["tenant_id"] = tenant_id
  141. super().__init__(**dump_model)