Only let agent use online search tool if connected to it

This commit is contained in:
Debanjum Singh Solanky 2024-04-20 11:05:28 +05:30
parent 75c9ebbc54
commit 002cd14a65
2 changed files with 13 additions and 1 deletions

View file

@ -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)

View file

@ -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