Skip to content

Commit 9418951

Browse files
howieleungCopilot
andauthored
Howie/samples 10 (#43968)
* More samples * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * update * code clean up * Update sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_browser_automation.py Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent f8997e5 commit 9418951

File tree

6 files changed

+203
-11
lines changed

6 files changed

+203
-11
lines changed

sdk/ai/azure-ai-projects/.env.template

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ MCP_PROJECT_CONNECTION_ID=
4343
FABRIC_PROJECT_CONNECTION_ID=
4444
AI_SEARCH_PROJECT_CONNECTION_ID=
4545
AI_SEARCH_INDEX_NAME=
46-
47-
48-
46+
BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID=
47+
BING_CUSTOM_SEARCH_INSTANCE_NAME=
48+
SHAREPOINT_PROJECT_CONNECTION_ID=
49+
A2A_PROJECT_CONNECTION_ID=
50+
BROWSER_AUTOMATION_PROJECT_CONNECTION_ID=

sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_ai_search.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@
7272
)
7373
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
7474

75-
user_input = input(
76-
"Enter your question for the AI Search agent available in the index "
77-
"(e.g., 'Tell me about the mental health services available from Premera'): \n"
78-
)
75+
user_input = input("Enter your question (e.g., 'Tell me about mental health services'): \n")
7976

8077
stream_response = openai_client.responses.create(
8178
stream=True,

sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_bing_custom_search.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@
7171
)
7272
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
7373

74-
user_input = input(
75-
"Enter your question for the Bing Custom Search agent " "(e.g., 'Tell me more about foundry agent service'): \n"
76-
)
74+
user_input = input("Enter your question (e.g., 'Tell me more about foundry agent service'): \n")
7775

7876
# Send initial request that will trigger the Bing Custom Search tool
7977
stream_response = openai_client.responses.create(
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with Browser Automation capabilities
9+
using the BrowserAutomationAgentTool and synchronous Azure AI Projects client. The agent can
10+
perform automated web browsing tasks and provide responses based on web interactions.
11+
12+
USAGE:
13+
python sample_agent_browser_automation.py
14+
15+
Before running the sample:
16+
17+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
18+
19+
Set these environment variables with your own values:
20+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
page of your Microsoft Foundry portal.
22+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Microsoft Foundry project.
24+
3) BROWSER_AUTOMATION_PROJECT_CONNECTION_ID - The browser automation project connection ID,
25+
as found in the "Connections" tab in your Microsoft Foundry project.
26+
"""
27+
28+
import os
29+
import json
30+
from dotenv import load_dotenv
31+
from azure.identity import DefaultAzureCredential
32+
from azure.ai.projects import AIProjectClient
33+
from azure.ai.projects.models import (
34+
PromptAgentDefinition,
35+
BrowserAutomationAgentTool,
36+
BrowserAutomationToolParameters,
37+
BrowserAutomationToolConnectionParameters,
38+
)
39+
load_dotenv()
40+
41+
project_client = AIProjectClient(
42+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
43+
credential=DefaultAzureCredential(),
44+
)
45+
46+
openai_client = project_client.get_openai_client()
47+
48+
browser_automation_tool = BrowserAutomationAgentTool(
49+
browser_automation_preview=BrowserAutomationToolParameters(
50+
connection=BrowserAutomationToolConnectionParameters(
51+
project_connection_id=os.environ["BROWSER_AUTOMATION_PROJECT_CONNECTION_ID"],
52+
)
53+
)
54+
)
55+
56+
with project_client:
57+
agent = project_client.agents.create_version(
58+
agent_name="MyAgent",
59+
definition=PromptAgentDefinition(
60+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
61+
instructions="""You are an Agent helping with browser automation tasks.
62+
You can answer questions, provide information, and assist with various tasks
63+
related to web browsing using the Browser Automation tool available to you.""",
64+
tools=[browser_automation_tool],
65+
),
66+
)
67+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
68+
69+
stream_response = openai_client.responses.create(
70+
stream=True,
71+
tool_choice="required",
72+
input="""
73+
Your goal is to report the percent of Microsoft year-to-date stock price change.
74+
To do that, go to the website finance.yahoo.com.
75+
At the top of the page, you will find a search bar.
76+
Enter the value 'MSFT', to get information about the Microsoft stock price.
77+
At the top of the resulting page you will see a default chart of Microsoft stock price.
78+
Click on 'YTD' at the top of that chart, and report the percent value that shows up just below it.""",
79+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
80+
)
81+
82+
for event in stream_response:
83+
if event.type == "response.created":
84+
print(f"Follow-up response created with ID: {event.response.id}")
85+
elif event.type == "response.output_text.delta":
86+
print(f"Delta: {event.delta}")
87+
elif event.type == "response.text.done":
88+
print(f"\nFollow-up response done!")
89+
elif event.type == "response.output_item.done":
90+
item = event.item
91+
if item.type == "browser_automation_preview_call": # TODO: support browser_automation_preview_call schema
92+
arguments_str = getattr(item, "arguments", "{}")
93+
94+
# Parse the arguments string into a dictionary
95+
arguments = json.loads(arguments_str)
96+
query = arguments.get("query")
97+
98+
print(f"Call ID: {getattr(item, 'call_id')}")
99+
print(f"Query arguments: {query}")
100+
elif event.type == "response.completed":
101+
print(f"\nFollow-up completed!")
102+
print(f"Full response: {event.response.output_text}")
103+
104+
print("\nCleaning up...")
105+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
106+
print("Agent deleted")

sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_fabric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
)
6565
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
6666

67-
user_input = input("Enter your question for Fabric (e.g., 'Tell me about sales records'): \n")
67+
user_input = input("Enter your question (e.g., 'Tell me about sales records'): \n")
6868

6969
response = openai_client.responses.create(
7070
tool_choice="required",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with Agent-to-Agent (A2A) capabilities
9+
using the A2ATool and synchronous Azure AI Projects client. The agent can communicate
10+
with other agents and provide responses based on inter-agent interactions using the
11+
A2A protocol (https://a2a-protocol.org/latest/).
12+
13+
USAGE:
14+
python sample_agent_to_agent.py
15+
16+
Before running the sample:
17+
18+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv
19+
20+
Set these environment variables with your own values:
21+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
22+
page of your Microsoft Foundry portal.
23+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
24+
the "Models + endpoints" tab in your Microsoft Foundry project.
25+
3) A2A_PROJECT_CONNECTION_ID - The A2A project connection ID,
26+
as found in the "Connections" tab in your Microsoft Foundry project.
27+
"""
28+
29+
import os
30+
from dotenv import load_dotenv
31+
from azure.identity import DefaultAzureCredential
32+
from azure.ai.projects import AIProjectClient
33+
from azure.ai.projects.models import (
34+
PromptAgentDefinition,
35+
A2ATool,
36+
)
37+
38+
load_dotenv()
39+
40+
project_client = AIProjectClient(
41+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
42+
credential=DefaultAzureCredential(),
43+
)
44+
45+
openai_client = project_client.get_openai_client()
46+
47+
with project_client:
48+
agent = project_client.agents.create_version(
49+
agent_name="MyAgent",
50+
definition=PromptAgentDefinition(
51+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
52+
instructions="You are a helpful assistant.",
53+
tools=[
54+
A2ATool(
55+
project_connection_id=os.environ["A2A_PROJECT_CONNECTION_ID"],
56+
)
57+
],
58+
),
59+
)
60+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
61+
62+
user_input = input("Enter your question (e.g., 'What can the secondary agent do?'): \n")
63+
64+
stream_response = openai_client.responses.create(
65+
stream=True,
66+
tool_choice="required",
67+
input=user_input,
68+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
69+
)
70+
71+
for event in stream_response:
72+
if event.type == "response.created":
73+
print(f"Follow-up response created with ID: {event.response.id}")
74+
elif event.type == "response.output_text.delta":
75+
print(f"Delta: {event.delta}")
76+
elif event.type == "response.text.done":
77+
print(f"\nFollow-up response done!")
78+
elif event.type == "response.output_item.done":
79+
item = event.item
80+
if item.type == "remote_function_call": # TODO: support remote_function_call schema
81+
print(f"Call ID: {getattr(item, 'call_id')}")
82+
print(f"Label: {getattr(item, 'label')}")
83+
elif event.type == "response.completed":
84+
print(f"\nFollow-up completed!")
85+
print(f"Full response: {event.response.output_text}")
86+
87+
print("\nCleaning up...")
88+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
89+
print("Agent deleted")

0 commit comments

Comments
 (0)