jina_search.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. import requests
  3. from open_webui.apps.retrieval.web.main import SearchResult
  4. from open_webui.env import SRC_LOG_LEVELS
  5. from yarl import URL
  6. log = logging.getLogger(__name__)
  7. log.setLevel(SRC_LOG_LEVELS["RAG"])
  8. def search_jina(query: str, count: int) -> list[SearchResult]:
  9. """
  10. Search using Jina's Search API and return the results as a list of SearchResult objects.
  11. Args:
  12. query (str): The query to search for
  13. count (int): The number of results to return
  14. Returns:
  15. list[SearchResult]: A list of search results
  16. """
  17. jina_search_endpoint = "https://s.jina.ai/"
  18. headers = {
  19. "Accept": "application/json",
  20. }
  21. url = str(URL(jina_search_endpoint + query))
  22. response = requests.get(url, headers=headers)
  23. response.raise_for_status()
  24. data = response.json()
  25. results = []
  26. for result in data["data"][:count]:
  27. results.append(
  28. SearchResult(
  29. link=result["url"],
  30. title=result.get("title"),
  31. snippet=result.get("content"),
  32. )
  33. )
  34. return results