azure-ai-foundry-agent

Azure AI Foundry Document Analyzer Agent

An AI agent that uses Azure Content Understanding to scan and analyze documents, extracting text, tables, and structured data.

Live demo: Open Azure AI Foundry Document Analyzer

Features

Prerequisites

  1. Azure Subscription: You need an active Azure subscription
  2. Azure AI Foundry Resource: Create a Foundry resource in a supported region
  3. Model Deployment: Deploy a model (e.g., gpt-4o) in your Foundry project
  4. Python 3.9+: Required for running the agent

Setup

1. Install Dependencies

pip install -r requirements.txt

2. Configure Credentials

Edit config.py and update the following values:

# Azure AI Foundry endpoint - found in your project overview
AZURE_AI_FOUNDRY_ENDPOINT = "https://YOUR_RESOURCE.services.ai.azure.com/api/projects/YOUR_PROJECT"

# Content Understanding endpoint
AZURE_CONTENT_UNDERSTANDING_ENDPOINT = "https://YOUR_RESOURCE.cognitiveservices.azure.com"

# Your API key - found under "Keys and Endpoint" in Azure portal
AZURE_API_KEY = "YOUR_API_KEY_HERE"

# Your deployed model name
MODEL_DEPLOYMENT_NAME = "gpt-4o"

Getting Your Azure Credentials

  1. Azure AI Foundry Endpoint:
    • Go to Azure AI Foundry Portal
    • Select your project
    • Find the endpoint in the project overview under “Project details”
  2. Content Understanding Endpoint:
    • In Azure Portal, go to your Cognitive Services / AI Services resource
    • Find the endpoint under “Keys and Endpoint”
  3. API Key:
    • In the same “Keys and Endpoint” section
    • Copy either KEY1 or KEY2
  4. Model Deployment Name:
    • In Azure AI Foundry, go to Deployments
    • Note the name of your deployed model (e.g., “gpt-4o”)

Usage

Interactive CLI

Run the main script for an interactive experience:

python main.py

Available commands:

Programmatic Usage

from agent import create_document_analyzer_agent

# Create the agent
agent = create_document_analyzer_agent()

# Upload a document
agent.upload_file("/path/to/document.pdf")

# Chat with the agent
response = agent.chat("Please analyze the uploaded document and summarize its contents")
print(response)

# Ask follow-up questions
response = agent.chat("What tables are in the document?")
print(response)

# Clean up when done
agent.cleanup()

Direct Content Understanding Usage

from content_understanding import ContentUnderstandingClient

client = ContentUnderstandingClient()

# Extract text from a document
text = client.extract_text("/path/to/document.pdf")
print(text)

# Extract tables
tables = client.extract_tables("/path/to/document.pdf")
for table in tables:
    print(f"Table: {table['rowCount']} rows x {table['columnCount']} columns")

# Analyze with custom field schema
fields = {
    "invoice_number": {"type": "string", "method": "extract"},
    "total_amount": {"type": "number", "method": "extract"},
    "vendor_name": {"type": "string", "method": "extract"}
}
result = client.analyze_with_custom_schema(
    file_path="/path/to/invoice.pdf",
    fields=fields
)

Supported File Types

Project Structure

azure-ai-foundry-agent/
├── config.py              # Configuration and credentials
├── content_understanding.py  # Content Understanding API client
├── agent.py               # AI Foundry Agent implementation
├── main.py                # Interactive CLI application
├── requirements.txt       # Python dependencies
└── README.md              # This file

Troubleshooting

“Configuration Required” Error

Make sure you’ve updated all placeholder values in config.py with your actual Azure credentials.

Authentication Errors

Content Understanding Errors

Resources