Files
dsProject/dsLightRag/Routes/JiMengRoute.py
2025-08-21 09:34:51 +08:00

51 lines
1.4 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.

import logging
import fastapi
from fastapi import APIRouter
from fastapi import HTTPException
from openai import AsyncOpenAI
from Config import Config
from JiMeng.Kit.JmTxt2ImgUtil import JmTxt2Img
# 创建路由路由器
router = APIRouter(prefix="/api/jimeng", tags=["即梦"])
# 配置日志
logger = logging.getLogger(__name__)
# 初始化异步 OpenAI 客户端
client = AsyncOpenAI(
api_key=Config.ALY_LLM_API_KEY,
base_url=Config.ALY_LLM_BASE_URL
)
@router.post("/prompt_input")
async def prompt_input(request: fastapi.Request):
try:
data = await request.json()
prompt = data.get("prompt")
if not prompt:
raise HTTPException(status_code=400, detail="缺少提示词参数")
logger.info(f"收到图片生成请求,提示词: {prompt}")
# 调用 JmTxt2Img 生成图片
image_url = JmTxt2Img.generate_image(prompt)
logger.info(f"图片生成成功URL: {image_url}")
return {
"code": 200,
"message": "成功",
"data": {
"image_url": image_url
}
}
except HTTPException as e:
logger.error(f"请求参数错误: {str(e.detail)}")
raise e
except Exception as e:
logger.error(f"图片生成失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片生成失败: {str(e)}")