[MODIFY] Code optimization
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
"""API Key Schema"""
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import Optional, List
|
||||
import datetime
|
||||
import uuid
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from pydantic.v1 import validator
|
||||
from typing import Optional, List
|
||||
|
||||
from app.models.api_key_model import ApiKeyType
|
||||
from app.models.api_key_model import ApiKeyType, ResourceType
|
||||
|
||||
|
||||
class ApiKeyCreate(BaseModel):
|
||||
@@ -14,27 +15,55 @@ class ApiKeyCreate(BaseModel):
|
||||
type: ApiKeyType = Field(..., description="API Key 类型")
|
||||
scopes: List[str] = Field(default_factory=list, description="权限范围列表")
|
||||
resource_id: Optional[uuid.UUID] = Field(None, description="关联资源ID")
|
||||
resource_type: Optional[str] = Field(None, description="资源类型")
|
||||
rate_limit: Optional[int] = Field(100, description="速率限制(请求/分钟)", ge=1)
|
||||
resource_type: Optional[ResourceType] = Field(None, description="资源类型")
|
||||
rate_limit: Optional[int] = Field(10, ge=1, le=1000, description="QPS限制(请求/秒)")
|
||||
daily_request_limit: Optional[int] = Field(10000, description="日请求限制", ge=1)
|
||||
quota_limit: Optional[int] = Field(None, description="配额限制(总请求数)", ge=1)
|
||||
expires_at: Optional[datetime.datetime] = Field(None, description="过期时间")
|
||||
|
||||
@validator('scopes')
|
||||
def validate_scopes(cls, v):
|
||||
"""验证权限范围格式"""
|
||||
valid_scopes = [
|
||||
"app:all",
|
||||
"rag:search", "rag:upload", "rag:delete",
|
||||
"memory:read", "memory:write", "memory:delete", "memory:search"
|
||||
]
|
||||
for scope in v:
|
||||
if scope not in valid_scopes:
|
||||
raise ValueError(f"无效范围: {scope}")
|
||||
return v
|
||||
|
||||
|
||||
class ApiKeyUpdate(BaseModel):
|
||||
"""更新 API Key"""
|
||||
"""更新 API Key配置"""
|
||||
name: Optional[str] = Field(None, description="API Key 名称", max_length=255)
|
||||
description: Optional[str] = Field(None, description="描述")
|
||||
scopes: Optional[List[str]] = Field(None, description="权限范围列表")
|
||||
rate_limit: Optional[int] = Field(None, description="速率限制(请求/分钟)", ge=1)
|
||||
daily_request_limit: Optional[int] = Field(10000, description="每日请求数限制", ge=1)
|
||||
quota_limit: Optional[int] = Field(None, description="配额限制(总请求数)", ge=1)
|
||||
is_active: Optional[bool] = Field(None, description="是否激活")
|
||||
expires_at: Optional[datetime.datetime] = Field(None, description="过期时间")
|
||||
|
||||
@validator('scopes')
|
||||
def validate_scopes(cls, v):
|
||||
"""验证权限范围格式"""
|
||||
valid_scopes = {
|
||||
'app:all',
|
||||
'rag:search', 'rag:upload', 'rag:delete',
|
||||
'memory:read', 'memory:write', 'memory:delete', 'memory:search'
|
||||
}
|
||||
for scope in v:
|
||||
if scope not in valid_scopes:
|
||||
raise ValueError(f"无效范围: {scope}")
|
||||
return v
|
||||
|
||||
|
||||
class ApiKeyResponse(BaseModel):
|
||||
"""API Key 响应(创建时返回,包含明文 Key)"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
id: uuid.UUID
|
||||
name: str
|
||||
description: Optional[str]
|
||||
@@ -45,6 +74,7 @@ class ApiKeyResponse(BaseModel):
|
||||
resource_id: Optional[uuid.UUID]
|
||||
resource_type: Optional[str]
|
||||
rate_limit: int
|
||||
daily_request_limit: int
|
||||
quota_limit: Optional[int]
|
||||
expires_at: Optional[datetime.datetime]
|
||||
created_at: datetime.datetime
|
||||
@@ -53,7 +83,7 @@ class ApiKeyResponse(BaseModel):
|
||||
class ApiKey(BaseModel):
|
||||
"""API Key 信息(不包含明文 Key)"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
id: uuid.UUID
|
||||
name: str
|
||||
description: Optional[str]
|
||||
@@ -63,6 +93,7 @@ class ApiKey(BaseModel):
|
||||
resource_id: Optional[uuid.UUID]
|
||||
resource_type: Optional[str]
|
||||
rate_limit: int
|
||||
daily_request_limit: int
|
||||
quota_limit: Optional[int]
|
||||
quota_used: int
|
||||
expires_at: Optional[datetime.datetime]
|
||||
@@ -102,3 +133,27 @@ class ApiKeyAuth(BaseModel):
|
||||
scopes: List[str]
|
||||
resource_id: Optional[uuid.UUID]
|
||||
resource_type: Optional[str]
|
||||
|
||||
|
||||
class ApiKeyLog(BaseModel):
|
||||
"""API Key 使用日志"""
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: uuid.UUID
|
||||
api_key_id: uuid.UUID
|
||||
|
||||
# 请求信息
|
||||
endpoint: str
|
||||
method: str
|
||||
ip_address: Optional[str]
|
||||
user_agent: Optional[str]
|
||||
|
||||
# 响应信息
|
||||
status_code: Optional[int]
|
||||
response_time: Optional[int] # 毫秒
|
||||
|
||||
# 业务信息
|
||||
tokens_used: Optional[int]
|
||||
|
||||
# 时间信息
|
||||
created_at: datetime.datetime
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from pydantic import BaseModel, Field
|
||||
import uuid
|
||||
from enum import StrEnum
|
||||
from app.core.rag.models.chunk import QAChunk
|
||||
from typing import Union
|
||||
|
||||
|
||||
class RetrieveType(StrEnum):
|
||||
@@ -9,12 +11,35 @@ class RetrieveType(StrEnum):
|
||||
SEMANTIC = "semantic"
|
||||
HYBRID = "hybrid"
|
||||
|
||||
|
||||
class ChunkCreate(BaseModel):
|
||||
content: str
|
||||
content: Union[str, QAChunk] = Field(
|
||||
description="Content can be either a string or a QAChunk object"
|
||||
)
|
||||
|
||||
@property
|
||||
def chunk_content(self) -> str:
|
||||
"""
|
||||
Get the actual content string regardless of input type
|
||||
"""
|
||||
if isinstance(self.content, QAChunk):
|
||||
return f"question: {self.content.question} answer: {self.content.answer}"
|
||||
return self.content
|
||||
|
||||
|
||||
class ChunkUpdate(BaseModel):
|
||||
content: str | None = Field(None)
|
||||
content: Union[str, QAChunk] = Field(
|
||||
description="Content can be either a string or a QAChunk object"
|
||||
)
|
||||
|
||||
@property
|
||||
def chunk_content(self) -> str:
|
||||
"""
|
||||
Get the actual content string regardless of input type
|
||||
"""
|
||||
if isinstance(self.content, QAChunk):
|
||||
return f"question: {self.content.question} answer: {self.content.answer}"
|
||||
return self.content
|
||||
|
||||
|
||||
class ChunkRetrieve(BaseModel):
|
||||
@@ -23,4 +48,4 @@ class ChunkRetrieve(BaseModel):
|
||||
similarity_threshold: float | None = Field(None)
|
||||
vector_similarity_weight: float | None = Field(None)
|
||||
top_k: int | None = Field(None)
|
||||
retrieve_type: RetrieveType | None = Field(None)
|
||||
retrieve_type: RetrieveType | None = Field(None)
|
||||
|
||||
@@ -15,3 +15,7 @@ class Write_UserInput(BaseModel):
|
||||
message: str
|
||||
group_id: str
|
||||
config_id: Optional[str] = None
|
||||
|
||||
class End_User_Information(BaseModel):
|
||||
end_user_name: str # 这是要更新的用户名
|
||||
id: str # 宿主ID,用于匹配条件
|
||||
|
||||
@@ -30,6 +30,31 @@ class ExecutionConfig(BaseModel):
|
||||
parallel_limit: int = Field(default=3, ge=1, le=10, description="并行限制")
|
||||
retry_on_failure: bool = Field(default=True, description="失败时是否重试")
|
||||
max_retries: int = Field(default=3, ge=0, le=10, description="最大重试次数")
|
||||
|
||||
# 新增:路由模式配置
|
||||
routing_mode: str = Field(
|
||||
default="master_agent",
|
||||
pattern="^(master_agent|llm_router|rule_only)$",
|
||||
description="路由模式:master_agent(Master Agent决策)| llm_router(旧LLM路由器)| rule_only(仅规则路由)"
|
||||
)
|
||||
enable_rule_fast_path: bool = Field(
|
||||
default=True,
|
||||
description="是否启用规则快速路径(性能优化,高置信度关键词直接返回)"
|
||||
)
|
||||
|
||||
# 新增:结果整合模式配置
|
||||
result_merge_mode: str = Field(
|
||||
default="smart",
|
||||
pattern="^(smart|master)$",
|
||||
description="结果整合模式:smart(规则去重,快速)| master(Master Agent 智能整合,连贯)"
|
||||
)
|
||||
|
||||
# 新增:子 Agent 执行模式配置
|
||||
sub_agent_execution_mode: str = Field(
|
||||
default="parallel",
|
||||
pattern="^(parallel|sequential)$",
|
||||
description="子 Agent 执行模式:parallel(并行执行,快速)| sequential(串行执行,节省资源)"
|
||||
)
|
||||
|
||||
|
||||
# ==================== 多 Agent 配置 ====================
|
||||
|
||||
@@ -65,6 +65,7 @@ class User(UserBase):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
@field_validator("last_login_at", mode="before")
|
||||
@classmethod
|
||||
def _last_login_to_ms(cls, v):
|
||||
if v is None:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user