refac: openai /models
Some checks are pending
Deploy to HuggingFace Spaces / check-secret (push) Waiting to run
Deploy to HuggingFace Spaces / deploy (push) Blocked by required conditions
Create and publish Docker images with specific build args / build-main-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-main-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / build-cuda-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-cuda-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / build-ollama-image (linux/amd64) (push) Waiting to run
Create and publish Docker images with specific build args / build-ollama-image (linux/arm64) (push) Waiting to run
Create and publish Docker images with specific build args / merge-main-images (push) Blocked by required conditions
Create and publish Docker images with specific build args / merge-cuda-images (push) Blocked by required conditions
Create and publish Docker images with specific build args / merge-ollama-images (push) Blocked by required conditions
Python CI / Format Backend (3.11) (push) Waiting to run
Frontend Build / Format & Build Frontend (push) Waiting to run
Frontend Build / Frontend Unit Tests (push) Waiting to run
Integration Test / Run Cypress Integration Tests (push) Waiting to run
Integration Test / Run Migration Tests (push) Waiting to run

This commit is contained in:
Timothy Jaeryang Baek 2024-11-11 14:11:20 -08:00
parent cdc2b4dd61
commit b82e25cac8

View File

@ -342,17 +342,26 @@ async def get_models(url_idx: Optional[int] = None, user=Depends(get_verified_us
r = None r = None
timeout = aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_OPENAI_MODEL_LIST)
async with aiohttp.ClientSession(timeout=timeout) as session:
try: try:
r = requests.request(method="GET", url=f"{url}/models", headers=headers) async with session.get(f"{url}/models", headers=headers) as r:
r.raise_for_status() if r.status != 200:
# Extract response error details if available
error_detail = f"HTTP Error: {r.status}"
res = await r.json()
if "error" in res:
error_detail = f"External Error: {res['error']}"
raise Exception(error_detail)
response_data = r.json() response_data = await r.json()
# Check if we're calling OpenAI API based on the URL
if "api.openai.com" in url: if "api.openai.com" in url:
# Filter the response data # Filter models according to the specified conditions
response_data["data"] = [ response_data["data"] = [
model model for model in response_data.get("data", [])
for model in response_data["data"]
if not any( if not any(
name in model["id"] name in model["id"]
for name in [ for name in [
@ -367,21 +376,19 @@ async def get_models(url_idx: Optional[int] = None, user=Depends(get_verified_us
] ]
return response_data return response_data
except Exception as e:
log.exception(e)
error_detail = "Open WebUI: Server Connection Error"
if r is not None:
try:
res = r.json()
if "error" in res:
error_detail = f"External: {res['error']}"
except Exception:
error_detail = f"External: {e}"
raise HTTPException( except aiohttp.ClientError as e:
status_code=r.status_code if r else 500, # ClientError covers all aiohttp requests issues
detail=error_detail, log.exception(f"Client error: {str(e)}")
) # Handle aiohttp-specific connection issues, timeout etc.
raise HTTPException(status_code=500, detail="Open WebUI: Server Connection Error")
except Exception as e:
log.exception(f"Unexpected error: {e}")
# Generic error handler in case parsing JSON or other steps fail
error_detail = f"Unexpected error: {str(e)}"
raise HTTPException(status_code=500, detail=error_detail)
@app.post("/chat/completions") @app.post("/chat/completions")