nex_docus/backend/app/models/llm_model_config.py

64 lines
2.5 KiB
Python
Raw Normal View History

2026-08-03 08:25:47 +00:00
"""
LLM 模型配置模型
"""
from sqlalchemy import Column, BigInteger, String, Integer, DateTime, Boolean, JSON
from sqlalchemy.sql import func
from app.core.database import Base
class LLMModelConfig(Base):
"""大模型配置表模型"""
__tablename__ = "llm_model_config"
config_id = Column(BigInteger, primary_key=True, autoincrement=True, comment="配置ID")
model_code = Column(String(128), nullable=False, unique=True, index=True, comment="模型编码")
model_name = Column(String(255), nullable=False, comment="模型名称")
model_type = Column(String(32), nullable=False, default="chat", index=True, comment="模型类型: chat/embedding")
provider = Column(String(64), comment="模型提供方")
endpoint_url = Column(String(512), comment="接口地址")
api_key = Column(String(512), comment="API Key")
llm_model_name = Column(String(128), nullable=False, comment="模型名称/部署名")
llm_timeout = Column(Integer, nullable=False, default=120, comment="超时时间(秒)")
type_config = Column(JSON, nullable=False, default=dict, comment="模型类型差异参数")
description = Column(String(500), comment="描述")
is_active = Column(Boolean, nullable=False, default=True, index=True, comment="是否启用")
is_default = Column(Boolean, nullable=False, default=False, comment="是否默认")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")
def _type_config_value(self, key, default=None):
return (self.type_config or {}).get(key, default)
@property
def llm_temperature(self):
return self._type_config_value("temperature", 0.70)
@property
def llm_top_p(self):
return self._type_config_value("top_p", 0.90)
@property
def llm_max_tokens(self):
return self._type_config_value("max_tokens", 8192)
@property
def llm_system_prompt(self):
return self._type_config_value("system_prompt")
@property
def embedding_dimension(self):
return self._type_config_value("dimension")
@property
def chunk_size(self):
return self._type_config_value("chunk_size", 800)
@property
def chunk_overlap(self):
return self._type_config_value("chunk_overlap", 150)
def __repr__(self):
return f"<LLMModelConfig(config_id={self.config_id}, model_code='{self.model_code}')>"