duckduckgo.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import logging
  2. from typing import List, Optional
  3. from apps.rag.search.main import SearchResult, get_filtered_results
  4. from duckduckgo_search import DDGS
  5. from config import SRC_LOG_LEVELS
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_duckduckgo(query: str, count: int, filter_list: Optional[List[str]] = None) -> list[SearchResult]:
  9. """
  10. Search using DuckDuckGo's Search API and return the results as a list of SearchResult objects.
  11. Args:
  12. query (str): The query to search for
  13. count (int): The number of results to return
  14. Returns:
  15. List[SearchResult]: A list of search results
  16. """
  17. # Use the DDGS context manager to create a DDGS object
  18. with DDGS() as ddgs:
  19. # Use the ddgs.text() method to perform the search
  20. ddgs_gen = ddgs.text(
  21. query, safesearch="moderate", max_results=count, backend="api"
  22. )
  23. # Check if there are search results
  24. if ddgs_gen:
  25. # Convert the search results into a list
  26. search_results = [r for r in ddgs_gen]
  27. # Create an empty list to store the SearchResult objects
  28. results = []
  29. # Iterate over each search result
  30. for result in search_results:
  31. # Create a SearchResult object and append it to the results list
  32. results.append(
  33. SearchResult(
  34. link=result["href"],
  35. title=result.get("title"),
  36. snippet=result.get("body"),
  37. )
  38. )
  39. if filter_list:
  40. results = get_filtered_results(results, filter_list)
  41. # Return the list of search results
  42. return results