7.5 KiB
7.5 KiB
MOPC AI System Architecture
Overview
The MOPC platform uses AI (OpenAI GPT models) for four core functions:
- Project Filtering - Automated eligibility screening against admin-defined criteria
- Jury Assignment - Smart juror-project matching based on expertise alignment
- Award Eligibility - Special award qualification determination
- Mentor Matching - Mentor-project recommendations based on expertise
System Architecture
┌─────────────────────────────────────────────────────────────────┐
│ ADMIN INTERFACE │
│ (Rounds, Filtering, Awards, Assignments, Mentor Assignment) │
└─────────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ tRPC ROUTERS │
│ filtering.ts │ assignment.ts │ specialAward.ts │ mentor.ts │
└─────────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ AI SERVICES │
│ ai-filtering.ts │ ai-assignment.ts │ ai-award-eligibility.ts │
│ │ mentor-matching.ts │
└─────────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ ANONYMIZATION LAYER │
│ anonymization.ts │
│ - PII stripping - ID replacement - Text sanitization │
└─────────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ OPENAI CLIENT │
│ lib/openai.ts │
│ - Model detection - Parameter building - Token tracking │
└─────────────────────────┬───────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ OPENAI API │
│ GPT-4o │ GPT-4o-mini │ o1 │ o3-mini (configurable) │
└─────────────────────────────────────────────────────────────────┘
Data Flow
- Admin triggers AI action (filter projects, suggest assignments)
- Router validates permissions and fetches data from database
- AI Service prepares data for processing
- Anonymization Layer strips PII, replaces IDs, sanitizes text
- OpenAI Client builds request with correct parameters for model type
- Request sent to OpenAI API
- Response parsed and de-anonymized
- Results stored in database, usage logged
- UI updated with results
Key Components
OpenAI Client (lib/openai.ts)
Handles communication with OpenAI API:
getOpenAI()- Get configured OpenAI clientgetConfiguredModel()- Get the admin-selected modelbuildCompletionParams()- Build API parameters (handles reasoning vs standard models)isReasoningModel()- Detect o1/o3/o4 series models
Anonymization Service (server/services/anonymization.ts)
GDPR-compliant data preparation:
anonymizeForAI()- Basic anonymization for assignmentanonymizeProjectsForAI()- Comprehensive project anonymization for filtering/awardsvalidateAnonymization()- Verify no PII in anonymized datadeanonymizeResults()- Map AI results back to real IDs
Token Tracking (server/utils/ai-usage.ts)
Cost and usage monitoring:
logAIUsage()- Log API calls to databasecalculateCost()- Compute estimated cost by modelgetAIUsageStats()- Retrieve usage statisticsgetCurrentMonthCost()- Get current billing period totals
Error Handling (server/services/ai-errors.ts)
Unified error classification:
classifyAIError()- Categorize API errorsshouldRetry()- Determine if error is retryablegetUserFriendlyMessage()- Get human-readable error messages
Batching Strategy
All AI services process data in batches to avoid token limits:
| Service | Batch Size | Reason |
|---|---|---|
| AI Assignment | 15 projects | Include all jurors per batch |
| AI Filtering | 20 projects | Balance throughput and cost |
| Award Eligibility | 20 projects | Consistent with filtering |
| Mentor Matching | 15 projects | All mentors per batch |
Fallback Behavior
All AI services have algorithmic fallbacks when AI is unavailable:
- Assignment - Expertise tag matching + load balancing
- Filtering - Flag all projects for manual review
- Award Eligibility - Flag all for manual review
- Mentor Matching - Keyword-based matching algorithm
Security Considerations
- API keys stored encrypted in database
- No PII sent to OpenAI (enforced by anonymization)
- Audit logging of all AI operations
- Role-based access to AI features (admin only)
Files Reference
| File | Purpose |
|---|---|
lib/openai.ts |
OpenAI client configuration |
server/services/ai-filtering.ts |
Project filtering service |
server/services/ai-assignment.ts |
Jury assignment service |
server/services/ai-award-eligibility.ts |
Award eligibility service |
server/services/mentor-matching.ts |
Mentor matching service |
server/services/anonymization.ts |
Data anonymization |
server/services/ai-errors.ts |
Error classification |
server/utils/ai-usage.ts |
Token tracking |