LogicGoInfotechSpaces commited on
Commit
5701ca8
·
1 Parent(s): 0a1d03f

Enhance OpenAI prompt to analyze actual spending data (trends, variability) for intelligent recommendations

Browse files
Files changed (1) hide show
  1. app/smart_recommendation.py +59 -12
app/smart_recommendation.py CHANGED
@@ -1045,12 +1045,47 @@ class SmartBudgetRecommender:
1045
  else:
1046
  history = ", ".join(f"{value:.0f}" for value in data["monthly_values"])
1047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1048
  summary = (
1049
  f"Category: {category}\n"
1050
- f"Monthly totals: [{history}]\n"
1051
- f"Average spend: {avg_expense:.2f}\n"
1052
- f"Std deviation: {data['std_dev']:.2f}\n"
1053
- f"Months observed: {data['months_analyzed']}\n"
 
 
 
1054
  )
1055
 
1056
  prompt = (
@@ -1099,14 +1134,26 @@ class SmartBudgetRecommender:
1099
  " - Apply the 50/30/20 rule principles when appropriate (needs/wants/savings)\n\n"
1100
  "Given the user's spending history:\n"
1101
  f"{summary}\n\n"
1102
- "YOUR INTELLIGENT RECOMMENDATION:\n"
1103
- "1. Analyze the spending pattern using your knowledge of this category globally\n"
1104
- "2. Consider economic factors, seasonal trends, and best practices\n"
1105
- "3. Provide a SMART recommendation that:\n"
1106
- " - Reflects actual trends in the data\n"
1107
- " - Accounts for category-specific factors\n"
1108
- " - Includes appropriate buffer for variability and inflation\n"
1109
- " - Is justified by your analysis and knowledge\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
1110
  "CRITICAL RULES - READ CAREFULLY:\n"
1111
  "⚠️ DO NOT ALWAYS RECOMMEND 'KEEP' - This is a common mistake. Analyze the data first!\n\n"
1112
  "MANDATORY ANALYSIS STEPS:\n"
 
1045
  else:
1046
  history = ", ".join(f"{value:.0f}" for value in data["monthly_values"])
1047
 
1048
+ # Build comprehensive data summary for OpenAI to analyze
1049
+ monthly_values = data.get("monthly_values", [])
1050
+
1051
+ # Calculate trend analysis
1052
+ trend_analysis = ""
1053
+ if len(monthly_values) > 1:
1054
+ first_half = monthly_values[:len(monthly_values)//2]
1055
+ second_half = monthly_values[len(monthly_values)//2:]
1056
+ first_avg = sum(first_half) / len(first_half) if first_half else avg_expense
1057
+ second_avg = sum(second_half) / len(second_half) if second_half else avg_expense
1058
+
1059
+ if second_avg > first_avg * 1.05:
1060
+ trend_analysis = f"UPWARD TREND: Early period average ({first_avg:,.0f}) vs Recent period average ({second_avg:,.0f}) - spending is increasing by {((second_avg/first_avg - 1) * 100):.1f}%"
1061
+ elif second_avg < first_avg * 0.95:
1062
+ trend_analysis = f"DOWNWARD TREND: Early period average ({first_avg:,.0f}) vs Recent period average ({second_avg:,.0f}) - spending is decreasing by {((1 - second_avg/first_avg) * 100):.1f}%"
1063
+ else:
1064
+ trend_analysis = f"STABLE TREND: Early period average ({first_avg:,.0f}) vs Recent period average ({second_avg:,.0f}) - spending is relatively stable"
1065
+ else:
1066
+ trend_analysis = "INSUFFICIENT DATA: Only one data point available"
1067
+
1068
+ # Calculate coefficient of variation
1069
+ cv = (data['std_dev'] / avg_expense * 100) if avg_expense > 0 else 0
1070
+ variability_level = ""
1071
+ if cv > 20:
1072
+ variability_level = "HIGH VARIABILITY - spending is very unpredictable"
1073
+ elif cv > 10:
1074
+ variability_level = "MODERATE VARIABILITY - some unpredictability"
1075
+ elif cv > 5:
1076
+ variability_level = "LOW VARIABILITY - relatively predictable"
1077
+ else:
1078
+ variability_level = "VERY LOW VARIABILITY - very predictable spending"
1079
+
1080
  summary = (
1081
  f"Category: {category}\n"
1082
+ f"Monthly spending values: [{history}]\n"
1083
+ f"Average monthly spend: {avg_expense:,.2f}\n"
1084
+ f"Standard deviation: {data['std_dev']:,.2f}\n"
1085
+ f"Coefficient of variation: {cv:.1f}% ({variability_level})\n"
1086
+ f"Number of months analyzed: {data['months_analyzed']}\n"
1087
+ f"Total spending: {data.get('total', avg_expense * data['months_analyzed']):,.2f}\n"
1088
+ f"Trend Analysis: {trend_analysis}\n"
1089
  )
1090
 
1091
  prompt = (
 
1134
  " - Apply the 50/30/20 rule principles when appropriate (needs/wants/savings)\n\n"
1135
  "Given the user's spending history:\n"
1136
  f"{summary}\n\n"
1137
+ "🚨 CRITICAL: YOU MUST ANALYZE THE ACTUAL DATA ABOVE!\n\n"
1138
+ "YOUR INTELLIGENT RECOMMENDATION PROCESS:\n"
1139
+ "STEP 1: ANALYZE THE DATA FIRST (This is mandatory!):\n"
1140
+ " - Look at the 'Monthly spending values' array - what pattern do you see?\n"
1141
+ " - Read the 'Trend Analysis' - is spending increasing, decreasing, or stable?\n"
1142
+ " - Check the 'Coefficient of variation' - how predictable is the spending?\n"
1143
+ " - Calculate: If there's an upward trend, you MUST recommend increase\n"
1144
+ " - Calculate: If variability is high, you MUST recommend increase with larger buffer\n\n"
1145
+ "STEP 2: APPLY YOUR KNOWLEDGE:\n"
1146
+ " - Consider category-specific factors (Food inflation, Transport volatility, etc.)\n"
1147
+ " - Factor in economic trends and inflation\n"
1148
+ " - Account for seasonal variations if relevant\n\n"
1149
+ "STEP 3: PROVIDE SMART RECOMMENDATION:\n"
1150
+ " - The recommended_budget MUST reflect the data analysis from STEP 1\n"
1151
+ " - If trend shows increase → recommended_budget should be higher than average_expense\n"
1152
+ " - If trend shows decrease → recommended_budget should be lower than average_expense\n"
1153
+ " - If variability is high → recommended_budget should have larger buffer (20-30%)\n"
1154
+ " - Include appropriate buffer for inflation and unexpected expenses\n"
1155
+ " - Your reason MUST reference the specific data patterns you observed\n\n"
1156
+ "⚠️ DO NOT give generic recommendations! Base your recommendation on the ACTUAL DATA provided above!\n\n"
1157
  "CRITICAL RULES - READ CAREFULLY:\n"
1158
  "⚠️ DO NOT ALWAYS RECOMMEND 'KEEP' - This is a common mistake. Analyze the data first!\n\n"
1159
  "MANDATORY ANALYSIS STEPS:\n"