mirror of
https://github.com/langgenius/dify.git
synced 2026-02-01 08:31:13 +08:00
Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: kenwoodjw <blackxin55+@gmail.com> Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com> Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: zhanluxianshen <zhanluxianshen@163.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: GuanMu <ballmanjq@gmail.com> Co-authored-by: Davide Delbianco <davide.delbianco@outlook.com> Co-authored-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com> Co-authored-by: kenwoodjw <blackxin55+@gmail.com> Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com> Co-authored-by: Yongtao Huang <99629139+hyongtao-db@users.noreply.github.com> Co-authored-by: Qiang Lee <18018968632@163.com> Co-authored-by: 李强04 <liqiang04@gaotu.cn> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Matri Qi <matrixdom@126.com> Co-authored-by: huayaoyue6 <huayaoyue@163.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: znn <jubinkumarsoni@gmail.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Muke Wang <shaodwaaron@gmail.com> Co-authored-by: wangmuke <wangmuke@kingsware.cn> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: quicksand <quicksandzn@gmail.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: Eric Guo <eric.guocz@gmail.com> Co-authored-by: Zhedong Cen <cenzhedong2@126.com> Co-authored-by: jiangbo721 <jiangbo721@163.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: hjlarry <25834719+hjlarry@users.noreply.github.com> Co-authored-by: lxsummer <35754229+lxjustdoit@users.noreply.github.com> Co-authored-by: 湛露先生 <zhanluxianshen@163.com> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Yessenia-d <yessenia.contact@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: 17hz <0x149527@gmail.com> Co-authored-by: Amy <1530140574@qq.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Nite Knite <nkCoding@gmail.com> Co-authored-by: Yeuoly <45712896+Yeuoly@users.noreply.github.com> Co-authored-by: Petrus Han <petrus.hanks@gmail.com> Co-authored-by: iamjoel <2120155+iamjoel@users.noreply.github.com> Co-authored-by: Kalo Chin <frog.beepers.0n@icloud.com> Co-authored-by: Ujjwal Maurya <ujjwalsbx@gmail.com> Co-authored-by: Maries <xh001x@hotmail.com>
204 lines
5.2 KiB
Python
204 lines
5.2 KiB
Python
from enum import Enum
|
|
from typing import Optional, Union
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
from core.entities.parameter_entities import (
|
|
AppSelectorScope,
|
|
CommonParameterType,
|
|
ModelSelectorScope,
|
|
ToolSelectorScope,
|
|
)
|
|
from core.model_runtime.entities.model_entities import ModelType
|
|
from core.tools.entities.common_entities import I18nObject
|
|
|
|
|
|
class ProviderQuotaType(Enum):
|
|
PAID = "paid"
|
|
"""hosted paid quota"""
|
|
|
|
FREE = "free"
|
|
"""third-party free quota"""
|
|
|
|
TRIAL = "trial"
|
|
"""hosted trial quota"""
|
|
|
|
@staticmethod
|
|
def value_of(value):
|
|
for member in ProviderQuotaType:
|
|
if member.value == value:
|
|
return member
|
|
raise ValueError(f"No matching enum found for value '{value}'")
|
|
|
|
|
|
class QuotaUnit(Enum):
|
|
TIMES = "times"
|
|
TOKENS = "tokens"
|
|
CREDITS = "credits"
|
|
|
|
|
|
class SystemConfigurationStatus(Enum):
|
|
"""
|
|
Enum class for system configuration status.
|
|
"""
|
|
|
|
ACTIVE = "active"
|
|
QUOTA_EXCEEDED = "quota-exceeded"
|
|
UNSUPPORTED = "unsupported"
|
|
|
|
|
|
class RestrictModel(BaseModel):
|
|
model: str
|
|
base_model_name: Optional[str] = None
|
|
model_type: ModelType
|
|
|
|
# pydantic configs
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
|
class QuotaConfiguration(BaseModel):
|
|
"""
|
|
Model class for provider quota configuration.
|
|
"""
|
|
|
|
quota_type: ProviderQuotaType
|
|
quota_unit: QuotaUnit
|
|
quota_limit: int
|
|
quota_used: int
|
|
is_valid: bool
|
|
restrict_models: list[RestrictModel] = []
|
|
|
|
|
|
class CredentialConfiguration(BaseModel):
|
|
"""
|
|
Model class for credential configuration.
|
|
"""
|
|
|
|
credential_id: str
|
|
credential_name: str
|
|
|
|
|
|
class SystemConfiguration(BaseModel):
|
|
"""
|
|
Model class for provider system configuration.
|
|
"""
|
|
|
|
enabled: bool
|
|
current_quota_type: Optional[ProviderQuotaType] = None
|
|
quota_configurations: list[QuotaConfiguration] = []
|
|
credentials: Optional[dict] = None
|
|
|
|
|
|
class CustomProviderConfiguration(BaseModel):
|
|
"""
|
|
Model class for provider custom configuration.
|
|
"""
|
|
|
|
credentials: dict
|
|
current_credential_id: Optional[str] = None
|
|
current_credential_name: Optional[str] = None
|
|
available_credentials: list[CredentialConfiguration] = []
|
|
|
|
|
|
class CustomModelConfiguration(BaseModel):
|
|
"""
|
|
Model class for provider custom model configuration.
|
|
"""
|
|
|
|
model: str
|
|
model_type: ModelType
|
|
credentials: dict | None
|
|
current_credential_id: Optional[str] = None
|
|
current_credential_name: Optional[str] = None
|
|
available_model_credentials: list[CredentialConfiguration] = []
|
|
|
|
# pydantic configs
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
|
class CustomConfiguration(BaseModel):
|
|
"""
|
|
Model class for provider custom configuration.
|
|
"""
|
|
|
|
provider: Optional[CustomProviderConfiguration] = None
|
|
models: list[CustomModelConfiguration] = []
|
|
|
|
|
|
class ModelLoadBalancingConfiguration(BaseModel):
|
|
"""
|
|
Class for model load balancing configuration.
|
|
"""
|
|
|
|
id: str
|
|
name: str
|
|
credentials: dict
|
|
credential_source_type: str | None = None
|
|
|
|
|
|
class ModelSettings(BaseModel):
|
|
"""
|
|
Model class for model settings.
|
|
"""
|
|
|
|
model: str
|
|
model_type: ModelType
|
|
enabled: bool = True
|
|
load_balancing_configs: list[ModelLoadBalancingConfiguration] = []
|
|
|
|
# pydantic configs
|
|
model_config = ConfigDict(protected_namespaces=())
|
|
|
|
|
|
class BasicProviderConfig(BaseModel):
|
|
"""
|
|
Base model class for common provider settings like credentials
|
|
"""
|
|
|
|
class Type(Enum):
|
|
SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
|
|
TEXT_INPUT = CommonParameterType.TEXT_INPUT.value
|
|
SELECT = CommonParameterType.SELECT.value
|
|
BOOLEAN = CommonParameterType.BOOLEAN.value
|
|
APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
|
|
MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
|
|
TOOLS_SELECTOR = CommonParameterType.TOOLS_SELECTOR.value
|
|
|
|
@classmethod
|
|
def value_of(cls, value: str) -> "ProviderConfig.Type":
|
|
"""
|
|
Get value of given mode.
|
|
|
|
:param value: mode value
|
|
:return: mode
|
|
"""
|
|
for mode in cls:
|
|
if mode.value == value:
|
|
return mode
|
|
raise ValueError(f"invalid mode value {value}")
|
|
|
|
type: Type = Field(..., description="The type of the credentials")
|
|
name: str = Field(..., description="The name of the credentials")
|
|
|
|
|
|
class ProviderConfig(BasicProviderConfig):
|
|
"""
|
|
Model class for common provider settings like credentials
|
|
"""
|
|
|
|
class Option(BaseModel):
|
|
value: str = Field(..., description="The value of the option")
|
|
label: I18nObject = Field(..., description="The label of the option")
|
|
|
|
scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
|
|
required: bool = False
|
|
default: Optional[Union[int, str, float, bool]] = None
|
|
options: Optional[list[Option]] = None
|
|
label: Optional[I18nObject] = None
|
|
help: Optional[I18nObject] = None
|
|
url: Optional[str] = None
|
|
placeholder: Optional[I18nObject] = None
|
|
|
|
def to_basic_provider_config(self) -> BasicProviderConfig:
|
|
return BasicProviderConfig(type=self.type, name=self.name)
|