oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
Documentation
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
//! Anomaly detection for conversation analytics
//!
//! This module contains the logic for detecting anomalies in conversations,
//! including unusual response times, low quality responses, high error rates,
//! and other conversational anomalies.

use anyhow::Result;
use std::{
    collections::VecDeque,
    time::{Duration, SystemTime},
};
use tracing::debug;

use crate::{analytics::types::*, Message};

/// Anomaly detection component for conversation analytics
pub struct AnomalyDetector {
    pub config: AnalyticsConfig,
    pub response_time_history: VecDeque<f32>,
    pub quality_history: VecDeque<f32>,
    pub error_history: VecDeque<bool>,
    pub sentiment_history: VecDeque<f32>,
    pub complexity_history: VecDeque<f32>,
}

impl AnomalyDetector {
    pub fn new(config: AnalyticsConfig) -> Self {
        Self {
            config,
            response_time_history: VecDeque::new(),
            quality_history: VecDeque::new(),
            error_history: VecDeque::new(),
            sentiment_history: VecDeque::new(),
            complexity_history: VecDeque::new(),
        }
    }

    pub fn add_response_time(&mut self, response_time: Duration) {
        self.response_time_history
            .push_back(response_time.as_secs_f32());
        if self.response_time_history.len() > 20 {
            self.response_time_history.pop_front();
        }
    }

    pub fn add_quality_score(&mut self, quality: f32) {
        self.quality_history.push_back(quality);
        if self.quality_history.len() > 20 {
            self.quality_history.pop_front();
        }
    }

    pub fn add_error(&mut self, is_error: bool) {
        self.error_history.push_back(is_error);
        if self.error_history.len() > 20 {
            self.error_history.pop_front();
        }
    }

    pub fn add_sentiment_score(&mut self, sentiment: f32) {
        self.sentiment_history.push_back(sentiment);
        if self.sentiment_history.len() > 20 {
            self.sentiment_history.pop_front();
        }
    }

    pub fn add_complexity_score(&mut self, complexity: f32) {
        self.complexity_history.push_back(complexity);
        if self.complexity_history.len() > 20 {
            self.complexity_history.pop_front();
        }
    }

    pub async fn detect_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
        message: &Message,
    ) -> Result<Vec<ConversationAnomaly>> {
        let mut anomalies = Vec::new();

        // Detect response time anomalies
        if let Some(anomaly) = self.detect_response_time_anomalies(message).await? {
            anomalies.push(anomaly);
        }

        // Detect quality anomalies
        if let Some(anomaly) = self.detect_quality_anomalies(message).await? {
            anomalies.push(anomaly);
        }

        // Detect error rate anomalies
        if let Some(anomaly) = self.detect_error_rate_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect sentiment anomalies
        if let Some(anomaly) = self.detect_sentiment_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect complexity anomalies
        if let Some(anomaly) = self.detect_complexity_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect engagement anomalies
        if let Some(anomaly) = self.detect_engagement_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect context loss anomalies
        if let Some(anomaly) = self.detect_context_loss_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect topic divergence anomalies
        if let Some(anomaly) = self.detect_topic_divergence_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect confidence collapse anomalies
        if let Some(anomaly) = self.detect_confidence_collapse_anomalies(analytics).await? {
            anomalies.push(anomaly);
        }

        // Detect repeated error anomalies
        if let Some(anomaly) = self.detect_repeated_error_anomalies().await? {
            anomalies.push(anomaly);
        }

        debug!("Detected {} anomalies", anomalies.len());
        Ok(anomalies)
    }

    async fn detect_response_time_anomalies(
        &mut self,
        message: &Message,
    ) -> Result<Option<ConversationAnomaly>> {
        if let Some(ref metadata) = message.metadata {
            if let Some(response_time_ms) = metadata.processing_time_ms {
                let response_time_secs = response_time_ms as f32 / 1000.0;
                let response_time = std::time::Duration::from_millis(response_time_ms);
                self.add_response_time(response_time);

                if self.response_time_history.len() >= 5 {
                    let mean = self.response_time_history.iter().sum::<f32>()
                        / self.response_time_history.len() as f32;
                    let variance = self
                        .response_time_history
                        .iter()
                        .map(|x| (x - mean).powi(2))
                        .sum::<f32>()
                        / self.response_time_history.len() as f32;
                    let std_dev = variance.sqrt();

                    if response_time_secs
                        > mean + (self.config.anomaly_detection_threshold * std_dev)
                    {
                        return Ok(Some(ConversationAnomaly {
                            anomaly_type: AnomalyType::UnusualResponseTime,
                            description: format!(
                                "Response time {:.2}s is {:.2} standard deviations above average",
                                response_time_secs,
                                (response_time_secs - mean) / std_dev
                            ),
                            severity: if response_time_secs > mean + (3.0 * std_dev) {
                                AnomalySeverity::High
                            } else {
                                AnomalySeverity::Medium
                            },
                            detected_at: SystemTime::now(),
                            message_context: vec![message.content.to_string()],
                            suggested_action: Some(
                                "Check system performance and optimize slow queries".to_string(),
                            ),
                        }));
                    }
                }
            }
        }

        Ok(None)
    }

    async fn detect_quality_anomalies(
        &mut self,
        message: &Message,
    ) -> Result<Option<ConversationAnomaly>> {
        if let Some(ref metadata) = message.metadata {
            if let Some(confidence) = metadata.confidence {
                self.add_quality_score(confidence as f32);

                if confidence < 0.3 {
                    return Ok(Some(ConversationAnomaly {
                        anomaly_type: AnomalyType::LowQualityResponses,
                        description: format!(
                            "Response confidence {confidence:.2} is below acceptable threshold"
                        ),
                        severity: AnomalySeverity::Medium,
                        detected_at: SystemTime::now(),
                        message_context: vec![message.content.to_string()],
                        suggested_action: Some(
                            "Review retrieval results and LLM outputs".to_string(),
                        ),
                    }));
                }
            }
        }

        Ok(None)
    }

    async fn detect_error_rate_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.conversation_quality.error_rate > 0.5 && analytics.message_count > 5 {
            return Ok(Some(ConversationAnomaly {
                anomaly_type: AnomalyType::HighErrorRate,
                description: format!(
                    "Error rate {:.2}% is above acceptable threshold",
                    analytics.conversation_quality.error_rate * 100.0
                ),
                severity: AnomalySeverity::High,
                detected_at: SystemTime::now(),
                message_context: vec![],
                suggested_action: Some(
                    "Investigate system errors and data quality issues".to_string(),
                ),
            }));
        }

        Ok(None)
    }

    async fn detect_sentiment_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.sentiment_progression.len() >= 2 {
            let latest_sentiment =
                &analytics.sentiment_progression[analytics.sentiment_progression.len() - 1];
            let previous_sentiment =
                &analytics.sentiment_progression[analytics.sentiment_progression.len() - 2];

            let sentiment_change =
                (latest_sentiment.intensity - previous_sentiment.intensity).abs();

            if sentiment_change > 0.5 && latest_sentiment.intensity < -0.5 {
                return Ok(Some(ConversationAnomaly {
                    anomaly_type: AnomalyType::UnexpectedSentiment,
                    description: format!(
                        "Sudden negative sentiment shift: {} -> {}",
                        previous_sentiment.emotion, latest_sentiment.emotion
                    ),
                    severity: AnomalySeverity::Medium,
                    detected_at: SystemTime::now(),
                    message_context: vec![],
                    suggested_action: Some(
                        "Review recent responses for potential issues".to_string(),
                    ),
                }));
            }
        }

        Ok(None)
    }

    async fn detect_complexity_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.complexity_progression.len() >= 2 {
            let latest =
                &analytics.complexity_progression[analytics.complexity_progression.len() - 1];
            let previous =
                &analytics.complexity_progression[analytics.complexity_progression.len() - 2];

            let complexity_spike = latest.overall_complexity - previous.overall_complexity;

            if complexity_spike > 0.5 {
                return Ok(Some(ConversationAnomaly {
                    anomaly_type: AnomalyType::ComplexitySpike,
                    description: format!(
                        "Complexity spike detected: {:.2} -> {:.2}",
                        previous.overall_complexity, latest.overall_complexity
                    ),
                    severity: AnomalySeverity::Low,
                    detected_at: SystemTime::now(),
                    message_context: vec![],
                    suggested_action: Some(
                        "Consider providing simpler explanations or breaking down complex topics"
                            .to_string(),
                    ),
                }));
            }
        }

        Ok(None)
    }

    async fn detect_engagement_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.conversation_quality.engagement_score < 0.3 && analytics.message_count > 5 {
            return Ok(Some(ConversationAnomaly {
                anomaly_type: AnomalyType::EngagementDrop,
                description: format!(
                    "Low engagement score: {:.2}",
                    analytics.conversation_quality.engagement_score
                ),
                severity: AnomalySeverity::Medium,
                detected_at: SystemTime::now(),
                message_context: vec![],
                suggested_action: Some(
                    "Try to re-engage user with questions or interesting topics".to_string(),
                ),
            }));
        }

        Ok(None)
    }

    async fn detect_context_loss_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.conversation_quality.coherence_score < 0.4 && analytics.message_count > 3 {
            return Ok(Some(ConversationAnomaly {
                anomaly_type: AnomalyType::ContextLoss,
                description: format!(
                    "Low coherence score indicates potential context loss: {:.2}",
                    analytics.conversation_quality.coherence_score
                ),
                severity: AnomalySeverity::Medium,
                detected_at: SystemTime::now(),
                message_context: vec![],
                suggested_action: Some(
                    "Review context management and conversation history".to_string(),
                ),
            }));
        }

        Ok(None)
    }

    async fn detect_topic_divergence_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.topics_discussed.len() > 5
            && analytics.conversation_quality.relevance_score < 0.5
        {
            return Ok(Some(ConversationAnomaly {
                anomaly_type: AnomalyType::TopicDivergence,
                description: format!(
                    "Topic divergence detected: {} topics, relevance score: {:.2}",
                    analytics.topics_discussed.len(),
                    analytics.conversation_quality.relevance_score
                ),
                severity: AnomalySeverity::Low,
                detected_at: SystemTime::now(),
                message_context: analytics.topics_discussed.clone(),
                suggested_action: Some(
                    "Focus on main topics and maintain conversation relevance".to_string(),
                ),
            }));
        }

        Ok(None)
    }

    async fn detect_confidence_collapse_anomalies(
        &mut self,
        analytics: &ConversationAnalytics,
    ) -> Result<Option<ConversationAnomaly>> {
        if analytics.confidence_progression.len() >= 3 {
            let recent_confidences: Vec<f64> = analytics
                .confidence_progression
                .iter()
                .rev()
                .take(3)
                .map(|c| c.overall_confidence)
                .collect();

            let avg_confidence =
                recent_confidences.iter().sum::<f64>() / recent_confidences.len() as f64;

            if avg_confidence < 0.4 {
                return Ok(Some(ConversationAnomaly {
                    anomaly_type: AnomalyType::ConfidenceCollapse,
                    description: format!(
                        "Confidence collapse detected: average confidence {avg_confidence:.2}"
                    ),
                    severity: AnomalySeverity::High,
                    detected_at: SystemTime::now(),
                    message_context: vec![],
                    suggested_action: Some(
                        "Review knowledge base and improve retrieval quality".to_string(),
                    ),
                }));
            }
        }

        Ok(None)
    }

    async fn detect_repeated_error_anomalies(&mut self) -> Result<Option<ConversationAnomaly>> {
        if self.error_history.len() >= 3 {
            let recent_errors: Vec<bool> =
                self.error_history.iter().rev().take(3).cloned().collect();
            let error_count = recent_errors.iter().filter(|&&e| e).count();

            if error_count >= 2 {
                return Ok(Some(ConversationAnomaly {
                    anomaly_type: AnomalyType::RepeatedErrors,
                    description: format!(
                        "Repeated errors detected: {error_count} out of last 3 responses"
                    ),
                    severity: AnomalySeverity::High,
                    detected_at: SystemTime::now(),
                    message_context: vec![],
                    suggested_action: Some("Investigate root cause of repeated errors".to_string()),
                }));
            }
        }

        Ok(None)
    }
}