Azure AI Search Integration for Microsoft Agent Framework

1.0.0b260409 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `AzureAISearch` tool and invoke it directly. It highlights the required environment variables for authentication and target index, and shows how to perform a search query. For full integration, this tool would be passed to an `Agent` instance from `agent-framework-core`.

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())

view raw JSON →