nex_docus/backend/app/models/chat_session.py

45 lines
2.0 KiB
Python

"""
知识库对话会话模型
"""
from sqlalchemy import Column, BigInteger, String, Integer, DateTime, Text, Boolean
from sqlalchemy.sql import func
from app.core.database import Base
class ChatSession(Base):
"""对话会话表"""
__tablename__ = "chat_session"
id = Column(BigInteger, primary_key=True, autoincrement=True, comment="会话ID")
project_id = Column(BigInteger, nullable=False, index=True, comment="项目ID")
user_id = Column(BigInteger, nullable=False, index=True, comment="用户ID")
llm_config_id = Column(BigInteger, nullable=False, comment="LLM配置ID")
title = Column(String(255), nullable=False, comment="会话标题")
description = Column(Text, comment="会话描述")
is_active = Column(Boolean, nullable=False, default=True, comment="是否激活")
message_count = Column(Integer, nullable=False, default=0, comment="消息数")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间")
def __repr__(self):
return f"<ChatSession(id={self.id}, project_id={self.project_id}, user_id={self.user_id})>"
class ChatMessage(Base):
"""对话消息表"""
__tablename__ = "chat_message"
id = Column(BigInteger, primary_key=True, autoincrement=True, comment="消息ID")
session_id = Column(BigInteger, nullable=False, index=True, comment="会话ID")
role = Column(String(32), nullable=False, comment="角色(user/assistant)")
content = Column(Text, nullable=False, comment="消息内容")
referenced_files = Column(Text, comment="参考文件(JSON数组)")
tokens_used = Column(Integer, comment="消耗的token数")
is_deleted = Column(Boolean, nullable=False, default=False, comment="是否已删除")
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
def __repr__(self):
return f"<ChatMessage(id={self.id}, session_id={self.session_id}, role='{self.role}')>"