Skip to content

Latest commit

 

History

History
226 lines (175 loc) · 5.52 KB

File metadata and controls

226 lines (175 loc) · 5.52 KB

Food Quantity Unit Extraction Plan

Problem Statement

When users log food with a specific unit of measure (e.g., "3 ounces of ground pork at 10am"), the AI extracts:

  • name: "ground pork"
  • value: "3.00"
  • Missing: the unit "ounces"

This forces users to manually convert between units when selecting products from the catalog (e.g., converting ounces to grams or cups).

Current AI Response

{
  "event_type": "food",
  "event_data": {
    "name": "ground pork",
    "value": "3.00"
  },
  "confidence": 90,
  "time_info": {
    "specific_time": "10:00"
  }
}

Desired AI Response

{
  "event_type": "food",
  "event_data": {
    "name": "ground pork",
    "value": "3.00",
    "quantity_unit": "oz"
  },
  "confidence": 90,
  "time_info": {
    "specific_time": "10:00"
  }
}

Implementation Plan

Phase 1: Update AI Schema

File: mobile/src/utils/eventParser.ts

  1. Add quantity_unit to the response schemas:
quantity_unit: {
  type: "STRING",
  nullable: true,
  description: "Unit of measure for food quantity (oz, g, cup, tbsp, tsp, ml, lb, piece, slice, serving, each)"
}
  1. Update system prompts in buildSystemPrompt() and parseAudioWithAI() to instruct the AI to extract quantity units:
For FOOD events: Extract quantity (value) and unit (quantity_unit) separately.
Examples:
- "3 ounces of chicken" → value: "3", quantity_unit: "oz"
- "2 cups of rice" → value: "2", quantity_unit: "cup"
- "a tablespoon of olive oil" → value: "1", quantity_unit: "tbsp"
- "an apple" → value: "1", quantity_unit: "each"
- "chicken breast" (no quantity specified) → value: null, quantity_unit: null

Standard unit abbreviations:
- Weight: oz, g, kg, lb
- Volume: cup, tbsp, tsp, ml, l, fl oz
- Count: each, piece, slice, serving

Phase 2: Update Event Data Flow

Files to update:

  • mobile/src/utils/voiceEventParser.js - Ensure quantity_unit passes through
  • Product selection screen - Receive and use quantity_unit

The quantity_unit should flow from:

  1. AI parsing →
  2. Event confirmation screen →
  3. Product selection screen →
  4. Final event storage (in event_data)

Phase 3: Add Unit Conversion Utility

New file: mobile/src/utils/unitConversion.js

// Weight conversions to grams
const WEIGHT_TO_GRAMS = {
  oz: 28.3495,
  lb: 453.592,
  g: 1,
  kg: 1000
};

// Volume conversions to milliliters
const VOLUME_TO_ML = {
  cup: 236.588,
  tbsp: 14.787,
  tsp: 4.929,
  ml: 1,
  l: 1000,
  'fl oz': 29.574
};

// Convert quantity from one unit to another
export function convertQuantity(value, fromUnit, toUnit) {
  // Implementation
}

// Check if two units are compatible (both weight or both volume)
export function areUnitsCompatible(unit1, unit2) {
  // Implementation
}

// Get gram weight for a quantity (for nutrition calculation)
export function toGrams(value, unit) {
  // Implementation
}

Phase 4: Smart Unit Handling in Product Selection UI

When the user selects a product:

  1. If units match: Use directly (user said "3 oz", product has oz serving)

  2. If units are compatible but different: Convert automatically

    • User: "3 oz" of ground pork
    • Product: serving size is "1 cup = 135g"
    • Calculate: 3 oz = 85g ≈ 0.63 cups
    • Pre-fill: 0.63 cups (or show "3 oz ≈ 0.6 cups")
  3. If units are incompatible: Show the user's original quantity and let them adjust

    • User: "2 pieces" of chicken
    • Product: serving size is "100g"
    • Show: quantity field with note about original input
  4. Display conversion help:

    • Show a small helper text: "3 oz = 85g" when relevant

Unit Categories

Weight Units

Unit Abbreviation Grams
Ounce oz 28.35
Pound lb 453.59
Gram g 1
Kilogram kg 1000

Volume Units

Unit Abbreviation Milliliters
Cup cup 236.59
Tablespoon tbsp 14.79
Teaspoon tsp 4.93
Fluid ounce fl oz 29.57
Milliliter ml 1
Liter l 1000

Count Units

Unit Usage
each Individual items (an apple, a banana)
piece Portions (a piece of chicken)
slice Sliced items (a slice of bread)
serving Generic serving

Testing Scenarios

  1. Weight unit specified:

    • Input: "3 ounces of ground pork"
    • Expected: value: "3", quantity_unit: "oz"
  2. Volume unit specified:

    • Input: "2 cups of rice"
    • Expected: value: "2", quantity_unit: "cup"
  3. Count unit implied:

    • Input: "an apple"
    • Expected: value: "1", quantity_unit: "each"
  4. No unit specified:

    • Input: "chicken breast"
    • Expected: value: null, quantity_unit: null
  5. Mixed with time:

    • Input: "3 oz ground beef at noon"
    • Expected: value: "3", quantity_unit: "oz", time_info: { specific_time: "12:00" }

Success Criteria

  • AI extracts quantity_unit from food inputs
  • Unit flows through to product selection screen
  • Product selection pre-populates with converted quantity when possible
  • User sees helpful conversion hints when units differ
  • No manual unit conversion required for common cases (oz ↔ g, cups ↔ ml)

Estimated Effort

Phase Effort Files Changed
Phase 1: Schema Small 1 file (eventParser.ts)
Phase 2: Data Flow Small 2-3 files
Phase 3: Conversion Utility Medium 1 new file
Phase 4: UI Enhancement Medium Product selection component

Total: ~2-3 hours of development