Implement Phase 1: AI-powered categorization with manual review
Add AI categorization service that suggests categories, merchants, and
rules for uncategorized transactions. Users can review and approve
suggestions before applying them.
Features:
- TransactionAICategorizer service using OpenAI GPT-4o-mini
- Batch processing (5 transactions at a time) to avoid rate limits
- Confidence scoring (0-100%) for each suggestion
- AI suggests category, canonical merchant name, and pattern rule
- ReviewAISuggestions page to list uncategorized transactions
- ReviewAISuggestionsWithProposals page for manual review
- Apply individual suggestions or bulk apply high confidence (≥80%)
- Optional rule creation for future auto-categorization
- Cost: ~$0.00015 per transaction (~$0.015 per 100)
CategoryMapping enhancements:
- Confidence field to track AI confidence score
- CreatedBy field ("AI" or "User") to track rule origin
- CreatedAt timestamp for audit trail
Updated ARCHITECTURE.md with complete documentation of:
- TransactionAICategorizer service details
- ReviewAISuggestions page descriptions
- AI categorization workflow (Phase 1)
- Updated CategoryMappings schema
Next steps (Phase 2):
- Auto-apply high confidence suggestions
- Background job processing
- Batch API requests for better efficiency
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
120
ARCHITECTURE.md
120
ARCHITECTURE.md
@@ -347,6 +347,47 @@ Pattern-based rules for auto-categorization with merchant linking.
|
||||
|
||||
**Location:** Services/OpenAIReceiptParser.cs:23-302
|
||||
|
||||
### TransactionAICategorizer (Services/TransactionAICategorizer.cs)
|
||||
**Interface:** `ITransactionAICategorizer`
|
||||
|
||||
**Responsibility:** AI-powered categorization for uncategorized transactions.
|
||||
|
||||
**Key Methods:**
|
||||
- `ProposeCategorizationAsync(Transaction transaction)`
|
||||
- Analyzes transaction details (name, memo, amount, date)
|
||||
- Calls OpenAI GPT-4o-mini with categorization prompt
|
||||
- Returns `AICategoryProposal` with category, merchant, pattern, and confidence
|
||||
- Auto-suggests rule creation for high confidence (≥70%)
|
||||
|
||||
- `ProposeBatchCategorizationAsync(List<Transaction> transactions)`
|
||||
- Processes transactions in batches of 5 to avoid rate limits
|
||||
- Returns list of proposals for review
|
||||
|
||||
- `ApplyProposalAsync(long transactionId, AICategoryProposal proposal, bool createRule)`
|
||||
- Updates transaction category and merchant
|
||||
- Optionally creates CategoryMapping rule for future auto-categorization
|
||||
- Returns `ApplyProposalResult` with success status
|
||||
|
||||
**API Configuration:**
|
||||
- Model: `gpt-4o-mini`
|
||||
- Temperature: 0.1 (deterministic)
|
||||
- Max tokens: 300
|
||||
- API key: Environment variable `OPENAI_API_KEY` or config `OpenAI:ApiKey`
|
||||
- Cost: ~$0.00015 per transaction (~$0.015 per 100 transactions)
|
||||
|
||||
**Prompt Strategy:**
|
||||
- Provides transaction details (name, memo, amount, date)
|
||||
- Requests JSON response with category, canonical_merchant, pattern, confidence, reasoning
|
||||
- Includes common category examples for context
|
||||
- High confidence threshold (≥70%) suggests automatic rule creation
|
||||
|
||||
**CategoryMapping Enhancements:**
|
||||
- `Confidence` (decimal?) - AI confidence score (0.0-1.0)
|
||||
- `CreatedBy` (string?) - "AI" or "User"
|
||||
- `CreatedAt` (DateTime?) - Rule creation timestamp
|
||||
|
||||
**Location:** Services/TransactionAICategorizer.cs
|
||||
|
||||
## Data Access Layer
|
||||
|
||||
### MoneyMapContext (Data/MoneyMapContext.cs)
|
||||
@@ -505,6 +546,38 @@ EF Core DbContext managing all database entities.
|
||||
|
||||
**Location:** Pages/Recategorize.cshtml.cs:16-87
|
||||
|
||||
### ReviewAISuggestions.cshtml / ReviewAISuggestionsModel
|
||||
**Route:** `/ReviewAISuggestions`
|
||||
|
||||
**Purpose:** AI-powered categorization suggestions for uncategorized transactions.
|
||||
|
||||
**Features:**
|
||||
- Lists up to 50 most recent uncategorized transactions
|
||||
- Generate AI suggestions button (processes up to 20 at a time)
|
||||
- Cost transparency (~$0.00015 per transaction)
|
||||
- Link to view uncategorized transactions
|
||||
|
||||
**Dependencies:** `ITransactionAICategorizer`
|
||||
|
||||
**Location:** Pages/ReviewAISuggestions.cshtml.cs
|
||||
|
||||
### ReviewAISuggestionsWithProposals.cshtml / ReviewAISuggestionsWithProposalsModel
|
||||
**Route:** `/ReviewAISuggestionsWithProposals`
|
||||
|
||||
**Purpose:** Review and apply AI categorization proposals.
|
||||
|
||||
**Features:**
|
||||
- Display AI proposals with confidence scores
|
||||
- Color-coded confidence indicators (green ≥80%, yellow 60-79%, red <60%)
|
||||
- Individual actions: Accept (with/without rule), Reject, Edit Manually
|
||||
- Bulk action: Apply all high-confidence suggestions (≥80%)
|
||||
- Shows AI reasoning for each suggestion
|
||||
- Stores proposals in session for review workflow
|
||||
|
||||
**Dependencies:** `ITransactionAICategorizer`
|
||||
|
||||
**Location:** Pages/ReviewAISuggestionsWithProposals.cshtml.cs
|
||||
|
||||
## Configuration
|
||||
|
||||
### appsettings.json
|
||||
@@ -648,6 +721,9 @@ Category (nvarchar(max), NOT NULL)
|
||||
Pattern (nvarchar(max), NOT NULL)
|
||||
MerchantId (int, FK → Merchants.Id, SET NULL)
|
||||
Priority (int, NOT NULL, DEFAULT 0)
|
||||
Confidence (decimal(18,2), NULL) -- AI confidence score
|
||||
CreatedBy (nvarchar(max), NULL) -- "AI" or "User"
|
||||
CreatedAt (datetime2, NULL) -- Rule creation timestamp
|
||||
```
|
||||
|
||||
## Key Workflows
|
||||
@@ -740,6 +816,50 @@ Return DashboardData DTO
|
||||
Render dashboard view
|
||||
```
|
||||
|
||||
### 5. AI-Powered Categorization (Phase 1 - Manual Review)
|
||||
|
||||
```
|
||||
User visits /ReviewAISuggestions
|
||||
↓
|
||||
ReviewAISuggestionsModel.OnGetAsync()
|
||||
- Loads up to 50 recent uncategorized transactions
|
||||
↓
|
||||
User clicks "Generate AI Suggestions"
|
||||
↓
|
||||
ReviewAISuggestionsModel.OnPostGenerateSuggestionsAsync()
|
||||
- Fetches up to 20 uncategorized transactions
|
||||
- Calls TransactionAICategorizer.ProposeBatchCategorizationAsync()
|
||||
↓
|
||||
For each transaction (batches of 5):
|
||||
TransactionAICategorizer.ProposeCategorizationAsync()
|
||||
- Builds prompt with transaction details
|
||||
- Calls OpenAI GPT-4o-mini API
|
||||
- Parses JSON response
|
||||
- Returns AICategoryProposal
|
||||
↓
|
||||
Store proposals in session
|
||||
Redirect to /ReviewAISuggestionsWithProposals
|
||||
↓
|
||||
User reviews proposals with confidence scores
|
||||
↓
|
||||
User actions:
|
||||
Option A: Apply + Create Rule
|
||||
- Updates transaction category and merchant
|
||||
- Creates CategoryMapping rule (CreatedBy="AI")
|
||||
- Future similar transactions auto-categorized
|
||||
Option B: Apply (No Rule)
|
||||
- Updates transaction only
|
||||
- No rule created
|
||||
Option C: Reject
|
||||
- Removes proposal from session
|
||||
Option D: Edit Manually
|
||||
- Redirects to EditTransaction page
|
||||
↓
|
||||
Proposal applied
|
||||
Remove from session
|
||||
Display success message
|
||||
```
|
||||
|
||||
## Design Patterns
|
||||
|
||||
### 1. Service Layer Pattern
|
||||
|
||||
Reference in New Issue
Block a user