call_record.py 3.19 KB
"""调用记录 SQLAlchemy 模型"""
import uuid
from datetime import datetime
from sqlalchemy import Integer, DateTime
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.mysql import VARCHAR, TEXT

from app.core.database import Base
from app.models._types import JsonAsText


class CallRecord(Base):
    """模型调用记录表"""
    __tablename__ = "ai_llmbase_call_records"

    id: Mapped[str] = mapped_column(
        VARCHAR(36), primary_key=True,
        default=lambda: str(uuid.uuid4())
    )
    request_md5: Mapped[str] = mapped_column(
        VARCHAR(32), nullable=False, unique=True,
        comment="请求内容 MD5 哈希,用于去重"
    )
    account_id: Mapped[str] = mapped_column(
        VARCHAR(36), nullable=False,
        comment="关联账号 ID(逻辑外键 → ai_llmbase_model_account_config.id)"
    )
    provider_id: Mapped[str] = mapped_column(
        VARCHAR(36), nullable=False,
        comment="关联供应商 ID(逻辑外键 → ai_llmbase_model_providers.id)"
    )
    model_name: Mapped[str] = mapped_column(
        VARCHAR(100), nullable=False,
        comment="调用的模型名称"
    )
    biz_type: Mapped[str | None] = mapped_column(
        VARCHAR(50), nullable=True,
        comment="业务类型"
    )
    template_id: Mapped[str | None] = mapped_column(
        VARCHAR(36), nullable=True,
        comment="关联提示词模板 ID(逻辑外键 → ai_llmbase_prompt_templates.id)"
    )
    sensitive_replacements: Mapped[list | None] = mapped_column(
        JsonAsText, nullable=True,
        comment="实际被替换的敏感词列表:[{word, replacement}, ...]"
    )
    request_body: Mapped[dict | None] = mapped_column(
        JsonAsText, nullable=False,
        comment="请求体 JSON"
    )
    response_body: Mapped[dict | None] = mapped_column(
        JsonAsText, nullable=True,
        comment="响应体 JSON"
    )
    prompt_tokens: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="Prompt Token 消耗"
    )
    completion_tokens: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="Completion Token 消耗"
    )
    total_tokens: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="总 Token 消耗"
    )
    duration_ms: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0,
        comment="调用耗时(毫秒)"
    )
    status: Mapped[str] = mapped_column(
        VARCHAR(20), nullable=False, default="pending",
        comment="状态:pending/success/failed/cancelled"
    )
    error_message: Mapped[str | None] = mapped_column(
        TEXT, nullable=True,
        comment="错误信息"
    )
    called_at: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.now,
        comment="调用时间"
    )
    created_at: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.now,
        comment="创建时间"
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.now, onupdate=datetime.now,
        comment="更新时间"
    )

    def __repr__(self) -> str:
        return f"<CallRecord {self.model_name} {self.status}>"