1.0.0初始化源代码
This commit is contained in:
53
backend/app/schemas/__init__.py
Normal file
53
backend/app/schemas/__init__.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Pydantic Schema 定义
|
||||
"""
|
||||
from .virtual_user import (
|
||||
VirtualUserCreate,
|
||||
VirtualUserUpdate,
|
||||
VirtualUserResponse,
|
||||
VirtualUserListResponse,
|
||||
VirtualUserGenerateRequest,
|
||||
VirtualUserImportRequest,
|
||||
ActivityLevel,
|
||||
UserStatus,
|
||||
)
|
||||
from .interaction import (
|
||||
InteractionRecordResponse,
|
||||
InteractionRecordListResponse,
|
||||
InteractionType,
|
||||
InteractionStatus,
|
||||
)
|
||||
from .token_usage import TokenUsageResponse, TokenUsageStats
|
||||
from .system_config import SystemConfigResponse, SystemConfigUpdate
|
||||
from .ai_model import AIModelConfigCreate, AIModelConfigUpdate, AIModelConfigResponse
|
||||
from .dashboard import DashboardStats, DashboardTokenStats
|
||||
|
||||
__all__ = [
|
||||
# Virtual User
|
||||
"VirtualUserCreate",
|
||||
"VirtualUserUpdate",
|
||||
"VirtualUserResponse",
|
||||
"VirtualUserListResponse",
|
||||
"VirtualUserGenerateRequest",
|
||||
"VirtualUserImportRequest",
|
||||
"ActivityLevel",
|
||||
"UserStatus",
|
||||
# Interaction
|
||||
"InteractionRecordResponse",
|
||||
"InteractionRecordListResponse",
|
||||
"InteractionType",
|
||||
"InteractionStatus",
|
||||
# Token Usage
|
||||
"TokenUsageResponse",
|
||||
"TokenUsageStats",
|
||||
# System Config
|
||||
"SystemConfigResponse",
|
||||
"SystemConfigUpdate",
|
||||
# AI Model
|
||||
"AIModelConfigCreate",
|
||||
"AIModelConfigUpdate",
|
||||
"AIModelConfigResponse",
|
||||
# Dashboard
|
||||
"DashboardStats",
|
||||
"DashboardTokenStats",
|
||||
]
|
||||
64
backend/app/schemas/ai_model.py
Normal file
64
backend/app/schemas/ai_model.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
AI 模型配置相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class AIModelConfigBase(BaseModel):
|
||||
"""AI 模型配置基础 Schema"""
|
||||
model_name: str = Field(..., description="模型名称", max_length=100)
|
||||
provider: str = Field(..., description="提供商", max_length=50)
|
||||
display_name: Optional[str] = Field(None, description="显示名称", max_length=200)
|
||||
api_url: str = Field(..., description="API 地址", max_length=500)
|
||||
api_key: str = Field(..., description="API Key", max_length=500)
|
||||
temperature: float = Field(0.7, description="温度", ge=0, le=1)
|
||||
max_tokens: int = Field(1000, description="最大 Token 数", ge=1)
|
||||
|
||||
|
||||
class AIModelConfigCreate(AIModelConfigBase):
|
||||
"""创建 AI 模型配置请求"""
|
||||
description: Optional[str] = Field(None, description="模型描述")
|
||||
|
||||
|
||||
class AIModelConfigUpdate(BaseModel):
|
||||
"""更新 AI 模型配置请求"""
|
||||
display_name: Optional[str] = Field(None, description="显示名称", max_length=200)
|
||||
api_url: Optional[str] = Field(None, description="API 地址", max_length=500)
|
||||
api_key: Optional[str] = Field(None, description="API Key", max_length=500)
|
||||
temperature: Optional[float] = Field(None, description="温度", ge=0, le=1)
|
||||
max_tokens: Optional[int] = Field(None, description="最大 Token 数", ge=1)
|
||||
is_default: Optional[bool] = Field(None, description="是否为默认模型")
|
||||
is_active: Optional[bool] = Field(None, description="是否启用")
|
||||
description: Optional[str] = Field(None, description="模型描述")
|
||||
|
||||
|
||||
class AIModelConfigResponse(AIModelConfigBase):
|
||||
"""AI 模型配置响应"""
|
||||
id: int
|
||||
api_version: Optional[str]
|
||||
top_p: float
|
||||
is_default: bool
|
||||
is_active: bool
|
||||
description: Optional[str]
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class AIModelTestRequest(BaseModel):
|
||||
"""AI 模型测试请求"""
|
||||
model_id: int = Field(..., description="模型 ID")
|
||||
test_prompt: str = Field(..., description="测试提示词", min_length=1, max_length=1000)
|
||||
|
||||
|
||||
class AIModelTestResponse(BaseModel):
|
||||
"""AI 模型测试响应"""
|
||||
success: bool
|
||||
content: Optional[str]
|
||||
tokens_used: int
|
||||
cost_time: float
|
||||
error_message: Optional[str]
|
||||
55
backend/app/schemas/dashboard.py
Normal file
55
backend/app/schemas/dashboard.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
控制台仪表盘相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import List, Optional
|
||||
|
||||
|
||||
class CoreStats(BaseModel):
|
||||
"""核心指标统计"""
|
||||
total_users: int = Field(0, description="虚拟用户总数")
|
||||
active_users: int = Field(0, description="已启用用户数")
|
||||
disabled_users: int = Field(0, description="已禁用用户数")
|
||||
|
||||
today_comments: int = Field(0, description="今日评论数")
|
||||
today_replies: int = Field(0, description="今日回复数")
|
||||
today_likes: int = Field(0, description="今日点赞数")
|
||||
today_favorites: int = Field(0, description="今日收藏数")
|
||||
today_shares: int = Field(0, description="今日转发数")
|
||||
|
||||
yesterday_comments: int = Field(0, description="昨日评论数")
|
||||
yesterday_replies: int = Field(0, description="昨日回复数")
|
||||
|
||||
month_tokens: int = Field(0, description="当月 Token 消耗")
|
||||
today_tokens: int = Field(0, description="今日 Token 消耗")
|
||||
remaining_tokens: int = Field(0, description="今日剩余 Token")
|
||||
|
||||
|
||||
class DashboardTokenStats(BaseModel):
|
||||
"""Token 统计"""
|
||||
today_used: int = Field(0, description="今日已用")
|
||||
today_limit: int = Field(0, description="今日限额")
|
||||
today_remaining: int = Field(0, description="今日剩余")
|
||||
usage_percentage: float = Field(0, description="使用百分比")
|
||||
|
||||
|
||||
class DailyUsageItem(BaseModel):
|
||||
"""每日使用项"""
|
||||
date: str
|
||||
tokens: int
|
||||
comments: int
|
||||
replies: int
|
||||
|
||||
|
||||
class MonthlyUsageItem(BaseModel):
|
||||
"""每月使用项"""
|
||||
month: str
|
||||
tokens: int
|
||||
|
||||
|
||||
class DashboardStats(BaseModel):
|
||||
"""控制台统计数据"""
|
||||
core_stats: CoreStats
|
||||
daily_token_usages: List[DailyUsageItem] = Field(default_factory=list)
|
||||
monthly_token_usages: List[MonthlyUsageItem] = Field(default_factory=list)
|
||||
recent_interactions: List[dict] = Field(default_factory=list)
|
||||
63
backend/app/schemas/interaction.py
Normal file
63
backend/app/schemas/interaction.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""
|
||||
互动记录相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class InteractionType(str, Enum):
|
||||
"""互动类型枚举"""
|
||||
COMMENT = "comment"
|
||||
REPLY = "reply"
|
||||
LIKE = "like"
|
||||
FAVORITE = "favorite"
|
||||
SHARE = "share"
|
||||
|
||||
|
||||
class InteractionStatus(str, Enum):
|
||||
"""互动状态枚举"""
|
||||
PENDING = "pending"
|
||||
SUCCESS = "success"
|
||||
FAILED = "failed"
|
||||
RETRYING = "retrying"
|
||||
|
||||
|
||||
class InteractionRecordBase(BaseModel):
|
||||
"""互动记录基础 Schema"""
|
||||
virtual_user_id: int = Field(..., description="虚拟用户 ID")
|
||||
news_id: str = Field(..., description="新闻 ID")
|
||||
interaction_type: InteractionType = Field(..., description="互动类型")
|
||||
content: Optional[str] = Field(None, description="互动内容")
|
||||
target_comment_id: Optional[str] = Field(None, description="目标评论 ID")
|
||||
|
||||
|
||||
class InteractionRecordResponse(InteractionRecordBase):
|
||||
"""互动记录响应"""
|
||||
id: int
|
||||
news_title: Optional[str]
|
||||
status: InteractionStatus
|
||||
retry_count: int
|
||||
error_message: Optional[str]
|
||||
ai_model_used: Optional[str]
|
||||
tokens_used: int
|
||||
execution_time: datetime
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class InteractionRecordListResponse(BaseModel):
|
||||
"""互动记录列表响应"""
|
||||
total: int
|
||||
items: List[InteractionRecordResponse]
|
||||
|
||||
|
||||
class InteractionExecuteRequest(BaseModel):
|
||||
"""执行互动请求"""
|
||||
virtual_user_id: int = Field(..., description="虚拟用户 ID")
|
||||
news_id: Optional[str] = Field(None, description="新闻 ID(不传则随机选择)")
|
||||
interaction_type: Optional[InteractionType] = Field(None, description="互动类型(不传则随机)")
|
||||
force_execute: bool = Field(False, description="是否强制执行(忽略限额)")
|
||||
60
backend/app/schemas/system_config.py
Normal file
60
backend/app/schemas/system_config.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
系统配置相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, Dict, Any
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class SystemConfigBase(BaseModel):
|
||||
"""系统配置基础 Schema"""
|
||||
config_key: str = Field(..., description="配置键", max_length=100)
|
||||
config_value: Dict[str, Any] = Field(..., description="配置值")
|
||||
config_type: Optional[str] = Field(None, description="配置类型", max_length=50)
|
||||
description: Optional[str] = Field(None, description="配置描述")
|
||||
|
||||
|
||||
class SystemConfigCreate(SystemConfigBase):
|
||||
"""创建系统配置请求"""
|
||||
pass
|
||||
|
||||
|
||||
class SystemConfigUpdate(BaseModel):
|
||||
"""更新系统配置请求"""
|
||||
config_value: Optional[Dict[str, Any]] = Field(None, description="配置值")
|
||||
description: Optional[str] = Field(None, description="配置描述")
|
||||
is_active: Optional[bool] = Field(None, description="是否启用")
|
||||
|
||||
|
||||
class SystemConfigResponse(SystemConfigBase):
|
||||
"""系统配置响应"""
|
||||
id: int
|
||||
is_active: bool
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class ScheduleConfig(BaseModel):
|
||||
"""调度配置"""
|
||||
task_start_hour: int = Field(9, description="活动开始时间", ge=0, le=23)
|
||||
task_end_hour: int = Field(22, description="活动结束时间", ge=0, le=23)
|
||||
task_interval_min: int = Field(10, description="最小间隔(分钟)", ge=1)
|
||||
task_interval_max: int = Field(30, description="最大间隔(分钟)", ge=1)
|
||||
is_task_running: bool = Field(False, description="任务是否运行中")
|
||||
|
||||
|
||||
class LimitConfig(BaseModel):
|
||||
"""限额配置"""
|
||||
max_tokens_per_day: int = Field(10000, description="每日 Token 上限", ge=0)
|
||||
max_comments_per_user_per_day: int = Field(20, description="单用户每日最大评论数", ge=0)
|
||||
max_replies_per_user_per_day: int = Field(10, description="单用户每日最大回复数", ge=0)
|
||||
|
||||
|
||||
class ProbabilityConfig(BaseModel):
|
||||
"""概率配置"""
|
||||
like_probability: float = Field(0.8, description="点赞概率", ge=0, le=1)
|
||||
favorite_probability: float = Field(0.5, description="收藏概率", ge=0, le=1)
|
||||
share_probability: float = Field(0.3, description="转发概率", ge=0, le=1)
|
||||
54
backend/app/schemas/token_usage.py
Normal file
54
backend/app/schemas/token_usage.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
Token 使用相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
from datetime import date, datetime
|
||||
|
||||
|
||||
class TokenUsageBase(BaseModel):
|
||||
"""Token 使用基础 Schema"""
|
||||
tokens_used: int = Field(..., description="使用的 Token 数量")
|
||||
ai_model: str = Field(..., description="使用的 AI 模型")
|
||||
action_type: Optional[str] = Field(None, description="操作类型")
|
||||
|
||||
|
||||
class TokenUsageResponse(TokenUsageBase):
|
||||
"""Token 使用响应"""
|
||||
id: int
|
||||
virtual_user_id: Optional[int]
|
||||
interaction_id: Optional[int]
|
||||
tokens_prompt: int
|
||||
tokens_completion: int
|
||||
usage_date: date
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TokenUsageStats(BaseModel):
|
||||
"""Token 使用统计"""
|
||||
today_tokens: int = Field(0, description="今日 Token 数")
|
||||
yesterday_tokens: int = Field(0, description="昨日 Token 数")
|
||||
month_tokens: int = Field(0, description="当月 Token 数")
|
||||
remaining_tokens: int = Field(0, description="剩余 Token 数")
|
||||
total_limit: int = Field(0, description="总限额")
|
||||
|
||||
|
||||
class DailyTokenUsage(BaseModel):
|
||||
"""每日 Token 使用"""
|
||||
date: str
|
||||
tokens: int
|
||||
|
||||
|
||||
class MonthlyTokenUsage(BaseModel):
|
||||
"""每月 Token 使用"""
|
||||
month: str
|
||||
tokens: int
|
||||
|
||||
|
||||
class TokenUsageChartResponse(BaseModel):
|
||||
"""Token 使用图表响应"""
|
||||
daily_usages: List[DailyTokenUsage]
|
||||
monthly_usages: List[MonthlyTokenUsage]
|
||||
86
backend/app/schemas/virtual_user.py
Normal file
86
backend/app/schemas/virtual_user.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
虚拟用户相关 Schema
|
||||
"""
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ActivityLevel(str, Enum):
|
||||
"""活跃度枚举"""
|
||||
LOW = "low"
|
||||
MEDIUM = "medium"
|
||||
HIGH = "high"
|
||||
|
||||
|
||||
class UserStatus(str, Enum):
|
||||
"""用户状态枚举"""
|
||||
ACTIVE = "active"
|
||||
DISABLED = "disabled"
|
||||
|
||||
|
||||
class VirtualUserBase(BaseModel):
|
||||
"""虚拟用户基础 Schema"""
|
||||
nickname: str = Field(..., description="昵称", min_length=1, max_length=100)
|
||||
username: str = Field(..., description="用户名(账号)", min_length=1, max_length=100)
|
||||
password: str = Field(..., description="密码", min_length=1)
|
||||
avatar_url: Optional[str] = Field(None, description="头像 URL", max_length=500)
|
||||
writing_style: Optional[str] = Field(None, description="写作风格", max_length=50)
|
||||
activity_level: ActivityLevel = Field(default=ActivityLevel.MEDIUM, description="活跃度")
|
||||
persona_description: Optional[str] = Field(None, description="人格描述")
|
||||
|
||||
|
||||
class VirtualUserCreate(VirtualUserBase):
|
||||
"""创建虚拟用户请求"""
|
||||
pass
|
||||
|
||||
|
||||
class VirtualUserUpdate(BaseModel):
|
||||
"""更新虚拟用户请求"""
|
||||
nickname: Optional[str] = Field(None, description="昵称", min_length=1, max_length=100)
|
||||
password: Optional[str] = Field(None, description="密码", min_length=1)
|
||||
avatar_url: Optional[str] = Field(None, description="头像 URL", max_length=500)
|
||||
writing_style: Optional[str] = Field(None, description="写作风格", max_length=50)
|
||||
activity_level: Optional[ActivityLevel] = Field(None, description="活跃度")
|
||||
persona_description: Optional[str] = Field(None, description="人格描述")
|
||||
status: Optional[UserStatus] = Field(None, description="状态")
|
||||
|
||||
|
||||
class VirtualUserResponse(VirtualUserBase):
|
||||
"""虚拟用户响应"""
|
||||
id: int
|
||||
status: UserStatus
|
||||
is_logged_in: bool
|
||||
total_interactions: int
|
||||
today_comments: int
|
||||
today_replies: int
|
||||
last_interaction_time: Optional[datetime]
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class VirtualUserListResponse(BaseModel):
|
||||
"""虚拟用户列表响应"""
|
||||
total: int
|
||||
items: List[VirtualUserResponse]
|
||||
|
||||
|
||||
class VirtualUserGenerateRequest(BaseModel):
|
||||
"""生成虚拟用户请求"""
|
||||
count: int = Field(1, description="生成数量", ge=1, le=100)
|
||||
writing_styles: Optional[List[str]] = Field(None, description="写作风格列表")
|
||||
activity_levels: Optional[List[ActivityLevel]] = Field(
|
||||
[ActivityLevel.LOW, ActivityLevel.MEDIUM, ActivityLevel.HIGH],
|
||||
description="活跃度级别列表"
|
||||
)
|
||||
generate_persona: bool = Field(True, description="是否生成 AI 人格描述")
|
||||
|
||||
|
||||
class VirtualUserImportRequest(BaseModel):
|
||||
"""导入虚拟用户请求"""
|
||||
users: List[Dict[str, Any]] = Field(..., description="用户数据列表")
|
||||
generate_persona: bool = Field(True, description="是否为导入的用户生成 AI 人格描述")
|
||||
Reference in New Issue
Block a user