oxirs_embed/contextual/
context_types.rs

1//! Context types and configurations for contextual embeddings
2
3use crate::ModelConfig;
4use serde::{Deserialize, Serialize};
5
6/// Contextual embedding configuration
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct ContextualConfig {
9    pub base_config: ModelConfig,
10    /// Base embedding dimension
11    pub base_dim: usize,
12    /// Context-aware output dimension
13    pub context_dim: usize,
14    /// Context types to consider
15    pub context_types: Vec<ContextType>,
16    /// Adaptation strategies
17    pub adaptation_strategy: AdaptationStrategy,
18    /// Context fusion method
19    pub fusion_method: ContextFusionMethod,
20    /// Temporal context settings
21    pub temporal_config: TemporalConfig,
22    /// Interactive refinement settings
23    pub interactive_config: InteractiveConfig,
24    /// Context cache settings
25    pub cache_config: ContextCacheConfig,
26}
27
28impl Default for ContextualConfig {
29    fn default() -> Self {
30        Self {
31            base_config: ModelConfig::default(),
32            base_dim: 768,
33            context_dim: 512,
34            context_types: vec![ContextType::Query, ContextType::User, ContextType::Task],
35            adaptation_strategy: AdaptationStrategy::DynamicAttention,
36            fusion_method: ContextFusionMethod::MultiHeadAttention,
37            temporal_config: TemporalConfig::default(),
38            interactive_config: InteractiveConfig::default(),
39            cache_config: ContextCacheConfig::default(),
40        }
41    }
42}
43
44/// Types of context for embedding adaptation
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
46pub enum ContextType {
47    /// Query-specific context from the current request
48    Query,
49    /// User-specific context from history and preferences
50    User,
51    /// Task-specific context for domain adaptation
52    Task,
53    /// Temporal context for time-aware embeddings
54    Temporal,
55    /// Interactive context from user feedback
56    Interactive,
57    /// Domain-specific context
58    Domain,
59    /// Cross-lingual context
60    CrossLingual,
61}
62
63/// Adaptation strategies for contextual embeddings
64#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
65pub enum AdaptationStrategy {
66    /// Dynamic attention over context vectors
67    DynamicAttention,
68    /// Context-conditional layer normalization
69    ConditionalLayerNorm,
70    /// Adapter layers for context-specific transformation
71    AdapterLayers,
72    /// Meta-learning for few-shot adaptation
73    MetaLearning,
74    /// Prompt-based context integration
75    PromptBased,
76    /// Memory-augmented adaptation
77    MemoryAugmented,
78}
79
80/// Context fusion methods
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub enum ContextFusionMethod {
83    /// Simple concatenation of contexts
84    Concatenation,
85    /// Weighted sum of context vectors
86    WeightedSum,
87    /// Multi-head attention fusion
88    MultiHeadAttention,
89    /// Cross-modal transformer fusion
90    TransformerFusion,
91    /// Graph-based context fusion
92    GraphFusion,
93    /// Neural module network fusion
94    NeuralModules,
95}
96
97/// Temporal context configuration
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct TemporalConfig {
100    /// Enable temporal adaptation
101    pub enabled: bool,
102    /// Time window for context (hours)
103    pub time_window_hours: f64,
104    /// Temporal decay factor
105    pub decay_factor: f32,
106    /// Enable trend analysis
107    pub trend_analysis: bool,
108    /// Seasonal adjustment
109    pub seasonal_adjustment: bool,
110    /// Maximum temporal history
111    pub max_history_entries: usize,
112}
113
114impl Default for TemporalConfig {
115    fn default() -> Self {
116        Self {
117            enabled: true,
118            time_window_hours: 24.0,
119            decay_factor: 0.9,
120            trend_analysis: true,
121            seasonal_adjustment: false,
122            max_history_entries: 1000,
123        }
124    }
125}
126
127/// Interactive refinement configuration
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct InteractiveConfig {
130    /// Enable interactive refinement
131    pub enabled: bool,
132    /// Learning rate for feedback integration
133    pub feedback_learning_rate: f32,
134    /// Maximum feedback history
135    pub max_feedback_history: usize,
136    /// Feedback aggregation method
137    pub aggregation_method: FeedbackAggregation,
138    /// Enable online learning
139    pub online_learning: bool,
140    /// Confidence threshold for adaptation
141    pub confidence_threshold: f32,
142}
143
144impl Default for InteractiveConfig {
145    fn default() -> Self {
146        Self {
147            enabled: true,
148            feedback_learning_rate: 0.01,
149            max_feedback_history: 500,
150            aggregation_method: FeedbackAggregation::WeightedAverage,
151            online_learning: false,
152            confidence_threshold: 0.7,
153        }
154    }
155}
156
157/// Context cache configuration
158#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct ContextCacheConfig {
160    /// Enable context caching
161    pub enabled: bool,
162    /// Maximum cache size
163    pub max_cache_size: usize,
164    /// Cache TTL in seconds
165    pub ttl_seconds: u64,
166    /// Enable LRU eviction
167    pub lru_eviction: bool,
168    /// Cache hit ratio threshold for warnings
169    pub hit_ratio_threshold: f32,
170}
171
172impl Default for ContextCacheConfig {
173    fn default() -> Self {
174        Self {
175            enabled: true,
176            max_cache_size: 10000,
177            ttl_seconds: 3600,
178            lru_eviction: true,
179            hit_ratio_threshold: 0.8,
180        }
181    }
182}
183
184/// Feedback aggregation methods
185#[derive(Debug, Clone, Serialize, Deserialize)]
186pub enum FeedbackAggregation {
187    /// Simple average of feedback scores
188    Average,
189    /// Weighted average with recency weighting
190    WeightedAverage,
191    /// Exponential moving average
192    ExponentialMoving,
193    /// Confidence-weighted aggregation
194    ConfidenceWeighted,
195}