model_provider_entities.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 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 ProviderQuotaType, 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. provider: str
  40. label: I18nObject
  41. description: Optional[I18nObject] = None
  42. icon_small: Optional[I18nObject] = None
  43. icon_large: Optional[I18nObject] = None
  44. background: Optional[str] = None
  45. help: Optional[ProviderHelpEntity] = None
  46. supported_model_types: list[ModelType]
  47. configurate_methods: list[ConfigurateMethod]
  48. provider_credential_schema: Optional[ProviderCredentialSchema] = None
  49. model_credential_schema: Optional[ModelCredentialSchema] = None
  50. preferred_provider_type: ProviderType
  51. custom_configuration: CustomConfigurationResponse
  52. system_configuration: SystemConfigurationResponse
  53. # pydantic configs
  54. model_config = ConfigDict(protected_namespaces=())
  55. def __init__(self, **data) -> None:
  56. super().__init__(**data)
  57. url_prefix = (dify_config.CONSOLE_API_URL
  58. + f"/console/api/workspaces/current/model-providers/{self.provider}")
  59. if self.icon_small is not None:
  60. self.icon_small = I18nObject(
  61. en_US=f"{url_prefix}/icon_small/en_US",
  62. zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  63. )
  64. if self.icon_large is not None:
  65. self.icon_large = I18nObject(
  66. en_US=f"{url_prefix}/icon_large/en_US",
  67. 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. provider: str
  74. label: I18nObject
  75. icon_small: Optional[I18nObject] = None
  76. icon_large: Optional[I18nObject] = None
  77. status: CustomConfigurationStatus
  78. models: list[ProviderModelWithStatusEntity]
  79. def __init__(self, **data) -> None:
  80. super().__init__(**data)
  81. url_prefix = (dify_config.CONSOLE_API_URL
  82. + f"/console/api/workspaces/current/model-providers/{self.provider}")
  83. if self.icon_small is not None:
  84. self.icon_small = I18nObject(
  85. en_US=f"{url_prefix}/icon_small/en_US",
  86. zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  87. )
  88. if self.icon_large is not None:
  89. self.icon_large = I18nObject(
  90. en_US=f"{url_prefix}/icon_large/en_US",
  91. zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  92. )
  93. class SimpleProviderEntityResponse(SimpleProviderEntity):
  94. """
  95. Simple provider entity response.
  96. """
  97. def __init__(self, **data) -> None:
  98. super().__init__(**data)
  99. url_prefix = (dify_config.CONSOLE_API_URL
  100. + f"/console/api/workspaces/current/model-providers/{self.provider}")
  101. if self.icon_small is not None:
  102. self.icon_small = I18nObject(
  103. en_US=f"{url_prefix}/icon_small/en_US",
  104. zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  105. )
  106. if self.icon_large is not None:
  107. self.icon_large = I18nObject(
  108. en_US=f"{url_prefix}/icon_large/en_US",
  109. 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, model: ModelWithProviderEntity) -> None:
  126. super().__init__(**model.model_dump())