37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
"""
|
||
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}')>"
|