duckduckgo.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. from typing import Optional
  3. from open_webui.retrieval.web.main import SearchResult, get_filtered_results
  4. from duckduckgo_search import DDGS
  5. from duckduckgo_search.exceptions import RatelimitException
  6. from open_webui.env import SRC_LOG_LEVELS
  7. log = logging.getLogger(__name__)
  8. log.setLevel(SRC_LOG_LEVELS["RAG"])
  9. def search_duckduckgo(
  10. query: str, count: int, filter_list: Optional[list[str]] = None
  11. ) -> list[SearchResult]:
  12. """
  13. Search using DuckDuckGo's Search API and return the results as a list of SearchResult objects.
  14. Args:
  15. query (str): The query to search for
  16. count (int): The number of results to return
  17. Returns:
  18. list[SearchResult]: A list of search results
  19. """
  20. # Use the DDGS context manager to create a DDGS object
  21. search_results = []
  22. with DDGS() as ddgs:
  23. # Use the ddgs.text() method to perform the search
  24. try:
  25. search_results = ddgs.text(
  26. query, safesearch="moderate", max_results=count, backend="lite"
  27. )
  28. except RatelimitException as e:
  29. log.error(f"RatelimitException: {e}")
  30. if filter_list:
  31. search_results = get_filtered_results(search_results, filter_list)
  32. # Return the list of search results
  33. return [
  34. SearchResult(
  35. link=result["href"],
  36. title=result.get("title"),
  37. snippet=result.get("body"),
  38. )
  39. for result in search_results
  40. ]