google_pse.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import json
  2. import logging
  3. import requests
  4. from apps.rag.search.main import SearchResult
  5. from config import SRC_LOG_LEVELS
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_google_pse(
  9. api_key: str, search_engine_id: str, query: str, count: int
  10. ) -> list[SearchResult]:
  11. """Search using Google's Programmable Search Engine API and return the results as a list of SearchResult objects.
  12. Args:
  13. api_key (str): A Programmable Search Engine API key
  14. search_engine_id (str): A Programmable Search Engine ID
  15. query (str): The query to search for
  16. """
  17. url = "https://www.googleapis.com/customsearch/v1"
  18. headers = {"Content-Type": "application/json"}
  19. params = {
  20. "cx": search_engine_id,
  21. "q": query,
  22. "key": api_key,
  23. "num": count,
  24. }
  25. response = requests.request("GET", url, headers=headers, params=params)
  26. response.raise_for_status()
  27. json_response = response.json()
  28. results = json_response.get("items", [])
  29. return [
  30. SearchResult(
  31. link=result["link"],
  32. title=result.get("title"),
  33. snippet=result.get("snippet"),
  34. )
  35. for result in results
  36. ]