updatedRAG
Browse files- docs/fiscal_knowledge.md +0 -0
- plaid_client.py +37 -8
docs/fiscal_knowledge.md
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
plaid_client.py
CHANGED
|
@@ -66,7 +66,6 @@ def get_balances(access_token: str) -> str:
|
|
| 66 |
# ---------- Transactions ----------
|
| 67 |
|
| 68 |
def get_transactions(access_token: str, days: int = 30) -> str:
|
| 69 |
-
"""Returns formatted transaction summary for the LLM prompt."""
|
| 70 |
end_date = date.today()
|
| 71 |
start_date = end_date - timedelta(days=days)
|
| 72 |
|
|
@@ -85,30 +84,60 @@ def get_transactions(access_token: str, days: int = 30) -> str:
|
|
| 85 |
if not transactions:
|
| 86 |
return "No transactions found in the last 30 days."
|
| 87 |
|
| 88 |
-
#
|
|
|
|
| 89 |
category_totals: dict[str, float] = {}
|
|
|
|
|
|
|
|
|
|
| 90 |
for txn in transactions:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
category = txn.get('personal_finance_category', {}).get('primary', 'OTHER')
|
| 94 |
-
category_totals[category] = category_totals.get(category, 0) + txn['amount']
|
| 95 |
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
lines = [f"SPENDING SUMMARY (last {days} days):"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
for category, total in sorted(category_totals.items(), key=lambda x: -x[1]):
|
| 99 |
readable = category.replace("_", " ").title()
|
| 100 |
lines.append(f"- {readable}: ${total:.2f}")
|
| 101 |
|
| 102 |
lines.append(f"\nTotal spent: ${sum(category_totals.values()):.2f}")
|
|
|
|
| 103 |
lines.append(f"\nRECENT TRANSACTIONS (last 10):")
|
| 104 |
for txn in transactions[:10]:
|
|
|
|
|
|
|
| 105 |
lines.append(
|
| 106 |
-
f"- {txn['date']} {txn['name']:<30} ${
|
| 107 |
)
|
| 108 |
|
| 109 |
return "\n".join(lines)
|
| 110 |
|
| 111 |
-
|
| 112 |
# ---------- Combined snapshot ----------
|
| 113 |
|
| 114 |
def get_financial_snapshot(access_token: str) -> str:
|
|
|
|
| 66 |
# ---------- Transactions ----------
|
| 67 |
|
| 68 |
def get_transactions(access_token: str, days: int = 30) -> str:
|
|
|
|
| 69 |
end_date = date.today()
|
| 70 |
start_date = end_date - timedelta(days=days)
|
| 71 |
|
|
|
|
| 84 |
if not transactions:
|
| 85 |
return "No transactions found in the last 30 days."
|
| 86 |
|
| 87 |
+
# Separate income and expenses
|
| 88 |
+
# In Plaid: positive amount = money OUT (debit), negative amount = money IN (credit)
|
| 89 |
category_totals: dict[str, float] = {}
|
| 90 |
+
income_total: float = 0
|
| 91 |
+
income_sources: list[str] = []
|
| 92 |
+
|
| 93 |
for txn in transactions:
|
| 94 |
+
amount = txn['amount']
|
| 95 |
+
name = txn['name']
|
| 96 |
category = txn.get('personal_finance_category', {}).get('primary', 'OTHER')
|
|
|
|
| 97 |
|
| 98 |
+
# Negative amount = money coming IN (income, deposits, refunds)
|
| 99 |
+
if amount < 0:
|
| 100 |
+
income_total += abs(amount)
|
| 101 |
+
income_sources.append(f"{name}: +${abs(amount):.2f}")
|
| 102 |
+
continue
|
| 103 |
+
|
| 104 |
+
# Skip internal transfers between own accounts
|
| 105 |
+
if category in ['TRANSFER_IN', 'TRANSFER_OUT', 'LOAN_PAYMENTS']:
|
| 106 |
+
# Check if it looks like payroll
|
| 107 |
+
name_lower = name.lower()
|
| 108 |
+
if any(word in name_lower for word in ['payroll', 'salary', 'direct dep', 'employer', 'wages']):
|
| 109 |
+
income_total += amount
|
| 110 |
+
income_sources.append(f"{name} (payroll): +${amount:.2f}")
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
category_totals[category] = category_totals.get(category, 0) + amount
|
| 114 |
+
|
| 115 |
lines = [f"SPENDING SUMMARY (last {days} days):"]
|
| 116 |
+
|
| 117 |
+
# Income section
|
| 118 |
+
if income_total > 0:
|
| 119 |
+
lines.append(f"\nINCOME RECEIVED: +${income_total:.2f}")
|
| 120 |
+
for source in income_sources[:3]:
|
| 121 |
+
lines.append(f" {source}")
|
| 122 |
+
|
| 123 |
+
# Expenses section
|
| 124 |
+
lines.append("\nEXPENSES BY CATEGORY:")
|
| 125 |
for category, total in sorted(category_totals.items(), key=lambda x: -x[1]):
|
| 126 |
readable = category.replace("_", " ").title()
|
| 127 |
lines.append(f"- {readable}: ${total:.2f}")
|
| 128 |
|
| 129 |
lines.append(f"\nTotal spent: ${sum(category_totals.values()):.2f}")
|
| 130 |
+
|
| 131 |
lines.append(f"\nRECENT TRANSACTIONS (last 10):")
|
| 132 |
for txn in transactions[:10]:
|
| 133 |
+
amount = txn['amount']
|
| 134 |
+
prefix = "+" if amount < 0 else "-"
|
| 135 |
lines.append(
|
| 136 |
+
f"- {txn['date']} {txn['name']:<30} {prefix}${abs(amount):.2f}"
|
| 137 |
)
|
| 138 |
|
| 139 |
return "\n".join(lines)
|
| 140 |
|
|
|
|
| 141 |
# ---------- Combined snapshot ----------
|
| 142 |
|
| 143 |
def get_financial_snapshot(access_token: str) -> str:
|