1
0

azure.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import logging
  2. from typing import Optional
  3. from open_webui.retrieval.web.main import SearchResult, get_filtered_results
  4. from open_webui.env import SRC_LOG_LEVELS
  5. log = logging.getLogger(__name__)
  6. log.setLevel(SRC_LOG_LEVELS["RAG"])
  7. """
  8. Azure AI Search integration for Open WebUI.
  9. Documentation: https://learn.microsoft.com/en-us/python/api/overview/azure/search-documents-readme?view=azure-python
  10. Required package: azure-search-documents
  11. Install: pip install azure-search-documents
  12. """
  13. def search_azure(
  14. api_key: str,
  15. endpoint: str,
  16. index_name: str,
  17. query: str,
  18. count: int,
  19. filter_list: Optional[list[str]] = None,
  20. ) -> list[SearchResult]:
  21. """
  22. Search using Azure AI Search.
  23. Args:
  24. api_key: Azure Search API key (query key or admin key)
  25. endpoint: Azure Search service endpoint (e.g., https://myservice.search.windows.net)
  26. index_name: Name of the search index to query
  27. query: Search query string
  28. count: Number of results to return
  29. filter_list: Optional list of domains to filter results
  30. Returns:
  31. List of SearchResult objects with link, title, and snippet
  32. """
  33. try:
  34. from azure.core.credentials import AzureKeyCredential
  35. from azure.search.documents import SearchClient
  36. except ImportError:
  37. log.error(
  38. "azure-search-documents package is not installed. "
  39. "Install it with: pip install azure-search-documents"
  40. )
  41. raise ImportError(
  42. "azure-search-documents is required for Azure AI Search. "
  43. "Install it with: pip install azure-search-documents"
  44. )
  45. try:
  46. # Create search client with API key authentication
  47. credential = AzureKeyCredential(api_key)
  48. search_client = SearchClient(
  49. endpoint=endpoint, index_name=index_name, credential=credential
  50. )
  51. # Perform the search
  52. results = search_client.search(search_text=query, top=count)
  53. # Convert results to list and extract fields
  54. search_results = []
  55. for result in results:
  56. # Azure AI Search returns documents with custom schemas
  57. # We need to extract common fields that might represent URL, title, and content
  58. # Common field names to look for:
  59. result_dict = dict(result)
  60. # Try to find URL field (common names)
  61. link = (
  62. result_dict.get("url")
  63. or result_dict.get("link")
  64. or result_dict.get("uri")
  65. or result_dict.get("metadata_storage_path")
  66. or ""
  67. )
  68. # Try to find title field (common names)
  69. title = (
  70. result_dict.get("title")
  71. or result_dict.get("name")
  72. or result_dict.get("metadata_title")
  73. or result_dict.get("metadata_storage_name")
  74. or None
  75. )
  76. # Try to find content/snippet field (common names)
  77. snippet = (
  78. result_dict.get("content")
  79. or result_dict.get("snippet")
  80. or result_dict.get("description")
  81. or result_dict.get("summary")
  82. or result_dict.get("text")
  83. or None
  84. )
  85. # Truncate snippet if too long
  86. if snippet and len(snippet) > 500:
  87. snippet = snippet[:497] + "..."
  88. if link: # Only add if we found a valid link
  89. search_results.append(
  90. {
  91. "link": link,
  92. "title": title,
  93. "snippet": snippet,
  94. }
  95. )
  96. # Apply domain filtering if specified
  97. if filter_list:
  98. search_results = get_filtered_results(search_results, filter_list)
  99. # Convert to SearchResult objects
  100. return [
  101. SearchResult(
  102. link=result["link"],
  103. title=result.get("title"),
  104. snippet=result.get("snippet"),
  105. )
  106. for result in search_results
  107. ]
  108. except Exception as ex:
  109. log.error(f"Azure AI Search error: {ex}")
  110. raise ex