mirror of
https://github.com/langgenius/dify.git
synced 2026-01-14 06:07:33 +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>
312 lines
14 KiB
Python
312 lines
14 KiB
Python
from datetime import datetime
|
|
from enum import Enum
|
|
from functools import cached_property
|
|
from typing import Optional
|
|
|
|
import sqlalchemy as sa
|
|
from sqlalchemy import DateTime, String, func, text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from .base import Base
|
|
from .engine import db
|
|
from .types import StringUUID
|
|
|
|
|
|
class ProviderType(Enum):
|
|
CUSTOM = "custom"
|
|
SYSTEM = "system"
|
|
|
|
@staticmethod
|
|
def value_of(value):
|
|
for member in ProviderType:
|
|
if member.value == value:
|
|
return member
|
|
raise ValueError(f"No matching enum found for value '{value}'")
|
|
|
|
|
|
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 Provider(Base):
|
|
"""
|
|
Provider model representing the API providers and their configurations.
|
|
"""
|
|
|
|
__tablename__ = "providers"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_pkey"),
|
|
sa.Index("provider_tenant_id_provider_idx", "tenant_id", "provider_name"),
|
|
sa.UniqueConstraint(
|
|
"tenant_id", "provider_name", "provider_type", "quota_type", name="unique_provider_name_type_quota"
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
provider_type: Mapped[str] = mapped_column(
|
|
String(40), nullable=False, server_default=text("'custom'::character varying")
|
|
)
|
|
is_valid: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=text("false"))
|
|
last_used: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
|
credential_id: Mapped[Optional[str]] = mapped_column(StringUUID, nullable=True)
|
|
|
|
quota_type: Mapped[Optional[str]] = mapped_column(
|
|
String(40), nullable=True, server_default=text("''::character varying")
|
|
)
|
|
quota_limit: Mapped[Optional[int]] = mapped_column(sa.BigInteger, nullable=True)
|
|
quota_used: Mapped[Optional[int]] = mapped_column(sa.BigInteger, default=0)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
def __repr__(self):
|
|
return (
|
|
f"<Provider(id={self.id}, tenant_id={self.tenant_id}, provider_name='{self.provider_name}',"
|
|
f" provider_type='{self.provider_type}')>"
|
|
)
|
|
|
|
@cached_property
|
|
def credential(self):
|
|
if self.credential_id:
|
|
return db.session.query(ProviderCredential).where(ProviderCredential.id == self.credential_id).first()
|
|
|
|
@property
|
|
def credential_name(self):
|
|
credential = self.credential
|
|
return credential.credential_name if credential else None
|
|
|
|
@property
|
|
def encrypted_config(self):
|
|
credential = self.credential
|
|
return credential.encrypted_config if credential else None
|
|
|
|
@property
|
|
def token_is_set(self):
|
|
"""
|
|
Returns True if the encrypted_config is not None, indicating that the token is set.
|
|
"""
|
|
return self.encrypted_config is not None
|
|
|
|
@property
|
|
def is_enabled(self):
|
|
"""
|
|
Returns True if the provider is enabled.
|
|
"""
|
|
if self.provider_type == ProviderType.SYSTEM.value:
|
|
return self.is_valid
|
|
else:
|
|
return self.is_valid and self.token_is_set
|
|
|
|
|
|
class ProviderModel(Base):
|
|
"""
|
|
Provider model representing the API provider_models and their configurations.
|
|
"""
|
|
|
|
__tablename__ = "provider_models"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_model_pkey"),
|
|
sa.Index("provider_model_tenant_id_provider_idx", "tenant_id", "provider_name"),
|
|
sa.UniqueConstraint(
|
|
"tenant_id", "provider_name", "model_name", "model_type", name="unique_provider_model_name"
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
credential_id: Mapped[Optional[str]] = mapped_column(StringUUID, nullable=True)
|
|
is_valid: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=text("false"))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
@cached_property
|
|
def credential(self):
|
|
if self.credential_id:
|
|
return (
|
|
db.session.query(ProviderModelCredential)
|
|
.where(ProviderModelCredential.id == self.credential_id)
|
|
.first()
|
|
)
|
|
|
|
@property
|
|
def credential_name(self):
|
|
credential = self.credential
|
|
return credential.credential_name if credential else None
|
|
|
|
@property
|
|
def encrypted_config(self):
|
|
credential = self.credential
|
|
return credential.encrypted_config if credential else None
|
|
|
|
|
|
class TenantDefaultModel(Base):
|
|
__tablename__ = "tenant_default_models"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="tenant_default_model_pkey"),
|
|
sa.Index("tenant_default_model_tenant_id_provider_type_idx", "tenant_id", "provider_name", "model_type"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class TenantPreferredModelProvider(Base):
|
|
__tablename__ = "tenant_preferred_model_providers"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="tenant_preferred_model_provider_pkey"),
|
|
sa.Index("tenant_preferred_model_provider_tenant_provider_idx", "tenant_id", "provider_name"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
preferred_provider_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class ProviderOrder(Base):
|
|
__tablename__ = "provider_orders"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_order_pkey"),
|
|
sa.Index("provider_order_tenant_provider_idx", "tenant_id", "provider_name"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
account_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
payment_product_id: Mapped[str] = mapped_column(String(191), nullable=False)
|
|
payment_id: Mapped[Optional[str]] = mapped_column(String(191))
|
|
transaction_id: Mapped[Optional[str]] = mapped_column(String(191))
|
|
quantity: Mapped[int] = mapped_column(sa.Integer, nullable=False, server_default=text("1"))
|
|
currency: Mapped[Optional[str]] = mapped_column(String(40))
|
|
total_amount: Mapped[Optional[int]] = mapped_column(sa.Integer)
|
|
payment_status: Mapped[str] = mapped_column(
|
|
String(40), nullable=False, server_default=text("'wait_pay'::character varying")
|
|
)
|
|
paid_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
pay_failed_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
refunded_at: Mapped[Optional[datetime]] = mapped_column(DateTime)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class ProviderModelSetting(Base):
|
|
"""
|
|
Provider model settings for record the model enabled status and load balancing status.
|
|
"""
|
|
|
|
__tablename__ = "provider_model_settings"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_model_setting_pkey"),
|
|
sa.Index("provider_model_setting_tenant_provider_model_idx", "tenant_id", "provider_name", "model_type"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=text("true"))
|
|
load_balancing_enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=text("false"))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class LoadBalancingModelConfig(Base):
|
|
"""
|
|
Configurations for load balancing models.
|
|
"""
|
|
|
|
__tablename__ = "load_balancing_model_configs"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="load_balancing_model_config_pkey"),
|
|
sa.Index("load_balancing_model_config_tenant_provider_model_idx", "tenant_id", "provider_name", "model_type"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuid_generate_v4()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
encrypted_config: Mapped[Optional[str]] = mapped_column(sa.Text, nullable=True)
|
|
credential_id: Mapped[Optional[str]] = mapped_column(StringUUID, nullable=True)
|
|
credential_source_type: Mapped[Optional[str]] = mapped_column(String(40), nullable=True)
|
|
enabled: Mapped[bool] = mapped_column(sa.Boolean, nullable=False, server_default=text("true"))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class ProviderCredential(Base):
|
|
"""
|
|
Provider credential - stores multiple named credentials for each provider
|
|
"""
|
|
|
|
__tablename__ = "provider_credentials"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_credential_pkey"),
|
|
sa.Index("provider_credential_tenant_provider_idx", "tenant_id", "provider_name"),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuidv7()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
credential_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
encrypted_config: Mapped[str] = mapped_column(sa.Text, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
|
|
|
|
class ProviderModelCredential(Base):
|
|
"""
|
|
Provider model credential - stores multiple named credentials for each provider model
|
|
"""
|
|
|
|
__tablename__ = "provider_model_credentials"
|
|
__table_args__ = (
|
|
sa.PrimaryKeyConstraint("id", name="provider_model_credential_pkey"),
|
|
sa.Index(
|
|
"provider_model_credential_tenant_provider_model_idx",
|
|
"tenant_id",
|
|
"provider_name",
|
|
"model_name",
|
|
"model_type",
|
|
),
|
|
)
|
|
|
|
id: Mapped[str] = mapped_column(StringUUID, server_default=text("uuidv7()"))
|
|
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
|
|
provider_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
model_type: Mapped[str] = mapped_column(String(40), nullable=False)
|
|
credential_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
encrypted_config: Mapped[str] = mapped_column(sa.Text, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.current_timestamp())
|