from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel
from typing import Optional, List
from module_admin.annotation.pydantic_annotation import as_query



class RagflowListQueryModel(BaseModel):
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    page: int  = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页数量')
    orderby: Optional[str] = Field(default='create_time', description='排序字段')
    desc: Optional[str] = Field(default='true', description='排序方式')
    name: Optional[str] = Field(default=None, description='名称')
    # dataset_id: Optional[str] = Field(default=None, description='数据集ID')
    # chat_id: Optional[str] = Field(default=None, description='聊天ID')
    
@as_query
class ListDocumentsQueryModel(BaseModel):
    """
    查询文档列表参数模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    page: int  = Field(default=1, description='当前页码')
    page_size: int = Field(default=10, description='每页数量')
    orderby: Optional[str] = Field(default='create_time', description='排序字段')
    desc: Optional[str] = Field(default='true', description='排序方式')
    keywords: Optional[str] = Field(default=None, description='关键字')
    document_id: Optional[str] = Field(default=None, description='文档ID')
    document_name: Optional[str] = Field(default=None, description='文档名称')
    
    
class UpdateFileModel(BaseModel):
    """
    更新文件模型
    """
    # name 要带文件后缀名
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    name: Optional[str] = Field(default=None, description='文件名称')
    meta_fields: Optional[dict] = Field(default=None, description='文件元数据')
    
    # naive, manual qa table paper book laws presentation picture one email
    chunk_method: Optional[str] = Field(default=None, description='分块方法')
    
    # 不同的分块方法有不同的参数.
    parser_config: Optional[dict] = Field(default=None, description='解析器配置')
    
    # status: Optional[str] = Field(default=None, description='状态')
    
    
class DocumentIdsModel(BaseModel):
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    
    documnet_ids: list[str] = Field(default=None, description='文件ID列表')
    
class DeleteFileModel(BaseModel):
    
    
    ids: List[str] = Field(description='文档ID列表')
    
    
class CreateDatasetModel(BaseModel):
    """
    创建数据集参数模型
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    
    name:str = Field(description='数据集名称')
    avatar: Optional[str] = Field(default=None, description='数据集头像, base64编码')
    description: Optional[str] = Field(default=None, description='数据集描述')
    embedding_model: Optional[str] = Field(default=None, description='数据集的embedding模型')
    permission: Optional[str] = Field(default = "me", description='数据集权限')
    chunk_method: Optional[str] = Field(default = "naive", description='数据集分块方法')
    parser_config: Optional[dict] = Field(default = None, description='数据集解析配置')
    
class LLM(BaseModel):
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    
    model_name: Optional[str] = Field(default=None, description='模型名称')
    temperature: Optional[float] = Field(default=0.1, description='模型温度')
    top_p: Optional[float] = Field(default=0.3, description='模型top_p')
    presence_penalty: Optional[float] = Field(default=0.2, description='模型presence_penalty')
    frequency_penalty: Optional[float] = Field(default=0.7, description='模型frequency_penalty')
    
class Prompt(BaseModel):
    """
    聊天助手提示词参数
    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    
    similarity_threshold: Optional[float] = Field(default=0.2, description='相似度阈值')
    keywords_similarity_weight: Optional[float] = Field(default=0.7, description='关键词相似度权重')
    top_n: Optional[int] = Field(default=8, description='返回结果数量')
    variables: Optional[List[dict]] = Field(default=[{"key": "knowledge", "optional": "true"}], description='变量列表')
    # 默认余弦相似度
    rerank_model: Optional[str] = Field(default=None, description='rerank模型')
    empty_response: Optional[str] = Field(default=None, description='空结果回复')
    opener: Optional[str] = Field(default=None, description='开启者')
    show_quote: Optional[bool] = Field(default=True, description='是否显示引用')
    prompt: Optional[str] = Field(default=None, description='提示语')
    
    
    
class UpdateChatAssistantModel(BaseModel):
    """ 修改聊天助手参数

    """
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True)
    chat_id: str = Field(default = None, description='聊天助手ID')
    name: Optional[str] = Field(default = None, description='聊天助手名称')
    # base64编码的图像
    avatar: Optional[str] = Field(default = None, description='聊天助手头像')
    # 启用了哪些数据集?
    dataset_ids: Optional[List[str]] = Field(default = None, description='数据集ID列表')
    
    # 模型配置参数
    llm: Optional[LLM] = Field(default = None, description='LLM模型')
    
    prompt: Optional[Prompt] = Field(default = None, description='LLM模型')
    
    
class CreateSessionWithChatModel(BaseModel):
    """
    创建会话及会话内容模型
    """
    
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True) 
    
    chat_id: str = Field(default = None, description='会话ID')
    name: str = Field(default = None, description='会话名称')
    user_id: Optional[str] = Field(default = None, description='用户ID')
    
class ConverseWithChatAssistantModel(BaseModel):
    """
    会话聊天模型
    """
    
    model_config = ConfigDict(alias_generator=to_camel, from_attributes=True) 
    
    chat_id: str = Field(default = None, description='会话ID')
    question: str = Field(default = None, description='问题')
    stream: Optional[bool] = Field(default = True, description='是否流式返回')
    session_id: Optional[str] = Field(default = None, description='会话ID')
    user_id: Optional[str] = Field(default = None, description='用户ID')
    

    
    
    
    
    
    