Spaces:
Paused
Paused
Update llm_agent.py
Browse files- llm_agent.py +380 -377
llm_agent.py
CHANGED
|
@@ -1,377 +1,380 @@
|
|
| 1 |
-
"""
|
| 2 |
-
llm_agent.py
|
| 3 |
-
|
| 4 |
-
FIXED VERSION:
|
| 5 |
-
1. Mistral 7B for Finance (NOT specialized finance model - good for conversations)
|
| 6 |
-
2. Phi-2 2.7B for Education (better than specialized - data quality > size)
|
| 7 |
-
3. 7B max per model (no huge models that fail)
|
| 8 |
-
4. Groq fallback with ONLY available models (mixtral deprecated, using llama)
|
| 9 |
-
5. No falling back immediately - models are ACTUALLY available
|
| 10 |
-
ENHANCEMENTS:
|
| 11 |
-
1. Scenario-aware prompts for what-if queries
|
| 12 |
-
2. Proactive insights integration
|
| 13 |
-
3. Better formatting for comparisons
|
| 14 |
-
4. Unchanged: Multi-LLM strategy (HF + Groq)
|
| 15 |
-
5. Unchanged: All existing analytics integration
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
from groq import Groq
|
| 19 |
-
from analytics import (
|
| 20 |
-
analyze_exam_performance,
|
| 21 |
-
calculate_budget_metrics,
|
| 22 |
-
get_time_improvement_suggestions,
|
| 23 |
-
generate_practice_questions,
|
| 24 |
-
calculate_projected_improvement,
|
| 25 |
-
parse_budget_query,
|
| 26 |
-
apply_scenario_changes,
|
| 27 |
-
compare_scenarios,
|
| 28 |
-
generate_proactive_insights
|
| 29 |
-
)
|
| 30 |
-
import json
|
| 31 |
-
import os
|
| 32 |
-
from huggingface_hub import InferenceClient
|
| 33 |
-
|
| 34 |
-
# HF Models - All 7B or smaller
|
| 35 |
-
HF_MODELS = {
|
| 36 |
-
"Finance": "mistralai/Mistral-7B-Instruct-v0.1",
|
| 37 |
-
"Education": "microsoft/phi-2",
|
| 38 |
-
"Family": "HuggingFaceH4/zephyr-7b-beta",
|
| 39 |
-
"Friends": "HuggingFaceH4/zephyr-7b-beta",
|
| 40 |
-
"Weekend/Vacation": "HuggingFaceH4/zephyr-7b-beta"
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
# Groq models - Available models only
|
| 44 |
-
GROQ_MODELS = {
|
| 45 |
-
"Finance": "llama-3.1-8b-instant",
|
| 46 |
-
"Education": "llama-3.3-70b-versatile",
|
| 47 |
-
"Family": "llama-3.1-8b-instant",
|
| 48 |
-
"Friends": "llama-3.1-8b-instant",
|
| 49 |
-
"Weekend/Vacation": "llama-3.1-8b-instant"
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY"
|
| 53 |
-
HF_API_KEY = os.getenv("HF_API_KEY"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
-
|
| 62 |
-
-
|
| 63 |
-
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
-
|
| 70 |
-
-
|
| 71 |
-
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
-
|
| 83 |
-
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
""
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
{
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
"
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
results +=
|
| 171 |
-
results +=
|
| 172 |
-
results += f"β’
|
| 173 |
-
|
| 174 |
-
results += "
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
results += "\n"
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
results += f"
|
| 188 |
-
results += f"
|
| 189 |
-
results += f"
|
| 190 |
-
results += f"
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
results += f"\
|
| 194 |
-
for
|
| 195 |
-
results += f"
|
| 196 |
-
results += f"\n"
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
results += f"\n"
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
results += f"\n"
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
results += f"
|
| 218 |
-
results += f"
|
| 219 |
-
results += f"
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
"
|
| 343 |
-
"
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
"
|
| 361 |
-
"
|
| 362 |
-
"
|
| 363 |
-
"
|
| 364 |
-
"
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
"
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
"
|
| 374 |
-
"
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
llm_agent.py
|
| 3 |
+
|
| 4 |
+
FIXED VERSION:
|
| 5 |
+
1. Mistral 7B for Finance (NOT specialized finance model - good for conversations)
|
| 6 |
+
2. Phi-2 2.7B for Education (better than specialized - data quality > size)
|
| 7 |
+
3. 7B max per model (no huge models that fail)
|
| 8 |
+
4. Groq fallback with ONLY available models (mixtral deprecated, using llama)
|
| 9 |
+
5. No falling back immediately - models are ACTUALLY available
|
| 10 |
+
ENHANCEMENTS:
|
| 11 |
+
1. Scenario-aware prompts for what-if queries
|
| 12 |
+
2. Proactive insights integration
|
| 13 |
+
3. Better formatting for comparisons
|
| 14 |
+
4. Unchanged: Multi-LLM strategy (HF + Groq)
|
| 15 |
+
5. Unchanged: All existing analytics integration
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
from groq import Groq
|
| 19 |
+
from analytics import (
|
| 20 |
+
analyze_exam_performance,
|
| 21 |
+
calculate_budget_metrics,
|
| 22 |
+
get_time_improvement_suggestions,
|
| 23 |
+
generate_practice_questions,
|
| 24 |
+
calculate_projected_improvement,
|
| 25 |
+
parse_budget_query,
|
| 26 |
+
apply_scenario_changes,
|
| 27 |
+
compare_scenarios,
|
| 28 |
+
generate_proactive_insights
|
| 29 |
+
)
|
| 30 |
+
import json
|
| 31 |
+
import os
|
| 32 |
+
from huggingface_hub import InferenceClient
|
| 33 |
+
|
| 34 |
+
# HF Models - All 7B or smaller
|
| 35 |
+
HF_MODELS = {
|
| 36 |
+
"Finance": "mistralai/Mistral-7B-Instruct-v0.1",
|
| 37 |
+
"Education": "microsoft/phi-2",
|
| 38 |
+
"Family": "HuggingFaceH4/zephyr-7b-beta",
|
| 39 |
+
"Friends": "HuggingFaceH4/zephyr-7b-beta",
|
| 40 |
+
"Weekend/Vacation": "HuggingFaceH4/zephyr-7b-beta"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Groq models - Available models only
|
| 44 |
+
GROQ_MODELS = {
|
| 45 |
+
"Finance": "llama-3.1-8b-instant",
|
| 46 |
+
"Education": "llama-3.3-70b-versatile",
|
| 47 |
+
"Family": "llama-3.1-8b-instant",
|
| 48 |
+
"Friends": "llama-3.1-8b-instant",
|
| 49 |
+
"Weekend/Vacation": "llama-3.1-8b-instant"
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 53 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 54 |
+
|
| 55 |
+
if not GROQ_API_KEY or not HF_API_KEY:
|
| 56 |
+
raise ValueError("Missing API keys in environment")
|
| 57 |
+
|
| 58 |
+
GENZ_PERSONA = r"""You are Aqua, a GenZ AI mentor who's like that super helpful older friend who's been through it all.
|
| 59 |
+
|
| 60 |
+
PERSONALITY TRAITS:
|
| 61 |
+
- Warm, friendly, and supportive (but not fake or overly enthusiastic)
|
| 62 |
+
- Direct and honest (no sugarcoating, but always constructive)
|
| 63 |
+
- Uses casual language naturally (occasional "fr", "ngl", "lowkey" - but don't overdo it)
|
| 64 |
+
- Encouraging but realistic about challenges
|
| 65 |
+
- Uses emojis purposefully (1-2 per response max)
|
| 66 |
+
- Breaks down complex stuff into digestible chunks
|
| 67 |
+
|
| 68 |
+
COMMUNICATION STYLE:
|
| 69 |
+
- Keep responses concise (2-4 sentences for simple questions, more for complex ones)
|
| 70 |
+
- Lead with the most actionable insight
|
| 71 |
+
- Use bullet points for steps or lists (but format naturally)
|
| 72 |
+
- Reference specific data from the user's profile when relevant
|
| 73 |
+
- Ask follow-up questions when you need more context
|
| 74 |
+
- Celebrate wins genuinely
|
| 75 |
+
|
| 76 |
+
CRITICAL: CURRENCY FORMATTING
|
| 77 |
+
- NEVER use bare dollar signs like $750
|
| 78 |
+
- ALWAYS write currency as: "750 dollars" or "USD 750"
|
| 79 |
+
- Example: "You've spent 750 dollars out of 2000 dollars"
|
| 80 |
+
|
| 81 |
+
WHAT TO AVOID:
|
| 82 |
+
- Being overly formal or corporate
|
| 83 |
+
- Using too many emojis or exclamation marks
|
| 84 |
+
- Giving vague advice like "try your best"
|
| 85 |
+
- Long walls of text without structure
|
| 86 |
+
- Ignoring the user's actual situation/data
|
| 87 |
+
|
| 88 |
+
YOUR GOAL:
|
| 89 |
+
Help this GenZ user make smart decisions about their life, finances, and education. Be the mentor they wish they had."""
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def get_system_prompt(profile, category, tool_results="", scenario_data=None):
|
| 93 |
+
"""Enhanced system prompt with scenario awareness"""
|
| 94 |
+
exam_status = f"Recent exam: {profile['recent_exam']}/{profile['goal']}"
|
| 95 |
+
budget_status = f"Spending: {profile['spend']} dollars/{profile['budget']} dollars"
|
| 96 |
+
|
| 97 |
+
weak_areas = []
|
| 98 |
+
if profile.get('math_weakness'):
|
| 99 |
+
weak_areas.append("math")
|
| 100 |
+
if profile.get('rushing'):
|
| 101 |
+
weak_areas.append("time management")
|
| 102 |
+
|
| 103 |
+
# Add scenario context if present
|
| 104 |
+
scenario_context = ""
|
| 105 |
+
if scenario_data:
|
| 106 |
+
scenario_context = f"""
|
| 107 |
+
SCENARIO ANALYSIS:
|
| 108 |
+
The user is asking a 'what-if' question. Here's the comparison:
|
| 109 |
+
|
| 110 |
+
Current Situation:
|
| 111 |
+
- Budget: {profile['budget']} dollars
|
| 112 |
+
- Spent: {profile['spend']} dollars
|
| 113 |
+
- Remaining: {profile['budget'] - profile['spend']} dollars
|
| 114 |
+
|
| 115 |
+
Scenario Result:
|
| 116 |
+
- Budget: {scenario_data['scenario']['budget']} dollars
|
| 117 |
+
- Spent: {scenario_data['scenario']['spent']} dollars
|
| 118 |
+
- Remaining: {scenario_data['scenario']['remaining']} dollars
|
| 119 |
+
|
| 120 |
+
Key Differences:
|
| 121 |
+
{chr(10).join(f"β’ {insight}" for insight in scenario_data['insights'])}
|
| 122 |
+
|
| 123 |
+
IMPORTANT: Frame your response around this comparison. Be specific about the trade-offs.
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
context = f"""
|
| 127 |
+
{GENZ_PERSONA}
|
| 128 |
+
|
| 129 |
+
CURRENT USER CONTEXT:
|
| 130 |
+
Name: {profile.get('name', 'User')}
|
| 131 |
+
Category Focus: {category}
|
| 132 |
+
{exam_status if category == 'Education' else ''}
|
| 133 |
+
{budget_status if category == 'Finance' else ''}
|
| 134 |
+
{'Areas needing work: ' + ', '.join(weak_areas) if weak_areas else ''}
|
| 135 |
+
Today's goals progress: {profile.get('goals_today', {})}
|
| 136 |
+
|
| 137 |
+
{scenario_context}
|
| 138 |
+
|
| 139 |
+
{tool_results}
|
| 140 |
+
|
| 141 |
+
IMPORTANT:
|
| 142 |
+
1. Reference specific numbers and data from the user's profile in your responses.
|
| 143 |
+
2. ALWAYS format currency as "X dollars" or "USD X" - NEVER use bare dollar signs like $X
|
| 144 |
+
3. If this is a scenario/what-if question, focus on the comparison and trade-offs.
|
| 145 |
+
"""
|
| 146 |
+
|
| 147 |
+
return context
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def should_use_analytics(query, category):
|
| 151 |
+
"""Detect if analytics tools are needed"""
|
| 152 |
+
analysis_keywords = [
|
| 153 |
+
"analyze", "analysis", "break down", "breakdown", "how am i doing",
|
| 154 |
+
"performance", "progress", "score", "exam", "test", "spending",
|
| 155 |
+
"budget", "where", "what", "show me", "tell me about", "compare",
|
| 156 |
+
"what if", "if i", "suppose", "imagine"
|
| 157 |
+
]
|
| 158 |
+
|
| 159 |
+
return any(keyword in query.lower() for keyword in analysis_keywords)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def get_tool_results(query, profile, category, scenario_profile=None):
|
| 163 |
+
"""Get analytics results, with scenario comparison if applicable"""
|
| 164 |
+
results = ""
|
| 165 |
+
|
| 166 |
+
# Check if this is a scenario query
|
| 167 |
+
if scenario_profile:
|
| 168 |
+
comparison = compare_scenarios(profile, scenario_profile)
|
| 169 |
+
|
| 170 |
+
results += "\n\nπ SCENARIO COMPARISON ANALYSIS\n\n"
|
| 171 |
+
results += "Current vs Scenario:\n"
|
| 172 |
+
results += f"β’ Spending: {profile['spend']} dollars β {scenario_profile['spend']} dollars\n"
|
| 173 |
+
results += f"β’ Change: {comparison['differences']['spend']:+.0f} dollars\n"
|
| 174 |
+
results += f"β’ Remaining: {comparison['current']['remaining']} dollars β {comparison['scenario']['remaining']} dollars\n"
|
| 175 |
+
results += f"β’ Change: {comparison['differences']['remaining']:+.0f} dollars\n\n"
|
| 176 |
+
|
| 177 |
+
results += "Key Insights:\n"
|
| 178 |
+
for insight in comparison['insights']:
|
| 179 |
+
results += f"β’ {insight}\n"
|
| 180 |
+
results += "\n"
|
| 181 |
+
|
| 182 |
+
return results
|
| 183 |
+
|
| 184 |
+
# Regular analytics (existing code)
|
| 185 |
+
if category == "Education" and should_use_analytics(query, category):
|
| 186 |
+
exam_data = analyze_exam_performance(profile)
|
| 187 |
+
results += f"\n\nπ EDUCATION ANALYSIS\n\n"
|
| 188 |
+
results += f"Total Score: {exam_data['total_score']}/{exam_data['goal_score']}\n"
|
| 189 |
+
results += f"Progress: {exam_data['progress_pct']}%\n"
|
| 190 |
+
results += f"Points Needed: {exam_data['points_needed']}\n"
|
| 191 |
+
results += f"Weakest Subject: {exam_data['weakest_subject']} ({exam_data['score_breakdown'][exam_data['weakest_subject']]} points)\n"
|
| 192 |
+
results += f"Strongest Subject: {exam_data['strongest_subject']} ({exam_data['score_breakdown'][exam_data['strongest_subject']]} points)\n"
|
| 193 |
+
results += f"\nScore Breakdown:\n"
|
| 194 |
+
for subject, score in exam_data['score_breakdown'].items():
|
| 195 |
+
results += f" - {subject}: {score} ({exam_data['score_percentages'][subject]}%)\n"
|
| 196 |
+
results += f"\nKey Insights:\n"
|
| 197 |
+
for insight in exam_data['insights']:
|
| 198 |
+
results += f" β’ {insight}\n"
|
| 199 |
+
results += f"\n"
|
| 200 |
+
|
| 201 |
+
if any(word in query.lower() for word in ['improve', 'better', 'increase', 'boost']):
|
| 202 |
+
suggestions = get_time_improvement_suggestions(profile)
|
| 203 |
+
results += f"\n\nπ‘ IMPROVEMENT SUGGESTIONS\n\n"
|
| 204 |
+
for suggestion in suggestions:
|
| 205 |
+
results += f" β’ {suggestion}\n"
|
| 206 |
+
results += f"\n"
|
| 207 |
+
|
| 208 |
+
if any(word in query.lower() for word in ['practice', 'questions', 'quiz', 'drill']):
|
| 209 |
+
practice_qs = generate_practice_questions(exam_data['weakest_subject'])
|
| 210 |
+
results += f"\n\nπ PRACTICE QUESTIONS\n\n"
|
| 211 |
+
for i, q in enumerate(practice_qs[:3], 1):
|
| 212 |
+
results += f" {i}. {q}\n"
|
| 213 |
+
results += f"\n"
|
| 214 |
+
|
| 215 |
+
elif category == "Finance" and should_use_analytics(query, category):
|
| 216 |
+
budget_data = calculate_budget_metrics(profile)
|
| 217 |
+
results += f"\n\nπ° FINANCE ANALYSIS\n\n"
|
| 218 |
+
results += f"Budget: {budget_data['budget']} dollars\n"
|
| 219 |
+
results += f"Spent: {budget_data['spent']} dollars ({budget_data['spend_percentage']}%)\n"
|
| 220 |
+
results += f"Remaining: {budget_data['remaining']} dollars\n"
|
| 221 |
+
results += f"Status: {'β
On track' if budget_data['on_track'] else 'β οΈ Over target'}\n"
|
| 222 |
+
results += f"\nSpending Breakdown:\n"
|
| 223 |
+
for cat_name, amount in budget_data['categories'].items():
|
| 224 |
+
pct = round((amount / budget_data['budget']) * 100, 1)
|
| 225 |
+
results += f" - {cat_name}: {amount} dollars ({pct}%)\n"
|
| 226 |
+
if budget_data['potential_savings']:
|
| 227 |
+
results += f"\nPotential Savings:\n"
|
| 228 |
+
for saving in budget_data['potential_savings']:
|
| 229 |
+
results += f" β’ {saving}\n"
|
| 230 |
+
results += f"\n"
|
| 231 |
+
|
| 232 |
+
return results
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def aqua_response_hf(query, profile, category, scenario_profile=None):
|
| 236 |
+
"""Try Hugging Face inference first"""
|
| 237 |
+
|
| 238 |
+
try:
|
| 239 |
+
hf_client = InferenceClient(api_key=HF_API_KEY)
|
| 240 |
+
|
| 241 |
+
model_name = HF_MODELS.get(category, "mistralai/Mistral-7B-Instruct-v0.1")
|
| 242 |
+
tool_results = get_tool_results(query, profile, category, scenario_profile)
|
| 243 |
+
|
| 244 |
+
# Build scenario data if present
|
| 245 |
+
scenario_data = None
|
| 246 |
+
if scenario_profile:
|
| 247 |
+
scenario_data = compare_scenarios(profile, scenario_profile)
|
| 248 |
+
|
| 249 |
+
system_message = get_system_prompt(profile, category, tool_results, scenario_data)
|
| 250 |
+
|
| 251 |
+
full_prompt = f"{system_message}\n\nUser Query: {query}"
|
| 252 |
+
|
| 253 |
+
response = hf_client.text_generation(
|
| 254 |
+
model=model_name,
|
| 255 |
+
prompt=full_prompt,
|
| 256 |
+
max_new_tokens=350,
|
| 257 |
+
temperature=0.75,
|
| 258 |
+
top_p=0.9
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
return response.strip()
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
print(f"HF Error: {str(e)[:100]}")
|
| 265 |
+
return None
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def aqua_response_groq(query, profile, category, scenario_profile=None):
|
| 269 |
+
"""Fallback to Groq with available models only"""
|
| 270 |
+
|
| 271 |
+
if not GROQ_API_KEY or GROQ_API_KEY == "gsk_xxx":
|
| 272 |
+
return "β οΈ Groq API Key Missing! Add it to your environment."
|
| 273 |
+
|
| 274 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 275 |
+
|
| 276 |
+
tool_results = get_tool_results(query, profile, category, scenario_profile)
|
| 277 |
+
|
| 278 |
+
# Build scenario data if present
|
| 279 |
+
scenario_data = None
|
| 280 |
+
if scenario_profile:
|
| 281 |
+
scenario_data = compare_scenarios(profile, scenario_profile)
|
| 282 |
+
|
| 283 |
+
system_message = get_system_prompt(profile, category, tool_results, scenario_data)
|
| 284 |
+
|
| 285 |
+
model = GROQ_MODELS.get(category, "llama-3.3-70b-versatile")
|
| 286 |
+
|
| 287 |
+
messages = [
|
| 288 |
+
{"role": "system", "content": system_message},
|
| 289 |
+
{"role": "user", "content": query}
|
| 290 |
+
]
|
| 291 |
+
|
| 292 |
+
try:
|
| 293 |
+
chat_completion = client.chat.completions.create(
|
| 294 |
+
messages=messages,
|
| 295 |
+
model=model,
|
| 296 |
+
temperature=0.75,
|
| 297 |
+
max_tokens=350,
|
| 298 |
+
top_p=0.9,
|
| 299 |
+
stop=None
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
response_content = chat_completion.choices[0].message.content
|
| 303 |
+
response_content = response_content.replace('$', '\\$')
|
| 304 |
+
return response_content.strip()
|
| 305 |
+
|
| 306 |
+
except Exception as e:
|
| 307 |
+
error_msg = str(e)[:100]
|
| 308 |
+
print(f"Groq Error: {error_msg}")
|
| 309 |
+
return f"π¬ Something went wrong: {error_msg}... Try again?"
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def aqua_response(query, profile, category, scenario_profile=None):
|
| 313 |
+
"""
|
| 314 |
+
Main response function with scenario awareness:
|
| 315 |
+
1. Try HF models first (area-specific, <= 7B)
|
| 316 |
+
2. Fallback to Groq (area-specific, available models)
|
| 317 |
+
3. Return error if both fail
|
| 318 |
+
|
| 319 |
+
NEW: Accepts optional scenario_profile for what-if queries
|
| 320 |
+
"""
|
| 321 |
+
|
| 322 |
+
# Try HF first
|
| 323 |
+
print(f"[DEBUG] Using HF model for {category}: {HF_MODELS.get(category)}")
|
| 324 |
+
hf_response = aqua_response_hf(query, profile, category, scenario_profile)
|
| 325 |
+
|
| 326 |
+
if hf_response:
|
| 327 |
+
return hf_response
|
| 328 |
+
|
| 329 |
+
# If HF fails, use Groq with same focus-area model
|
| 330 |
+
print(f"[DEBUG] HF failed, falling back to Groq: {GROQ_MODELS.get(category)}")
|
| 331 |
+
groq_response = aqua_response_groq(query, profile, category, scenario_profile)
|
| 332 |
+
|
| 333 |
+
return groq_response
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def get_contextual_welcome_message(category, profile):
|
| 337 |
+
"""Generate proactive welcome with insights"""
|
| 338 |
+
# Get proactive insights
|
| 339 |
+
insights = generate_proactive_insights(profile, category)
|
| 340 |
+
|
| 341 |
+
base_messages = {
|
| 342 |
+
"Finance": f"Hey! π° I see you're at {profile['spend']} dollars/{profile['budget']} dollars this month.",
|
| 343 |
+
"Education": f"Hey! π Last score was {profile['recent_exam']} - you need {profile['goal'] - profile['recent_exam']} more points to hit your goal.",
|
| 344 |
+
"Family": f"Hey! π¨π©π§ Family stuff can be tricky to balance. I'm here to help you stay connected while crushing your own goals.",
|
| 345 |
+
"Friends": f"Hey! π₯ Balancing friends and responsibilities is an art. Let's figure out how to stay social without breaking the bank.",
|
| 346 |
+
"Weekend/Vacation": f"Hey! ποΈ Everyone needs a break. Let's plan something fun that won't wreck your budget."
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
base = base_messages.get(category, "Hey! π I'm Aqua, your personal mentor. What's on your mind?")
|
| 350 |
+
|
| 351 |
+
# Add top insight if critical
|
| 352 |
+
if insights and insights[0].get('type') == 'critical':
|
| 353 |
+
base += f" {insights[0]['icon']} {insights[0]['text']}"
|
| 354 |
+
|
| 355 |
+
return base
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
def get_default_profile():
|
| 359 |
+
return {
|
| 360 |
+
"name": "Suzy",
|
| 361 |
+
"recent_exam": 1200,
|
| 362 |
+
"goal": 1600,
|
| 363 |
+
"math_weakness": True,
|
| 364 |
+
"rushing": True,
|
| 365 |
+
"budget": 2000,
|
| 366 |
+
"spend": 1576,
|
| 367 |
+
"goals_today": {
|
| 368 |
+
"Calories": 85,
|
| 369 |
+
"Money": 75,
|
| 370 |
+
"Steps": 54
|
| 371 |
+
},
|
| 372 |
+
"last_scores": {
|
| 373 |
+
"Reading": 240,
|
| 374 |
+
"Writing": 220,
|
| 375 |
+
"Reasoning": 140,
|
| 376 |
+
"Algebra": 100,
|
| 377 |
+
"Geometry": 100
|
| 378 |
+
},
|
| 379 |
+
}
|
| 380 |
+
|