2024-06-11 19:49:08 +05:30
|
|
|
import logging
|
2024-08-14 13:46:31 +01:00
|
|
|
from typing import Optional
|
2024-08-28 00:10:27 +02:00
|
|
|
|
2024-12-11 18:05:42 -08:00
|
|
|
from open_webui.retrieval.web.main import SearchResult, get_filtered_results
|
2024-06-11 19:49:08 +05:30
|
|
|
from duckduckgo_search import DDGS
|
2025-04-08 13:51:54 +02:00
|
|
|
from duckduckgo_search.exceptions import RatelimitException
|
2024-09-04 16:54:48 +02:00
|
|
|
from open_webui.env import SRC_LOG_LEVELS
|
2024-06-11 19:49:08 +05:30
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["RAG"])
|
|
|
|
|
|
|
|
|
|
|
2024-06-17 14:32:23 -07:00
|
|
|
def search_duckduckgo(
|
2024-08-14 13:46:31 +01:00
|
|
|
query: str, count: int, filter_list: Optional[list[str]] = None
|
2024-06-17 14:32:23 -07:00
|
|
|
) -> list[SearchResult]:
|
2024-06-11 19:49:08 +05:30
|
|
|
"""
|
|
|
|
|
Search using DuckDuckGo's Search API and return the results as a list of SearchResult objects.
|
|
|
|
|
Args:
|
|
|
|
|
query (str): The query to search for
|
|
|
|
|
count (int): The number of results to return
|
|
|
|
|
|
|
|
|
|
Returns:
|
2024-08-14 13:46:31 +01:00
|
|
|
list[SearchResult]: A list of search results
|
2024-06-11 19:49:08 +05:30
|
|
|
"""
|
|
|
|
|
# Use the DDGS context manager to create a DDGS object
|
2025-04-08 13:51:54 +02:00
|
|
|
search_results = []
|
2024-06-11 19:49:08 +05:30
|
|
|
with DDGS() as ddgs:
|
|
|
|
|
# Use the ddgs.text() method to perform the search
|
2025-04-08 13:51:54 +02:00
|
|
|
try:
|
|
|
|
|
search_results = ddgs.text(
|
2025-04-08 14:01:44 +02:00
|
|
|
query, safesearch="moderate", max_results=count, backend="lite"
|
2025-04-08 13:51:54 +02:00
|
|
|
)
|
|
|
|
|
except RatelimitException as e:
|
|
|
|
|
log.error(f"RatelimitException: {e}")
|
2024-06-17 14:34:59 +07:00
|
|
|
if filter_list:
|
2025-02-15 16:45:56 -08:00
|
|
|
search_results = get_filtered_results(search_results, filter_list)
|
|
|
|
|
|
2024-06-11 19:49:08 +05:30
|
|
|
# Return the list of search results
|
2025-02-15 16:45:56 -08:00
|
|
|
return [
|
|
|
|
|
SearchResult(
|
|
|
|
|
link=result["href"],
|
|
|
|
|
title=result.get("title"),
|
|
|
|
|
snippet=result.get("body"),
|
|
|
|
|
)
|
|
|
|
|
for result in search_results
|
|
|
|
|
]
|