fix: add backwards compat with older searxng urls

This commit is contained in:
Jun Siang Cheah 2024-06-03 21:13:10 +01:00
parent ba56edab19
commit 7fefbb316d

View File

@ -10,14 +10,16 @@ log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["RAG"]) log.setLevel(SRC_LOG_LEVELS["RAG"])
def search_searxng(query_url: str, query: str, count: int, **kwargs) -> List[SearchResult]: def search_searxng(
query_url: str, query: str, count: int, **kwargs
) -> List[SearchResult]:
""" """
Search a SearXNG instance for a given query and return the results as a list of SearchResult objects. Search a SearXNG instance for a given query and return the results as a list of SearchResult objects.
The function allows passing additional parameters such as language or time_range to tailor the search result. The function allows passing additional parameters such as language or time_range to tailor the search result.
Args: Args:
query_url (str): The base URL of the SearXNG server with a placeholder for the query "<query>". query_url (str): The base URL of the SearXNG server.
query (str): The search term or question to find in the SearXNG database. query (str): The search term or question to find in the SearXNG database.
count (int): The maximum number of results to retrieve from the search. count (int): The maximum number of results to retrieve from the search.
@ -34,24 +36,28 @@ def search_searxng(query_url: str, query: str, count: int, **kwargs) -> List[Sea
""" """
# Default values for optional parameters are provided as empty strings or None when not specified. # Default values for optional parameters are provided as empty strings or None when not specified.
language = kwargs.get('language', 'en-US') language = kwargs.get("language", "en-US")
time_range = kwargs.get('time_range', '') time_range = kwargs.get("time_range", "")
categories = ''.join(kwargs.get('categories', [])) categories = "".join(kwargs.get("categories", []))
params = { params = {
"q": query, "q": query,
"format": "json", "format": "json",
"pageno": 1, "pageno": 1,
"results_per_page": count, "results_per_page": count,
'language': language, "language": language,
'time_range': time_range, "time_range": time_range,
'engines': '', "engines": "",
'categories': categories, "categories": categories,
'theme': 'simple', "theme": "simple",
'image_proxy': 0 "image_proxy": 0,
} }
# Legacy query format
if "<query>" in query_url:
# Strip all query parameters from the URL
query_url = query_url.split("?")[0]
log.debug(f"searching {query_url}") log.debug(f"searching {query_url}")
response = requests.get( response = requests.get(