mojeek.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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_mojeek(
  9. api_key: str, query: str, count: int, filter_list: Optional[list[str]] = None
  10. ) -> list[SearchResult]:
  11. """Search using Mojeek's Search API and return the results as a list of SearchResult objects.
  12. Args:
  13. api_key (str): A Mojeek Search API key
  14. query (str): The query to search for
  15. """
  16. url = "https://api.mojeek.com/search"
  17. headers = {
  18. "Accept": "application/json",
  19. }
  20. params = {"q": query, "api_key": api_key, "fmt": "json", "t": count}
  21. response = requests.get(url, headers=headers, params=params)
  22. response.raise_for_status()
  23. json_response = response.json()
  24. results = json_response.get("response", {}).get("results", [])
  25. print(results)
  26. if filter_list:
  27. results = get_filtered_results(results, filter_list)
  28. return [
  29. SearchResult(
  30. link=result["url"], title=result.get("title"), snippet=result.get("desc")
  31. )
  32. for result in results
  33. ]