1# pylint: disable=line-too-long,useless-suppression2# ------------------------------------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 from10 the Azure Agents service using a synchronous client.11 12USAGE:13 python sample_agents_browser_automation.py14 15 Before running the sample:16 17 pip install azure-ai-agents --pre18 pip install azure-ai-projects azure-identity19 20 Set these environment variables with your own values:21 1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview22 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 in24 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 is26 listed in Azure AI Foundry connected resources.27"""28 29import os30from azure.ai.projects import AIProjectClient31from azure.ai.agents.models import (32 MessageRole,33 RunStepToolCallDetails,34 BrowserAutomationTool,35 RunStepBrowserAutomationToolCall,36)37from azure.identity import DefaultAzureCredential38 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"]).id43 44# Initialize Browser Automation tool and add the connection id45browser_automation = BrowserAutomationTool(connection_id=connection_id)46 47with project_client:48 49 agents_client = project_client.agents50 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 communication70 thread = agents_client.threads.create()71 print(f"Created thread, ID: {thread.id}")72 73 # Create message to thread74 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 tools89 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 run97 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_calls104 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 steps119 120 print() # add an extra newline between tool calls121 122 print() # add an extra newline between run steps123 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 citation130 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})")
Developer SDKs, repos, connectors, and infrastructure helpers. APIs, development tools, and integration resources.