Azure AI Search Integration for Microsoft Agent Framework
The `agent-framework-azure-ai-search` library provides an integration for Azure AI Search with Microsoft's Agent Framework. It offers an `AzureAISearch` tool wrapper, enabling agents to perform search queries against Azure AI Search indexes. Currently in beta version `1.0.0b260409`, its development is active with frequent updates as part of the broader Agent Framework initiative.
Warnings
- breaking As a beta library, `agent-framework-azure-ai-search` is subject to frequent breaking changes. API signatures, class names, or configuration methods may change without a major version bump, following the active development of the broader Agent Framework.
- gotcha Incorrect or missing environment variables for Azure AI Search (`AZURE_AI_SEARCH_ENDPOINT`, `AZURE_AI_SEARCH_API_KEY`) are common causes of runtime errors. The tool relies on these for authentication.
- gotcha This package is part of the Microsoft Agent Framework ecosystem. Compatibility issues can arise if the `agent-framework-core` dependency is not aligned with the version expected by this package. The specified dependency is `agent-framework-core>=1.0.0b240327`.
Install
-
pip install agent-framework-azure-ai-search
Imports
- AzureAISearch
from agent_framework_azure_ai_search import AzureAISearch
from agent_framework_azure_ai_search.tool import AzureAISearch
Quickstart
import asyncio
import os
from agent_framework_azure_ai_search.tool import AzureAISearch
async def main():
# Set these environment variables or replace with actual values
# AZURE_AI_SEARCH_ENDPOINT = os.environ.get('AZURE_AI_SEARCH_ENDPOINT', 'YOUR_AI_SEARCH_ENDPOINT')
# AZURE_AI_SEARCH_API_KEY = os.environ.get('AZURE_AI_SEARCH_API_KEY', 'YOUR_AI_SEARCH_API_KEY')
# AZURE_AI_SEARCH_INDEX_NAME = os.environ.get('AZURE_AI_SEARCH_INDEX_NAME', 'YOUR_AI_SEARCH_INDEX_NAME')
# The AzureAISearch tool automatically picks up environment variables.
# Ensure AZURE_AI_SEARCH_ENDPOINT and AZURE_AI_SEARCH_API_KEY are set.
# AZURE_AI_SEARCH_INDEX_NAME is optional for default index.
# For demonstration, we'll try to get values from env, provide placeholders if not found
endpoint = os.environ.get('AZURE_AI_SEARCH_ENDPOINT', 'https://example.search.windows.net')
api_key = os.environ.get('AZURE_AI_SEARCH_API_KEY', 'YOUR_AI_SEARCH_API_KEY')
index_name = os.environ.get('AZURE_AI_SEARCH_INDEX_NAME', 'your-default-index')
if endpoint == 'https://example.search.windows.net' or api_key == 'YOUR_AI_SEARCH_API_KEY':
print("WARNING: Azure AI Search credentials or endpoint not set. Using placeholder values.")
print("Please set AZURE_AI_SEARCH_ENDPOINT and AZURE_AI_SEARCH_API_KEY environment variables.")
return
# Temporarily set environment variables for quickstart if not already present
# In a real application, set them once before running.
os.environ['AZURE_AI_SEARCH_ENDPOINT'] = endpoint
os.environ['AZURE_AI_SEARCH_API_KEY'] = api_key
os.environ['AZURE_AI_SEARCH_INDEX_NAME'] = index_name
try:
# Create an instance of the Azure AI Search tool
search_tool = AzureAISearch()
# Example of invoking the tool directly (as an agent might)
print(f"Searching for 'Python programming language':")
result = await search_tool.invoke(query='Python programming language', index_name=index_name)
print(f"Search Result: {result[:500]}...") # Truncate for display
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Clean up temporary environment variables
if 'AZURE_AI_SEARCH_ENDPOINT' in os.environ: del os.environ['AZURE_AI_SEARCH_ENDPOINT']
if 'AZURE_AI_SEARCH_API_KEY' in os.environ: del os.environ['AZURE_AI_SEARCH_API_KEY']
if 'AZURE_AI_SEARCH_INDEX_NAME' in os.environ: del os.environ['AZURE_AI_SEARCH_INDEX_NAME']
if __name__ == '__main__':
asyncio.run(main())