Skip to content

Deploy Updated Cloudflare Worker

Quick Deploy Instructions

  1. Copy the worker code:

    cat cloudflare-worker/worker.js | pbcopy
    

  2. Go to Cloudflare Dashboard:

  3. Navigate to: https://dash.cloudflare.com/
  4. Select your account
  5. Click "Workers & Pages" in left sidebar

  6. Find your worker:

  7. Look for the worker with URL: b4c056e9-b5ad-4859-bc35-a34d0f8d6b06.stigjohnny.no
  8. Click "Edit Code" or "Quick Edit"

  9. Replace the code:

  10. Select all existing code (Cmd+A)
  11. Delete it
  12. Paste the new code from cloudflare-worker/worker.js

  13. Save and Deploy:

  14. Click "Save and Deploy"
  15. Wait for deployment confirmation

  16. Verify it works:

  17. The worker will automatically detect requests with an image field
  18. No configuration changes needed
  19. Test by taking a food photo in the app

What's New in This Update

Added: Single-Image Vision Support

The worker now supports food photo analysis from the main /api/openai endpoint:

Request Format:

{
  "prompt": "Analyze this food...",
  "system_prompt": "You are a nutritionist...",
  "model": "gpt-4o",
  "temperature": 0,
  "image": "base64_jpeg_string",
  "response_format": {"type": "json_object"}
}

What happens: 1. Worker detects image field in request 2. Routes to handleSingleImageVisionRequest() 3. Formats request for OpenAI Vision API (gpt-4o) 4. Sends image with detail: "high" for accurate food recognition 5. Returns structured JSON response

Key Features

  • Deterministic: Temperature = 0 (same photo = same result)
  • No caching: Food requests always fresh
  • Rate limited: 50 vision requests/hour per IP
  • System prompts: Supports custom system messages
  • JSON mode: Returns structured nutrition data

Backward Compatible

  • Existing supplement label OCR still works
  • Multi-image /vision endpoint unchanged
  • Text-only requests unchanged
  • All existing caching logic preserved

Environment Variables Required

Make sure these are set in your Cloudflare Worker settings: - OPENAI_API_KEY - Your OpenAI API key with GPT-4 Vision access - RATE_LIMIT_KV - KV namespace for rate limiting - CACHE_KV - KV namespace for caching (supplement labels only)

Testing After Deployment

Test 1: Vision Request Works

curl -X POST https://b4c056e9-b5ad-4859-bc35-a34d0f8d6b06.stigjohnny.no/api/openai \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "What food is in this image?",
    "model": "gpt-4o",
    "temperature": 0,
    "image": "BASE64_IMAGE_HERE"
  }'

Expected response:

{
  "success": true,
  "data": "JSON response from GPT-4",
  "usage": {"total_tokens": 1234}
}

Test 2: Regular Text Requests Still Work

curl -X POST https://b4c056e9-b5ad-4859-bc35-a34d0f8d6b06.stigjohnny.no/api/openai \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Extract supplement information from: Vitamin D3 5000 IU",
    "model": "gpt-4-turbo-preview"
  }'

Rollback Instructions

If something goes wrong:

  1. Go to Cloudflare Dashboard → Workers
  2. Click on your worker
  3. Click "Deployments" tab
  4. Find the previous version
  5. Click "Rollback to this deployment"

Cost Impact

GPT-4 Vision (gpt-4o) pricing: - Input: ~$2.50 per 1M tokens - Output: ~$10 per 1M tokens - Images: ~100-300 tokens depending on detail level

With rate limit of 50 requests/hour per IP, max cost is minimal.

Support

If you encounter issues: 1. Check Cloudflare Worker logs 2. Verify environment variables are set 3. Test with curl to isolate iOS app vs worker issues 4. Check OpenAI API key has vision access