jina_search.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import logging
  2. import requests
  3. from open_webui.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(api_key: str, 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. "Content-Type": "application/json",
  21. "Authorization": api_key,
  22. "X-Retain-Images": "none",
  23. }
  24. payload = {"q": query, "count": count if count <= 10 else 10}
  25. url = str(URL(jina_search_endpoint))
  26. response = requests.post(url, headers=headers, json=payload)
  27. response.raise_for_status()
  28. data = response.json()
  29. results = []
  30. for result in data["data"]:
  31. results.append(
  32. SearchResult(
  33. link=result["url"],
  34. title=result.get("title"),
  35. snippet=result.get("content"),
  36. )
  37. )
  38. return results