Deploy Updated Cloudflare Worker¶
Quick Deploy Instructions¶
-
Copy the worker code:
-
Go to Cloudflare Dashboard:
- Navigate to: https://dash.cloudflare.com/
- Select your account
-
Click "Workers & Pages" in left sidebar
-
Find your worker:
- Look for the worker with URL:
b4c056e9-b5ad-4859-bc35-a34d0f8d6b06.stigjohnny.no -
Click "Edit Code" or "Quick Edit"
-
Replace the code:
- Select all existing code (Cmd+A)
- Delete it
-
Paste the new code from
cloudflare-worker/worker.js -
Save and Deploy:
- Click "Save and Deploy"
-
Wait for deployment confirmation
-
Verify it works:
- The worker will automatically detect requests with an
imagefield - No configuration changes needed
- 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
/visionendpoint 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:
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:
- Go to Cloudflare Dashboard → Workers
- Click on your worker
- Click "Deployments" tab
- Find the previous version
- 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