Add AI rules support for document processing and matching; enhance tax analysis with flag_for_review and auto_approve fields

This commit is contained in:
bolade
2025-10-08 00:12:09 +01:00
parent f582110674
commit 2e020437a8
5 changed files with 394 additions and 49 deletions
+39 -37
View File
@@ -1,8 +1,9 @@
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
from schemas import Match, Receipt, Transaction
from services.ai_matcher import AIMatcher
from services.ai_rules import AIRulesEngine
from services.ai_rules_matcher import AIRulesMatcher
from services.feedback_logger import FeedbackLogger
from services.llm_tax_analyzer import LLMTaxAnalyzer
from services.manual_tax_calculator import ManualTaxCalculator
@@ -15,6 +16,7 @@ class MatchingEngine:
self.feedback_logger = FeedbackLogger()
self.llm_tax_analyzer = LLMTaxAnalyzer()
self.manual_tax_calculator = ManualTaxCalculator()
self.ai_rules_matcher = AIRulesMatcher()
self.use_manual_tax_calculator = use_manual_tax_calculator
def process_matching(
@@ -22,51 +24,51 @@ class MatchingEngine:
receipts: List[Receipt],
transactions: List[Transaction],
user_location: str = "ON",
ai_rules: Optional[List[Dict]] = None,
) -> List[Match]:
# Get AI matches
ai_matches = self.ai_matcher.match_receipts_to_transactions(
receipts, transactions
)
# Apply traditional rules first (lightweight, no API calls)
for match in ai_matches:
rule_results = self.rules_engine.apply_rules(
match.receipt, match.transaction
)
# # Apply traditional rules first (lightweight, no API calls)
# for match in ai_matches:
# rule_results = self.rules_engine.apply_rules(
# match.receipt, match.transaction
# )
# Apply confidence boost from traditional rules
if rule_results["confidence_boost"] > 0:
match.confidence_score = min(
1.0, match.confidence_score + rule_results["confidence_boost"]
)
# Auto-approve if rules say so
if rule_results["auto_approve"]:
match.confidence_score = 1.0
match.match_reason += " (Auto-approved by rules)"
# Apply tax analysis - use manual calculator or LLM based on configuration
if self.use_manual_tax_calculator:
# Use deterministic rule-based calculator
enhanced_matches = self._apply_manual_tax_analysis(
ai_matches, user_location
)
# else:
# # Use LLM-based tax analysis in a SINGLE batch call
# try:
# enhanced_matches = (
# self.llm_tax_analyzer.analyze_and_apply_tax_rules_batch(
# ai_matches, user_location
# )
# # Apply confidence boost from traditional rules
# if rule_results["confidence_boost"] > 0:
# match.confidence_score = min(
# 1.0, match.confidence_score + rule_results["confidence_boost"]
# )
# except Exception as e:
# # If batch LLM analysis fails, log it and continue with matches as-is
# import logging
# logging.error(f"Batch LLM tax analysis failed: {str(e)}")
# for match in ai_matches:
# match.match_reason += " (Note: Advanced tax analysis unavailable)"
# enhanced_matches = ai_matches
# # Auto-approve if rules say so
# if rule_results["auto_approve"]:
# match.confidence_score = 1.0
# match.match_reason += " (Auto-approved by rules)"
# # Apply tax analysis - use manual calculator or LLM based on configuration
# if self.use_manual_tax_calculator:
# # Use deterministic rule-based calculator
# enhanced_matches = self._apply_manual_tax_analysis(
# ai_matches, user_location
# )
# else:
# # No tax analysis, just use the matches as-is
# enhanced_matches = ai_matches
# Apply AI rules for post-matching evaluation
# This adds flag_for_review and auto_approve fields based on custom rules
if ai_rules:
enhanced_matches = self.ai_rules_matcher.apply_rules_to_matches(
ai_matches, ai_rules
)
else:
# Even without custom rules, apply built-in rules (e.g., currency mismatch)
enhanced_matches = self.ai_rules_matcher.apply_rules_to_matches(
ai_matches, None
)
return enhanced_matches