"""对话记录模型。""" from sqlalchemy import Column, Integer, String, Text, TIMESTAMP, func from insurance.db.compat import db class ChatRecord(db.Model): """对话记录表。""" __tablename__ = "insurance_chat_records" id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(String(64), nullable=False, comment="用户 ID") session_id = Column(String(128), nullable=False, comment="会话 ID") role = Column(String(16), nullable=False, comment="角色: user/assistant") content = Column(Text, nullable=False, comment="消息内容") message_id = Column(String(128), comment="BaoDan 消息 ID") model_id = Column(String(100), comment="模型 ID") message_tokens = Column(Integer, default=0, comment="用户输入 token 数") answer_tokens = Column(Integer, default=0, comment="助手回复 token 数") rating = Column(String(16), comment="评分: helpful/not_helpful") correction = Column(Text, comment="用户纠错内容") created_at = Column(TIMESTAMP, server_default=func.now()) def to_dict(self): return { "id": self.id, "user_id": self.user_id, "session_id": self.session_id, "role": self.role, "content": self.content, "message_id": self.message_id, "model_id": self.model_id, "message_tokens": self.message_tokens or 0, "answer_tokens": self.answer_tokens or 0, "rating": self.rating, "correction": self.correction, "created_at": str(self.created_at) if self.created_at else None, }