Azure コンテンツ理解

ContentUnderstandingContextProviderは、Azure Content Understanding を使用して添付ファイルを分析し、構造化された結果をエージェント コンテキストに挿入します。 ドキュメント、画像、音声、動画をサポートしており、OCR、表、構造化フィールド、文字起こし、話者分離、セグメント要約に対応しています。

この統合では、前処理パターンが使用されます。これは、モデル呼び出しの前に受信コンテンツを変換し、後で処理された状態を保持できます。

大規模なドキュメントの場合、プロバイダーは、結果全体をモデル コンテキストに配置するのではなく、抽出されたマークダウンをファイル検索ベクター ストアにアップロードできます。

前提条件

  • Azure サブスクリプション。
  • サポートされているリージョンでの Azure Content Understanding。
  • サービスに必要なモデルのデプロイ。
  • リソースへの Azure ID アクセス

パッケージをインストールする

pip install agent-framework-azure-contentunderstanding --pre

ドキュメントを分析する

ContentUnderstandingContextProviderをエージェントにアタッチし、サポートされているバイナリ添付ファイルを送信します。 プロバイダーは、分析後にバイナリ入力を削除し、抽出されたコンテンツをモデルに提供します。

async def main() -> None:
    credential = AzureCliCredential()

    # Set up Azure Content Understanding context provider
    cu = ContentUnderstandingContextProvider(
        endpoint=os.environ["AZURE_CONTENTUNDERSTANDING_ENDPOINT"],
        credential=credential,
        analyzer_id="prebuilt-documentSearch",  # RAG-optimized document analyzer
        max_wait=None,  # wait until CU analysis finishes (no background deferral)
    )

    # Set up the LLM client
    client = FoundryChatClient(
        project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
        model=os.environ["FOUNDRY_MODEL"],
        credential=credential,
    )

    # Create agent with CU context provider.
    # The provider extracts document content via CU and injects it into the
    # LLM context so the agent can answer questions about the document.
    async with credential, cu:
        agent = Agent(
            client=client,
            name="DocumentQA",
            instructions=(
                "You are a helpful document analyst. Use the analyzed document "
                "content and extracted fields to answer questions precisely."
            ),
            context_providers=[cu],
        )

        # --- Turn 1: Upload PDF and ask a question ---
        # 4. Upload PDF and ask questions
        # The CU provider extracts markdown + fields from the PDF and injects
        # the full content into context so the agent can answer precisely.
        print("--- Upload PDF and ask questions ---")

        pdf_bytes = SAMPLE_PDF_PATH.read_bytes()

        response = await agent.run(
            Message(
                role="user",
                contents=[
                    Content.from_text(
                        "What is this document about? Who is the vendor, and what is the total amount due?"
                    ),
                    Content.from_data(
                        pdf_bytes,
                        "application/pdf",
                        # Always provide filename — used as the document key
                        additional_properties={"filename": SAMPLE_PDF_PATH.name},
                    ),
                ],
            )
        )
        usage = response.usage_details or {}
        print(f"Agent: {response}")
        print(f"  [Input tokens: {usage.get('input_token_count', 'N/A')}]\n")

処理オプション

  • analyzer_id設定を解除して、メディアの種類からドキュメント、オーディオ、またはビデオ検索アナライザーを選択します。
  • 実行が分析の完了を待機する必要がある場合に max_wait=None を設定します。
  • FileSearchConfigを使用して、大きな抽出されたドキュメントに対してトークン効率の高い取得を行います。
  • AgentSessionを再利用して、分析されたドキュメントの状態を順番に保持します。

次のステップ

より深く進む: