From 014e52c072cd45147c4785f0870d683150a4096a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Fri, 17 May 2024 10:49:12 -0700 Subject: [PATCH] fix: connection timeout --- backend/apps/ollama/main.py | 3 ++- backend/apps/openai/main.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/apps/ollama/main.py b/backend/apps/ollama/main.py index cb80eeed2..b4451d35d 100644 --- a/backend/apps/ollama/main.py +++ b/backend/apps/ollama/main.py @@ -124,8 +124,9 @@ async def cancel_ollama_request(request_id: str, user=Depends(get_current_user)) async def fetch_url(url): + timeout = aiohttp.ClientTimeout(total=5) try: - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=timeout) as session: async with session.get(url) as response: return await response.json() except Exception as e: diff --git a/backend/apps/openai/main.py b/backend/apps/openai/main.py index fa4237a67..a153bde0b 100644 --- a/backend/apps/openai/main.py +++ b/backend/apps/openai/main.py @@ -182,10 +182,11 @@ async def speech(request: Request, user=Depends(get_verified_user)): async def fetch_url(url, key): + timeout = aiohttp.ClientTimeout(total=5) try: if key != "": headers = {"Authorization": f"Bearer {key}"} - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=timeout) as session: async with session.get(url, headers=headers) as response: return await response.json() else: