Dynamic Budget Suggestions with confidence scoring and trend analysis (#73) - #79
Dynamic Budget Suggestions with confidence scoring and trend analysis (#73)#79aliraza556 wants to merge 12 commits into
Conversation
|
@rohitdash08 The checks have passed. Please review the code and share your feedback. |
|
Hi @aliraza556, looks good to me but we have some modification related to your PR as per now this is not providing insights you should add persona instead of system prompt. also can you confirm did you check with your openai api key as it was failing to generate insight for the changes you made. Could you please share your Discord? |
- Replace single user prompt with system persona + user data pattern - Add response_format for reliable JSON output - Include insights field in AI response - Log OpenAI failures instead of silently swallowing errors
|
Hi @rohitdash08, thanks for the feedback! I've fixed the OpenAI integration — added a proper FinMind persona as the system message instead of the old single-prompt approach, enabled JSON response format so it won't fail to parse, and added an insights field to the output. Also added error logging so failures are visible now instead of silently falling back. My Discord is |
|
@aliraza556, our tests are still failing. Can you please share your test script here? |
|
|
@aliraza556, it's only related to the budget of the consumer and all I can see here the tests are related to the changes you made. Please resolve the comments and I still can't see openai/gemini integration here, feel free to use gemini free api here |
|
@aliraza556, I'm having trouble to add you on discord. Can you please add me @geekster007 |
@rohitdash08 Sure |
Hi @rohitdash08, I've added Gemini integration for the budget suggestions — it now uses the free Gemini API as the primary AI provider since we don't have an OpenAI key set. The priority chain is: Gemini -> OpenAI -> Heuristic fallback. Also added the FinMind persona, an |
|
@aliraza556, again I'm saying it's not specific to the budget contraint. The priority chain is like user can provide their free gemini api key and get the insights if they don't have openai key. Persona still need to be improved we are still getting test failure for your PR |
…nger valid. Google renamed it.
Hi @rohitdash08, I understand — the Gemini integration is already set up as a general fallback: if user has Gemini key → use Gemini, if OpenAI key → use OpenAI, otherwise → heuristic. I've also updated the default model to |
|
@aliraza556, I think you're aligned with the requirement which I told you on discord feel free to ask please change it back to gemini-1.5-flash in your next commit |
- Add 25 mocked tests for Gemini/OpenAI integration covering API calls, persona verification, fallback chain, JSON parsing, and error handling - Improve FinMind persona with structured sections (Expertise, Personality, Rules) for richer AI-generated insights - Revert default model to gemini-1.5-flash (confirmed working) - All 50 tests pass with black and flake8 clean
@rohitdash08 Done |
|
@aliraza556, still failing the tests resolve the comments. you can use the mock data for insights testings |
|
Hi @rohitdash08, I've fixed the formatting issue and added comprehensive AI integration tests. Here's the local test run:
Model reverted to
|
rohitdash08
left a comment
There was a problem hiding this comment.
@aliraza556 , also please resolve the merge conflicts
| ) | ||
|
|
||
|
|
||
| def _openai_budget(uid: int, ym: str, lookback: int = MAX_MONTHS): |
| return _heuristic_budget(uid, ym, lookback) | ||
|
|
||
|
|
||
| FINMIND_PERSONA = ( |
There was a problem hiding this comment.
improve the persona, this should analyze the manual input as well
| "rising you say so clearly. You always use the 50/30/20 rule " | ||
| "(needs/wants/savings) as a starting framework and adjust based on " | ||
| "the user's actual patterns. You respond ONLY with valid JSON." | ||
| "You are FinMind, a certified personal finance advisor and data analyst " |
There was a problem hiding this comment.
Don't put finmind, it should be financial analyst
| "2. Highlight the single biggest saving opportunity first.\n" | ||
| "3. Provide at least 2 actionable tips tied to specific categories.\n" | ||
| "4. Include short motivational insight (one sentence).\n" | ||
| "5. Respond ONLY with valid JSON — no markdown, no commentary." |
'- Financial analyst persona validation' in the module docstring to match the renamed class and the reviewer's branding request.
|
Hi @rohitdash08, I've addressed all your review comments. Here's a summary of everything done: 1. Method names renamed (
2. "FinMind" removed from persona (
3. Persona improved with manual input handling (
4. Persona depth improved (
5. Merge conflicts resolved
All 63 backend tests pass. Please let me know if you'd like any further changes! |

Summary
Implements the Dynamic Budget Suggestions feature (Issue #73) that suggests budget limits
using past spending patterns to improve onboarding and financial guidance.
What changed
Backend: Budget suggestion engine (
services/ai.py) — Rewrote the heuristic budget algorithm to analyze 3–6 months of historical spending data using weighted averages (recent months weighted higher). Computes a confidence score based on data availability, detects per-category spending trends (increasing/decreasing/stable), and generates actionable budget limits with a 50/30/20 breakdown.Backend: API endpoint (
routes/insights.py) — Enhanced/insights/budget-suggestionwithmonth(YYYY-MM) andmonths(3-6) query parameters. Added Redis caching with 30-minute TTL and input validation.Backend: Cache invalidation (
routes/expenses.py,services/cache.py) — Addedbudget_suggestion_keyand ensured budget suggestion cache is cleared when expenses are created, updated, or deleted.Backend: OpenAPI spec (
openapi.yaml) — Updated endpoint documentation with new query parameters and response schemas (BudgetSuggestion,ConfidenceScore,CategorySuggestion).Frontend: API client (
api/insights.ts) — New TypeScript API client with full type definitions for the budget suggestion response.Frontend: Budgets page (
pages/Budgets.tsx) — Replaced static content with a dynamic dashboard showing suggested budget, spending trend, 50/30/20 breakdown, per-category suggestions with trend indicators, and monthly spending history bars. Includes 3/6 month toggle and refresh button.Tests (
tests/test_budget_suggestions.py) — 18 comprehensive tests covering no-data defaults, single/multi-month analysis, confidence scoring, trend detection (increasing/decreasing/stable), lookback parameter clamping, income exclusion, 50/30/20 breakdown accuracy, and monthly history.Dependency (
requirements.txt) — Addedpython-dateutilfor reliable month arithmetic viarelativedelta.Closed: #73
Acceptance Criteria
ScreenShorts:
Test Plan
docker compose up --build— all services start successfullydocker compose run --rm backend python -m pytest -v tests/— 28/28 tests passupdate with real data on the Budgets page