1# pylint: disable=line-too-long,useless-suppression
2# ------------------------------------
3# Copyright (c) Microsoft Corporation.
4# Licensed under the MIT License.
5# ------------------------------------
6
7"""
8DESCRIPTION:
9 This sample demonstrates how to use agent operations with the Browser Automation tool from
10 the Azure Agents service using a synchronous client.
11
12USAGE:
13 python sample_agents_browser_automation.py
14
15 Before running the sample:
16
17 pip install azure-ai-agents --pre
18 pip install azure-ai-projects azure-identity
19
20 Set these environment variables with your own values:
21 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
22 page of your Azure AI Foundry portal.
23 2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
24 the "Models + endpoints" tab in your Azure AI Foundry project.
25 3) AZURE_PLAYWRIGHT_CONNECTION_NAME - The name of a connection to the Azure Playwright Workspace as it is
26 listed in Azure AI Foundry connected resources.
27"""
28
29import os
30from azure.ai.projects import AIProjectClient
31from azure.ai.agents.models import (
32 MessageRole,
33 RunStepToolCallDetails,
34 BrowserAutomationTool,
35 RunStepBrowserAutomationToolCall,
36)
37from azure.identity import DefaultAzureCredential
38
39project_client = AIProjectClient(endpoint=os.environ["PROJECT_ENDPOINT"], credential=DefaultAzureCredential())
40
41# [START create_agent_with_browser_automation]
42connection_id = project_client.connections.get(os.environ["AZURE_PLAYWRIGHT_CONNECTION_NAME"]).id
43
44# Initialize Browser Automation tool and add the connection id
45browser_automation = BrowserAutomationTool(connection_id=connection_id)
46
47with project_client:
48
49 agents_client = project_client.agents
50
51 # Create a new Agent that has the Browser Automation tool attached.
52 # Note: To add Browser Automation tool to an existing Agent with an `agent_id`, do the following:
53 # agent = agents_client.update_agent(agent_id, tools=browser_automation.definitions)
54 agent = agents_client.create_agent(
55 model=os.environ["MODEL_DEPLOYMENT_NAME"],
56 name="my-agent",
57 instructions="""
58 You are an Agent helping with browser automation tasks.
59 You can answer questions, provide information, and assist with various tasks
60 related to web browsing using the Browser Automation tool available to you.
61 """,
62 tools=browser_automation.definitions,
63 )
64
65 # [END create_agent_with_browser_automation]
66
67 print(f"Created agent, ID: {agent.id}")
68
69 # Create thread for communication
70 thread = agents_client.threads.create()
71 print(f"Created thread, ID: {thread.id}")
72
73 # Create message to thread
74 message = agents_client.messages.create(
75 thread_id=thread.id,
76 role=MessageRole.USER,
77 content="""
78 Your goal is to report the percent of Microsoft year-to-date stock price change.
79 To do that, go to the website finance.yahoo.com.
80 At the top of the page, you will find a search bar.
81 Enter the value 'MSFT', to get information about the Microsoft stock price.
82 At the top of the resulting page you will see a default chart of Microsoft stock price.
83 Click on 'YTD' at the top of that chart, and report the percent value that shows up just below it.
84 """,
85 )
86 print(f"Created message, ID: {message.id}")
87
88 # Create and process agent run in thread with tools
89 print(f"Waiting for Agent run to complete. Please wait...")
90 run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
91 print(f"Run finished with status: {run.status}")
92
93 if run.status == "failed":
94 print(f"Run failed: {run.last_error}")
95
96 # Fetch run steps to get the details of the agent run
97 run_steps = agents_client.run_steps.list(thread_id=thread.id, run_id=run.id)
98 for step in run_steps:
99 print(f"Step {step.id} status: {step.status}")
100
101 if isinstance(step.step_details, RunStepToolCallDetails):
102 print(" Tool calls:")
103 tool_calls = step.step_details.tool_calls
104
105 for call in tool_calls:
106 print(f" Tool call ID: {call.id}")
107 print(f" Tool call type: {call.type}")
108
109 if isinstance(call, RunStepBrowserAutomationToolCall):
110 print(f" Browser automation input: {call.browser_automation.input}")
111 print(f" Browser automation output: {call.browser_automation.output}")
112
113 print(" Steps:")
114 for tool_step in call.browser_automation.steps:
115 print(f" Last step result: {tool_step.last_step_result}")
116 print(f" Current state: {tool_step.current_state}")
117 print(f" Next step: {tool_step.next_step}")
118 print() # add an extra newline between tool steps
119
120 print() # add an extra newline between tool calls
121
122 print() # add an extra newline between run steps
123
124 # Optional: Delete the agent once the run is finished.
125 # Comment out this line if you plan to reuse the agent later.
126 agents_client.delete_agent(agent.id)
127 print("Deleted agent")
128
129 # Print the Agent's response message with optional citation
130 response_message = agents_client.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT)
131 if response_message:
132 for text_message in response_message.text_messages:
133 print(f"Agent response: {text_message.text.value}")
134 for annotation in response_message.url_citation_annotations:
135 print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")