This guide covers deployment for both local development and Azure production environments.
- Python 3.11+
- For local development (default): Google Gemini API key (free from https://makersuite.google.com/app/apikey)
- For local development (optional): Azure OpenAI access (endpoint, API key, deployment names)
# Clone and navigate to project
cd policybot-ai-agent
# Create virtual environment
python -m venv venv
# Activate virtual environment (Windows)
venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt# Copy environment template
copy .env.example .envEdit .env and configure your LLM provider:
Option 1: Google Gemini (Default - Free)
ENVIRONMENT=local
LLM_PROVIDER=gemini
GOOGLE_GEMINI_API_KEY=your_actual_key_hereOption 2: Azure OpenAI (Alternative)
ENVIRONMENT=local
LLM_PROVIDER=azure
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_API_KEY=your_azure_key_here
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4
AZURE_OPENAI_EMBEDDING_DEPLOYMENT=text-embedding-ada-002python scripts\setup_vectorstore.pyThis processes all documents in data/documents/ and creates FAISS embeddings.
# Option A: Direct Python
python -m app.main
# Option B: Uvicorn with reload
uvicorn app.main:app --reload
# Option C: Docker
cd deployment
docker-compose up --build- Interactive: http://localhost:8000/docs
- CLI:
python scripts\test_agent.py - API:
curl http://localhost:8000/health
cd deployment
docker-compose up --buildServices:
- AI Agent API: http://localhost:8000
docker build -t policybot-ai-agent -f deployment/Dockerfile .
docker run -p 8000:8000 --env-file .env policybot-ai-agent- Azure CLI installed and configured
- Azure subscription with:
- Azure OpenAI access
- Azure AI Search service
- GitHub repository (for CI/CD)
- Terraform (for Infrastructure as Code deployment)
Infrastructure as Code - Production-ready, version-controlled infrastructure.
- ✅ Reproducible: Infrastructure defined as code
- ✅ Version Controlled: Track infrastructure changes in Git
- ✅ Idempotent: Safe to run multiple times
- ✅ State Management: Track resource state
- ✅ Production Ready: Industry standard for IaC
# Navigate to Terraform directory
cd deployment/terraform
# Copy and configure variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your specific values
# Initialize Terraform
terraform init
# Review planned changes
terraform plan
# Apply configuration
terraform apply- Resource Group: Container for all resources
- Azure Container Registry: Docker image storage
- App Service Plan + Web App: Application hosting
- Azure OpenAI: GPT-4 and embedding models
- Azure AI Search: Vector search service
- Application Insights: Monitoring and logging
- Log Analytics Workspace: Centralized logging
After Terraform completes:
# 1. Build and push Docker image
ACR_LOGIN_SERVER=$(terraform output -raw acr_login_server)
az acr login --name $(echo $ACR_LOGIN_SERVER | cut -d'.' -f1)
docker build -t $ACR_LOGIN_SERVER/policybot-ai-agent:latest -f deployment/Dockerfile .
docker push $ACR_LOGIN_SERVER/policybot-ai-agent:latest
# 2. Initialize vector store
export ENVIRONMENT=production
export AZURE_OPENAI_ENDPOINT=$(terraform output -raw openai_endpoint)
export AZURE_OPENAI_API_KEY=$(terraform output -raw openai_api_key)
export AZURE_SEARCH_ENDPOINT=$(terraform output -raw search_endpoint)
export AZURE_SEARCH_API_KEY=$(terraform output -raw search_api_key)
python scripts/setup_vectorstore.py
# 3. Verify deployment
APP_URL=$(terraform output -raw app_service_url)
curl $APP_URL/health→ See deployment/terraform/README.md for complete Terraform guide
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
export AZURE_OPENAI_API_KEY="your_key"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4"
export AZURE_OPENAI_EMBEDDING_DEPLOYMENT="text-embedding-ada-002"
export AZURE_SEARCH_ENDPOINT="https://your-search.search.windows.net"
export AZURE_SEARCH_API_KEY="your_key"cd deployment/azure
chmod +x deploy.sh
./deploy.shThe script will:
- Create resource group
- Create Azure Container Registry
- Build and push Docker image
- Create App Service Plan
- Deploy Web App
- Configure Application Insights
- Set environment variables
# Test health endpoint
curl https://your-app.azurewebsites.net/health
# Test query
curl -X POST https://your-app.azurewebsites.net/ask \
-H "Content-Type: application/json" \
-d '{"query":"What is the leave policy?"}'Add these secrets to your GitHub repository (Settings → Secrets and variables → Actions):
AZURE_CREDENTIALS- Service principal JSONAZURE_OPENAI_ENDPOINTAZURE_OPENAI_API_KEYAZURE_OPENAI_DEPLOYMENT_NAMEAZURE_OPENAI_EMBEDDING_DEPLOYMENTAZURE_SEARCH_ENDPOINTAZURE_SEARCH_API_KEY
az ad sp create-for-rbac \
--name "policybot-ai-agent-deploy" \
--role contributor \
--scopes /subscriptions/{subscription-id}/resourceGroups/policybot-ai-agent-rg \
--sdk-authCopy the JSON output to AZURE_CREDENTIALS secret.
# Push to main branch
git add .
git commit -m "Deploy to Azure"
git push origin mainGitHub Actions will automatically:
- Build Docker image
- Push to Azure Container Registry
- Deploy to Azure App Service
- Run health checks
az cognitiveservices account create \
--name your-openai-resource \
--resource-group policybot-ai-agent-rg \
--kind OpenAI \
--sku S0 \
--location eastus# Deploy GPT-4
az cognitiveservices account deployment create \
--name your-openai-resource \
--resource-group policybot-ai-agent-rg \
--deployment-name gpt-4 \
--model-name gpt-4 \
--model-version "1106-Preview" \
--model-format OpenAI \
--sku-capacity 10 \
--sku-name "Standard"
# Deploy Embedding Model
az cognitiveservices account deployment create \
--name your-openai-resource \
--resource-group policybot-ai-agent-rg \
--deployment-name text-embedding-ada-002 \
--model-name text-embedding-ada-002 \
--model-version "2" \
--model-format OpenAI \
--sku-capacity 10 \
--sku-name "Standard"az search service create \
--name your-search-service \
--resource-group policybot-ai-agent-rg \
--sku Standard \
--location eastusAfter deployment, run the setup script to create the search index:
# Update config to use Azure
# Set ENVIRONMENT=production in App Service configuration
# Run from Azure Cloud Shell or locally
python scripts/setup_vectorstore.pyAccess metrics and logs:
- Go to Azure Portal
- Navigate to your App Service
- Select "Application Insights"
- View:
- Request rates
- Response times
- Failures
- Live metrics
# Stream logs
az webapp log tail \
--name policybot-ai-agent-app \
--resource-group policybot-ai-agent-rg
# Download logs
az webapp log download \
--name policybot-ai-agent-app \
--resource-group policybot-ai-agent-rg \
--log-file logs.zip# Upgrade App Service Plan
az appservice plan update \
--name policybot-ai-agent-plan \
--resource-group policybot-ai-agent-rg \
--sku P1V2# Add more instances
az appservice plan update \
--name policybot-ai-agent-plan \
--resource-group policybot-ai-agent-rg \
--number-of-workers 3Issue: "Configuration validation failed"
- Solution: Check all required environment variables are set
Issue: "Vector store empty"
- Solution: Run
python scripts/setup_vectorstore.py
Issue: "Azure OpenAI quota exceeded"
- Solution: Check quota limits in Azure Portal, request increase if needed
Issue: "Container fails to initialize running"
- Solution: Check logs with
az webapp log tail, verify environment variables
# Local
curl http://localhost:8000/health
# Azure
curl https://your-app.azurewebsites.net/health- Never commit
.envfile - Always in.gitignore - Use Azure Key Vault for production secrets
- Enable HTTPS only in App Service
- Restrict CORS to known origins
- Enable Application Insights for monitoring
- Regular security updates - Update dependencies
- Free: Google Gemini API (generous free tier)
- Free: FAISS (local storage)
- AI Azure OpenAI: Pay-per-token
- Azure AI Search: ~$250/month (Standard tier)
- App Service: ~$75/month (B1 Basic tier)
- Application Insights: First 5GB free, then $2.30/GB
Total Estimated Cost: ~$325-400/month for production
- Use Free or Basic tier App Service for development
- Enable autoscaling to scale down during low traffic
- Use reserved capacity pricing for OpenAI if high volume
- Consider Azure AI Search Free tier for development
# FAISS (local)
cp -r data/vector_stores/ backup/
# Azure AI Search
# Indexes are managed by Azure with automatic backup# Documents are in source control
git push origin main- All code in GitHub
- Vector stores can be regenerated from documents
- Azure resources can be redeployed with scripts
- Configuration in environment variables (documented)
Recovery Time Objective (RTO): < 1 hour Recovery Point Objective (RPO): Latest git commit
For additional help, see: