exa.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import logging
  2. from dataclasses import dataclass
  3. from typing import Optional
  4. import requests
  5. from open_webui.env import SRC_LOG_LEVELS
  6. from open_webui.retrieval.web.main import SearchResult
  7. log = logging.getLogger(__name__)
  8. log.setLevel(SRC_LOG_LEVELS["RAG"])
  9. EXA_API_BASE = "https://api.exa.ai"
  10. @dataclass
  11. class ExaResult:
  12. url: str
  13. title: str
  14. text: str
  15. def search_exa(
  16. api_key: str,
  17. query: str,
  18. count: int,
  19. filter_list: Optional[list[str]] = None,
  20. ) -> list[SearchResult]:
  21. """Search using Exa Search API and return the results as a list of SearchResult objects.
  22. Args:
  23. api_key (str): A Exa Search API key
  24. query (str): The query to search for
  25. count (int): Number of results to return
  26. filter_list (Optional[list[str]]): List of domains to filter results by
  27. """
  28. log.info(f"Searching with Exa for query: {query}")
  29. headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
  30. payload = {
  31. "query": query,
  32. "numResults": count or 5,
  33. "includeDomains": filter_list,
  34. "contents": {"text": True, "highlights": True},
  35. "type": "auto", # Use the auto search type (keyword or neural)
  36. }
  37. try:
  38. response = requests.post(
  39. f"{EXA_API_BASE}/search", headers=headers, json=payload
  40. )
  41. response.raise_for_status()
  42. data = response.json()
  43. results = []
  44. for result in data["results"]:
  45. results.append(
  46. ExaResult(
  47. url=result["url"],
  48. title=result["title"],
  49. text=result["text"],
  50. )
  51. )
  52. log.info(f"Found {len(results)} results")
  53. return [
  54. SearchResult(
  55. link=result.url,
  56. title=result.title,
  57. snippet=result.text,
  58. )
  59. for result in results
  60. ]
  61. except Exception as e:
  62. log.error(f"Error searching Exa: {e}")
  63. return []