Skip to content

Commit 239708f

Browse files
authored
Merge pull request #6543 from aahill/agents-java-2
updating browser automation code
2 parents 2405de1 + af0b535 commit 239708f

File tree

2 files changed

+80
-62
lines changed

2 files changed

+80
-62
lines changed

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

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ 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
31+
pip install --pre azure-ai-agents>=1.2.0b2
32+
pip install azure-ai-projects
3333
pip install azure-identity
3434
```
3535
* The **contributor** role assigned to your AI Foundry project from within your Playwright workplace.
36-
* 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.
36+
* 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.
3737
3838
<!--
3939
:::image type="content" source="../../media/tools/deep-research/bing-resource-name.png" alt-text="A screenshot showing the Playwright connection. " lightbox="../../media/tools/deep-research/bing-resource-name.png":::
4040
-->
41-
Save this name to an environment variable named `PLAYWRIGHT_CONNECTION_NAME`.
41+
Save this name to an environment variable named `AZURE_PLAYWRIGHT_CONNECTION_NAME`.
4242

4343
* [!INCLUDE [model-name-portal](../../includes/model-name-portal.md)]
4444

@@ -48,87 +48,105 @@ Use this article to find step-by-step instructions and code samples for using th
4848

4949
```python
5050
import os
51-
from azure.identity import DefaultAzureCredential
52-
from azure.ai.agents import AgentsClient
53-
from azure.ai.agents.models import MessageRole
5451
from azure.ai.projects import AIProjectClient
52+
from azure.ai.agents.models import (
53+
MessageRole,
54+
RunStepToolCallDetails,
55+
BrowserAutomationTool,
56+
RunStepBrowserAutomationToolCall,
57+
)
58+
from azure.identity import DefaultAzureCredential
5559

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

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

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

6867
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-
}],
68+
69+
agents_client = project_client.agents
70+
71+
# Create a new Agent that has the Browser Automation tool attached.
72+
# Note: To add Browser Automation tool to an existing Agent with an `agent_id`, do the following:
73+
# agent = agents_client.update_agent(agent_id, tools=browser_automation.definitions)
74+
agent = agents_client.create_agent(
75+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
76+
name="my-agent",
77+
instructions="""
78+
You are an Agent helping with browser automation tasks.
79+
You can answer questions, provide information, and assist with various tasks
80+
related to web browsing using the Browser Automation tool available to you.
81+
""",
82+
tools=browser_automation.definitions,
8183
)
8284

85+
8386
print(f"Created agent, ID: {agent.id}")
8487

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

8892
# 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}")
93+
message = agents_client.messages.create(
94+
thread_id=thread.id,
95+
role=MessageRole.USER,
96+
content="""
97+
Your goal is to report the percent of Microsoft year-to-date stock price change.
98+
To do that, go to the website finance.yahoo.com.
99+
At the top of the page, you will find a search bar.
100+
Enter the value 'MSFT', to get information about the Microsoft stock price.
101+
At the top of the resulting page you will see a default chart of Microsoft stock price.
102+
Click on 'YTD' at the top of that chart, and report the percent value that shows up just below it.
103+
""",
104+
)
105+
print(f"Created message, ID: {message.id}")
106+
107+
# Create and process agent run in thread with tools
108+
print(f"Waiting for Agent run to complete. Please wait...")
109+
run = agents_client.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
101110
print(f"Run finished with status: {run.status}")
102111

103112
if run.status == "failed":
104113
print(f"Run failed: {run.last_error}")
105114

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

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:
120+
if isinstance(step.step_details, RunStepToolCallDetails):
116121
print(" Tool calls:")
122+
tool_calls = step.step_details.tool_calls
123+
117124
for call in tool_calls:
118-
print(f" Tool Call ID: {call.get('id')}")
119-
print(f" Type: {call.get('type')}")
125+
print(f" Tool call ID: {call.id}")
126+
print(f" Tool call type: {call.type}")
127+
128+
if isinstance(call, RunStepBrowserAutomationToolCall):
129+
print(f" Browser automation input: {call.browser_automation.input}")
130+
print(f" Browser automation output: {call.browser_automation.output}")
131+
132+
print(" Steps:")
133+
for tool_step in call.browser_automation.steps:
134+
print(f" Last step result: {tool_step.last_step_result}")
135+
print(f" Current state: {tool_step.current_state}")
136+
print(f" Next step: {tool_step.next_step}")
137+
print() # add an extra newline between tool steps
138+
139+
print() # add an extra newline between tool calls
120140

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
141+
print() # add an extra newline between run steps
125142

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

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)
148+
# Print the Agent's response message with optional citation
149+
response_message = agents_client.messages.get_last_message_by_role(thread_id=thread.id, role=MessageRole.AGENT)
132150
if response_message:
133151
for text_message in response_message.text_messages:
134152
print(f"Agent response: {text_message.text.value}")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ An example flow would be:
6363

6464
For more information on getting this value, see the [PlayWright documentation](https://aka.ms/pww/docs/configure-service-endpoint)
6565

66-
1. * **Key**: [Get the Playwright access token](https://aka.ms/pww/docs/generate-access-token)
66+
* **Key**: [Get the Playwright access token](https://aka.ms/pww/docs/generate-access-token)
6767

6868
For more information on creating a connection, see [Create a connection](../../../how-to/connections-add.md?pivots=fdp-project).
6969

70-
1. Create a Browser Automation tool with your connection ID.
70+
1. Configure your client by adding a Browser Automation tool using the Azure Playwright connection ID.
7171

7272
## Transparency note
7373

0 commit comments

Comments
 (0)