serply.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import logging
  2. from typing import Optional
  3. from urllib.parse import urlencode
  4. import requests
  5. from open_webui.retrieval.web.main import SearchResult, get_filtered_results
  6. from open_webui.env import SRC_LOG_LEVELS
  7. log = logging.getLogger(__name__)
  8. log.setLevel(SRC_LOG_LEVELS["RAG"])
  9. def search_serply(
  10. api_key: str,
  11. query: str,
  12. count: int,
  13. hl: str = "us",
  14. limit: int = 10,
  15. device_type: str = "desktop",
  16. proxy_location: str = "US",
  17. filter_list: Optional[list[str]] = None,
  18. ) -> list[SearchResult]:
  19. """Search using serper.dev's API and return the results as a list of SearchResult objects.
  20. Args:
  21. api_key (str): A serply.io API key
  22. query (str): The query to search for
  23. hl (str): Host Language code to display results in (reference https://developers.google.com/custom-search/docs/xml_results?hl=en#wsInterfaceLanguages)
  24. limit (int): The maximum number of results to return [10-100, defaults to 10]
  25. """
  26. log.info("Searching with Serply")
  27. url = "https://api.serply.io/v1/search/"
  28. query_payload = {
  29. "q": query,
  30. "language": "en",
  31. "num": limit,
  32. "gl": proxy_location.upper(),
  33. "hl": hl.lower(),
  34. }
  35. url = f"{url}{urlencode(query_payload)}"
  36. headers = {
  37. "X-API-KEY": api_key,
  38. "X-User-Agent": device_type,
  39. "User-Agent": "open-webui",
  40. "X-Proxy-Location": proxy_location,
  41. }
  42. response = requests.request("GET", url, headers=headers)
  43. response.raise_for_status()
  44. json_response = response.json()
  45. log.info(f"results from serply search: {json_response}")
  46. results = sorted(
  47. json_response.get("results", []), key=lambda x: x.get("realPosition", 0)
  48. )
  49. if filter_list:
  50. results = get_filtered_results(results, filter_list)
  51. return [
  52. SearchResult(
  53. link=result["link"],
  54. title=result.get("title"),
  55. snippet=result.get("description"),
  56. )
  57. for result in results[:count]
  58. ]