Niansuh commited on
Commit
23c4d31
·
verified ·
1 Parent(s): 7bb9c94

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +17 -25
main.py CHANGED
@@ -22,7 +22,7 @@ logger = logging.getLogger(__name__)
22
  load_dotenv()
23
  app = FastAPI()
24
  BASE_URL = "https://aichatonlineorg.erweima.ai/aichatonline"
25
- APP_SECRET = os.getenv("APP_SECRET","666")
26
  ACCESS_TOKEN = os.getenv("SD_ACCESS_TOKEN","")
27
  headers = {
28
  'accept': '*/*',
@@ -199,31 +199,23 @@ async def chat_completions(
199
 
200
 
201
 
202
- async def generate():
203
- async with httpx.AsyncClient() as client:
204
- try:
205
- async with client.stream('POST', 'https://sider.ai/api/v2/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
206
- response.raise_for_status()
207
- async for line in response.aiter_lines():
208
- if line and ("[DONE]" not in line):
209
- data = json.loads(line[5:])
210
- content = data.get("data")
211
-
212
- # Check if content is None or does not contain the text key
213
- if content and 'text' in content:
214
- yield f"data: {json.dumps(create_chat_completion_data(content['text'], request.model))}\n\n"
215
- else:
216
- logger.error(f"Unexpected content format: {data}")
217
- yield f"data: {json.dumps(create_chat_completion_data('Error: Unexpected content format', request.model, 'stop'))}\n\n"
218
  yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
219
  yield "data: [DONE]\n\n"
220
- except httpx.HTTPStatusError as e:
221
- logger.error(f"HTTP error occurred: {e}")
222
- raise HTTPException(status_code=e.response.status_code, detail=str(e))
223
- except httpx.RequestError as e:
224
- logger.error(f"An error occurred while requesting: {e}")
225
- raise HTTPException(status_code=500, detail=str(e))
226
-
227
 
228
  if request.stream:
229
  logger.info("Streaming response")
@@ -256,4 +248,4 @@ async def generate():
256
 
257
 
258
  if __name__ == "__main__":
259
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
22
  load_dotenv()
23
  app = FastAPI()
24
  BASE_URL = "https://aichatonlineorg.erweima.ai/aichatonline"
25
+ APP_SECRET = os.getenv("APP_SECRET","786")
26
  ACCESS_TOKEN = os.getenv("SD_ACCESS_TOKEN","")
27
  headers = {
28
  'accept': '*/*',
 
199
 
200
 
201
 
202
+ async def generate():
203
+ async with httpx.AsyncClient() as client:
204
+ try:
205
+ async with client.stream('POST', 'https://sider.ai/api/v2/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
206
+ response.raise_for_status()
207
+ async for line in response.aiter_lines():
208
+ if line and ("[DONE]" not in line):
209
+ content = json.loads(line[5:])["data"]
210
+ yield f"data: {json.dumps(create_chat_completion_data(content.get('text',''), request.model))}\n\n"
 
 
 
 
 
 
 
211
  yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
212
  yield "data: [DONE]\n\n"
213
+ except httpx.HTTPStatusError as e:
214
+ logger.error(f"HTTP error occurred: {e}")
215
+ raise HTTPException(status_code=e.response.status_code, detail=str(e))
216
+ except httpx.RequestError as e:
217
+ logger.error(f"An error occurred while requesting: {e}")
218
+ raise HTTPException(status_code=500, detail=str(e))
 
219
 
220
  if request.stream:
221
  logger.info("Streaming response")
 
248
 
249
 
250
  if __name__ == "__main__":
251
+ uvicorn.run(app, host="0.0.0.0", port=7860)