Architecture overview
The application includes four HTTP-triggered functions:- generateImage: Creates images using DALL-E 3 and stores them in blob storage
- getImages: Retrieves all stored images with SAS-authenticated URLs
- getChatGPTSuggestion: Generates creative prompts using GPT-3.5
- generateSASToken: Provides SAS tokens for blob storage access
- Runtime: Node.js 18+
- Trigger: HTTP (anonymous authentication)
- Framework: Azure Functions v4
Function configuration
host.json
Thehost.json file configures the Azure Functions runtime:
azure/host.json
- Application Insights sampling: Enabled for telemetry with Request types excluded
- Extension bundle: Uses v3.15.0+ for binding extensions
package.json
Dependencies for all Azure Functions:azure/package.json
generateImage
Generates images using DALL-E 3 and stores them in Azure Blob Storage.Implementation
azure/src/functions/generateImage.js
Workflow
1
Receive prompt
Accepts a POST request with JSON body containing the image prompt:
2
Generate image
Calls DALL-E 3 API with:
- Model:
dall-e-3 - Size:
1024x1024 - Number of images:
1
3
Download image
Fetches the generated image from the temporary OpenAI URL as an array buffer.
4
Generate SAS token
Creates a fresh SAS token with write permissions for blob storage.
5
Upload to blob storage
Stores the image with filename format:
{prompt}_{timestamp}.pngThe function uses array buffers to efficiently handle binary image data without file system operations.
getImages
Retrieves all stored images from blob storage with authenticated URLs.Implementation
azure/src/functions/getImages.js
Response format
getChatGPTSuggestion
Generates creative DALL-E prompts using GPT-3.5-Turbo-Instruct.Implementation
azure/src/functions/getChatGPTSuggestion.js
Parameters
- Model:
gpt-3.5-turbo-instruct(optimized for completion tasks) - Max tokens: 100 (sufficient for creative prompts)
- Temperature: 0.9 (high creativity and variety)
Example response
The high temperature (0.9) ensures diverse and creative prompt suggestions for each request.
generateSASToken
Provides SAS tokens for client-side blob storage access.Implementation
azure/src/functions/generateSASToken.js
Response
Returns a SAS token string:OpenAI configuration
Shared OpenAI client used bygenerateImage and getChatGPTSuggestion:
azure/lib/openai.js
Local development
Start the Functions runtime
http://localhost:7071/api/generateImagehttp://localhost:7071/api/getImageshttp://localhost:7071/api/getChatGPTSuggestionhttp://localhost:7071/api/generateSASToken
Test locally
Environment variables for local development
Create alocal.settings.json file in the azure directory:
Error handling
All functions include basic error handling:- Return proper HTTP status codes (400, 500, etc.)
- Include error messages in response body
- Log errors to Application Insights
- Implement retry logic for transient failures
Performance optimization
Cold start mitigation
- Use consumption plan for cost-effective scaling
- Consider Premium plan for production to reduce cold starts
- Keep dependencies minimal
Concurrency
- Default concurrency: 200 simultaneous executions per instance
- Monitor throttling in Application Insights
- Implement rate limiting if needed
Caching
- Cache SAS tokens for their lifetime (30 minutes)
- Consider caching image lists to reduce blob storage queries
Monitoring
View function logs in real-time:- Navigate to your Function App
- Select Monitor > Metrics
- View execution count, duration, and errors