Skip to content

Commit 020910e

Browse files
committed
updating browser automation code
1 parent 4cf19f1 commit 020910e

File tree

1 file changed

+76
-59
lines changed

1 file changed

+76
-59
lines changed

articles/ai-foundry/agents/how-to/tools/browser-automation-samples.md

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ Use this article to find step-by-step instructions and code samples for using th
2828
* The following packages:
2929

3030
```console
31-
pip install --pre azure-ai-projects
32-
pip install azure-ai-agents==1.2.0b1
33-
pip install azure-identity
31+
pip install azure-ai-agents --pre
32+
pip install azure-ai-projects azure-identity
3433
```
3534
* The **contributor** role assigned to your AI Foundry project from within your Playwright workplace.
3635
* Your playwright connection ID. You can find it in the Azure AI Foundry portal by selecting **Management center** from the left navigation menu. Then select **Connected resources**. The URI should start with `wss://` instead of `https://` if presented.
@@ -48,87 +47,105 @@ Use this article to find step-by-step instructions and code samples for using th
4847

4948
```python
5049
import os
51-
from azure.identity import DefaultAzureCredential
52-
from azure.ai.agents import AgentsClient
53-
from azure.ai.agents.models import MessageRole
5450
from azure.ai.projects import AIProjectClient
51+
from azure.ai.agents.models import (
52+
MessageRole,
53+
RunStepToolCallDetails,
54+
BrowserAutomationTool,
55+
RunStepBrowserAutomationToolCall,
56+
)
57+
from azure.identity import DefaultAzureCredential
5558

56-
project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set
59+
project_client = AIProjectClient(endpoint=os.environ["PROJECT_ENDPOINT"], credential=DefaultAzureCredential())
5760

58-
project_client = AIProjectClient(
59-
endpoint=project_endpoint,
60-
credential=DefaultAzureCredential()
61-
)
61+
connection_id = os.environ["AZURE_PLAYWRIGHT_CONNECTION_ID"]
6262

63-
playwright_connection = project_client.connections.get(
64-
name=os.environ["PLAYWRIGHT_CONNECTION_NAME"]
65-
)
66-
print(playwright_connection.id)
63+
# Initialize Browser Automation tool and add the connection id
64+
browser_automation = BrowserAutomationTool(connection_id=connection_id)
6765

6866
with project_client:
69-
agent = project_client.agents.create_agent(
70-
model=os.environ["MODEL_DEPLOYMENT_NAME"],
71-
name="my-agent",
72-
instructions="use the tool to respond",
73-
tools=[{
74-
"type": "browser_automation",
75-
"browser_automation": {
76-
"connection": {
77-
"id": playwright_connection.id,
78-
}
79-
}
80-
}],
67+
68+
agents_client = project_client.agents
69+
70+
# Create a new Agent that has the Browser Automation tool attached.
71+
# Note: To add Browser Automation tool to an existing Agent with an `agent_id`, do the following:
72+
# agent = agents_client.update_agent(agent_id, tools=browser_automation.definitions)
73+
agent = agents_client.create_agent(
74+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
75+
name="my-agent",
76+
instructions="""
77+
You are an Agent helping with browser automation tasks.
78+
You can answer questions, provide information, and assist with various tasks
79+
related to web browsing using the Browser Automation tool available to you.
80+
""",
81+
tools=browser_automation.definitions,
8182
)
8283

84+
8385
print(f"Created agent, ID: {agent.id}")
8486

85-
thread = project_client.agents.threads.create()
86-
print(f"Created thread and run, ID: {thread.id}")
87+
# Create thread for communication
88+
thread = agents_client.threads.create()
89+
print(f"Created thread, ID: {thread.id}")
8790

8891
# Create message to thread
89-
message = project_client.agents.messages.create(
90-
thread_id=thread.id,
91-
role="user",
92-
content="something you want the tool to perform")
93-
print(f"Created message: {message['id']}")
94-
95-
# Create and process an Agent run in thread with tools
96-
run = project_client.agents.runs.create_and_process(
97-
thread_id=thread.id,
98-
agent_id=agent.id,
99-
)
100-
print(f"Run created, ID: {run.id}")
92+
message = agents_client.messages.create(
93+
thread_id=thread.id,
94+
role=MessageRole.USER,
95+
content="""
96+
Your goal is to report the percent of Microsoft year-to-date stock price change.
97+
To do that, go to the website finance.yahoo.com.
98+
At the top of the page, you will find a search bar.
99+
Enter the value 'MSFT', to get information about the Microsoft stock price.
100+
At the top of the resulting page you will see a default chart of Microsoft stock price.
101+
Click on 'YTD' at the top of that chart, and report the percent value that shows up just below it.
102+
""",
103+
)
104+
print(f"Created message, ID: {message.id}")
105+
106+
# Create and process agent run in thread with tools
107+
print(f"Waiting for Agent run to complete. Please wait...")
108+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
101109
print(f"Run finished with status: {run.status}")
102110

103111
if run.status == "failed":
104112
print(f"Run failed: {run.last_error}")
105113

106-
run_steps = project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id)
114+
# Fetch run steps to get the details of the agent run
115+
run_steps = agents_client.run_steps.list(thread_id=thread.id, run_id=run.id)
107116
for step in run_steps:
108-
print(step)
109-
print(f"Step {step['id']} status: {step['status']}")
117+
print(f"Step {step.id} status: {step.status}")
110118

111-
# Check if there are tool calls in the step details
112-
step_details = step.get("step_details", {})
113-
tool_calls = step_details.get("tool_calls", [])
114-
115-
if tool_calls:
119+
if isinstance(step.step_details, RunStepToolCallDetails):
116120
print(" Tool calls:")
121+
tool_calls = step.step_details.tool_calls
122+
117123
for call in tool_calls:
118-
print(f" Tool Call ID: {call.get('id')}")
119-
print(f" Type: {call.get('type')}")
124+
print(f" Tool call ID: {call.id}")
125+
print(f" Tool call type: {call.type}")
126+
127+
if isinstance(call, RunStepBrowserAutomationToolCall):
128+
print(f" Browser automation input: {call.browser_automation.input}")
129+
print(f" Browser automation output: {call.browser_automation.output}")
130+
131+
print(" Steps:")
132+
for tool_step in call.browser_automation.steps:
133+
print(f" Last step result: {tool_step.last_step_result}")
134+
print(f" Current state: {tool_step.current_state}")
135+
print(f" Next step: {tool_step.next_step}")
136+
print() # add an extra newline between tool steps
137+
138+
print() # add an extra newline between tool calls
120139

121-
function_details = call.get("function", {})
122-
if function_details:
123-
print(f" Function name: {function_details.get('name')}")
124-
print() # add an extra newline between steps
140+
print() # add an extra newline between run steps
125141

126-
# Delete the Agent when done
127-
project_client.agents.delete_agent(agent.id)
142+
# Optional: Delete the agent once the run is finished.
143+
# Comment out this line if you plan to reuse the agent later.
144+
agents_client.delete_agent(agent.id)
128145
print("Deleted agent")
129146

130-
# Fetch and log all messages
131-
response_message = project_client.agents.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT)
147+
# Print the Agent's response message with optional citation
148+
response_message = agents_client.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT)
132149
if response_message:
133150
for text_message in response_message.text_messages:
134151
print(f"Agent response: {text_message.text.value}")

0 commit comments

Comments
 (0)