model_provider_entities.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 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 ProviderQuotaType, 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. provider: str
  43. label: I18nObject
  44. description: Optional[I18nObject] = None
  45. icon_small: Optional[I18nObject] = None
  46. icon_large: Optional[I18nObject] = None
  47. background: Optional[str] = None
  48. help: Optional[ProviderHelpEntity] = None
  49. supported_model_types: list[ModelType]
  50. configurate_methods: list[ConfigurateMethod]
  51. provider_credential_schema: Optional[ProviderCredentialSchema] = None
  52. model_credential_schema: Optional[ModelCredentialSchema] = None
  53. preferred_provider_type: ProviderType
  54. custom_configuration: CustomConfigurationResponse
  55. system_configuration: SystemConfigurationResponse
  56. # pydantic configs
  57. model_config = ConfigDict(protected_namespaces=())
  58. def __init__(self, **data) -> None:
  59. super().__init__(**data)
  60. url_prefix = dify_config.CONSOLE_API_URL + f"/console/api/workspaces/current/model-providers/{self.provider}"
  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. 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 + f"/console/api/workspaces/current/model-providers/{self.provider}"
  82. if self.icon_small is not None:
  83. self.icon_small = I18nObject(
  84. en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
  85. )
  86. if self.icon_large is not None:
  87. self.icon_large = I18nObject(
  88. en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
  89. )
  90. class SimpleProviderEntityResponse(SimpleProviderEntity):
  91. """
  92. Simple provider entity response.
  93. """
  94. def __init__(self, **data) -> None:
  95. super().__init__(**data)
  96. url_prefix = dify_config.CONSOLE_API_URL + f"/console/api/workspaces/current/model-providers/{self.provider}"
  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 DefaultModelResponse(BaseModel):
  106. """
  107. Default model entity.
  108. """
  109. model: str
  110. model_type: ModelType
  111. provider: SimpleProviderEntityResponse
  112. # pydantic configs
  113. model_config = ConfigDict(protected_namespaces=())
  114. class ModelWithProviderEntityResponse(ModelWithProviderEntity):
  115. """
  116. Model with provider entity.
  117. """
  118. # FIXME type error ignore here
  119. provider: SimpleProviderEntityResponse # type: ignore
  120. def __init__(self, model: ModelWithProviderEntity) -> None:
  121. super().__init__(**model.model_dump())