This repository contains a minimal Python example showing how to use the Azure AI Projects SDK with a Bing grounding tool to build an AI agent that can answer questions using live web search (via a Bing connection).
The example script, agent.py, creates:
- Azure AI agent: Powered by a deployed model in your Azure AI Project.
- Bing grounding tool: Configured via an Azure AI connection to Bing.
- Conversation thread: Sends a user message like “what's the weather in London now?”.
- Agent run: Executes the agent with the Bing tool and returns a grounded answer.
- Console output:
- Assistant response: Textual answer from the agent.
- URL citations: Source links returned by Bing used to ground the answer.
- Python: 3.9+ (recommended)
- Azure:
- Azure subscription
- Azure AI Project set up
- A deployed model in your Azure AI Project (e.g., GPT-style deployment)
- A Bing connection configured in the same project
- Credentials:
- Access to your Azure subscription (e.g., via
az loginor environment-based credentials)
- Access to your Azure subscription (e.g., via
- Clone this repo
git clone <your-repo-url>.git
cd Bing_Search_Agent- Create and activate a virtual environment (recommended)
python -m venv .venv
.\.venv\Scripts\activate # On Windows
# source .venv/bin/activate # On macOS / Linux- Install dependencies
pip install -r requirements.txtThis project uses python-dotenv to load environment variables from a .env file.
Create a .env file in the project root with:
PROJECT_CONNECTION_STRING="your-azure-ai-project-connection-string"
MODEL_DEPLOYMENT_NAME="your-model-deployment-name"
BING_CONNECTION_NAME="your-bing-connection-name"PROJECT_CONNECTION_STRING: Found in your Azure AI Project (e.g., in Azure AI Studio).MODEL_DEPLOYMENT_NAME: The name of the model deployment you want to use.BING_CONNECTION_NAME: The name of the Bing connection you created in the project.
The script uses DefaultAzureCredential, so ensure your local environment is authenticated with Azure (for example with az login, managed identity, or environment variables).
agent.py performs the following steps:
- Load configuration from environment variables using
dotenv. - Create an
AIProjectClientwithDefaultAzureCredentialandPROJECT_CONNECTION_STRING. - Look up the Bing connection by
BING_CONNECTION_NAMEand get itsid. - Create a
BingGroundingToolusing that connection ID. - Create an agent with:
modelfromMODEL_DEPLOYMENT_NAME- Name:
bing-assistant - Instructions:
"You are a helpful assistant" tools:bing.definitions- Headers:
{"x-ms-enable-preview": "true"}
- Create a thread and send a message with user content:
- Example:
"whats the weather in London now?"
- Example:
- Create and process a run for that thread and agent.
- Log results to the console:
- Agent and thread IDs
- Run status (and error if any)
- The assistant’s text answer
- The URL citations extracted from the message annotations.
Run the script:
python agent.pyYou should see:
- Agent ID and Thread ID printed
- Run status (e.g.,
completed) - The assistant’s answer to your question
- A list of URL citations used as sources
-
Change the user question
Edit thecontentin thecreate_messagecall withinagent.py:content="whats the weather in London now?"
Replace this with any query where web search is useful.
-
Change agent behavior
Modify theinstructionsparameter increate_agentto adjust the agent’s tone, role, or behavior. -
Extend tools
If your Azure AI Project has more tools configured, you can add them to thetoolslist when creating the agent.
- This repository is a minimal sample for learning how to:
- Wire Azure AI Projects with a Bing grounding tool.
- Create simple agents and threads.
- Use citations from web search results.
- For production scenarios, consider:
- Structured logging and error handling
- Persisting agents/threads/messages
- Adding an API or UI layer on top of this script.
Specify your preferred license here (for example, MIT or Apache-2.0).