Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

model_provider_entities.py 5.3KB

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