Spaces:
Sleeping
Sleeping
File size: 12,785 Bytes
024938b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 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 |
"""
LlamaIndex core module for SmartLedger
Handles document indexing and intelligent querying of financial data
"""
import pandas as pd
from typing import List, Dict
import json
try:
from llama_index.core import Document, VectorStoreIndex, Settings
from modal_llama_integration import create_modal_llm, create_modal_embedding
except ImportError:
# Graceful fallback if LlamaIndex not installed
Document = None
VectorStoreIndex = None
Settings = None
create_modal_llm = None
create_modal_embedding = None
class LedgerIndexer:
"""
Handles indexing and querying of financial ledger data using LlamaIndex
"""
def __init__(self, use_modal_llm: bool = True):
"""
Initialize the LedgerIndexer
Args:
use_modal_llm: Whether to use Modal-deployed LLM (True) or OpenAI directly (False)
"""
self.index = None
self.df = None
self.use_modal_llm = use_modal_llm
# Initialize LlamaIndex settings
if Settings is not None:
if use_modal_llm and create_modal_llm is not None:
# Use Modal-hosted models
try:
Settings.llm = create_modal_llm(temperature=0.1, max_new_tokens=512)
Settings.embed_model = create_modal_embedding()
print("β
Using Modal-hosted models")
except Exception as e:
print(f"β οΈ Failed to initialize Modal models: {e}")
print("π‘ Run 'modal deploy modal_functions.py' to enable Modal models")
Settings.llm = None
Settings.embed_model = None
else:
print("β οΈ Modal integration not available - LlamaIndex indexing disabled")
Settings.llm = None
Settings.embed_model = None
def create_monthly_summaries(self, df: pd.DataFrame) -> List[Document]:
"""
Create monthly summary documents optimized for anomaly detection
Args:
df: DataFrame containing transaction data
Returns:
List of LlamaIndex Documents containing monthly summaries
"""
if Document is None:
raise ImportError("LlamaIndex not available")
documents = []
# Ensure date column is datetime
df['date'] = pd.to_datetime(df['date'])
df['year_month'] = df['date'].dt.to_period('M')
# Group by month
monthly_groups = df.groupby('year_month')
for period, month_data in monthly_groups:
# Calculate monthly statistics
total_amount = month_data['amount'].sum()
transaction_count = len(month_data)
avg_transaction = month_data['amount'].mean()
unique_vendors = month_data['vendor'].nunique()
# Category breakdown
category_breakdown = {}
if 'category' in month_data.columns:
category_breakdown = month_data.groupby('category')['amount'].sum().to_dict()
# Vendor breakdown (top 10)
vendor_breakdown = month_data.groupby('vendor')['amount'].sum().nlargest(10).to_dict()
# Unusual patterns detection data
large_transactions = month_data[month_data['amount'] > month_data['amount'].quantile(0.95)]
frequent_vendors = month_data['vendor'].value_counts().head(5).to_dict()
# Create document content
content = f"""
FINANCIAL SUMMARY FOR {period}
OVERVIEW:
- Total Spending: ${total_amount:,.2f}
- Transaction Count: {transaction_count}
- Average Transaction: ${avg_transaction:.2f}
- Unique Vendors: {unique_vendors}
- Date Range: {month_data['date'].min().strftime('%Y-%m-%d')} to {month_data['date'].max().strftime('%Y-%m-%d')}
CATEGORY BREAKDOWN:
{json.dumps(category_breakdown, indent=2)}
TOP VENDORS BY SPENDING:
{json.dumps(vendor_breakdown, indent=2)}
FREQUENT VENDORS (by transaction count):
{json.dumps(frequent_vendors, indent=2)}
LARGE TRANSACTIONS (95th percentile):
{large_transactions[['date', 'vendor', 'amount', 'category']].to_string(index=False)}
DAILY SPENDING PATTERN:
{month_data.groupby(month_data['date'].dt.day)['amount'].sum().to_dict()}
"""
# Create metadata for better querying
metadata = {
"period": str(period),
"year": period.year,
"month": period.month,
"total_amount": float(total_amount),
"transaction_count": int(transaction_count),
"avg_transaction": float(avg_transaction),
"unique_vendors": int(unique_vendors),
"top_category": max(category_breakdown.items(), key=lambda x: x[1])[0] if category_breakdown else "Unknown",
"document_type": "monthly_summary"
}
doc = Document(
text=content,
metadata=metadata
)
documents.append(doc)
return documents
def create_transaction_documents(self, df: pd.DataFrame) -> List[Document]:
"""
Create individual transaction documents for granular analysis
Args:
df: DataFrame containing transaction data
Returns:
List of LlamaIndex Documents for individual transactions
"""
if Document is None:
raise ImportError("LlamaIndex not available")
documents = []
for idx, row in df.iterrows():
content = f"""
TRANSACTION RECORD
Date: {row['date']}
Vendor: {row['vendor']}
Amount: ${row['amount']:.2f}
Category: {row.get('category', 'Uncategorized')}
Description: {row.get('description', 'No description')}
Context:
- Day of week: {pd.to_datetime(row['date']).strftime('%A')}
- Month: {pd.to_datetime(row['date']).strftime('%B %Y')}
"""
metadata = {
"transaction_id": str(idx),
"date": str(row['date']),
"vendor": str(row['vendor']),
"amount": float(row['amount']),
"category": str(row.get('category', 'Uncategorized')),
"document_type": "transaction"
}
doc = Document(
text=content,
metadata=metadata
)
documents.append(doc)
return documents
def index_ledger_data(self, df: pd.DataFrame, include_transactions: bool = False) -> bool:
"""
Index the ledger data using LlamaIndex
Args:
df: DataFrame containing ledger data
include_transactions: Whether to include individual transactions
Returns:
True if indexing successful, False otherwise
"""
try:
if VectorStoreIndex is None:
print("β LlamaIndex not available - install with: pip install llama-index")
return False
if Settings.llm is None or Settings.embed_model is None:
print("β Modal models not configured - deploy with: modal deploy modal_functions.py")
return False
self.df = df.copy()
documents = []
# Create monthly summary documents (primary for anomaly detection)
monthly_docs = self.create_monthly_summaries(df)
documents.extend(monthly_docs)
# Optionally include individual transactions
if include_transactions:
transaction_docs = self.create_transaction_documents(df)
documents.extend(transaction_docs)
print(f"π Created {len(documents)} documents for indexing")
# Create the index
self.index = VectorStoreIndex.from_documents(documents)
print("β
Successfully indexed financial data")
return True
except Exception as e:
print(f"β Error indexing data: {e}")
return False
def query_anomalies(self, query: str = None) -> str:
"""
Query for anomalies in the financial data
Args:
query: Custom query string, defaults to anomaly detection
Returns:
LLM response about anomalies found
"""
if self.index is None:
return "β No data indexed. Please upload and analyze a CSV file first, then ensure Modal models are deployed."
if Settings.llm is None:
return "β Modal LLM not available. Please deploy Modal functions: modal deploy modal_functions.py"
if query is None:
query = """
Analyze this financial data for anomalies and unusual patterns. Look for:
1. Month-over-month spending increases or decreases > 20%
2. Unusual vendor patterns or new large expenses
3. Category spending that deviates from normal patterns
4. Suspicious transaction amounts or frequencies
5. Seasonal anomalies or unexpected spikes
Provide specific examples with amounts and dates where possible.
Focus on actionable insights for business expense management.
"""
try:
print("π Querying indexed data for anomalies...")
query_engine = self.index.as_query_engine(
response_mode="tree_summarize",
verbose=False
)
response = query_engine.query(query)
return str(response)
except Exception as e:
return f"β Error querying data: {e}\nπ‘ Ensure Modal models are deployed and accessible."
def query_insights(self, question: str) -> str:
"""
Query the indexed data for specific insights
Args:
question: Natural language question about the financial data
Returns:
LLM response with insights
"""
if self.index is None:
return "β No data indexed. Please upload and analyze a CSV file first, then ensure Modal models are deployed."
if Settings.llm is None:
return "β Modal LLM not available. Please deploy Modal functions: modal deploy modal_functions.py"
try:
print(f"π¬ Answering question: {question}")
query_engine = self.index.as_query_engine(
response_mode="compact",
verbose=False
)
response = query_engine.query(question)
return str(response)
except Exception as e:
return f"β Error querying data: {e}\nπ‘ Ensure Modal models are deployed and accessible."
def get_index_stats(self) -> Dict:
"""
Get statistics about the current index
Returns:
Dictionary with index statistics
"""
if self.index is None:
return {"status": "No index created"}
try:
return {
"status": "Index ready",
"document_count": len(self.index.docstore.docs),
"data_rows": len(self.df) if self.df is not None else 0,
"date_range": {
"start": self.df['date'].min().strftime('%Y-%m-%d') if self.df is not None else None,
"end": self.df['date'].max().strftime('%Y-%m-%d') if self.df is not None else None
} if self.df is not None else None
}
except Exception as e:
return {"status": f"Error getting stats: {e}"}
# Global indexer instance
_indexer = None
def get_indexer() -> LedgerIndexer:
"""Get or create the global indexer instance"""
global _indexer
if _indexer is None:
_indexer = LedgerIndexer()
return _indexer
def index_dataframe(df: pd.DataFrame) -> bool:
"""Convenience function to index a DataFrame"""
indexer = get_indexer()
return indexer.index_ledger_data(df)
def query_financial_anomalies(custom_query: str = None) -> str:
"""Convenience function to query for anomalies"""
indexer = get_indexer()
return indexer.query_anomalies(custom_query)
def query_financial_insights(question: str) -> str:
"""Convenience function to query for insights"""
indexer = get_indexer()
return indexer.query_insights(question) |