serpstack.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import logging
  2. from typing import Optional
  3. import requests
  4. from open_webui.retrieval.web.main import SearchResult, get_filtered_results
  5. from open_webui.env import SRC_LOG_LEVELS
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_serpstack(
  9. api_key: str,
  10. query: str,
  11. count: int,
  12. filter_list: Optional[list[str]] = None,
  13. https_enabled: bool = True,
  14. ) -> list[SearchResult]:
  15. """Search using serpstack.com's and return the results as a list of SearchResult objects.
  16. Args:
  17. api_key (str): A serpstack.com API key
  18. query (str): The query to search for
  19. https_enabled (bool): Whether to use HTTPS or HTTP for the API request
  20. """
  21. url = f"{'https' if https_enabled else 'http'}://api.serpstack.com/search"
  22. headers = {"Content-Type": "application/json"}
  23. params = {
  24. "access_key": api_key,
  25. "query": query,
  26. }
  27. response = requests.request("POST", url, headers=headers, params=params)
  28. response.raise_for_status()
  29. json_response = response.json()
  30. results = sorted(
  31. json_response.get("organic_results", []), key=lambda x: x.get("position", 0)
  32. )
  33. if filter_list:
  34. results = get_filtered_results(results, filter_list)
  35. return [
  36. SearchResult(
  37. link=result["url"], title=result.get("title"), snippet=result.get("snippet")
  38. )
  39. for result in results[:count]
  40. ]