From c1786b38a758c70c581ae24e124fa1ccafa4f904 Mon Sep 17 00:00:00 2001 From: Giles Odigwe <79032838+giles17@users.noreply.github.com> Date: Fri, 14 Nov 2025 11:12:51 -0800 Subject: [PATCH] Python: Added Bing Custom Search Sample using HostedWebSearchTool (#2226) * custom search sample using hostedwebsearch * small fixes * Update python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_custom_search.py Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> --------- Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> --- .../agents/azure_ai_agent/README.md | 17 +++++ .../azure_ai_with_bing_custom_search.py | 63 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_custom_search.py diff --git a/python/samples/getting_started/agents/azure_ai_agent/README.md b/python/samples/getting_started/agents/azure_ai_agent/README.md index 375f682474..19165b89ed 100644 --- a/python/samples/getting_started/agents/azure_ai_agent/README.md +++ b/python/samples/getting_started/agents/azure_ai_agent/README.md @@ -7,6 +7,7 @@ This folder contains examples demonstrating different ways to create and use age | File | Description | |------|-------------| | [`azure_ai_basic.py`](azure_ai_basic.py) | The simplest way to create an agent using `ChatAgent` with `AzureAIAgentClient`. It automatically handles all configuration using environment variables. | +| [`azure_ai_with_bing_custom_search.py`](azure_ai_with_bing_custom_search.py) | Shows how to use Bing Custom Search with Azure AI agents to find real-time information from the web using custom search configurations. Demonstrates how to set up and use HostedWebSearchTool with custom search instances. | | [`azure_ai_with_bing_grounding.py`](azure_ai_with_bing_grounding.py) | Shows how to use Bing Grounding search with Azure AI agents to find real-time information from the web. Demonstrates web search capabilities with proper source citations and comprehensive error handling. | | [`azure_ai_with_code_interpreter.py`](azure_ai_with_code_interpreter.py) | Shows how to use the HostedCodeInterpreterTool with Azure AI agents to write and execute Python code. Includes helper methods for accessing code interpreter data from response chunks. | | [`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. | @@ -49,6 +50,18 @@ Before running the examples, you need to set up your environment variables. You - Add a new connection for "Grounding with Bing Search" - Copy the ID +4. For samples using Bing Custom Search (like `azure_ai_with_bing_custom_search.py`), you'll also need: + ``` + BING_CUSTOM_CONNECTION_ID="your-bing-custom-connection-id" + BING_CUSTOM_INSTANCE_NAME="your-bing-custom-instance-name" + ``` + + To get your Bing Custom Search connection details: + - Go to [Azure AI Foundry portal](https://ai.azure.com) + - Navigate to your project's "Connected resources" section + - Add a new connection for "Grounding with Bing Custom Search" + - Copy the connection ID and instance name + ### Option 2: Using environment variables directly Set the environment variables in your shell: @@ -57,6 +70,8 @@ Set the environment variables in your shell: export AZURE_AI_PROJECT_ENDPOINT="your-project-endpoint" export AZURE_AI_MODEL_DEPLOYMENT_NAME="your-model-deployment-name" export BING_CONNECTION_ID="your-bing-connection-id" +export BING_CUSTOM_CONNECTION_ID="your-bing-custom-connection-id" +export BING_CUSTOM_INSTANCE_NAME="your-bing-custom-instance-name" ``` ### Required Variables @@ -67,3 +82,5 @@ export BING_CONNECTION_ID="your-bing-connection-id" ### Optional Variables - `BING_CONNECTION_ID`: Your Bing connection ID (required for `azure_ai_with_bing_grounding.py` and `azure_ai_with_multiple_tools.py`) +- `BING_CUSTOM_CONNECTION_ID`: Your Bing Custom Search connection ID (required for `azure_ai_with_bing_custom_search.py`) +- `BING_CUSTOM_INSTANCE_NAME`: Your Bing Custom Search instance name (required for `azure_ai_with_bing_custom_search.py`) diff --git a/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_custom_search.py b/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_custom_search.py new file mode 100644 index 0000000000..0474d6c5bc --- /dev/null +++ b/python/samples/getting_started/agents/azure_ai_agent/azure_ai_with_bing_custom_search.py @@ -0,0 +1,63 @@ +# Copyright (c) Microsoft. All rights reserved. + +import asyncio + +from agent_framework import ChatAgent, HostedWebSearchTool +from agent_framework.azure import AzureAIAgentClient +from azure.identity.aio import AzureCliCredential + +""" +The following sample demonstrates how to create an Azure AI agent that +uses Bing Custom Search to find real-time information from the web. + +More information on Bing Custom Search and difference from Bing Grounding can be found here: +https://learn.microsoft.com/en-us/azure/ai-foundry/agents/how-to/tools/bing-custom-search + +Prerequisites: +1. A connected Grounding with Bing Custom Search resource in your Azure AI project +2. Set BING_CUSTOM_CONNECTION_ID environment variable + Example: BING_CUSTOM_CONNECTION_ID="your-bing-custom-connection-id" +3. Set BING_CUSTOM_INSTANCE_NAME environment variable + Example: BING_CUSTOM_INSTANCE_NAME="your-bing-custom-instance-name" + +To set up Bing Custom Search: +1. Go to Azure AI Foundry portal (https://ai.azure.com) +2. Navigate to your project's "Connected resources" section +3. Add a new connection for "Grounding with Bing Custom Search" +4. Copy the connection ID and instance name and set the appropriate environment variables +""" + + +async def main() -> None: + """Main function demonstrating Azure AI agent with Bing Custom Search.""" + # 1. Create Bing Custom Search tool using HostedWebSearchTool + # The connection ID and instance name will be automatically picked up from environment variables + bing_search_tool = HostedWebSearchTool( + name="Bing Custom Search", + description="Search the web for current information using Bing Custom Search", + ) + + # 2. Use AzureAIAgentClient as async context manager for automatic cleanup + async with ( + AzureAIAgentClient(async_credential=AzureCliCredential()) as client, + ChatAgent( + chat_client=client, + name="BingSearchAgent", + instructions=( + "You are a helpful agent that can use Bing Custom Search tools to assist users. " + "Use the available Bing Custom Search tools to answer questions and perform tasks." + ), + tools=bing_search_tool, + ) as agent, + ): + # 3. Demonstrate agent capabilities with bing custom search + print("=== Azure AI Agent with Bing Custom Search ===\n") + + user_input = "Tell me more about foundry agent service" + print(f"User: {user_input}") + response = await agent.run(user_input) + print(f"Agent: {response.text}\n") + + +if __name__ == "__main__": + asyncio.run(main())