2024-05-06 04:27:46 +00:00
|
|
|
import logging
|
2024-08-14 12:46:31 +00:00
|
|
|
from typing import Optional
|
2024-06-03 09:44:46 +00:00
|
|
|
|
2024-08-27 22:10:27 +00:00
|
|
|
import requests
|
2024-09-28 00:23:09 +00:00
|
|
|
from open_webui.apps.retrieval.web.main import SearchResult, get_filtered_results
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.env import SRC_LOG_LEVELS
|
2024-05-06 04:27:46 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["RAG"])
|
|
|
|
|
|
|
|
|
2024-06-03 20:13:10 +00:00
|
|
|
def search_searxng(
|
2024-06-17 21:32:23 +00:00
|
|
|
query_url: str,
|
|
|
|
query: str,
|
|
|
|
count: int,
|
2024-08-14 12:46:31 +00:00
|
|
|
filter_list: Optional[list[str]] = None,
|
2024-06-17 21:32:23 +00:00
|
|
|
**kwargs,
|
2024-08-14 12:46:31 +00:00
|
|
|
) -> list[SearchResult]:
|
2024-06-03 09:44:46 +00:00
|
|
|
"""
|
|
|
|
Search a SearXNG instance for a given query and return the results as a list of SearchResult objects.
|
2024-06-03 20:13:10 +00:00
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
The function allows passing additional parameters such as language or time_range to tailor the search result.
|
2024-05-06 04:27:46 +00:00
|
|
|
|
|
|
|
Args:
|
2024-06-03 20:13:10 +00:00
|
|
|
query_url (str): The base URL of the SearXNG server.
|
2024-06-03 09:44:46 +00:00
|
|
|
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.
|
2024-06-03 20:13:10 +00:00
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
Keyword Args:
|
|
|
|
language (str): Language filter for the search results; e.g., "en-US". Defaults to an empty string.
|
2024-06-12 14:33:33 +00:00
|
|
|
safesearch (int): Safe search filter for safer web results; 0 = off, 1 = moderate, 2 = strict. Defaults to 1 (moderate).
|
2024-06-03 09:44:46 +00:00
|
|
|
time_range (str): Time range for filtering results by date; e.g., "2023-04-05..today" or "all-time". Defaults to ''.
|
2024-08-14 12:46:31 +00:00
|
|
|
categories: (Optional[list[str]]): Specific categories within which the search should be performed, defaulting to an empty string if not provided.
|
2024-06-03 20:13:10 +00:00
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
Returns:
|
2024-08-14 12:46:31 +00:00
|
|
|
list[SearchResult]: A list of SearchResults sorted by relevance score in descending order.
|
2024-06-03 20:13:10 +00:00
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
Raise:
|
|
|
|
requests.exceptions.RequestException: If a request error occurs during the search process.
|
2024-05-06 04:27:46 +00:00
|
|
|
"""
|
2024-06-03 20:13:10 +00:00
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
# Default values for optional parameters are provided as empty strings or None when not specified.
|
2024-06-12 18:47:19 +00:00
|
|
|
language = kwargs.get("language", "en-US")
|
2024-06-12 14:33:33 +00:00
|
|
|
safesearch = kwargs.get("safesearch", "1")
|
2024-06-03 20:13:10 +00:00
|
|
|
time_range = kwargs.get("time_range", "")
|
|
|
|
categories = "".join(kwargs.get("categories", []))
|
2024-06-03 09:44:46 +00:00
|
|
|
|
|
|
|
params = {
|
|
|
|
"q": query,
|
|
|
|
"format": "json",
|
|
|
|
"pageno": 1,
|
2024-06-12 14:33:33 +00:00
|
|
|
"safesearch": safesearch,
|
2024-06-03 20:13:10 +00:00
|
|
|
"language": language,
|
|
|
|
"time_range": time_range,
|
|
|
|
"categories": categories,
|
|
|
|
"theme": "simple",
|
|
|
|
"image_proxy": 0,
|
2024-06-03 09:44:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 20:13:10 +00:00
|
|
|
# Legacy query format
|
|
|
|
if "<query>" in query_url:
|
|
|
|
# Strip all query parameters from the URL
|
|
|
|
query_url = query_url.split("?")[0]
|
|
|
|
|
2024-06-03 09:48:40 +00:00
|
|
|
log.debug(f"searching {query_url}")
|
2024-06-03 09:44:46 +00:00
|
|
|
|
|
|
|
response = requests.get(
|
|
|
|
query_url,
|
2024-05-06 04:27:46 +00:00
|
|
|
headers={
|
|
|
|
"User-Agent": "Open WebUI (https://github.com/open-webui/open-webui) RAG Bot",
|
|
|
|
"Accept": "text/html",
|
|
|
|
"Accept-Encoding": "gzip, deflate",
|
|
|
|
"Accept-Language": "en-US,en;q=0.5",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
},
|
2024-06-03 09:44:46 +00:00
|
|
|
params=params,
|
2024-05-06 04:27:46 +00:00
|
|
|
)
|
|
|
|
|
2024-06-03 09:44:46 +00:00
|
|
|
response.raise_for_status() # Raise an exception for HTTP errors.
|
|
|
|
|
|
|
|
json_response = response.json()
|
2024-05-06 04:27:46 +00:00
|
|
|
results = json_response.get("results", [])
|
|
|
|
sorted_results = sorted(results, key=lambda x: x.get("score", 0), reverse=True)
|
2024-06-17 07:36:56 +00:00
|
|
|
if filter_list:
|
2024-06-19 11:22:29 +00:00
|
|
|
sorted_results = get_filtered_results(sorted_results, filter_list)
|
2024-05-06 04:27:46 +00:00
|
|
|
return [
|
|
|
|
SearchResult(
|
|
|
|
link=result["url"], title=result.get("title"), snippet=result.get("content")
|
|
|
|
)
|
2024-06-17 07:36:56 +00:00
|
|
|
for result in sorted_results[:count]
|
2024-05-06 04:27:46 +00:00
|
|
|
]
|