AI agent for Azure SQL Database using agent-framework with Azure OpenAI. Includes CLI and Jupyter notebook.
AI agent capabilities:
- List tables in an Azure SQL Database
- Describe table schemas (columns, data types, constraints)
- Execute SQL queries based on natural language requests
- Python 3.12+
- Azure OpenAI account with deployed model
- Azure SQL Database
- ODBC Driver for SQL Server
Using uv (Recommended):
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
uv syncUsing pip:
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txtCreate a .env file:
# Azure OpenAI
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.us/
AZURE_OPENAI_MODEL=gpt-4o
AZURE_OPENAI_API_KEY=your-api-key
AZURE_OPENAI_API_VERSION=2025-01-01-preview
# Azure SQL Database (local development with interactive auth)
AZURE_SQL_CONNECTIONSTRING=Driver={ODBC Driver 18 for SQL Server};Server=tcp:your-server.database.usgovcloudapi.net,1433;Database=your-database;Encrypt=yes;Uid=your-user@your-tenant.onmicrosoft.us;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryInteractive
# Azure SQL Database (App Service with Managed Identity)
# AZURE_SQL_CONNECTIONSTRING=Driver={ODBC Driver 18 for SQL Server};Server=tcp:your-server.database.usgovcloudapi.net,1433;Database=your-database;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryMsi;UID=your-managed-identity-client-id
# Azure AI Search
SEARCH_SERVICE_ENDPOINT=https://your-search-service.search.azure.us
SEARCH_INDEX_NAME=your-index-name
SEARCH_API_KEY=your-search-api-key
# Azure Government OAuth (optional)
OAUTH_AZURE_GOV_AD_CLIENT_ID=your-client-id
OAUTH_AZURE_GOV_AD_CLIENT_SECRET=your-client-secret
OAUTH_AZURE_GOV_AD_TENANT_ID=your-tenant-id
# Chainlit
CHAINLIT_AUTH_SECRET=your-auth-secretCLI:
uv run main.pyJupyter:
uv run jupyter notebookOr open notebook.ipynb in VS Code.
Deploy using Azure Developer CLI:
azd upConfigure environment variables:
azd env set AZURE_OPENAI_ENDPOINT "https://your-resource.openai.azure.com/"
azd env set AZURE_OPENAI_MODEL "your-deployment-name"
azd env set AZURE_OPENAI_API_KEY "your-api-key"
azd env set AZURE_SQL_CONNECTIONSTRING "your-connection-string"
azd deployGrant managed identity access to SQL Database:
CREATE USER [<app-service-name>] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [<app-service-name>];See sql_queries/ for helper scripts.
azd provision # Infrastructure only
azd deploy # Code only
azd down # Clean upuv sync --group dev
uv run pre-commit install
uv run pre-commit run --all-files- Import errors: Run
uv syncorpip install -r requirements.txt - Database errors: Check connection string, ODBC driver, and firewall rules
- Azure OpenAI errors: Verify endpoint, model name, and API key
This application supports Azure Government OAuth authentication via custom providers in custom_oauth.py.
- Register an app in Azure Active Directory → App registrations in the Azure Government Portal
- Set redirect URI to
https://your-app.azurewebsites.us/auth/oauth/azure-gov/callback - Create a client secret and note the client ID, tenant ID, and secret value
OAUTH_AZURE_GOV_AD_CLIENT_ID=your-client-id
OAUTH_AZURE_GOV_AD_CLIENT_SECRET=your-client-secret
OAUTH_AZURE_GOV_AD_TENANT_ID=your-tenant-idMIT License - see LICENSE file for details.