Files
demo2-qwen/main.py
2026-03-10 10:35:03 +08:00

82 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""FastAPI 应用主文件"""
import logging
import asyncio
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from app.agents import orchestrate_agents
from app.config import get_settings
from app.routers import session as session_router
# 初始化日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# 初始化设置
settings = get_settings()
# 创建 FastAPI 应用
app = FastAPI(
title=settings.project_name,
version=settings.project_version,
debug=settings.fastapi_debug,
)
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册交互式会话路由
app.include_router(session_router.router)
class FullWorkflowResponse(BaseModel):
"""完整工作流响应"""
status: str = "success"
@app.post("/workflow/full", response_model=FullWorkflowResponse)
async def full_workflow(request: Request):
try:
body_bytes = await request.body()
message = body_bytes.decode('utf-8')
logger.info(f"开始处理需求: {message}")
# 调用编排函数执行三个Agent的工作流
asyncio.create_task( orchestrate_agents(message))
# 构建响应
response = FullWorkflowResponse(
status="success"
)
return response
except Exception as e:
logger.error(f"处理需求时出错: {str(e)}", exc_info=True)
return {
"error": str(e),
"status": "error"
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host=settings.fastapi_host,
port=settings.fastapi_port,
log_level="info",
)