# !/usr/bin/python3 # -*- coding: utf-8 -*- """ @Author : Ailove ------------------------------------ @File : routes.py @CreateTime : 2025/5/22 20:36 ------------------------------------ """ import json import aiohttp from fastapi.responses import JSONResponse from fastapi import APIRouter, HTTPException, Query, Body from services import analyze_image from models import QuestionRequest router = APIRouter() @router.get("/") async def root(): return {"message": "Hello World"} @router.get("/health") async def health_check(): return {"status": "healthy"} @router.get("/image_similarity") async def analyze(url: str = Query(..., description="通过 URL 查询参数传递的 url")): try: body = {'url': url} data = analyze_image(body) result = {'status': 200, 'response': data} return JSONResponse(content=result, status_code=200) except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @router.post("/v1/question") async def ask_question( params: str = Query(..., description="通过 URL 查询参数传递的 token"), req: QuestionRequest = Body(...) ): try: auth_data = json.loads(params) except json.JSONDecodeError: raise HTTPException(status_code=400, detail="参数 `params` 必须是有效的 JSON 字符串") headers = { "content-type": "application/json", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36", } url = "https://api.journeydraw.ai/chatgpt/v3/question" data = {"text": req.text, "end_flag": req.end_flag, "streaming": req.streaming, "model": req.model} async with aiohttp.ClientSession() as session: try: async with session.post(url, params=auth_data, json=data, headers=headers) as resp: return await resp.text() except Exception as e: error_detail = str(e) status_code = 504 raise HTTPException(status_code=status_code, detail=f"xAI API错误: {error_detail}")