hf1024 commited on
Commit
bd1f585
·
verified ·
1 Parent(s): 95406a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -3
app.py CHANGED
@@ -9,6 +9,8 @@
9
  """
10
  import logging
11
  import json
 
 
12
  import requests
13
  from requests.exceptions import RequestException
14
  from fastapi import FastAPI, HTTPException, Request, status, Query, Body
@@ -108,8 +110,9 @@ async def chat_completions(req: ChatRequest, request: Request):
108
 
109
 
110
  class QuestionRequest(BaseModel):
111
- question: str = Field(..., description="用户提出的问题")
112
- context: str | None = Field(None, description="可选上下文,用于更准确回答")
 
113
  model: str = Field(..., description="要使用的模型ID")
114
 
115
 
@@ -121,7 +124,25 @@ async def ask_question(
121
  auth_data = json.loads(params)
122
  except json.JSONDecodeError:
123
  raise HTTPException(status_code=400, detail="参数 `params` 必须是有效的 JSON 字符串")
124
- return json.dumps({"req": auth_data})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
 
127
  # 保持原来的模型列表端点
 
9
  """
10
  import logging
11
  import json
12
+
13
+ import aiohttp
14
  import requests
15
  from requests.exceptions import RequestException
16
  from fastapi import FastAPI, HTTPException, Request, status, Query, Body
 
110
 
111
 
112
  class QuestionRequest(BaseModel):
113
+ text: str = Field(..., description="用户提出的问题")
114
+ end_flag: bool = True
115
+ streaming: bool = False
116
  model: str = Field(..., description="要使用的模型ID")
117
 
118
 
 
124
  auth_data = json.loads(params)
125
  except json.JSONDecodeError:
126
  raise HTTPException(status_code=400, detail="参数 `params` 必须是有效的 JSON 字符串")
127
+ headers = {
128
+ "content-type": "application/json",
129
+ "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",
130
+ }
131
+ url = "https://api.journeydraw.ai/chatgpt/v3/question"
132
+ data = {
133
+ "text":req.text,
134
+ "end_flag":req.end_flag,
135
+ "streaming":req.streaming,
136
+ "model": req.model,
137
+ }
138
+ async with aiohttp.ClientSession() as session:
139
+ try:
140
+ async with session.post(url, params=auth_data, json=data, headers=headers) as resp:
141
+ return await resp.text()
142
+ except Exception as e:
143
+ error_detail = str(e)
144
+ status_code = 504
145
+ raise HTTPException(status_code=status_code, detail=f"xAI API错误: {error_detail}")
146
 
147
 
148
  # 保持原来的模型列表端点