Search Tutorials


Azure AI Foundry Tutorials - Bing Web Search Agent | JavaInUse

Azure AI Foundry Tutorials - Bing Web Search Agent

The Knowledge Cutoff Problem

In previous tutorial we had created an agent using Azure AI Foundry Python SDK. The agent we created could answer general questions and maintain conversation history, but it had a significant limitation. Let us test this limitation. If we ask our agent "What is the stock price of Microsoft?", we get this response:
I currently cannot provide real-time stock information. You can check up-to-date 
stock prices for Microsoft (MSFT) on financial platforms like Yahoo Finance, 
Google Finance, or your broker's website or app.

Agent Cannot Answer Real-time Questions
The agent cannot answer this question because LLMs like GPT-4o are trained on data up to a specific cutoff date. They do not have access to real-time information.
Why LLMs Have Knowledge Cutoffs:
Training large language models costs billions of dollars and takes months. It is economically not feasible to retrain these models every day with the latest information. As a result, LLMs have a knowledge cutoff date - a point in time after which they do not know about events, data, or changes.
This limitation affects many real-world use cases:
  • Stock prices and financial data - Changes every second
  • Weather information - Changes daily
  • Current news and events - Happening right now
  • Sports scores - Updated continuously
  • Product availability and pricing - Changes frequently
So how do we solve this problem? We give the agent access to external tools that can retrieve real-time information. This is where Bing Grounding comes in.

Azure AI Agents - Table of Contents

Azure AI Foundry Hello World Example Azure AI Foundry - Azure AI Agent Hello World Example Azure AI Foundry - Foundry vs Hub Based Projects Azure AI Foundry - Build Agent with Azure AI Foundry SDK Azure AI Foundry - Bing Web Search Agent

What is Bing Grounding?

Bing Grounding (also called Grounding with Bing Search) is a tool that allows Azure AI agents to search the web using Bing and retrieve current information. When you give an agent the Bing Grounding tool:
  • The agent can autonomously decide when to search the web
  • It searches Bing for relevant, up-to-date information
  • It reads the search results and incorporates them into its response
  • It provides answers grounded in current, real-world data

Bing Grounding Architecture
This is an example of tool calling or function calling. The LLM recognizes that it needs external information, calls the Bing search tool, gets the results, and formulates a response based on that data.

How Bing Grounding Works

Here is the flow when an agent with Bing Grounding receives a question: Step 1: User asks a question requiring current information
User: "What is the stock price of Microsoft?"
Step 2: Agent analyzes the question and recognizes it needs real-time data
Agent thinks: "This question requires current stock price data, 
which I don't have. I should use the Bing search tool."
Step 3: Agent calls the Bing Grounding tool
Agent executes: bing_search("Microsoft stock price")
Step 4: Bing searches the web and returns relevant results Step 5: Agent reads the search results and formulates a response
Agent: "As of [current date], Microsoft (MSFT) is trading at $XXX.XX, 
up X.X% from yesterday's close."
This all happens automatically - you just need to give the agent access to the Bing Grounding tool.

Implementation

In previous tutorial we had implemented azure ai agent using azure foundry sdk. In this tutorial we will be modifying this code to implement bing grounding. Step 1: Create Bing Search Resource Go to the Azure portal and search for "Bing resources" in the search bar.
Search Bing Resources
Step 2: Add Grounding with Bing Search Click on "Grounding with Bing Search" to create a new Bing search resource.
Add Bing Grounding
Step 3: Configure the Resource Create a new grounding resource with the following details:
  • Name: javainuse-bing-search (or any name you prefer)
  • Subscription: Select your Azure subscription
  • Resource Group: Select the same resource group as your AI Foundry project
  • Region: Choose the same region as your AI Foundry project
  • Pricing Tier: Select based on your needs (Free tier available for testing)

Create Bing Resource
Click Review + Create and then Create to provision the resource.
Note: The Bing Search resource will take a few minutes to deploy. Wait for the deployment to complete before proceeding to the next step.
h2>Connecting Bing Search to Your AI Foundry Project Now we need to connect the Bing search resource to our AI Foundry project so our agents can use it. Step 1: Open Your AI Foundry Project Go to the AI Foundry project we created in the previous tutorial.
AI Foundry Project
Step 2: Navigate to Connected Resources In the left sidebar, look for "Connected resources" or "Connections" and click on it. Click on "New connection" or "Add connection".
Add New Connection
Step 3: Select Bing Search In the connection type list, you will see "Bing Search" or "Grounding with Bing Search". Select it. You will see the Bing search resource we just created (javainuse-bing-search). Select it and click "Add connection".
Select Bing Resource
Step 4: Note the Connection ID After the connection is created, you will see a connection ID. We need this ID in our code. The connection ID follows this format:
/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.CognitiveServices/accounts/{ai_service_name}/projects/{project_name}/connections/{connection_name}
You can copy this from the connection details page.
Tip: You can also construct the connection ID manually if you know your subscription ID, resource group, and connection name. We will add this to our environment variables in the next step.

Updating Environment Variables

Now we need to add the Bing connection ID to our environment configuration. Update your .env file to include the Bing connection:
MODEL_DEPLOYMENT_NAME="gpt-4o"
AZURE_AI_ENDPOINT="https://new-foundry-ai-project--resource.services.ai.azure.com/api/projects/new-foundry-ai-project-1"
BING_CONNECTION_ID="/subscriptions/800863d4-5e1f-466d-b9bf-d039fcf217e0/resourceGroups/javainuse-aifoundry/providers/Microsoft.CognitiveServices/accounts/new-foundry-ai-project--resource/projects/new-foundry-ai-project-1/connections/javainuse-bing-search"
Important: Make sure to replace the connection ID with your actual values:
  • Replace subscription_id with your Azure subscription ID
  • Replace resource_group with your resource group name
  • Replace ai_service_name with your AI Foundry resource name
  • Replace project_name with your project name
  • Replace connection_name with your Bing connection name
Now let us modify our agent code to use the Bing Grounding tool. Key Changes:
  • Import BingGroundingTool from azure.ai.agents.models
  • Create a BingGroundingTool instance with the connection ID
  • Pass the tool to the agent when creating it
Here is the complete updated code:
import os
import time
from azure.ai.agents import AgentsClient
from azure.ai.agents.models import ListSortOrder, BingGroundingTool
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Get configuration
endpoint = os.getenv("AZURE_AI_ENDPOINT")
model_deployment = os.getenv("MODEL_DEPLOYMENT_NAME")
bing_connection_id = os.getenv("BING_CONNECTION_ID")

# Validate configuration
if not all([endpoint, model_deployment, bing_connection_id]):
    raise ValueError("Missing required environment variables")

# Create AgentsClient
agents_client = AgentsClient(
    endpoint=endpoint,
    credential=DefaultAzureCredential(exclude_environment_credential=True)
)

print("Connected to Azure AI Foundry")

# Create Bing Grounding Tool
bing_tool = BingGroundingTool(connection_id=bing_connection_id)

# Create agent with Bing tool
with agents_client:
    agent = agents_client.create_agent(
        model=model_deployment,
        name="bing-search-agent",
        instructions="You are a helpful agent with access to web search. Use Bing search to find current information when needed.",
        tools=bing_tool.definitions
    )
    print(f"Created agent with Bing search: {agent.id}")

    # Create thread
    thread = agents_client.threads.create()
    print(f"Created thread: {thread.id}")

    # Ask a question requiring current information
    message = agents_client.messages.create(
        thread_id=thread.id,
        role="user",
        content="What is the current stock price of Microsoft?"
    )
    print(f"Created message: {message.id}")

    # Run agent
    run = agents_client.runs.create(
        thread_id=thread.id,
        agent_id=agent.id
    )
    print(f"Started run: {run.id}")

    # Poll until complete
    while run.status in ["queued", "in_progress", "requires_action"]:
        time.sleep(1)
        run = agents_client.runs.get(thread_id=thread.id, run_id=run.id)
        print(f"Run status: {run.status}")

    print(f"Run completed with status: {run.status}")

    # Check for errors
    if run.status == "failed":
        print(f"Run failed. Error: {run.last_error}")
    
    # Get messages
    messages = agents_client.messages.list(
        thread_id=thread.id,
        order=ListSortOrder.ASCENDING
    )

    # Display conversation
    print("\n=== Conversation ===")
    for msg in messages:
        if msg.text_messages:
            last_text = msg.text_messages[-1]
            print(f"{msg.role}: {last_text.text.value}")
        else:
            print(f"{msg.role}: [No text messages]")

Code Explanation - What Changed?

Let us understand the key changes we made:

1. Import BingGroundingTool

from azure.ai.agents.models import BingGroundingTool
This imports the Bing Grounding tool class that we will use to enable web search.

2. Create Bing Tool Instance

bing_connection_id = os.getenv("BING_CONNECTION_ID")
bing_tool = BingGroundingTool(connection_id=bing_connection_id)
We create an instance of BingGroundingTool with our Bing connection ID. This tool will allow the agent to search Bing.

3. Pass Tool to Agent

agent = agents_client.create_agent(
    model=model_deployment,
    name="bing-search-agent",
    instructions="You are a helpful agent with access to web search. Use Bing search to find current information when needed.",
    tools=bing_tool.definitions  # <-- This is the key addition
)
The tools parameter tells the agent which tools it has access to. By passing bing_tool.definitions, we give the agent the ability to search the web. Notice we also updated the instructions to mention web search capability. This helps the agent understand when to use the tool.

Running the Code

Make sure you are in your virtual environment and run:
python agent.py
You should see output similar to:
PS E:\agents\1> python .\lab2\agent.py
Created thread, thread ID: thread_5kJ2bpdYcRcco8266j6uH7sR
Created message, message ID: msg_7A5lwRncjNXjIF160LuLorBx
Created run, run ID: run_2zBE0VEYaONtOmuXOz6rFKrq, status: RunStatus.QUEUED
Run status: RunStatus.IN_PROGRESS
Run status: RunStatus.COMPLETED
Run completed with status: RunStatus.COMPLETED

Messages in thread:
MessageRole.USER: What is the stock price of Microsoft?
MessageRole.AGENT: As of the latest available trading session, Microsoft's stock (MSFT) is priced at $479.28(3:0 source)(3:1 sourc).

Agent SDK Output

Download Source Code

Download it -
Bing Web Search Agent