Coverage for apps / ai / migrations / 0005_update_serving_adjustment_v2.py: 55%

22 statements  

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

1"""Update serving_adjustment prompt v2 for QA-031 (instructions) and QA-032 (cooking times).""" 

2 

3from django.db import migrations 

4 

5 

6def update_prompt(apps, schema_editor): 

7 """Update the serving_adjustment prompt to include instructions and cooking times.""" 

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 instructions: 

33- Copy ALL original instruction steps 

34- Update any quantity references to match the scaled ingredients 

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

36- Keep step numbers and structure the same 

37- Do not add or remove steps 

38 

39Rules for cooking times: 

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

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

42- Larger batches generally need longer cooking times 

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

44 

45Rules for notes: 

46- Add notes about cooking time adjustments if scaling significantly 

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

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

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

50- Return an empty array if no adjustments are needed 

51 

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

53 

54 prompt.user_prompt_template = '''Recipe: {title} 

55 

56Current ingredients (for {original_servings} servings): 

57{ingredients} 

58 

59Instructions: 

60{instructions} 

61 

62Cooking times: 

63- Prep time: {prep_time} 

64- Cook time: {cook_time} 

65- Total time: {total_time} 

66 

67Target servings: {new_servings} 

68 

69Scale all ingredients, update instruction quantities, and provide any necessary cooking time adjustments.''' 

70 

71 prompt.save() 

72 except AIPrompt.DoesNotExist: 

73 pass 

74 

75 

76def revert_prompt(apps, schema_editor): 

77 """Revert to the v1 prompt.""" 

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

79 

80 try: 

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

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

83Given a list of ingredients and a serving adjustment, recalculate all quantities accurately. 

84 

85Always respond with valid JSON in this exact format: 

86{ 

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

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

89} 

90 

91Rules for ingredients: 

92- Maintain proper ratios between ingredients 

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

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

95- Keep original ingredient names and preparation instructions 

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

97 

98Rules for notes: 

99- Add notes about cooking time adjustments if scaling significantly (50% or more change) 

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

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

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

103- Return an empty array if no adjustments are needed 

104 

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

106 

107 prompt.user_prompt_template = '''Current ingredients (for {original_servings} servings): 

108{ingredients} 

109 

110Target servings: {new_servings} 

111 

112Scale all ingredients and provide any necessary cooking adjustment notes.''' 

113 

114 prompt.save() 

115 except AIPrompt.DoesNotExist: 

116 pass 

117 

118 

119class Migration(migrations.Migration): 

120 dependencies = [ 

121 ('ai', '0004_update_serving_adjustment_prompt'), 

122 ] 

123 

124 operations = [ 

125 migrations.RunPython(update_prompt, revert_prompt), 

126 ] 

← Back to Dashboard