Update app.py
Browse files8 unmodified lines91011121314151693 unmodified lines1101111121111121131141151161171185 unmodified lines1241251261241271281291301311321331341351361371381391401411421431441451461471488 unmodified lines"""import loggingimport json
import aiohttpimport requestsfrom requests.exceptions import RequestExceptionfrom fastapi import FastAPI, HTTPException, Request, status, Query, Body93 unmodified lines
class QuestionRequest(BaseModel): question: str = Field(..., description="用户提出的问题") context: str | None = Field(None, description="可选上下文,用于更准确回答") text: str = Field(..., description="用户提出的问题") end_flag: bool = True streaming: bool = False model: str = Field(..., description="要使用的模型ID")
5 unmodified lines auth_data = json.loads(params) except json.JSONDecodeError: raise HTTPException(status_code=400, detail="参数 `params` 必须是有效的 JSON 字符串") return json.dumps({"req": auth_data}) 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}")
# 保持原来的模型列表端点app.py+24-3