File size: 48,716 Bytes
2ec0d39 |
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 |
"""
Contextual Personalization & User Profiling System
===============================================
Advanced user profiling system that builds user-specific contextual profiles,
continuously updates them based on interactions, and enables behavioral adaptation.
"""
import asyncio
import json
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional, Set, Tuple, Union
from dataclasses import dataclass, field, asdict
from enum import Enum
import numpy as np
from collections import defaultdict, deque
import hashlib
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.cluster import DBSCAN
from sklearn.decomposition import PCA
from ai_agent_framework.core.context_engineering_agent import (
ContextElement, ContextModality, ContextDimension, ContextEngineeringAgent
)
logger = logging.getLogger(__name__)
class ProfileType(Enum):
"""Types of user profiles."""
BEHAVIORAL = "behavioral"
PREFERENTIAL = "preferential"
CONTEXTUAL = "contextual"
INTERACTION = "interaction"
LEARNING = "learning"
COLLABORATIVE = "collaborative"
TEMPORAL = "temporal"
class LearningType(Enum):
"""Types of learning patterns."""
GRADUAL = "gradual"
RAPID = "rapid"
CYCLICAL = "cyclical"
EVENT_DRIVEN = "event_driven"
ADAPTIVE = "adaptive"
@dataclass
class UserInteraction:
"""Represents a user interaction with the system."""
interaction_id: str
user_id: str
interaction_type: str
content: Dict[str, Any]
context: Dict[str, Any]
timestamp: datetime
duration: float
success: bool
satisfaction_score: Optional[float] = None
adaptation_needed: bool = False
def __post_init__(self):
if not self.interaction_id:
self.interaction_id = f"interaction_{int(time.time())}_{hash(str(self.content))}"
if not self.timestamp:
self.timestamp = datetime.utcnow()
@dataclass
class UserPreference:
"""Represents a user preference."""
preference_id: str
user_id: str
category: str
preference_type: str
value: Any
confidence: float
stability: float
last_updated: datetime
evidence_count: int
def __post_init__(self):
if not self.preference_id:
self.preference_id = f"pref_{self.user_id}_{self.category}_{hash(str(self.value))}"
if not self.last_updated:
self.last_updated = datetime.utcnow()
@dataclass
class ContextualPattern:
"""Represents a contextual usage pattern."""
pattern_id: str
user_id: str
pattern_type: str
context_elements: Set[str]
frequency: float
success_rate: float
last_observed: datetime
confidence: float
def __post_init__(self):
if not self.pattern_id:
self.pattern_id = f"pattern_{self.user_id}_{self.pattern_type}_{int(time.time())}"
@dataclass
class UserProfile:
"""Comprehensive user profile for contextual personalization."""
user_id: str
profile_type: ProfileType
data: Dict[str, Any]
created_at: datetime
updated_at: datetime
version: int
completeness_score: float
confidence_score: float
def __post_init__(self):
if not self.created_at:
self.created_at = datetime.utcnow()
if not self.updated_at:
self.updated_at = self.created_at
if self.version == 0:
self.version = 1
class ContextualPersonalizationEngine:
"""Core engine for contextual personalization and user profiling."""
def __init__(self):
self.user_profiles = {} # user_id -> dict of profile_type -> UserProfile
self.interaction_history = {} # user_id -> List[UserInteraction]
self.preference_database = {} # user_id -> List[UserPreference]
self.pattern_database = {} # user_id -> List[ContextualPattern]
self.profile_weights = {
ProfileType.BEHAVIORAL: 0.25,
ProfileType.PREFERENTIAL: 0.20,
ProfileType.CONTEXTUAL: 0.20,
ProfileType.INTERACTION: 0.15,
ProfileType.LEARNING: 0.10,
ProfileType.COLLABORATIVE: 0.05,
ProfileType.TEMPORAL: 0.05
}
self.learning_algorithms = {
"incremental": self._incremental_learning,
"batch": self._batch_learning,
"reinforcement": self._reinforcement_learning,
"association": self._association_learning
}
self.adaptation_strategies = {
"gradual": self._gradual_adaptation,
"immediate": self._immediate_adaptation,
"predictive": self._predictive_adaptation
}
async def process_user_interaction(self, interaction: UserInteraction) -> Dict[str, Any]:
"""Process a new user interaction and update profiles."""
try:
# Step 1: Extract insights from interaction
insights = await self._extract_interaction_insights(interaction)
# Step 2: Update relevant profiles
updated_profiles = await self._update_profiles_from_interaction(interaction, insights)
# Step 3: Identify new patterns
new_patterns = await self._identify_contextual_patterns(interaction, insights)
# Step 4: Update preferences
updated_preferences = await self._update_preferences(interaction, insights)
# Step 5: Generate adaptation recommendations
adaptations = await self._generate_adaptation_recommendations(
interaction, updated_profiles, insights
)
return {
"interaction_id": interaction.interaction_id,
"insights": insights,
"updated_profiles": updated_profiles,
"new_patterns": [asdict(pattern) for pattern in new_patterns],
"updated_preferences": updated_preferences,
"adaptation_recommendations": adaptations,
"processing_success": True
}
except Exception as e:
logger.error(f"Failed to process user interaction: {e}")
return {
"interaction_id": interaction.interaction_id,
"error": str(e),
"processing_success": False
}
async def build_user_profile(
self,
user_id: str,
profile_type: ProfileType,
include_interaction_history: bool = True
) -> UserProfile:
"""Build a comprehensive user profile."""
# Get available data for the user
interaction_data = self.interaction_history.get(user_id, [])
preference_data = self.preference_database.get(user_id, [])
pattern_data = self.pattern_database.get(user_id, [])
if not interaction_data and not preference_data:
return await self._create_empty_profile(user_id, profile_type)
# Build profile based on type
if profile_type == ProfileType.BEHAVIORAL:
return await self._build_behavioral_profile(user_id, interaction_data, preference_data)
elif profile_type == ProfileType.PREFERENTIAL:
return await self._build_preferential_profile(user_id, preference_data, interaction_data)
elif profile_type == ProfileType.CONTEXTUAL:
return await self._build_contextual_profile(user_id, interaction_data, pattern_data)
elif profile_type == ProfileType.INTERACTION:
return await self._build_interaction_profile(user_id, interaction_data)
elif profile_type == ProfileType.LEARNING:
return await self._build_learning_profile(user_id, interaction_data)
elif profile_type == ProfileType.COLLABORATIVE:
return await self._build_collaborative_profile(user_id, interaction_data, pattern_data)
elif profile_type == ProfileType.TEMPORAL:
return await self._build_temporal_profile(user_id, interaction_data, pattern_data)
else:
return await self._build_generic_profile(user_id, profile_type, interaction_data)
async def update_cross_session_context(
self,
user_id: str,
current_context: Dict[str, Any],
session_duration: float
) -> Dict[str, Any]:
"""Update and maintain cross-session context continuity."""
# Get or create user profile
profile = await self.get_user_profile(user_id, ProfileType.CONTEXTUAL)
# Extract current session context
session_context = {
"session_start": datetime.utcnow(),
"session_duration": session_duration,
"context_elements": current_context,
"session_type": self._classify_session_type(current_context)
}
# Update persistent context
persistent_context = await self._update_persistent_context(
profile.data.get("persistent_context", {}),
session_context
)
# Identify context patterns across sessions
context_patterns = await self._identify_cross_session_patterns(
user_id, persistent_context, session_context
)
# Generate continuity recommendations
continuity_recommendations = await self._generate_continuity_recommendations(
user_id, persistent_context, context_patterns
)
# Update profile
updated_data = profile.data.copy()
updated_data.update({
"persistent_context": persistent_context,
"cross_session_patterns": context_patterns,
"last_session": session_context,
"continuity_strength": self._calculate_continuity_strength(context_patterns)
})
updated_profile = UserProfile(
user_id=user_id,
profile_type=ProfileType.CONTEXTUAL,
data=updated_data,
created_at=profile.created_at,
updated_at=datetime.utcnow(),
version=profile.version + 1,
completeness_score=self._calculate_profile_completeness(updated_data),
confidence_score=self._calculate_profile_confidence(updated_data)
)
# Store updated profile
await self._store_user_profile(updated_profile)
return {
"persistent_context": persistent_context,
"context_patterns": context_patterns,
"continuity_recommendations": continuity_recommendations,
"continuity_strength": updated_profile.data.get("continuity_strength", 0.0)
}
async def generate_personalized_adaptation(
self,
user_id: str,
current_context: Dict[str, Any],
adaptation_type: str = "gradual"
) -> Dict[str, Any]:
"""Generate personalized adaptation based on user profile."""
# Get relevant profiles
profiles = await self._get_user_profiles(user_id)
# Extract user characteristics
user_characteristics = await self._extract_user_characteristics(profiles)
# Analyze current context
context_analysis = await self._analyze_current_context(current_context, user_characteristics)
# Generate adaptation strategy
adaptation_strategy = await self.adaptation_strategies.get(
adaptation_type, self._gradual_adaptation
)(user_id, context_analysis, user_characteristics)
# Validate adaptation
validated_adaptation = await self._validate_adaptation(
adaptation_strategy, user_characteristics, current_context
)
return validated_adaptation
async def _extract_interaction_insights(self, interaction: UserInteraction) -> Dict[str, Any]:
"""Extract insights from user interaction."""
insights = {
"interaction_complexity": self._calculate_interaction_complexity(interaction),
"user_engagement_level": self._calculate_engagement_level(interaction),
"context_sensitivity": self._calculate_context_sensitivity(interaction),
"learning_velocity": self._calculate_learning_velocity(interaction),
"preference_signals": self._extract_preference_signals(interaction),
"behavioral_patterns": self._extract_behavioral_patterns(interaction),
"success_factors": self._identify_success_factors(interaction),
"adaptation_triggers": self._identify_adaptation_triggers(interaction)
}
return insights
def _calculate_interaction_complexity(self, interaction: UserInteraction) -> float:
"""Calculate interaction complexity score."""
content_complexity = 0.0
context_complexity = 0.0
duration_factor = min(1.0, interaction.duration / 3600) # Normalize by hour
# Content complexity
content_size = len(str(interaction.content))
if content_size > 10000:
content_complexity = 1.0
elif content_size > 1000:
content_complexity = 0.7
elif content_size > 100:
content_complexity = 0.4
else:
content_complexity = 0.2
# Context complexity
context_size = len(str(interaction.context))
if context_size > 5000:
context_complexity = 1.0
elif context_size > 500:
context_complexity = 0.7
elif context_size > 50:
context_complexity = 0.4
else:
context_complexity = 0.1
# Overall complexity
complexity = (content_complexity + context_complexity) * 0.4 + duration_factor * 0.2
return min(1.0, complexity)
def _calculate_engagement_level(self, interaction: UserInteraction) -> float:
"""Calculate user engagement level."""
# Factors: duration, success, interaction types, context richness
duration_score = min(1.0, interaction.duration / 1800) # 30 minutes max
success_score = 1.0 if interaction.success else 0.3
interaction_type_diversity = len(set(
interaction.content.get("interaction_types", [])
)) / 10 # Normalize
context_richness = len(interaction.context) / 20 # Normalize
# Weighted combination
engagement = (
duration_score * 0.3 +
success_score * 0.3 +
min(1.0, interaction_type_diversity) * 0.2 +
min(1.0, context_richness) * 0.2
)
return min(1.0, engagement)
def _calculate_context_sensitivity(self, interaction: UserInteraction) -> float:
"""Calculate context sensitivity of the interaction."""
# Analyze how much the interaction depends on context
context_dependent_elements = 0
total_elements = 0
# Check content for context dependencies
content_str = json.dumps(interaction.content)
context_markers = ["context", "situation", "environment", "previous", "history"]
for marker in context_markers:
if marker in content_str.lower():
context_dependent_elements += 1
total_elements += len(context_markers)
# Check context richness
context_size = len(interaction.context)
context_richness = min(1.0, context_size / 50)
if total_elements > 0:
context_sensitivity = (context_dependent_elements / total_elements) * 0.6 + context_richness * 0.4
else:
context_sensitivity = context_richness
return min(1.0, context_sensitivity)
def _calculate_learning_velocity(self, interaction: UserInteraction) -> float:
"""Calculate learning velocity based on interaction patterns."""
# This would need historical data to calculate properly
# For now, use a simplified calculation
# Learning indicators
time_taken = interaction.duration
success = interaction.success
adaptation_needed = interaction.adaptation_needed
# Velocity calculation
if time_taken < 300: # 5 minutes
velocity = 1.0 if success else 0.7
elif time_taken < 900: # 15 minutes
velocity = 0.8 if success else 0.5
else:
velocity = 0.6 if success else 0.3
# Adjust for adaptation need
if adaptation_needed:
velocity *= 0.8
return min(1.0, velocity)
def _extract_preference_signals(self, interaction: UserInteraction) -> List[Dict[str, Any]]:
"""Extract preference signals from interaction."""
signals = []
content = interaction.content
context = interaction.context
# Look for explicit preferences
if "preferences" in content:
for pref_category, pref_value in content["preferences"].items():
signals.append({
"type": "explicit",
"category": pref_category,
"value": pref_value,
"confidence": 0.9,
"timestamp": interaction.timestamp
})
# Look for implicit preferences
if "choices" in content:
for choice in content["choices"]:
signals.append({
"type": "implicit",
"category": choice.get("category", "unknown"),
"value": choice.get("selected", choice.get("value")),
"confidence": 0.7,
"timestamp": interaction.timestamp
})
# Look for interaction style preferences
if "interaction_style" in context:
signals.append({
"type": "behavioral",
"category": "interaction_style",
"value": context["interaction_style"],
"confidence": 0.8,
"timestamp": interaction.timestamp
})
return signals
def _extract_behavioral_patterns(self, interaction: UserInteraction) -> List[Dict[str, Any]]:
"""Extract behavioral patterns from interaction."""
patterns = []
# Time patterns
if "timestamp" in interaction.context:
hour = interaction.timestamp.hour
patterns.append({
"type": "temporal",
"pattern": f"active_at_hour_{hour}",
"strength": 1.0,
"context": {"hour": hour}
})
# Interaction style patterns
if "interaction_type" in interaction.content:
patterns.append({
"type": "interaction_style",
"pattern": f"prefers_{interaction.content['interaction_type']}",
"strength": 0.8,
"context": interaction.content["interaction_type"]
})
# Success pattern
if interaction.success:
patterns.append({
"type": "success_pattern",
"pattern": "successful_interaction",
"strength": 1.0,
"context": {"duration": interaction.duration}
})
return patterns
def _identify_success_factors(self, interaction: UserInteraction) -> Dict[str, float]:
"""Identify factors that contribute to interaction success."""
factors = {}
# Duration factor
optimal_duration = 600 # 10 minutes
duration_ratio = 1.0 - abs(interaction.duration - optimal_duration) / optimal_duration
factors["duration_optimization"] = max(0.0, duration_ratio)
# Context factor
context_richness = min(1.0, len(interaction.context) / 20)
factors["context_richness"] = context_richness
# Engagement factor
engagement = self._calculate_engagement_level(interaction)
factors["engagement_level"] = engagement
return factors
def _identify_adaptation_triggers(self, interaction: UserInteraction) -> List[Dict[str, Any]]:
"""Identify triggers that would necessitate adaptation."""
triggers = []
# Performance triggers
if not interaction.success:
triggers.append({
"type": "performance",
"trigger": "interaction_failed",
"severity": 0.8,
"timestamp": interaction.timestamp
})
# Engagement triggers
engagement = self._calculate_engagement_level(interaction)
if engagement < 0.5:
triggers.append({
"type": "engagement",
"trigger": "low_engagement",
"severity": 0.6,
"context": {"engagement": engagement}
})
# Duration triggers
if interaction.duration > 3600: # 1 hour
triggers.append({
"type": "duration",
"trigger": "prolonged_interaction",
"severity": 0.4,
"context": {"duration": interaction.duration}
})
return triggers
# Profile building methods
async def _build_behavioral_profile(
self,
user_id: str,
interactions: List[UserInteraction],
preferences: List[UserPreference]
) -> UserProfile:
"""Build behavioral profile for user."""
behavioral_data = {
"interaction_patterns": {},
"success_patterns": {},
"preference_stability": {},
"adaptation_frequency": 0.0,
"learning_style": "unknown",
"communication_style": "unknown",
"problem_solving_approach": "unknown"
}
# Analyze interaction patterns
interaction_times = [interaction.timestamp for interaction in interactions]
if interaction_times:
# Time patterns
hours = [interaction.timestamp.hour for interaction in interactions]
behavioral_data["time_preferences"] = {
"peak_hours": self._find_peak_hours(hours),
"session_duration_pattern": self._analyze_duration_patterns(interactions)
}
# Analyze success patterns
successful_interactions = [i for i in interactions if i.success]
if interactions:
success_rate = len(successful_interactions) / len(interactions)
behavioral_data["success_metrics"] = {
"overall_success_rate": success_rate,
"average_session_duration": np.mean([i.duration for i in interactions]),
"adaptation_frequency": np.mean([i.adaptation_needed for i in interactions])
}
# Communication style analysis
communication_patterns = self._analyze_communication_style(interactions)
behavioral_data.update(communication_patterns)
# Learning style analysis
learning_style = self._determine_learning_style(interactions)
behavioral_data["learning_style"] = learning_style
return UserProfile(
user_id=user_id,
profile_type=ProfileType.BEHAVIORAL,
data=behavioral_data,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
version=1,
completeness_score=self._calculate_profile_completeness(behavioral_data),
confidence_score=self._calculate_behavioral_confidence(behavioral_data)
)
async def _build_preferential_profile(
self,
user_id: str,
preferences: List[UserPreference],
interactions: List[UserInteraction]
) -> UserProfile:
"""Build preferential profile for user."""
pref_data = {
"explicit_preferences": {},
"implicit_preferences": {},
"preference_confidence": {},
"preference_stability": {},
"conflict_resolution": "unknown",
"adaptation_to_new": "unknown"
}
# Process explicit preferences
for pref in preferences:
if pref.confidence > 0.7: # High confidence preferences
if pref.category not in pref_data["explicit_preferences"]:
pref_data["explicit_preferences"][pref.category] = {}
pref_data["explicit_preferences"][pref.category][pref.preference_type] = {
"value": pref.value,
"confidence": pref.confidence,
"stability": pref.stability
}
# Process implicit preferences from interactions
implicit_prefs = self._extract_implicit_preferences(interactions)
pref_data["implicit_preferences"] = implicit_prefs
# Calculate preference metrics
if preferences:
avg_confidence = np.mean([p.confidence for p in preferences])
avg_stability = np.mean([p.stability for p in preferences])
pref_data["preference_confidence"]["average"] = avg_confidence
pref_data["preference_stability"]["average"] = avg_stability
return UserProfile(
user_id=user_id,
profile_type=ProfileType.PREFERENTIAL,
data=pref_data,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
version=1,
completeness_score=self._calculate_profile_completeness(pref_data),
confidence_score=self._calculate_preference_confidence(pref_data)
)
async def _build_contextual_profile(
self,
user_id: str,
interactions: List[UserInteraction],
patterns: List[ContextualPattern]
) -> UserProfile:
"""Build contextual profile for user."""
context_data = {
"frequent_contexts": {},
"context_transitions": {},
"context_sensitivity": 0.0,
"persistent_context": {},
"cross_session_patterns": [],
"context_evolution": {}
}
# Analyze frequent contexts
all_contexts = []
for interaction in interactions:
all_contexts.append(interaction.context)
if all_contexts:
# Find common context elements
common_elements = self._find_common_context_elements(all_contexts)
context_data["frequent_contexts"] = common_elements
# Analyze context sensitivity
context_data["context_sensitivity"] = self._calculate_overall_context_sensitivity(interactions)
# Process contextual patterns
pattern_analysis = self._analyze_contextual_patterns(patterns)
context_data.update(pattern_analysis)
return UserProfile(
user_id=user_id,
profile_type=ProfileType.CONTEXTUAL,
data=context_data,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
version=1,
completeness_score=self._calculate_profile_completeness(context_data),
confidence_score=self._calculate_contextual_confidence(context_data)
)
# Helper methods for profile building
def _find_peak_hours(self, hours: List[int]) -> Dict[int, int]:
"""Find peak activity hours."""
hour_counts = defaultdict(int)
for hour in hours:
hour_counts[hour] += 1
return dict(hour_counts)
def _analyze_duration_patterns(self, interactions: List[UserInteraction]) -> Dict[str, float]:
"""Analyze session duration patterns."""
durations = [interaction.duration for interaction in interactions]
return {
"average_duration": np.mean(durations),
"median_duration": np.median(durations),
"std_duration": np.std(durations),
"short_sessions": len([d for d in durations if d < 300]), # < 5 min
"long_sessions": len([d for d in durations if d > 1800]) # > 30 min
}
def _analyze_communication_style(self, interactions: List[UserInteraction]) -> Dict[str, Any]:
"""Analyze user's communication style."""
# Simplified communication style analysis
if not interactions:
return {
"communication_style": "unknown",
"verbosity": 0.0,
"formality": 0.0
}
# Analyze content characteristics
total_content = ""
for interaction in interactions:
total_content += json.dumps(interaction.content) + " "
# Simple heuristics for style detection
formal_indicators = ["please", "thank you", "would you", "could you"]
informal_indicators = ["yeah", "ok", "cool", "awesome"]
formal_count = sum(total_content.lower().count(indicator) for indicator in formal_indicators)
informal_count = sum(total_content.lower().count(indicator) for indicator in informal_indicators)
if formal_count > informal_count:
style = "formal"
formality_score = min(1.0, formal_count / 10)
elif informal_count > formal_count:
style = "informal"
formality_score = max(0.0, 1.0 - informal_count / 10)
else:
style = "neutral"
formality_score = 0.5
# Verbosity analysis
avg_content_size = np.mean([len(json.dumps(i.content)) for i in interactions])
verbosity_score = min(1.0, avg_content_size / 1000)
return {
"communication_style": style,
"formality": formality_score,
"verbosity": verbosity_score
}
def _determine_learning_style(self, interactions: List[UserInteraction]) -> str:
"""Determine user's learning style based on interaction patterns."""
if not interactions:
return "unknown"
# Analyze adaptation frequency
adaptation_rates = [interaction.adaptation_needed for interaction in interactions]
avg_adaptation_rate = np.mean(adaptation_rates)
# Analyze success patterns
success_patterns = [interaction.success for interaction in interactions]
success_improvement = self._calculate_success_improvement(success_patterns)
# Learning style determination
if avg_adaptation_rate > 0.7:
return "adaptive_learner"
elif success_improvement > 0.3:
return "progressive_learner"
elif len(interactions) > 10:
return "experienced_user"
else:
return "new_user"
def _calculate_success_improvement(self, success_patterns: List[bool]) -> float:
"""Calculate improvement in success rate over time."""
if len(success_patterns) < 5:
return 0.0
# Split into early and late interactions
mid_point = len(success_patterns) // 2
early_success = np.mean(success_patterns[:mid_point])
late_success = np.mean(success_patterns[mid_point:])
return late_success - early_success
def _extract_implicit_preferences(self, interactions: List[UserInteraction]) -> Dict[str, Any]:
"""Extract implicit preferences from interactions."""
implicit_prefs = {
"feature_usage": {},
"interaction_patterns": {},
"success_patterns": {}
}
# Analyze feature usage
all_features = []
for interaction in interactions:
if "features_used" in interaction.content:
all_features.extend(interaction.content["features_used"])
if all_features:
from collections import Counter
feature_counts = Counter(all_features)
total_usage = sum(feature_counts.values())
for feature, count in feature_counts.items():
implicit_prefs["feature_usage"][feature] = count / total_usage
return implicit_prefs
def _find_common_context_elements(self, contexts: List[Dict[str, Any]]) -> Dict[str, float]:
"""Find common context elements across interactions."""
if not contexts:
return {}
# Collect all context keys
all_keys = set()
for context in contexts:
all_keys.update(context.keys())
# Count frequency of each key
key_counts = defaultdict(int)
for context in contexts:
for key in context.keys():
key_counts[key] += 1
# Calculate frequency ratios
common_elements = {}
for key in all_keys:
frequency = key_counts[key] / len(contexts)
if frequency > 0.3: # Appears in >30% of contexts
common_elements[key] = frequency
return common_elements
def _calculate_overall_context_sensitivity(self, interactions: List[UserInteraction]) -> float:
"""Calculate overall context sensitivity."""
if not interactions:
return 0.0
sensitivities = []
for interaction in interactions:
sensitivity = self._calculate_context_sensitivity(interaction)
sensitivities.append(sensitivity)
return np.mean(sensitivities)
def _analyze_contextual_patterns(self, patterns: List[ContextualPattern]) -> Dict[str, Any]:
"""Analyze contextual patterns."""
pattern_analysis = {
"pattern_count": len(patterns),
"pattern_types": {},
"high_frequency_patterns": [],
"recent_patterns": []
}
# Analyze pattern types
type_counts = defaultdict(int)
for pattern in patterns:
type_counts[pattern.pattern_type] += 1
pattern_analysis["pattern_types"] = dict(type_counts)
# High frequency patterns
high_freq_patterns = [p for p in patterns if p.frequency > 0.5]
pattern_analysis["high_frequency_patterns"] = [
{
"type": p.pattern_type,
"frequency": p.frequency,
"confidence": p.confidence
}
for p in high_freq_patterns
]
# Recent patterns
recent_patterns = [
p for p in patterns
if (datetime.utcnow() - p.last_observed).days < 7
]
pattern_analysis["recent_patterns"] = [
{
"type": p.pattern_type,
"last_observed": p.last_observed.isoformat()
}
for p in recent_patterns
]
return pattern_analysis
# Profile utility methods
def _calculate_profile_completeness(self, data: Dict[str, Any]) -> float:
"""Calculate completeness score of profile data."""
if not data:
return 0.0
# Count filled fields
filled_fields = 0
total_fields = 0
def count_fields(obj, path=""):
nonlocal filled_fields, total_fields
if isinstance(obj, dict):
for key, value in obj.items():
new_path = f"{path}.{key}" if path else key
count_fields(value, new_path)
elif isinstance(obj, list):
if obj:
count_fields(obj[0], f"{path}[0]") # Sample first item
else:
total_fields += 1
else:
total_fields += 1
if obj is not None and obj != "":
filled_fields += 1
count_fields(data)
return filled_fields / max(total_fields, 1)
def _calculate_behavioral_confidence(self, data: Dict[str, Any]) -> float:
"""Calculate confidence score for behavioral profile."""
# Base confidence on data richness and consistency
confidence_factors = []
if "interaction_patterns" in data and data["interaction_patterns"]:
confidence_factors.append(0.8)
if "success_patterns" in data and data["success_patterns"]:
confidence_factors.append(0.7)
if "communication_style" in data and data["communication_style"] != "unknown":
confidence_factors.append(0.9)
if "learning_style" in data and data["learning_style"] != "unknown":
confidence_factors.append(0.8)
return np.mean(confidence_factors) if confidence_factors else 0.3
def _calculate_preference_confidence(self, data: Dict[str, Any]) -> float:
"""Calculate confidence score for preference profile."""
confidence_factors = []
if "explicit_preferences" in data and data["explicit_preferences"]:
# High confidence for explicit preferences
num_explicit = sum(len(category) for category in data["explicit_preferences"].values())
confidence_factors.append(min(1.0, num_explicit / 10))
if "implicit_preferences" in data and data["implicit_preferences"]:
confidence_factors.append(0.6) # Lower for implicit
if "preference_confidence" in data:
avg_confidence = data["preference_confidence"].get("average", 0)
confidence_factors.append(avg_confidence)
return np.mean(confidence_factors) if confidence_factors else 0.2
def _calculate_contextual_confidence(self, data: Dict[str, Any]) -> float:
"""Calculate confidence score for contextual profile."""
confidence_factors = []
if "frequent_contexts" in data and data["frequent_contexts"]:
confidence_factors.append(0.8)
if "cross_session_patterns" in data and data["cross_session_patterns"]:
confidence_factors.append(0.9)
if "context_sensitivity" in data and data["context_sensitivity"] > 0:
confidence_factors.append(data["context_sensitivity"])
return np.mean(confidence_factors) if confidence_factors else 0.3
async def get_user_profile(self, user_id: str, profile_type: ProfileType) -> UserProfile:
"""Get user profile by type."""
if user_id not in self.user_profiles:
return await self._create_empty_profile(user_id, profile_type)
user_profiles = self.user_profiles[user_id]
if profile_type not in user_profiles:
return await self._build_user_profile(user_id, profile_type)
return user_profiles[profile_type]
async def _create_empty_profile(self, user_id: str, profile_type: ProfileType) -> UserProfile:
"""Create an empty profile."""
return UserProfile(
user_id=user_id,
profile_type=profile_type,
data={},
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
version=1,
completeness_score=0.0,
confidence_score=0.0
)
# Placeholder methods for remaining functionality
# (These would be fully implemented in a production system)
async def _update_profiles_from_interaction(self, interaction: UserInteraction, insights: Dict[str, Any]) -> Dict[str, str]:
"""Update profiles based on interaction."""
# Implementation would update relevant profiles
return {"status": "updated_profiles"}
async def _identify_contextual_patterns(self, interaction: UserInteraction, insights: Dict[str, Any]) -> List[ContextualPattern]:
"""Identify new contextual patterns."""
# Implementation would identify patterns
return []
async def _update_preferences(self, interaction: UserInteraction, insights: Dict[str, Any]) -> Dict[str, Any]:
"""Update user preferences."""
# Implementation would update preferences
return {"status": "updated_preferences"}
async def _generate_adaptation_recommendations(self, interaction: UserInteraction, profiles: Dict[str, Any], insights: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Generate adaptation recommendations."""
# Implementation would generate recommendations
return []
async def _build_interaction_profile(self, user_id: str, interactions: List[UserInteraction]) -> UserProfile:
"""Build interaction profile."""
return await self._create_empty_profile(user_id, ProfileType.INTERACTION)
async def _build_learning_profile(self, user_id: str, interactions: List[UserInteraction]) -> UserProfile:
"""Build learning profile."""
return await self._create_empty_profile(user_id, ProfileType.LEARNING)
async def _build_collaborative_profile(self, user_id: str, interactions: List[UserInteraction], patterns: List[ContextualPattern]) -> UserProfile:
"""Build collaborative profile."""
return await self._create_empty_profile(user_id, ProfileType.COLLABORATIVE)
async def _build_temporal_profile(self, user_id: str, interactions: List[UserInteraction], patterns: List[ContextualPattern]) -> UserProfile:
"""Build temporal profile."""
return await self._create_empty_profile(user_id, ProfileType.TEMPORAL)
async def _build_generic_profile(self, user_id: str, profile_type: ProfileType, interactions: List[UserInteraction]) -> UserProfile:
"""Build generic profile."""
return await self._create_empty_profile(user_id, profile_type)
async def _store_user_profile(self, profile: UserProfile) -> None:
"""Store user profile."""
if profile.user_id not in self.user_profiles:
self.user_profiles[profile.user_id] = {}
self.user_profiles[profile.user_id][profile.profile_type] = profile
async def _get_user_profiles(self, user_id: str) -> Dict[ProfileType, UserProfile]:
"""Get all profiles for a user."""
return self.user_profiles.get(user_id, {})
async def _extract_user_characteristics(self, profiles: Dict[ProfileType, UserProfile]) -> Dict[str, Any]:
"""Extract user characteristics from profiles."""
characteristics = {}
for profile_type, profile in profiles.items():
weight = self.profile_weights.get(profile_type, 0.1)
characteristics[profile_type.value] = {
"data": profile.data,
"confidence": profile.confidence_score,
"weight": weight
}
return characteristics
async def _analyze_current_context(self, context: Dict[str, Any], characteristics: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze current context in relation to user characteristics."""
# Simplified analysis
return {
"context_relevance": 0.8,
"adaptation_need": 0.5,
"personalization_opportunities": ["style", "preferences"]
}
# Adaptation strategy methods
async def _gradual_adaptation(self, user_id: str, context_analysis: Dict[str, Any], characteristics: Dict[str, Any]) -> Dict[str, Any]:
"""Gradual adaptation strategy."""
return {
"strategy": "gradual",
"adaptation_rate": 0.1,
"target_aspects": ["communication_style", "interaction_pace"],
"timeline": "multiple_sessions"
}
async def _immediate_adaptation(self, user_id: str, context_analysis: Dict[str, Any], characteristics: Dict[str, Any]) -> Dict[str, Any]:
"""Immediate adaptation strategy."""
return {
"strategy": "immediate",
"adaptation_rate": 0.8,
"target_aspects": ["user_preferences", "interface_layout"],
"timeline": "current_session"
}
async def _predictive_adaptation(self, user_id: str, context_analysis: Dict[str, Any], characteristics: Dict[str, Any]) -> Dict[str, Any]:
"""Predictive adaptation strategy."""
return {
"strategy": "predictive",
"adaptation_rate": 0.3,
"target_aspects": ["upcoming_needs", "anticipated_preferences"],
"timeline": "future_sessions"
}
async def _validate_adaptation(self, adaptation: Dict[str, Any], characteristics: Dict[str, Any], context: Dict[str, Any]) -> Dict[str, Any]:
"""Validate adaptation strategy."""
# Simplified validation
validated_adaptation = adaptation.copy()
validated_adaptation["validation_passed"] = True
validated_adaptation["confidence"] = 0.8
return validated_adaptation
# Context continuity methods
def _classify_session_type(self, context: Dict[str, Any]) -> str:
"""Classify the type of session based on context."""
# Simplified classification
if "task_complexity" in context:
if context["task_complexity"] > 0.7:
return "complex_task"
else:
return "simple_task"
return "general_session"
async def _update_persistent_context(self, current_persistent: Dict[str, Any], session_context: Dict[str, Any]) -> Dict[str, Any]:
"""Update persistent context with session information."""
# Simplified persistence logic
updated_persistent = current_persistent.copy()
updated_persistent["last_session"] = session_context
return updated_persistent
async def _identify_cross_session_patterns(self, user_id: str, persistent: Dict[str, Any], session: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Identify patterns across sessions."""
# Simplified pattern identification
return [
{
"pattern_type": "session_continuity",
"strength": 0.7,
"context": "persistent_preferences"
}
]
async def _generate_continuity_recommendations(self, user_id: str, persistent: Dict[str, Any], patterns: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Generate recommendations for maintaining continuity."""
# Simplified recommendations
return [
{
"recommendation": "maintain_preferred_style",
"priority": "high",
"context": "user_communication_preferences"
}
]
def _calculate_continuity_strength(self, patterns: List[Dict[str, Any]]) -> float:
"""Calculate strength of continuity patterns."""
if not patterns:
return 0.0
return np.mean([pattern.get("strength", 0.5) for pattern in patterns])
# Learning methods
async def _incremental_learning(self, interaction: UserInteraction) -> Dict[str, Any]:
"""Incremental learning from interaction."""
return {"learning_type": "incremental", "progress": 0.1}
async def _batch_learning(self, interactions: List[UserInteraction]) -> Dict[str, Any]:
"""Batch learning from multiple interactions."""
return {"learning_type": "batch", "progress": 0.3}
async def _reinforcement_learning(self, interaction: UserInteraction) -> Dict[str, Any]:
"""Reinforcement learning from interaction outcome."""
return {"learning_type": "reinforcement", "reward": interaction.success}
async def _association_learning(self, interaction: UserInteraction) -> Dict[str, Any]:
"""Association learning between context and outcomes."""
return {"learning_type": "association", "connections": 2}
if __name__ == "__main__":
print("Contextual Personalization & User Profiling System Initialized")
print("=" * 70)
engine = ContextualPersonalizationEngine()
print("Ready for advanced user profiling and personalization!") |