/** * AI Personalization Module * * Provides AI-powered personalization features including content recommendations, * user profiling, sentiment analysis, content classification, and summarization. */ import type { AIResult } from './types' import { anthropicApiVersion } from './drivers/anthropic' import { DEFAULT_ANTHROPIC_MODEL } from './models' // ============================================================================ // Types // ============================================================================ export interface UserProfile { id: string preferences: Record interactions: Interaction[] segments: string[] metadata?: Record } export interface Interaction { type: 'view' | 'click' | 'purchase' | 'like' | 'dislike' | 'share' | 'bookmark' | 'custom' itemId: string timestamp: number weight?: number metadata?: Record } export interface ContentItem { id: string content: string category?: string tags?: string[] metadata?: Record } export interface RecommendationOptions { provider?: 'anthropic' | 'openai' | 'ollama' model?: string maxTokens?: number temperature?: number limit?: number } export interface RecommendationResult { recommendations: Array<{ itemId: string score: number reason: string }> model: string provider: string } export interface SentimentResult { sentiment: 'positive' | 'negative' | 'neutral' | 'mixed' score: number confidence: number aspects?: Array<{ aspect: string sentiment: 'positive' | 'negative' | 'neutral' score: number }> } export interface ClassificationResult { label: string confidence: number allLabels: Array<{ label: string confidence: number }> } export interface SummaryOptions { provider?: 'anthropic' | 'openai' | 'ollama' model?: string maxLength?: number style?: 'concise' | 'detailed' | 'bullet-points' language?: string } // ============================================================================ // Sentiment Analysis // ============================================================================ /** * Analyze the sentiment of text using AI. */ export async function analyzeSentiment( text: string, options: { provider?: 'anthropic' | 'openai' | 'ollama' model?: string aspects?: string[] } = {}, ): Promise { const { provider = 'anthropic', aspects } = options const aspectInstruction = aspects ? `\nAlso analyze sentiment for these specific aspects: ${aspects.join(', ')}` : '' const systemPrompt = `You are a sentiment analysis expert. Analyze the sentiment of the given text and respond with ONLY valid JSON in this exact format: { "sentiment": "positive" | "negative" | "neutral" | "mixed", "score": , "confidence": ${aspects ? `, "aspects": [{"aspect": "", "sentiment": "positive" | "negative" | "neutral", "score": }]` : ''} }${aspectInstruction}` const result = await callProvider(provider, systemPrompt, text, options.model) try { return JSON.parse(result.content) as SentimentResult } catch { // Fallback: extract sentiment from text response const lower = result.content.toLowerCase() const isPositive = lower.includes('positive') const isNegative = lower.includes('negative') return { sentiment: isPositive && isNegative ? 'mixed' : isPositive ? 'positive' : isNegative ? 'negative' : 'neutral', score: isPositive ? 0.5 : isNegative ? -0.5 : 0, confidence: 0.5, } } } // ============================================================================ // Text Classification // ============================================================================ /** * Classify text into one of the provided labels. */ export async function classifyText( text: string, labels: string[], options: { provider?: 'anthropic' | 'openai' | 'ollama' model?: string multiLabel?: boolean } = {}, ): Promise { const { provider = 'anthropic', multiLabel = false } = options const systemPrompt = `You are a text classification expert. Classify the given text into ${multiLabel ? 'one or more of' : 'exactly one of'} these categories: ${labels.join(', ')}. Respond with ONLY valid JSON in this exact format: { "label": "", "confidence": , "allLabels": [{"label": "