google_pse.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import logging
  2. from typing import Optional
  3. import requests
  4. from open_webui.apps.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_google_pse(
  9. api_key: str,
  10. search_engine_id: str,
  11. query: str,
  12. count: int,
  13. filter_list: Optional[list[str]] = None,
  14. ) -> list[SearchResult]:
  15. """Search using Google's Programmable Search Engine API and return the results as a list of SearchResult objects.
  16. Args:
  17. api_key (str): A Programmable Search Engine API key
  18. search_engine_id (str): A Programmable Search Engine ID
  19. query (str): The query to search for
  20. """
  21. url = "https://www.googleapis.com/customsearch/v1"
  22. headers = {"Content-Type": "application/json"}
  23. params = {
  24. "cx": search_engine_id,
  25. "q": query,
  26. "key": api_key,
  27. "num": count,
  28. }
  29. response = requests.request("GET", url, headers=headers, params=params)
  30. response.raise_for_status()
  31. json_response = response.json()
  32. results = json_response.get("items", [])
  33. if filter_list:
  34. results = get_filtered_results(results, filter_list)
  35. return [
  36. SearchResult(
  37. link=result["link"],
  38. title=result.get("title"),
  39. snippet=result.get("snippet"),
  40. )
  41. for result in results
  42. ]