Files
2025-05-25 14:16:56 +08:00

69 lines
2.0 KiB
Python

import asyncio
from datetime import datetime
from pydantic import BaseModel, Field
from global_context import GlobalContext
# 定义请求模型,用于接收用户的查询请求
class QueryRequest(BaseModel):
"""
用户查询请求的数据模型。
包含提示词、最大生成 token 数量和是否启用思考模式。
"""
prompt: str = Field(..., description="用户输入的提示词")
max_new_tokens: int = Field(
default=512, ge=1, le=32768, description="最大生成 token 数"
)
enable_thinking: bool = Field(default=True, description="是否启用思考模式")
# 定义响应模型,用于返回查询结果
class QueryResponse(BaseModel):
"""
查询结果的响应模型。
包含任务 ID、思考内容、模型回复内容和时间戳。
"""
task_id: str = Field(..., description="任务 ID")
thinking_content: str | None = Field(None, description="模型思考内容")
content: str = Field(..., description="模型回复内容")
timestamp: datetime = Field(..., description="响应时间")
async def process_query_task(
task_id: str, request: QueryRequest, manager: GlobalContext
):
"""
后台处理查询任务。
模拟处理过程并更新任务进度。
"""
manager.set_task_status(task_id, "processing")
for i in range(10):
await asyncio.sleep(10) # 模拟处理时间
progress = (i + 1) * 10.0
manager.update_task_progress(
task_id,
progress,
f"处理进度: {progress}%",
)
# os.system("python detect.py")
# with open(".....json", "r") as f:
# data = f.read()
# res = json.loads(f)
# 任务完成后,更新状态并存储结果
manager.set_task_status(task_id, "completed")
result = QueryResponse(
task_id=task_id,
thinking_content="思考中...",
content=f"模型回复: {request.prompt}",
timestamp=datetime.now(),
)
manager.set_task_result(task_id, result)