コンテンツ解釈アナライザーを作成する

完了

ほとんどのシナリオでは、Microsoft Foundry ポータルのビジュアル インターフェイスを使用してアナライザーの作成とテストを検討する必要があります。 ただし、場合によっては、目的のコンテンツ フィールドのスキーマの JSON 定義を REST API に送信してアナライザーを作成することが必要な場合があります。

アナライザーのスキーマの定義

アナライザーは、コンテンツ ファイルから抽出または生成するフィールドを定義するスキーマに基づいています。 最も単純なスキーマは、次のアナライザー定義の例に示すように、JSON ドキュメントで指定できるフィールドのセットです。

{
    "description": "Simple business card",
    "baseAnalyzerId": "prebuilt-documentAnalyzer",
    "config": {
        "returnDetails": true
    },
    "fieldSchema": {
        "fields": {
            "ContactName": {
                "type": "string",
                "method": "extract",
                "description": "Name on business card"
            },
            "EmailAddress": {
                "type": "string",
                "method": "extract",
                "description": "Email address on business card"
            }
        }
    }
}

カスタム アナライザー スキーマのこの例は、事前構築済みの ドキュメント アナライザーに基づいており、名刺で見つかる 2 つのフィールド (ContactNameEmailAddress) について説明します。 どちらのフィールドも文字列データ型として定義されており、ドキュメントから 抽出 されることが想定されています (つまり、文字列値はドキュメント内に存在することが予想されるため、ドキュメントに関する情報を推測して 生成 できるフィールドではなく、"読み取り" できます)。

この例は、作業アナライザーを作成するために必要な最小限の情報を使用して、意図的に単純です。 実際には、スキーマにはさまざまな型のフィールドが含まれる可能性が高く、アナライザー定義にはより多くの構成設定が含まれます。 JSON にはサンプル ドキュメントが含まれている場合もあります。 詳細については、 Azure Content Understanding REST API のドキュメント を参照してください。

REST API を使用してアナライザーを作成する

アナライザー定義を設定したら、REST API を使用して Azure Content Understanding に送信して作成できます。 JSON データは、アナライザー作成操作を開始するために、要求ヘッダーの API キーを使用してエンドポイントに PUT 要求として送信されます。

PUT要求からの応答には、ヘッダーに Operation-Location が含まれています。このヘッダーには、要求を送信して要求の状態を確認するために使用できるGET URL が用意されています。

HTTP 対応のクライアント ツールまたは言語を使用して、要求を送信できます。 たとえば、次の Python コードは、 card.json という名前のファイルの内容に基づいてアナライザーを作成する要求を送信します (前述の JSON 定義が含まれていると想定されます)。

import json
import requests

# Get the buisness card schema
with open("card.json", "r") as file:
    schema_json = json.load(file)

# Use a PUT request to submit the schema for a new analyzer
analyzer_name = "business_card_analyser"

headers = {
    "Ocp-Apim-Subscription-Key": "<YOUR_API_KEY>",
    "Content-Type": "application/json"}

url = f"{<YOUR_ENDPOINT>}/contentunderstanding/analyzers/{analyzer_name}?api-version=2025-05-01-preview"

response = requests.put(url, headers=headers, data=json.dumps(schema_json))

# Get the response and extract the ID assigned to the operation
callback_url = response.headers["Operation-Location"]

# Use a GET request to check the status of the operation
result_response = requests.get(callback_url, headers=headers)

# Keep polling until the operation is complete
status = result_response.json().get("status")
while status == "Running":
    result_response = requests.get(callback_url, headers=headers)
    status = result_response.json().get("status")

print("Done!")