@@ -28,17 +28,17 @@ Use this article to find step-by-step instructions and code samples for using th
28
28
* The following packages:
29
29
30
30
``` 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
33
33
pip install azure-identity
34
34
```
35
35
* 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.
37
37
38
38
<!--
39
39
:::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":::
40
40
-->
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 `.
42
42
43
43
* [!INCLUDE [model-name-portal](../../includes/model-name-portal.md)]
44
44
@@ -48,87 +48,105 @@ Use this article to find step-by-step instructions and code samples for using th
48
48
49
49
```python
50
50
import os
51
- from azure.identity import DefaultAzureCredential
52
- from azure.ai.agents import AgentsClient
53
- from azure.ai.agents.models import MessageRole
54
51
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
55
59
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())
57
61
58
- project_client = AIProjectClient(
59
- endpoint=project_endpoint,
60
- credential=DefaultAzureCredential()
61
- )
62
+ connection_id = os.environ["AZURE_PLAYWRIGHT_CONNECTION_ID"]
62
63
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)
67
66
68
67
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,
81
83
)
82
84
85
+
83
86
print(f"Created agent, ID: {agent.id}")
84
87
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}")
87
91
88
92
# 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)
101
110
print(f"Run finished with status: {run.status}")
102
111
103
112
if run.status == "failed":
104
113
print(f"Run failed: {run.last_error}")
105
114
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)
107
117
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}")
110
119
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):
116
121
print(" Tool calls:")
122
+ tool_calls = step.step_details.tool_calls
123
+
117
124
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
120
140
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
125
142
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)
128
146
print("Deleted agent")
129
147
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)
132
150
if response_message:
133
151
for text_message in response_message.text_messages:
134
152
print(f"Agent response: {text_message.text.value}")
0 commit comments