Files
demo2-qwen/main.py
2026-03-05 15:26:07 +08:00

97 lines
2.3 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
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
# 初始化日志
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=["*"],
)
class FullWorkflowResponse(BaseModel):
"""完整工作流响应"""
status: str = "success"
@app.post("/workflow/full", response_model=FullWorkflowResponse)
async def full_workflow(request: Request):
"""
完整工作流PM Agent -> QA Agent -> Dev Agent
这个端点接收一个简单的需求描述通过三个Agent的协作
最终生成完整的需求分析、测试用例和Java代码。
Args:
request: RequirementRequest 包含简单的需求描述
Returns:
FullWorkflowResponse 包含:
- requirement_analysis: PM Agent的需求分析结果
- test_cases: QA Agent生成的测试用例
- code_generation: Dev Agent生成的Java代码和单元测试
Example:
{
"requirement": "创建一个用户管理系统,支持用户注册、登录、个人信息管理"
}
"""
try:
body_bytes = await request.body()
message = body_bytes.decode('utf-8')
logger.info(f"开始处理需求: {message}")
# 调用编排函数执行三个Agent的工作流
await 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",
)