63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
"""
|
|
AI 模型使用日志模型
|
|
==================
|
|
|
|
存储模型调用的使用日志,用于成本统计和监控。
|
|
|
|
模型配置已迁移到 YAML 文件:config/ai_models.yaml
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, Column, DateTime, Float, Index, Integer, String, Text
|
|
|
|
from app.db.session import Base
|
|
|
|
|
|
class ModelUsageLog(Base):
|
|
"""模型使用日志 - 用于成本统计和监控"""
|
|
|
|
__tablename__ = "model_usage_logs"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
# 调用信息
|
|
model_id = Column(String(100), nullable=False)
|
|
platform_id = Column(String(50), nullable=False)
|
|
|
|
# 调用类型
|
|
task_type = Column(String(50), nullable=False) # script、polish、chat
|
|
|
|
# Token 用量
|
|
prompt_tokens = Column(Integer, default=0)
|
|
completion_tokens = Column(Integer, default=0)
|
|
total_tokens = Column(Integer, default=0)
|
|
|
|
# 成本(计算后的人民币)
|
|
cost_cny = Column(Float, default=0.0)
|
|
|
|
# 性能
|
|
response_time_ms = Column(Integer, nullable=True) # 响应时间
|
|
|
|
# 结果
|
|
success = Column(Boolean, default=True)
|
|
error_message = Column(Text, nullable=True)
|
|
|
|
# 用户/项目
|
|
user_id = Column(String(50), nullable=True)
|
|
project_id = Column(String(50), nullable=True)
|
|
|
|
# 时间
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# 索引定义
|
|
__table_args__ = (
|
|
# 索引:按用户查询使用记录
|
|
Index("ix_model_usage_logs_user_id", "user_id"),
|
|
# 索引:按时间查询(用于统计)
|
|
Index("ix_model_usage_logs_created_at", "created_at"),
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f"<UsageLog {self.model_id}: {self.total_tokens} tokens>"
|