Skip to main content
VisionaryAI uses Azure Functions to provide a serverless backend for image generation, storage, and AI-powered suggestions.

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
All functions use:
  • Runtime: Node.js 18+
  • Trigger: HTTP (anonymous authentication)
  • Framework: Azure Functions v4

Function configuration

host.json

The host.json file configures the Azure Functions runtime:
azure/host.json
Key settings:
  • 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}.png
The 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

Images are sorted by timestamp (newest first) for chronological display in the gallery.

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:
See blob storage for details on token generation and permissions.

OpenAI configuration

Shared OpenAI client used by generateImage and getChatGPTSuggestion:
azure/lib/openai.js

Local development

Start the Functions runtime

Functions will be available at:
  • http://localhost:7071/api/generateImage
  • http://localhost:7071/api/getImages
  • http://localhost:7071/api/getChatGPTSuggestion
  • http://localhost:7071/api/generateSASToken

Test locally

Environment variables for local development

Create a local.settings.json file in the azure directory:
Never commit local.settings.json to source control. It’s included in .gitignore by default.

Error handling

All functions include basic error handling:
Consider enhancing error handling for production:
  • 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:
Monitor metrics in Azure Portal:
  1. Navigate to your Function App
  2. Select Monitor > Metrics
  3. View execution count, duration, and errors

Next steps