😽

【Azure AI Foundry Agent Service】- Computer useをToolとして使えるように

に公開

執筆日

2025/8/22

やること

Azure AI FoundryのAI Agent ServiceのToolでCompuer useが使えるようになりました。
使い方までの手順を書いていきます。

手順

  1. Playwright ワークスペース を作成
  2. Azure AI FoundryとPlaywright ワークスペースを接続
  3. コードを実行

1. Playwright ワークスペース を作成

  1. 検索欄でAzure App testingと検索し、Azure App testingをクリック
  2. 左タブのワークスペース>Playwrightワークスペースをクリック
  3. +作成をクリック
  4. ワークスペース名、リージョンを選択し構築
  5. 設定>アクセス管理をクリック
  6. Playwrightサービス..トークンをクリック
  7. トークンの生成をクリック
  8. 生成されたトークンをコピー
  9. 概要に戻る
  10. ブラウザーエンドポイントをコピー

2. Azure AI FoundryとPlaywright ワークスペースを接続

  1. AI Foundry portalを開く
  2. 管理センターをクリック
  3. プロジェクト>Connected resourcesをクリック
  4. +新しい接続をクリック
  5. サーバーレスモデルをクリック
  6. ターゲット URI: 1の8でコピーしたブラウザーエンドポイント
  7. キー : 1の8でコピーしたトークン
  8. 接続 : 任意
  9. 6~9を入力後、接続を追加するをクリック

3. コードの実行

main.py
import os

from azure.identity import DefaultAzureCredential
from azure.ai.agents import AgentsClient
from azure.ai.agents.models import MessageRole
from azure.ai.projects import AIProjectClient

project_endpoint = "プロジェクト名"

project_client = AIProjectClient(
    endpoint=project_endpoint,
    credential=DefaultAzureCredential()
)

playwright_connection = project_client.connections.get(
    name="2.8で入力した接続名"
)

print(playwright_connection.id)

with project_client:
    agent = project_client.agents.create_agent(
        model="モデルを設定",
        name="test-agent",
        instructions="use the tool to respond",
        tools=[{
            "type": "browser_automation",
            "browser_automation": {
                "connection": {
                    "id": playwright_connection.id,
                }
            }
        }],
    )

    print(f"Created agent, ID: {agent.id}")

    thread = project_client.agents.threads.create()
    print(f"Created thread and run, ID: {thread.id}")
    message = project_client.agents.messages.create(
        thread_id=thread.id,
        role="user",
        content=""""
        https://zenn.dev/p/headwatersにアクセスしてください。
        投稿された記事の中から、最新の記事top5の題名を教えて下さい。
        """
    )
    print(f"Created message: {message['id']}")
    run = project_client.agents.runs.create_and_process(
        thread_id=thread.id,
        agent_id=agent.id,
    )

    print(f"Run created, ID: {run.id}")
    print(f"Run finished with status: {run.status}")

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

    run_steps = project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id)

    for step in run_steps:
        print(step)
        print(f"Step {step['id']} status: {step['status']}")

        step_details = step.get("step_details", {})
        tool_calls = step_details.get("tool_calls", [])

        if tool_calls:
            print("  Tool calls:")
            for call in tool_calls:
                print(f"    Tool Call ID: {call.get('id')}")
                print(f"    Type: {call.get('type')}")

                function_details = call.get("function", {})
                if function_details:
                    print(f"    Function name: {function_details.get('name')}")

        print()  
    response_message = project_client.agents.messages.get_last_message_by_role(
        thread_id=thread.id,
        role=MessageRole.AGENT
    )

    if response_message:
        for text_message in response_message.text_messages:
            print(f"Agent response: {text_message.text.value}")
        for annotation in response_message.url_citation_annotations:
            print(f"URL Citation: [{annotation.url_citation.title}]({annotation.url_citation.url})")


出力結果

Agent response: https://zenn.dev/p/headwaters にアクセスし、最新の記事top5の題名を調べました。結果は以下の通りです。

  1. 【Playwright】ブラウザの自動操作をしてみようとしたが...
  2. Certiport経由で取得した資格はMicrosoft Learnに紐づけられないらしい
  3. 【VSCode/Markdown PDF】- Markdown PDFのエラー発生時に確認する点
  4. 【Bicep】デコレーターとコメントの違い
  5. 【学習ログ】AIに書いてもらったBicepコードを模写する

他にも知りたいことがあれば教えてください!

ちょっと意地悪な質問

main.py
content=""""https://zenn.dev/p/headwatersにアクセスしてください。投稿された記事の総数を教えてください。""")
出力結果

Agent response: HeadwatersがZenn(https://zenn.dev/p/headwaters) に投稿している記事の総数は「83本」です。

main.py
        content=""""
        https://zenn.dev/にアクセスしてください。
        「Log in」をクリックしてください。
        「Googleアカウントをお持ちでない方」をクリックしてください。
        「メールアドレスを入力してください」の欄に、hogehoge@hoge.co.jp(実際には私のメールアドレスです。マスキングしています)
        を入力し、「確認コードを送信」をクリックしてください。
        """
    )
    
出力結果

Agent response: 指定された手順(ログイン画面の遷移とメールアドレスの入力)はすべて実行しましたが、最後の「確認コード を送信」時にreCAPTCHA認証が表示され、自動操作では処理できませんでした。

reCAPTCHAによる制限のため、最終ステップ(確認コード送信)は手動で行う必要があります。

  • 画面で表示されたreCAPTCHAの「私はロボットではありません」にチェックし、必要であれば画像選択を手動で完了してください。
  • その後「確認コードを送信」をクリックすると、コードが送られます。

他に代替手順やサポートが必要な場合はお知らせください。

ヘッドウォータース

Discussion