2024-01-02 23:42:00 +08:00
|
|
|
from enum import Enum
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-06-14 01:05:37 +08:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-07-12 12:25:38 +08:00
|
|
|
from configs import dify_config
|
2024-12-24 18:38:51 +08:00
|
|
|
from core.entities.model_entities import (
|
|
|
|
ModelWithProviderEntity,
|
|
|
|
ProviderModelWithStatusEntity,
|
|
|
|
)
|
2025-02-17 17:05:13 +08:00
|
|
|
from core.entities.provider_entities import ProviderQuotaType, QuotaConfiguration
|
2024-01-02 23:42:00 +08:00
|
|
|
from core.model_runtime.entities.common_entities import I18nObject
|
2024-06-05 00:13:04 +08:00
|
|
|
from core.model_runtime.entities.model_entities import ModelType
|
2024-02-06 13:21:13 +08:00
|
|
|
from core.model_runtime.entities.provider_entities import (
|
|
|
|
ConfigurateMethod,
|
|
|
|
ModelCredentialSchema,
|
|
|
|
ProviderCredentialSchema,
|
|
|
|
ProviderHelpEntity,
|
|
|
|
SimpleProviderEntity,
|
|
|
|
)
|
2025-02-17 17:05:13 +08:00
|
|
|
from models.provider import ProviderType
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomConfigurationStatus(Enum):
|
|
|
|
"""
|
|
|
|
Enum class for custom configuration status.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
|
|
|
ACTIVE = "active"
|
|
|
|
NO_CONFIGURE = "no-configure"
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomConfigurationResponse(BaseModel):
|
|
|
|
"""
|
|
|
|
Model class for provider custom configuration response.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
status: CustomConfigurationStatus
|
|
|
|
|
|
|
|
|
|
|
|
class SystemConfigurationResponse(BaseModel):
|
|
|
|
"""
|
|
|
|
Model class for provider system configuration response.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
enabled: bool
|
|
|
|
current_quota_type: Optional[ProviderQuotaType] = None
|
|
|
|
quota_configurations: list[QuotaConfiguration] = []
|
|
|
|
|
|
|
|
|
|
|
|
class ProviderResponse(BaseModel):
|
|
|
|
"""
|
|
|
|
Model class for provider response.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
tenant_id: str
|
2024-01-02 23:42:00 +08:00
|
|
|
provider: str
|
|
|
|
label: I18nObject
|
|
|
|
description: Optional[I18nObject] = None
|
|
|
|
icon_small: Optional[I18nObject] = None
|
|
|
|
icon_large: Optional[I18nObject] = None
|
|
|
|
background: Optional[str] = None
|
|
|
|
help: Optional[ProviderHelpEntity] = None
|
|
|
|
supported_model_types: list[ModelType]
|
|
|
|
configurate_methods: list[ConfigurateMethod]
|
|
|
|
provider_credential_schema: Optional[ProviderCredentialSchema] = None
|
|
|
|
model_credential_schema: Optional[ModelCredentialSchema] = None
|
|
|
|
preferred_provider_type: ProviderType
|
|
|
|
custom_configuration: CustomConfigurationResponse
|
|
|
|
system_configuration: SystemConfigurationResponse
|
|
|
|
|
2024-06-14 01:05:37 +08:00
|
|
|
# pydantic configs
|
|
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
def __init__(self, **data) -> None:
|
|
|
|
super().__init__(**data)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
url_prefix = (
|
|
|
|
dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
|
|
|
|
)
|
2024-01-02 23:42:00 +08:00
|
|
|
if self.icon_small is not None:
|
|
|
|
self.icon_small = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if self.icon_large is not None:
|
|
|
|
self.icon_large = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ProviderWithModelsResponse(BaseModel):
|
|
|
|
"""
|
|
|
|
Model class for provider with models response.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
tenant_id: str
|
2024-01-02 23:42:00 +08:00
|
|
|
provider: str
|
|
|
|
label: I18nObject
|
|
|
|
icon_small: Optional[I18nObject] = None
|
|
|
|
icon_large: Optional[I18nObject] = None
|
|
|
|
status: CustomConfigurationStatus
|
2024-06-05 00:13:04 +08:00
|
|
|
models: list[ProviderModelWithStatusEntity]
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
def __init__(self, **data) -> None:
|
|
|
|
super().__init__(**data)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
url_prefix = (
|
|
|
|
dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
|
|
|
|
)
|
2024-01-02 23:42:00 +08:00
|
|
|
if self.icon_small is not None:
|
|
|
|
self.icon_small = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if self.icon_large is not None:
|
|
|
|
self.icon_large = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SimpleProviderEntityResponse(SimpleProviderEntity):
|
|
|
|
"""
|
|
|
|
Simple provider entity response.
|
|
|
|
"""
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
tenant_id: str
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
def __init__(self, **data) -> None:
|
|
|
|
super().__init__(**data)
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
url_prefix = (
|
|
|
|
dify_config.CONSOLE_API_URL + f"/console/api/workspaces/{self.tenant_id}/model-providers/{self.provider}"
|
|
|
|
)
|
2024-01-02 23:42:00 +08:00
|
|
|
if self.icon_small is not None:
|
|
|
|
self.icon_small = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_small/en_US", zh_Hans=f"{url_prefix}/icon_small/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if self.icon_large is not None:
|
|
|
|
self.icon_large = I18nObject(
|
2024-08-26 13:43:57 +08:00
|
|
|
en_US=f"{url_prefix}/icon_large/en_US", zh_Hans=f"{url_prefix}/icon_large/zh_Hans"
|
2024-01-02 23:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultModelResponse(BaseModel):
|
|
|
|
"""
|
|
|
|
Default model entity.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
model: str
|
|
|
|
model_type: ModelType
|
|
|
|
provider: SimpleProviderEntityResponse
|
|
|
|
|
2024-06-14 01:05:37 +08:00
|
|
|
# pydantic configs
|
|
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
class ModelWithProviderEntityResponse(ProviderModelWithStatusEntity):
|
2024-01-02 23:42:00 +08:00
|
|
|
"""
|
|
|
|
Model with provider entity.
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
provider: SimpleProviderEntityResponse
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
def __init__(self, tenant_id: str, model: ModelWithProviderEntity) -> None:
|
|
|
|
dump_model = model.model_dump()
|
|
|
|
dump_model["provider"]["tenant_id"] = tenant_id
|
|
|
|
super().__init__(**dump_model)
|