diff --git a/src/khoj/processor/tools/online_search.py b/src/khoj/processor/tools/online_search.py index 82cf0da7..d7d951f0 100644 --- a/src/khoj/processor/tools/online_search.py +++ b/src/khoj/processor/tools/online_search.py @@ -15,7 +15,7 @@ from khoj.routers.helpers import ( generate_online_subqueries, infer_webpage_urls, ) -from khoj.utils.helpers import is_none_or_empty, timer +from khoj.utils.helpers import is_internet_connected, is_none_or_empty, timer from khoj.utils.rawconfig import LocationData logger = logging.getLogger(__name__) @@ -48,6 +48,9 @@ async def search_online( if not online_search_enabled(): logger.warn("SERPER_DEV_API_KEY is not set") return {} + if not is_internet_connected(): + logger.warn("Cannot search online as not connected to internet") + return {} # Breakdown the query into subqueries to get the correct answer subqueries = await generate_online_subqueries(query, conversation_history, location) diff --git a/src/khoj/utils/helpers.py b/src/khoj/utils/helpers.py index e621f53e..3cb5bfac 100644 --- a/src/khoj/utils/helpers.py +++ b/src/khoj/utils/helpers.py @@ -18,6 +18,7 @@ from typing import TYPE_CHECKING, Optional, Union from urllib.parse import urlparse import psutil +import requests import torch from asgiref.sync import sync_to_async from magika import Magika @@ -397,3 +398,11 @@ def is_valid_url(url: str) -> bool: return all([result.scheme, result.netloc]) except: return False + + +def is_internet_connected(): + try: + response = requests.head("https://www.google.com") + return response.status_code == 200 + except: + return False