95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
数据表模型
|
||
"""
|
||
from datetime import datetime, timedelta
|
||
from random import choices
|
||
|
||
from pydantic_ai._uuid import uuid7
|
||
from sqlmodel import Field, JSON, SQLModel
|
||
|
||
|
||
class CaptchaRecord(SQLModel, table=True):
|
||
"""
|
||
验证码记录
|
||
邮箱、验证码和失效时间联合作为主键
|
||
"""
|
||
|
||
email: str = Field(..., primary_key=True, description="邮箱")
|
||
captcha: str = Field(
|
||
default_factory=lambda: "".join(choices("0123456789", k=6)),
|
||
primary_key=True,
|
||
description="验证码",
|
||
)
|
||
is_valid: bool = Field(
|
||
default=True,
|
||
index=True,
|
||
description="验证码有效:True 表示有效,False 表示无效",
|
||
)
|
||
is_verified: bool = Field(
|
||
default=False,
|
||
index=True,
|
||
description="验证码已核验:True 表示已核验,False 表示未核验",
|
||
)
|
||
expired_at: datetime = Field(
|
||
default_factory=lambda: datetime.now() + timedelta(minutes=30),
|
||
primary_key=True,
|
||
description="失效时间",
|
||
)
|
||
|
||
|
||
# 用户记录
|
||
class UserRecord(SQLModel, table=True):
|
||
id: str = Field(
|
||
default_factory=lambda: str(uuid7()),
|
||
primary_key=True,
|
||
description="用户唯一标识",
|
||
)
|
||
email: str = Field(..., index=True, description="邮箱")
|
||
|
||
|
||
# 会话表
|
||
class ConversationRecord(SQLModel, table=True):
|
||
id: str = Field(
|
||
default_factory=lambda: str(uuid7()),
|
||
primary_key=True,
|
||
description="会话唯一标识",
|
||
)
|
||
user_id: str = Field(..., index=True, description="用户唯一标识")
|
||
description: str = Field(..., description="会话描述")
|
||
created_at: datetime = Field(default_factory=datetime.now, description="创建时间")
|
||
is_deleted: bool = Field(
|
||
default=False,
|
||
index=True,
|
||
description="已删除:True 表示已删除,False 表示未删除",
|
||
)
|
||
|
||
|
||
# 思考节点表(仅定义)
|
||
class ThoughtNodeRecord(SQLModel):
|
||
|
||
kind: str = Field(description="思考节点类型")
|
||
content: str = Field(default="", description="思考节点内容")
|
||
|
||
|
||
# 对话表
|
||
class DialogRecord(SQLModel, table=True):
|
||
id: str = Field(
|
||
default_factory=lambda: str(uuid7()),
|
||
primary_key=True,
|
||
description="对话唯一标识",
|
||
)
|
||
conversation_id: str = Field(..., index=True, description="会话唯一标识")
|
||
question: str = Field(..., description="问题")
|
||
thought_nodes: dict[int, ThoughtNodeRecord] = Field(
|
||
default_factory=dict, sa_type=JSON, description="思考节点字典"
|
||
)
|
||
answer: str = Field(default="", description="回答")
|
||
|
||
|
||
# 运行结果表
|
||
class RunResultRecord(SQLModel, table=True):
|
||
conversation_id: str = Field(primary_key=True, description="会话唯一标识")
|
||
dialog_id: str = Field(primary_key=True, description="对话唯一标识")
|
||
new_messages: str = Field(description="新增消息")
|