from datetime import datetime, timezone
from typing import Optional
from sqlmodel import SQLModel, Relationship, Field

class APIProvider(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True,allow_mutation=False)
    base_url: str = Field(...,min_length=1,description="API的基础URL，不能为空")
    model_id: str = Field(...,min_length=1,description="API使用的模型ID，不能为空")
    api_key: Optional[str] = Field(default=None, description="用于身份验证的API密钥")
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(timezone.utc),
        description="记录创建时间"
    )

class LLMParameters(SQLModel):
    temperature: Optional[float] = None
    max_tokens: Optional[int] = None
    top_p: Optional[float] = None
    frequency_penalty: Optional[float] = None
    presence_penalty: Optional[float] = None
    seed: Optional[int] = None

class TokensUsage(SQLModel):
    prompt_tokens: int = Field(default=0, description="提示词使用的token数量")
    completion_tokens: int = Field(default=0, description="完成部分使用的token数量")
    prompt_cache_hit_tokens: Optional[int] = Field(default=None, description="缓存命中token数量")
    prompt_cache_miss_tokens: Optional[int] = Field(default=None, description="缓存未命中token数量")

class LLMResponse(SQLModel):
    timestamp: datetime  = Field(
        default_factory=lambda: datetime.now(timezone.utc),
        description="响应的时间戳"
    )
    response_id: str = Field(..., description="响应的唯一ID")
    tokens_usage: TokensUsage = Field(default_factory=TokensUsage, description="token使用信息")
    content: str = Field(default_factory=dict, description="API响应的内容")
    total_duration: float = Field(default=0.0, description="请求的总时长，单位为秒")
    llm_parameters: Optional[LLMParameters] = Field(default=None, description="LLM参数")

class LLMRequest(SQLModel):
    prompt: str = Field(..., description="发送给API的提示词") 
    api_provider: APIProvider = Field(..., description="API提供者的信息")
    format: Optional[str] = Field(default=None, description="API响应的格式")
    response: list[LLMResponse] = Field(default_factory=list, description="API响应列表")
    error: Optional[list[str]] = Field(default=None, description="API请求过程中发生的错误信息")
    total_duration: float = Field(default=0.0, description="请求的总时长，单位为秒")
    total_tokens_usage: TokensUsage = Field(default_factory=TokensUsage, description="token使用信息")
