Azure Inhaltsverständnis

ContentUnderstandingContextProvider analysiert Dateianhänge mit Azure Content Understanding und fügt strukturierte Ergebnisse in den Kontext des Agents ein. Es unterstützt Dokumente, Bilder, Audio und Video, einschließlich OCR, Tabellen, strukturierte Felder, Transkription, Diarisierung und Segmentzusammenfassungen.

Diese Integration verwendet das Vorverarbeitungsmuster: Sie transformiert eingehende Inhalte vor dem Modellaufruf und kann den verarbeiteten Zustand für spätere Wendungen beibehalten.

Bei großen Dokumenten kann der Anbieter extrahiertes Markdown in einen Dateisuchvektorspeicher hochladen, anstatt das gesamte Ergebnis im Modellkontext zu platzieren.

Voraussetzungen

  • Ein Azure-Abonnement.
  • Azure Content Understanding in einer unterstützten Region.
  • Die für den Dienst erforderlichen Modellbereitstellungen.
  • Azure Identitätszugriff auf die Ressource.

Installiere das Paket

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

Analysieren eines Dokuments

ContentUnderstandingContextProvider an den Agent anhängen und einen unterstützten binären Anhang senden. Der Anbieter entfernt die binäre Eingabe nach der Analyse und stellt den extrahierten Inhalt für das Modell bereit.

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")

Verarbeitungsoptionen

  • Lassen Sie analyzer_id nicht festgelegt, um anhand des Medientyps einen Dokument-, Audio- oder Video-Suchanalysator auszuwählen.
  • Legen Sie max_wait=None fest, wann die Ausführung auf den Abschluss der Analyse warten muss.
  • Verwenden Sie FileSearchConfig für den tokeneffizienten Abruf in großen extrahierten Dokumenten.
  • Verwenden Sie ein AgentSession erneut, um den Status des analysierten Dokuments über mehrere Interaktionen hinweg beizubehalten.

Nächste Schritte

Gehen Sie tiefer: