48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
|
|
import json
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from fastapi import APIRouter, HTTPException
|
||
|
|
from fastapi.responses import StreamingResponse
|
||
|
|
|
||
|
|
from app.schemas.fashion_agent import FashionAgentRequest
|
||
|
|
from app.service.fashion_agent.service import FashionAgentService
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
logger = logging.getLogger()
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/agent/stream")
|
||
|
|
async def fashion_agent_stream(request_item: FashionAgentRequest):
|
||
|
|
"""
|
||
|
|
服装设计 Agent(流式输出)
|
||
|
|
|
||
|
|
- **message**: 用户输入的消息(必填)
|
||
|
|
- **user_id**: 用户ID,默认 "agent"
|
||
|
|
- **enable_thinking**: 是否开启思考模式
|
||
|
|
- **call_print**: 是否直接调用 print 生成印花
|
||
|
|
- **print_need_prompt_generation**: print 是否需要 LLM 生成 prompt
|
||
|
|
- **call_logo**: 是否直接调用 logo 生成装饰图案
|
||
|
|
- **call_sketch**: 是否直接调用 sketch 生成草图
|
||
|
|
- **sketch_need_prompt_generation**: sketch 是否需要 LLM 生成 prompt
|
||
|
|
- **call_design**: 是否直接调用 design 生成设计系列
|
||
|
|
- **design_request_data**: design 请求参数(objects, process_id, requestId, callback_url)
|
||
|
|
- **call_trending**: 是否直接调用 trending 趋势分析
|
||
|
|
- **call_explor**: 是否直接调用 explorer 灵感探索
|
||
|
|
- **provider**: 图片源 (pexels/unsplash),默认 unsplash
|
||
|
|
|
||
|
|
返回 SSE 事件流:
|
||
|
|
- **tools** 事件:工具调用的 started/finished 状态
|
||
|
|
- **custom** 事件:design 工具的逐个生成结果
|
||
|
|
- **Done** 事件:流结束标记
|
||
|
|
"""
|
||
|
|
try:
|
||
|
|
logger.info(f"fashion_agent stream request: {json.dumps(request_item.model_dump(), indent=4, ensure_ascii=False)}")
|
||
|
|
service = FashionAgentService()
|
||
|
|
return StreamingResponse(
|
||
|
|
service.run_stream(request_item),
|
||
|
|
media_type="text/event-stream",
|
||
|
|
)
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"fashion_agent stream exception: {e}")
|
||
|
|
raise HTTPException(status_code=500, detail=str(e))
|