serply.py 2.0 KB

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