Files
huihuiSquare/backend/app/models/token_usage.py
yuqianqian10204095yu cebc0a288f 1.0.0初始化源代码
2026-03-23 15:40:36 +08:00

37 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Token 使用记录模型
"""
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Float, Date
from sqlalchemy.sql import func
from .base import Base
class TokenUsage(Base):
"""Token 使用记录表"""
__tablename__ = "token_usages"
id = Column(Integer, primary_key=True, autoincrement=True, comment="记录 ID")
# 关联信息
virtual_user_id = Column(Integer, ForeignKey("virtual_users.id"), nullable=True, index=True, comment="虚拟用户 ID可为空系统级消耗")
interaction_id = Column(Integer, ForeignKey("interaction_records.id"), nullable=True, comment="互动记录 ID")
# Token 信息
tokens_used = Column(Integer, nullable=False, comment="使用的 Token 数量")
tokens_prompt = Column(Integer, default=0, comment="提示词 Token 数")
tokens_completion = Column(Integer, default=0, comment="完成响应 Token 数")
# AI 模型信息
ai_model = Column(String(100), nullable=False, comment="使用的 AI 模型")
action_type = Column(String(50), comment="操作类型generate_comment/generate_reply 等)")
# 日期分区(便于统计)
usage_date = Column(Date, server_default=func.now(), index=True, comment="使用日期")
# 时间戳
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
def __repr__(self):
return f"<TokenUsage(id={self.id}, tokens={self.tokens_used}, model='{self.ai_model}')>"