Project structure
The Azure Functions backend follows this structure:OpenAI client configuration
The OpenAI client is configured with organization and API key:Environment variables are managed through Azure Function App Settings for secure credential storage.
Azure Functions
Generate image function
The core function that generates images using DALL-E 3:1
Receive prompt
Extract the text prompt from the POST request body.
2
Call DALL-E 3 API
Use OpenAI’s
createImage method with DALL-E 3 model to generate a 1024x1024 image.3
Download image
Fetch the generated image from OpenAI’s temporary URL using axios with
arraybuffer response type.4
Generate SAS token
Create a time-limited Shared Access Signature token for secure Blob Storage access.
5
Upload to Blob Storage
Store the image in Azure Blob Storage with a filename format:
{prompt}_{timestamp}.png.The function uses
authLevel: "anonymous" which means no authentication is required. In production, consider using function keys or Azure AD authentication.Get images function
Retrieves all generated images from Blob Storage:Image sorting logic
Image sorting logic
Images are sorted by extracting the timestamp from filenames:
- Split filename by underscore:
"landscape_1234567890.png".split("_")→["landscape", "1234567890.png"] - Get last element:
.pop()→"1234567890.png" - Remove extension:
.split(".").shift()→"1234567890" - Compare timestamps numerically:
bName - aName(descending order)
ChatGPT suggestion function
Generates creative prompt suggestions using GPT-3.5:The function uses
temperature: 0.9 for high creativity, generating diverse and interesting prompt suggestions.Azure Blob Storage integration
SAS token generation
Shared Access Signature tokens provide secure, time-limited access to Blob Storage:SAS token permissions
SAS token permissions
- Read: Allows downloading images from Blob Storage
- Write: Enables updating existing blobs
- Create: Permits creating new blobs
SAS tokens expire after 30 minutes, requiring regeneration for long-running sessions. Consider implementing token refresh logic in production applications.
Environment variables
The backend requires the following environment variables:- OpenAI
- Azure Storage
In Azure Functions, environment variables are configured in Configuration > Application Settings in the Azure Portal.
DALL-E 3 configuration
The image generation uses these parameters:Available DALL-E 3 sizes
Available DALL-E 3 sizes
1024x1024(square, used in VisionaryAI)1024x1792(portrait)1792x1024(landscape)
DALL-E 3 only supports
n=1 (one image per request). To generate multiple images, make multiple API calls.Error handling
The current implementation includes basic error handling:Consider implementing more robust error handling with proper error responses, retry logic, and detailed logging for production deployments.
Deployment
Azure Functions can be deployed using:1
Azure Functions Core Tools
2
VS Code Azure Functions extension
Right-click on the
azure folder and select “Deploy to Function App”.3
Azure DevOps or GitHub Actions
Set up CI/CD pipelines for automated deployments.
Performance considerations
Cold start optimization
Cold start optimization
Azure Functions may experience cold starts after periods of inactivity. Consider using Premium or Dedicated plans for production workloads requiring consistent performance.
Connection pooling
Connection pooling
The BlobServiceClient is instantiated at the module level in
getImages.js, enabling connection reuse across function invocations.Rate limiting
Rate limiting
OpenAI APIs have rate limits. Implement queue-based processing or request throttling for high-volume scenarios.
Security best practices
- Store API keys in Azure Key Vault instead of Function App Settings
- Use Azure AD authentication for Function Apps in production
- Implement CORS policies to restrict frontend origins
- Add input validation and sanitization for user prompts
- Monitor OpenAI API usage to prevent abuse
Next steps
Architecture overview
Understand how frontend and backend communicate
Local setup
Set up the development environment