Skip to content

Commit 07db911

Browse files
authored
Python: Fix Azure AI Getting Started samples: Improve documentation and code readability (microsoft#1089)
* Initial plan * Fix Azure AI samples: Add dotenv support, fix async input, improve docs Co-authored-by: dmytrostruk <13853051+dmytrostruk@users.noreply.github.com> * Remove dotenv.load_dotenv() and revert async input() changes per review feedback Co-authored-by: dmytrostruk <13853051+dmytrostruk@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dmytrostruk <13853051+dmytrostruk@users.noreply.github.com>
1 parent cd1bdc1 commit 07db911

4 files changed

Lines changed: 61 additions & 5 deletions

File tree

‎python/.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Azure AI
22
AZURE_AI_PROJECT_ENDPOINT=""
33
AZURE_AI_MODEL_DEPLOYMENT_NAME=""
4+
# Bing connection for web search (optional, used by samples with web search)
5+
BING_CONNECTION_ID=""
46
# OpenAI
57
OPENAI_API_KEY=""
68
OPENAI_CHAT_MODEL_ID=""

‎python/samples/getting_started/agents/azure_ai/README.md‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,47 @@ This folder contains examples demonstrating different ways to create and use age
2222

2323
## Environment Variables
2424

25-
Make sure to set the following environment variables before running the examples:
25+
Before running the examples, you need to set up your environment variables. You can do this in one of two ways:
2626

27-
- `AZURE_AI_PROJECT_ENDPOINT`: Your Azure AI project endpoint
28-
- `AZURE_AI_MODEL_DEPLOYMENT_NAME`: The name of your model deployment
27+
### Option 1: Using a .env file (Recommended)
28+
29+
1. Copy the `.env.example` file from the `python` directory to create a `.env` file:
30+
```bash
31+
cp ../../.env.example ../../.env
32+
```
33+
34+
2. Edit the `.env` file and add your values:
35+
```
36+
AZURE_AI_PROJECT_ENDPOINT="your-project-endpoint"
37+
AZURE_AI_MODEL_DEPLOYMENT_NAME="your-model-deployment-name"
38+
```
39+
40+
3. For samples using Bing Grounding search (like `azure_ai_with_bing_grounding.py` and `azure_ai_with_multiple_tools.py`), you'll also need:
41+
```
42+
BING_CONNECTION_ID="/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.CognitiveServices/accounts/{ai-service-name}/projects/{project-name}/connections/{connection-name}"
43+
```
44+
45+
To get your Bing connection ID:
46+
- Go to [Azure AI Foundry portal](https://ai.azure.com)
47+
- Navigate to your project's "Connected resources" section
48+
- Add a new connection for "Grounding with Bing Search"
49+
- Copy the connection ID
50+
51+
### Option 2: Using environment variables directly
52+
53+
Set the environment variables in your shell:
54+
55+
```bash
56+
export AZURE_AI_PROJECT_ENDPOINT="your-project-endpoint"
57+
export AZURE_AI_MODEL_DEPLOYMENT_NAME="your-model-deployment-name"
58+
export BING_CONNECTION_ID="your-bing-connection-id" # Optional, only needed for web search samples
59+
```
60+
61+
### Required Variables
62+
63+
- `AZURE_AI_PROJECT_ENDPOINT`: Your Azure AI project endpoint (required for all examples)
64+
- `AZURE_AI_MODEL_DEPLOYMENT_NAME`: The name of your model deployment (required for all examples)
65+
66+
### Optional Variables
67+
68+
- `BING_CONNECTION_ID`: Your Bing connection ID (required for `azure_ai_with_bing_grounding.py` and `azure_ai_with_multiple_tools.py`)

‎python/samples/getting_started/agents/azure_ai/azure_ai_with_code_interpreter.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ async def main() -> None:
4343
):
4444
agent = chat_client.create_agent(
4545
name="CodingAgent",
46-
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
46+
instructions=(
47+
"You are a helpful assistant that can write and execute Python code to solve problems."
48+
),
4749
tools=HostedCodeInterpreterTool(),
4850
)
4951
query = "Generate the factorial of 100 using python code, show the code and execute it."

‎python/samples/getting_started/agents/azure_ai/azure_ai_with_multiple_tools.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
1919
This sample demonstrates integrating multiple tools (MCP and Web Search) with Azure AI Agents,
2020
including user approval workflows for function call security.
21+
22+
Prerequisites:
23+
1. Set AZURE_AI_PROJECT_ENDPOINT and AZURE_AI_MODEL_DEPLOYMENT_NAME environment variables
24+
2. For Bing search functionality, set BING_CONNECTION_ID environment variable to your Bing connection ID
25+
Example: BING_CONNECTION_ID="/subscriptions/{subscription-id}/resourceGroups/{resource-group}/
26+
providers/Microsoft.CognitiveServices/accounts/{ai-service-name}/projects/{project-name}/
27+
connections/{connection-name}"
28+
29+
To set up Bing Grounding:
30+
1. Go to Azure AI Foundry portal (https://ai.azure.com)
31+
2. Navigate to your project's "Connected resources" section
32+
3. Add a new connection for "Grounding with Bing Search"
33+
4. Copy the connection ID and set it as the BING_CONNECTION_ID environment variable
2134
"""
2235

2336

@@ -66,7 +79,6 @@ async def main() -> None:
6679
name="Microsoft Learn MCP",
6780
url="https://learn.microsoft.com/api/mcp",
6881
),
69-
# needs BING_CONNECTION_ID set in the env
7082
HostedWebSearchTool(count=5),
7183
get_time,
7284
],

0 commit comments

Comments
 (0)