enh: openai error message handling

This commit is contained in:
Timothy J. Baek 2024-09-19 16:20:00 +02:00
parent 5c16631ec5
commit ff737a9e25

View File

@ -423,6 +423,7 @@ async def generate_chat_completion(
r = None r = None
session = None session = None
streaming = False streaming = False
response = None
try: try:
session = aiohttp.ClientSession( session = aiohttp.ClientSession(
@ -435,8 +436,6 @@ async def generate_chat_completion(
headers=headers, headers=headers,
) )
r.raise_for_status()
# Check if response is SSE # Check if response is SSE
if "text/event-stream" in r.headers.get("Content-Type", ""): if "text/event-stream" in r.headers.get("Content-Type", ""):
streaming = True streaming = True
@ -449,19 +448,23 @@ async def generate_chat_completion(
), ),
) )
else: else:
response_data = await r.json() try:
return response_data response = await r.json()
except Exception as e:
log.error(e)
response = await r.text()
r.raise_for_status()
return response
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)
error_detail = "Open WebUI: Server Connection Error" error_detail = "Open WebUI: Server Connection Error"
if r is not None: if isinstance(response, dict):
try: if "error" in response:
res = await r.json() error_detail = f"{response['error']['message'] if 'message' in response['error'] else response['error']}"
print(res) elif isinstance(response, str):
if "error" in res: error_detail = response
error_detail = f"External: {res['error']['message'] if 'message' in res['error'] else res['error']}"
except Exception:
error_detail = f"External: {e}"
raise HTTPException(status_code=r.status if r else 500, detail=error_detail) raise HTTPException(status_code=r.status if r else 500, detail=error_detail)
finally: finally:
if not streaming and session: if not streaming and session: