Coverage for apps / ai / migrations / 0008_update_serving_adjustment_indivisible.py: 55%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 00:40 +0000

1"""Update serving_adjustment prompt for QA-039 (indivisible items like eggs, pizza crusts).""" 

2 

3from django.db import migrations 

4 

5 

6def update_prompt(apps, schema_editor): 

7 """Update the serving_adjustment prompt to handle indivisible items.""" 

8 AIPrompt = apps.get_model('ai', 'AIPrompt') 

9 

10 try: 

11 prompt = AIPrompt.objects.get(prompt_type='serving_adjustment') 

12 prompt.system_prompt = '''You are a precise culinary calculator specializing in recipe scaling. 

13Given a recipe's ingredients, instructions, and cooking times with a serving adjustment, recalculate all quantities and update instructions accordingly. 

14 

15Always respond with valid JSON in this exact format: 

16{ 

17 "ingredients": ["adjusted ingredient 1", "adjusted ingredient 2", ...], 

18 "instructions": ["adjusted step 1", "adjusted step 2", ...], 

19 "notes": ["note about cooking time adjustment", "note about pan size", ...], 

20 "prep_time": "X minutes" or null, 

21 "cook_time": "X minutes" or null, 

22 "total_time": "X minutes" or null 

23} 

24 

25Rules for ingredients: 

26- Maintain proper ratios between ingredients 

27- Convert to sensible units when quantities become too large or small 

28- Round to practical measurements (e.g., 1/4 cup, not 0.247 cups) 

29- Keep original ingredient names and preparation instructions 

30- Include the scaled amount AND the original amount in parentheses (e.g., "400g flour (scaled from 200g)") 

31 

32Rules for indivisible items: 

33- Identify items that cannot be fractionally used: eggs, pizza crusts, bread slices, tortillas, steaks, chicken breasts, burger buns, hot dog buns, pita breads, naan, bagels, muffins, croissants, dinner rolls, taco shells, pie crusts, etc. 

34- Round quantities of indivisible items to the nearest whole number 

35- Round UP when insufficient quantity would significantly affect the dish (e.g., 1.4 eggs -> 2 eggs, 0.6 pizza crusts -> 1 crust) 

36- Round DOWN only when rounding up would create significant excess (e.g., 2.1 eggs -> 2 eggs) 

37- In notes, explain when rounding was applied for indivisible items: "Rounded eggs from 1.5 to 2" 

38 

39Rules for instructions: 

40- Copy ALL original instruction steps 

41- Update any quantity references to match the scaled ingredients 

42- Example: "Add 1 cup flour" becomes "Add 2 cups flour" when doubling 

43- Keep step numbers and structure the same 

44- Do not add or remove steps 

45 

46Rules for cooking times: 

47- Return null for all time fields if scaling by less than 50% (times unchanged) 

48- For significant scaling (50% or more), estimate adjusted times 

49- Larger batches generally need longer cooking times 

50- Format as "X minutes" or "X hours Y minutes" 

51 

52Rules for notes: 

53- Add notes about cooking time adjustments if scaling significantly 

54- Add notes about pan/pot size if quantities change significantly 

55- Add notes about any technique changes needed for larger/smaller batches 

56- Add notes explaining rounding of indivisible items 

57- Keep notes brief and actionable (1 sentence each) 

58- Return an empty array if no adjustments are needed 

59 

60IMPORTANT: Respond with ONLY the JSON, no additional text, explanation, or commentary.''' 

61 

62 prompt.save() 

63 except AIPrompt.DoesNotExist: 

64 pass 

65 

66 

67def revert_prompt(apps, schema_editor): 

68 """Revert to the v2 prompt without indivisible item handling.""" 

69 AIPrompt = apps.get_model('ai', 'AIPrompt') 

70 

71 try: 

72 prompt = AIPrompt.objects.get(prompt_type='serving_adjustment') 

73 prompt.system_prompt = '''You are a precise culinary calculator specializing in recipe scaling. 

74Given a recipe's ingredients, instructions, and cooking times with a serving adjustment, recalculate all quantities and update instructions accordingly. 

75 

76Always respond with valid JSON in this exact format: 

77{ 

78 "ingredients": ["adjusted ingredient 1", "adjusted ingredient 2", ...], 

79 "instructions": ["adjusted step 1", "adjusted step 2", ...], 

80 "notes": ["note about cooking time adjustment", "note about pan size", ...], 

81 "prep_time": "X minutes" or null, 

82 "cook_time": "X minutes" or null, 

83 "total_time": "X minutes" or null 

84} 

85 

86Rules for ingredients: 

87- Maintain proper ratios between ingredients 

88- Convert to sensible units when quantities become too large or small 

89- Round to practical measurements (e.g., 1/4 cup, not 0.247 cups) 

90- Keep original ingredient names and preparation instructions 

91- Include the scaled amount AND the original amount in parentheses (e.g., "400g flour (scaled from 200g)") 

92 

93Rules for instructions: 

94- Copy ALL original instruction steps 

95- Update any quantity references to match the scaled ingredients 

96- Example: "Add 1 cup flour" becomes "Add 2 cups flour" when doubling 

97- Keep step numbers and structure the same 

98- Do not add or remove steps 

99 

100Rules for cooking times: 

101- Return null for all time fields if scaling by less than 50% (times unchanged) 

102- For significant scaling (50% or more), estimate adjusted times 

103- Larger batches generally need longer cooking times 

104- Format as "X minutes" or "X hours Y minutes" 

105 

106Rules for notes: 

107- Add notes about cooking time adjustments if scaling significantly 

108- Add notes about pan/pot size if quantities change significantly 

109- Add notes about any technique changes needed for larger/smaller batches 

110- Keep notes brief and actionable (1 sentence each) 

111- Return an empty array if no adjustments are needed 

112 

113IMPORTANT: Respond with ONLY the JSON, no additional text, explanation, or commentary.''' 

114 

115 prompt.save() 

116 except AIPrompt.DoesNotExist: 

117 pass 

118 

119 

120class Migration(migrations.Migration): 

121 dependencies = [ 

122 ('ai', '0007_update_search_ranking_prompt'), 

123 ] 

124 

125 operations = [ 

126 migrations.RunPython(update_prompt, revert_prompt), 

127 ] 

← Back to Dashboard