Python: AzureAI Agent AI Search Sample (#1281)

* azure ai search sample

* small fix
This commit is contained in:
Giles Odigwe
2025-10-08 08:52:02 -07:00
committed by GitHub
Unverified
parent 523127fbf4
commit 334d52f300
3 changed files with 78 additions and 0 deletions
@@ -907,6 +907,10 @@ class AzureAIAgentClient(BaseChatClient):
filter=additional_props.get("filter", ""),
)
tool_definitions.extend(ai_search.definitions)
# Add tool resources for Azure AI Search
if run_options is not None:
run_options.setdefault("tool_resources", {})
run_options["tool_resources"].update(ai_search.resources)
case ToolDefinition():
tool_definitions.append(tool)
case dict():
@@ -12,6 +12,7 @@ This folder contains examples demonstrating different ways to create and use age
| [`azure_ai_with_existing_agent.py`](azure_ai_with_existing_agent.py) | Shows how to work with a pre-existing agent by providing the agent ID to the Azure AI chat client. This example also demonstrates proper cleanup of manually created agents. |
| [`azure_ai_with_existing_thread.py`](azure_ai_with_existing_thread.py) | Shows how to work with a pre-existing thread by providing the thread ID to the Azure AI chat client. This example also demonstrates proper cleanup of manually created threads. |
| [`azure_ai_with_explicit_settings.py`](azure_ai_with_explicit_settings.py) | Shows how to create an agent with explicitly configured `AzureAIAgentClient` settings, including project endpoint, model deployment, credentials, and agent name. |
| [`azure_ai_with_azure_ai_search.py`](azure_ai_with_azure_ai_search.py) | Demonstrates how to use Azure AI Search with Azure AI agents to search through indexed data. Shows how to configure search parameters, query types, and integrate with existing search indexes. |
| [`azure_ai_with_file_search.py`](azure_ai_with_file_search.py) | Demonstrates how to use the HostedFileSearchTool with Azure AI agents to search through uploaded documents. Shows file upload, vector store creation, and querying document content. Includes both streaming and non-streaming examples. |
| [`azure_ai_with_function_tools.py`](azure_ai_with_function_tools.py) | Demonstrates how to use function tools with agents. Shows both agent-level tools (defined when creating the agent) and query-level tools (provided with specific queries). |
| [`azure_ai_with_hosted_mcp.py`](azure_ai_with_hosted_mcp.py) | Shows how to integrate Azure AI agents with hosted Model Context Protocol (MCP) servers for enhanced functionality and tool integration. Demonstrates remote MCP server connections and tool discovery. |
@@ -0,0 +1,73 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import ChatAgent, HostedFileSearchTool
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential
"""
Azure AI Agent with Azure AI Search Example
This sample demonstrates how to create an Azure AI agent that uses Azure AI Search
to search through indexed hotel data and answer user questions about hotels.
Prerequisites:
1. Set AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME environment variables
2. Ensure you have an Azure AI Search connection configured in your Azure AI project
3. The search index "hotels-sample-index" should exist in your Azure AI Search service
(you can create this using the Azure portal with sample hotel data)
Environment variables:
- AZURE_AI_PROJECT_ENDPOINT: Your Azure AI project endpoint
- AZURE_AI_MODEL_DEPLOYMENT_NAME: The name of your model deployment
"""
# Test queries to verify Azure AI Search is working with the hotels-sample-index
USER_INPUTS = [
"Search the hotel database for Stay-Kay City Hotel and give me detailed information.",
]
async def main() -> None:
"""Main function demonstrating Azure AI agent with Azure AI Search capabilities."""
# 1. Create Azure AI Search tool using HostedFileSearchTool
# The tool will automatically use the default Azure AI Search connection from your project
azure_ai_search_tool = HostedFileSearchTool(
additional_properties={
"index_name": "hotels-sample-index", # Name of your search index
"query_type": "simple", # Use simple search
"top_k": 10, # Get more comprehensive results
},
)
# 2. Use AzureAIAgentClient as async context manager for automatic cleanup
async with (
AzureAIAgentClient(async_credential=AzureCliCredential()) as client,
ChatAgent(
chat_client=client,
name="HotelSearchAgent",
instructions=("You are a helpful travel assistant that searches hotel information."),
tools=azure_ai_search_tool,
) as agent,
):
print("=== Azure AI Agent with Azure AI Search ===")
print("This agent can search through hotel data to help you find accommodations.\n")
# 3. Simulate conversation with the agent
for user_input in USER_INPUTS:
print(f"User: {user_input}")
print("Agent: ", end="", flush=True)
# Stream the response for better user experience
async for chunk in agent.run_stream(user_input):
if chunk.text:
print(chunk.text, end="", flush=True)
print("\n" + "=" * 50 + "\n")
print("Hotel search conversation completed!")
if __name__ == "__main__":
asyncio.run(main())