Files
demo2-qwen/main.py

116 lines
2.9 KiB
Python
Raw Normal View History

2026-03-04 13:56:08 +08:00
"""FastAPI 应用主文件"""
import logging
2026-03-05 15:22:21 +08:00
from fastapi import FastAPI, Request
2026-03-04 13:56:08 +08:00
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 RequirementRequest(BaseModel):
"""需求分析请求模型"""
2026-03-05 15:10:51 +08:00
message: str
2026-03-04 13:56:08 +08:00
class FullWorkflowResponse(BaseModel):
"""完整工作流响应"""
status: str = "success"
@app.post("/workflow/full", response_model=FullWorkflowResponse)
2026-03-05 15:22:21 +08:00
async def full_workflow(request: Request):
2026-03-04 13:56:08 +08:00
"""
完整工作流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:
2026-03-05 15:22:21 +08:00
# 2. 获取原始 body字节串
body_bytes = await request.body()
# 3. 尝试解码为字符串并打印
try:
body_str = body_bytes.decode('utf-8')
print(f"Raw body (string): {body_str}")
except UnicodeDecodeError:
print("Raw body is not valid UTF-8")
# 4. 尝试解析 JSON如果失败则记录错误但不影响返回
try:
json_data = await request.json()
print(f"Parsed JSON: {json_data}")
except Exception as e:
print(f"JSON parse error: {e}")
# logger.info(f"开始处理需求: {request.message}")
#
# # 调用编排函数执行三个Agent的工作流
# await orchestrate_agents(request.message)
#
# # 构建响应
2026-03-04 13:56:08 +08:00
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",
)