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