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
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
//! Query Refinement System
//!
//! Helps users refine and improve their queries through interactive suggestions.
//! Enhanced with multi-turn conversation support and context awareness.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

/// Query refinement suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefinementSuggestion {
    /// Suggestion type
    pub suggestion_type: RefinementType,
    /// Original query part
    pub original: String,
    /// Suggested improvement
    pub suggested: String,
    /// Reason for suggestion
    pub reason: String,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
    /// Example usage
    pub example: Option<String>,
}

/// Types of refinement suggestions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RefinementType {
    /// Add filters to narrow results
    AddFilter,
    /// Add sorting/ordering
    AddOrdering,
    /// Limit result count
    AddLimit,
    /// Add grouping/aggregation
    AddAggregation,
    /// Simplify complex query
    Simplify,
    /// Add constraints
    AddConstraints,
    /// Improve performance
    OptimizePerformance,
    /// Clarify ambiguity
    ClarifyAmbiguity,
    /// Add joins
    AddJoin,
    /// Use better predicates
    BetterPredicates,
}

/// Query analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryAnalysis {
    /// Original query
    pub original_query: String,
    /// Query complexity (1-10)
    pub complexity: u8,
    /// Estimated result count
    pub estimated_results: Option<usize>,
    /// Performance issues detected
    pub performance_issues: Vec<String>,
    /// Ambiguities detected
    pub ambiguities: Vec<String>,
    /// Missing optimizations
    pub missing_optimizations: Vec<String>,
}

/// Query refinement configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefinementConfig {
    /// Maximum number of suggestions
    pub max_suggestions: usize,
    /// Minimum confidence threshold
    pub min_confidence: f32,
    /// Enable performance suggestions
    pub suggest_performance: bool,
    /// Enable clarity suggestions
    pub suggest_clarity: bool,
    /// Enable feature suggestions
    pub suggest_features: bool,
}

impl Default for RefinementConfig {
    fn default() -> Self {
        Self {
            max_suggestions: 5,
            min_confidence: 0.6,
            suggest_performance: true,
            suggest_clarity: true,
            suggest_features: true,
        }
    }
}

/// Query refiner with multi-turn conversation support
pub struct QueryRefiner {
    config: RefinementConfig,
    /// Conversation history for context-aware refinement
    conversation_history: Vec<ConversationTurn>,
    /// Learned patterns from user interactions
    learned_patterns: HashMap<String, Vec<String>>,
}

/// Conversation turn for context tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationTurn {
    pub query: String,
    pub refined_query: Option<String>,
    pub intent: Option<String>,
    pub entities: Vec<String>,
    pub timestamp: chrono::DateTime<chrono::Utc>,
}

impl QueryRefiner {
    /// Create a new query refiner with multi-turn support
    pub fn new(config: RefinementConfig) -> Self {
        info!("Initialized context-aware query refiner with multi-turn conversation support");
        Self {
            config,
            conversation_history: Vec::new(),
            learned_patterns: HashMap::new(),
        }
    }

    /// Add a conversation turn for context
    pub fn add_turn(
        &mut self,
        query: String,
        refined_query: Option<String>,
        intent: Option<String>,
        entities: Vec<String>,
    ) {
        self.conversation_history.push(ConversationTurn {
            query,
            refined_query,
            intent,
            entities,
            timestamp: chrono::Utc::now(),
        });

        // Keep only last 10 turns
        if self.conversation_history.len() > 10 {
            self.conversation_history.remove(0);
        }
    }

    /// Get context from conversation history
    fn get_context(&self) -> HashMap<String, String> {
        let mut context = HashMap::new();

        // Get recent entities
        let recent_entities: Vec<String> = self
            .conversation_history
            .iter()
            .rev()
            .take(3)
            .flat_map(|turn| turn.entities.clone())
            .collect();

        if !recent_entities.is_empty() {
            context.insert("recent_entities".to_string(), recent_entities.join(", "));
        }

        // Get recent intent
        if let Some(recent_turn) = self.conversation_history.last() {
            if let Some(ref intent) = recent_turn.intent {
                context.insert("recent_intent".to_string(), intent.clone());
            }
        }

        context
    }

    /// Analyze a query and suggest refinements with context awareness
    pub fn refine(&self, query: &str) -> Result<Vec<RefinementSuggestion>> {
        debug!("Analyzing query for refinements: {}", query);

        let context = self.get_context();
        let analysis = self.analyze_query(query)?;
        let mut suggestions = Vec::new();

        // Context-aware suggestions
        suggestions.extend(self.suggest_context_aware_refinements(query, &context)?);

        // Performance suggestions
        if self.config.suggest_performance {
            suggestions.extend(self.suggest_performance_improvements(&analysis)?);
        }

        // Clarity suggestions
        if self.config.suggest_clarity {
            suggestions.extend(self.suggest_clarity_improvements(&analysis)?);
        }

        // Feature suggestions
        if self.config.suggest_features {
            suggestions.extend(self.suggest_feature_additions(&analysis)?);
        }

        // Filter by confidence
        suggestions.retain(|s| s.confidence >= self.config.min_confidence);

        // Sort by confidence
        suggestions.sort_by(|a, b| {
            b.confidence
                .partial_cmp(&a.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Limit results
        suggestions.truncate(self.config.max_suggestions);

        info!("Generated {} refinement suggestions", suggestions.len());

        Ok(suggestions)
    }

    /// Analyze query for issues
    fn analyze_query(&self, query: &str) -> Result<QueryAnalysis> {
        let lowercase = query.to_lowercase();

        let mut performance_issues = Vec::new();
        let mut ambiguities = Vec::new();
        let mut missing_optimizations = Vec::new();

        // Check for missing LIMIT
        if !lowercase.contains("limit") {
            missing_optimizations
                .push("No LIMIT clause - query may return too many results".to_string());
        }

        // Check for missing ORDER BY
        if (lowercase.contains("list") || lowercase.contains("show"))
            && !lowercase.contains("order")
            && !lowercase.contains("sort")
        {
            missing_optimizations
                .push("Results not ordered - consider adding ORDER BY".to_string());
        }

        // Check for vague terms
        if lowercase.contains("thing")
            || lowercase.contains("stuff")
            || lowercase.contains("something")
        {
            ambiguities.push("Vague terms detected - be more specific".to_string());
        }

        // Check for performance issues
        if lowercase.contains("all") && !lowercase.contains("limit") {
            performance_issues.push("Requesting 'all' without LIMIT may be slow".to_string());
        }

        // Estimate complexity
        let complexity = self.estimate_complexity(query);

        Ok(QueryAnalysis {
            original_query: query.to_string(),
            complexity,
            estimated_results: None,
            performance_issues,
            ambiguities,
            missing_optimizations,
        })
    }

    /// Estimate query complexity
    fn estimate_complexity(&self, query: &str) -> u8 {
        let mut complexity = 1u8;

        // More words = more complex
        complexity += (query.split_whitespace().count() / 10).min(3) as u8;

        // Complex keywords
        let complex_keywords = [
            "aggregate",
            "group",
            "having",
            "union",
            "optional",
            "filter",
            "minus",
        ];
        for keyword in &complex_keywords {
            if query.to_lowercase().contains(keyword) {
                complexity += 1;
            }
        }

        complexity.min(10)
    }

    /// Suggest performance improvements
    fn suggest_performance_improvements(
        &self,
        analysis: &QueryAnalysis,
    ) -> Result<Vec<RefinementSuggestion>> {
        let mut suggestions = Vec::new();

        // Suggest LIMIT
        if !analysis.original_query.to_lowercase().contains("limit") {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::AddLimit,
                original: analysis.original_query.clone(),
                suggested: format!("{} LIMIT 100", analysis.original_query.trim()),
                reason: "Add LIMIT to prevent returning too many results".to_string(),
                confidence: 0.9,
                example: Some("SELECT * WHERE { ?s ?p ?o } LIMIT 100".to_string()),
            });
        }

        // Suggest indexing hints
        for issue in &analysis.performance_issues {
            if issue.contains("slow") {
                suggestions.push(RefinementSuggestion {
                    suggestion_type: RefinementType::OptimizePerformance,
                    original: analysis.original_query.clone(),
                    suggested: "Consider adding filters to narrow the search".to_string(),
                    reason: issue.clone(),
                    confidence: 0.75,
                    example: Some(
                        "SELECT * WHERE { ?s rdf:type :Movie . ?s :year 2023 } LIMIT 100"
                            .to_string(),
                    ),
                });
            }
        }

        Ok(suggestions)
    }

    /// Suggest clarity improvements
    fn suggest_clarity_improvements(
        &self,
        analysis: &QueryAnalysis,
    ) -> Result<Vec<RefinementSuggestion>> {
        let mut suggestions = Vec::new();

        for ambiguity in &analysis.ambiguities {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::ClarifyAmbiguity,
                original: analysis.original_query.clone(),
                suggested: "Replace vague terms with specific entity types or properties"
                    .to_string(),
                reason: ambiguity.clone(),
                confidence: 0.8,
                example: Some(
                    "Instead of 'show me things', use 'show me movies' or 'show me people'"
                        .to_string(),
                ),
            });
        }

        // Suggest being more specific
        if analysis.original_query.len() < 20 {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::ClarifyAmbiguity,
                original: analysis.original_query.clone(),
                suggested: "Add more details to your query".to_string(),
                reason: "Short query may be too vague".to_string(),
                confidence: 0.65,
                example: Some("Instead of 'movies', try 'movies released in 2023'".to_string()),
            });
        }

        Ok(suggestions)
    }

    /// Suggest feature additions
    fn suggest_feature_additions(
        &self,
        analysis: &QueryAnalysis,
    ) -> Result<Vec<RefinementSuggestion>> {
        let mut suggestions = Vec::new();
        let lowercase = analysis.original_query.to_lowercase();

        // Suggest ORDER BY
        if !lowercase.contains("order") && !lowercase.contains("sort") {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::AddOrdering,
                original: analysis.original_query.clone(),
                suggested: "Add ORDER BY to sort results".to_string(),
                reason: "Ordered results are easier to browse".to_string(),
                confidence: 0.7,
                example: Some("SELECT * WHERE { ?s ?p ?o } ORDER BY ?s LIMIT 100".to_string()),
            });
        }

        // Suggest aggregation
        if (lowercase.contains("list") || lowercase.contains("show"))
            && !lowercase.contains("count")
        {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::AddAggregation,
                original: analysis.original_query.clone(),
                suggested: "Consider counting results instead of listing all".to_string(),
                reason: "Aggregation provides quick overview".to_string(),
                confidence: 0.6,
                example: Some(
                    "SELECT (COUNT(?s) as ?count) WHERE { ?s rdf:type :Movie }".to_string(),
                ),
            });
        }

        // Suggest filters
        if lowercase.contains("all") || lowercase.contains("everything") {
            suggestions.push(RefinementSuggestion {
                suggestion_type: RefinementType::AddFilter,
                original: analysis.original_query.clone(),
                suggested: "Add filters to narrow down results".to_string(),
                reason: "Filtering improves result relevance".to_string(),
                confidence: 0.75,
                example: Some("FILTER (?year > 2020)".to_string()),
            });
        }

        Ok(suggestions)
    }

    /// Apply a refinement suggestion
    pub fn apply_suggestion(
        &self,
        query: &str,
        suggestion: &RefinementSuggestion,
    ) -> Result<String> {
        match suggestion.suggestion_type {
            RefinementType::AddLimit => {
                if !query.to_lowercase().contains("limit") {
                    Ok(format!("{} LIMIT 100", query.trim()))
                } else {
                    Ok(query.to_string())
                }
            }
            RefinementType::AddOrdering => {
                if !query.to_lowercase().contains("order by") {
                    // Insert ORDER BY before LIMIT if present
                    if let Some(limit_pos) = query.to_lowercase().find("limit") {
                        let mut refined = query.to_string();
                        refined.insert_str(limit_pos, "ORDER BY ?s ");
                        Ok(refined)
                    } else {
                        Ok(format!("{} ORDER BY ?s", query.trim()))
                    }
                } else {
                    Ok(query.to_string())
                }
            }
            _ => {
                // For other types, return the suggested query
                Ok(suggestion.suggested.clone())
            }
        }
    }

    /// Get interactive refinement session
    pub fn start_refinement_session(&self, initial_query: &str) -> RefinementSession {
        RefinementSession {
            original_query: initial_query.to_string(),
            current_query: initial_query.to_string(),
            applied_suggestions: Vec::new(),
            iteration: 0,
            context: self.get_context(),
        }
    }

    /// Suggest context-aware refinements based on conversation history
    fn suggest_context_aware_refinements(
        &self,
        query: &str,
        context: &HashMap<String, String>,
    ) -> Result<Vec<RefinementSuggestion>> {
        let mut suggestions = Vec::new();

        // If there are recent entities, suggest incorporating them
        if let Some(recent_entities) = context.get("recent_entities") {
            if !query
                .to_lowercase()
                .contains(&recent_entities.to_lowercase())
            {
                suggestions.push(RefinementSuggestion {
                    suggestion_type: RefinementType::AddConstraints,
                    original: query.to_string(),
                    suggested: format!("{} related to {}", query, recent_entities),
                    reason: format!("Building on previous context about {}", recent_entities),
                    confidence: 0.75,
                    example: Some(format!(
                        "Continue from previous query about {}",
                        recent_entities
                    )),
                });
            }
        }

        // Suggest follow-up queries based on previous turns
        if self.conversation_history.len() >= 2 {
            let prev_queries: Vec<String> = self
                .conversation_history
                .iter()
                .rev()
                .take(3)
                .map(|t| t.query.clone())
                .collect();

            if prev_queries
                .iter()
                .all(|q| q.to_lowercase().contains("count"))
                && !query.to_lowercase().contains("count")
            {
                suggestions.push(RefinementSuggestion {
                    suggestion_type: RefinementType::AddAggregation,
                    original: query.to_string(),
                    suggested: format!("count {}", query),
                    reason: "You've been asking for counts - continue the pattern?".to_string(),
                    confidence: 0.65,
                    example: Some("SELECT (COUNT(?x) as ?count) WHERE ...".to_string()),
                });
            }
        }

        Ok(suggestions)
    }

    /// Learn from user feedback on refinements
    pub fn learn_from_feedback(&mut self, original: String, refined: String, accepted: bool) {
        if accepted {
            self.learned_patterns
                .entry(original.clone())
                .or_default()
                .push(refined.clone());

            debug!(
                "Learned new refinement pattern: {} -> {}",
                original, refined
            );
        }
    }

    /// Get conversation history
    pub fn get_history(&self) -> &[ConversationTurn] {
        &self.conversation_history
    }

    /// Clear conversation history
    pub fn clear_history(&mut self) {
        self.conversation_history.clear();
        info!("Cleared conversation history");
    }
}

/// Interactive refinement session with context tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefinementSession {
    /// Original query
    pub original_query: String,
    /// Current refined query
    pub current_query: String,
    /// Applied suggestions
    pub applied_suggestions: Vec<RefinementSuggestion>,
    /// Iteration count
    pub iteration: usize,
    /// Conversation context
    pub context: HashMap<String, String>,
}

impl RefinementSession {
    /// Apply a suggestion to the session
    pub fn apply(&mut self, suggestion: RefinementSuggestion, refined_query: String) {
        self.current_query = refined_query;
        self.applied_suggestions.push(suggestion);
        self.iteration += 1;
    }

    /// Get refinement summary
    pub fn summary(&self) -> String {
        format!(
            "Refined query in {} iterations, applied {} suggestions",
            self.iteration,
            self.applied_suggestions.len()
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_query_analysis() {
        let refiner = QueryRefiner::new(RefinementConfig::default());
        let analysis = refiner
            .analyze_query("show me all things")
            .expect("should succeed");

        assert!(!analysis.performance_issues.is_empty());
        assert!(!analysis.ambiguities.is_empty());
    }

    #[test]
    fn test_limit_suggestion() {
        let refiner = QueryRefiner::new(RefinementConfig::default());
        let suggestions = refiner
            .refine("SELECT * WHERE { ?s ?p ?o }")
            .expect("should succeed");

        assert!(suggestions
            .iter()
            .any(|s| s.suggestion_type == RefinementType::AddLimit));
    }

    #[test]
    fn test_apply_limit() {
        let refiner = QueryRefiner::new(RefinementConfig::default());
        let suggestion = RefinementSuggestion {
            suggestion_type: RefinementType::AddLimit,
            original: "SELECT * WHERE { ?s ?p ?o }".to_string(),
            suggested: "SELECT * WHERE { ?s ?p ?o } LIMIT 100".to_string(),
            reason: "Add limit".to_string(),
            confidence: 0.9,
            example: None,
        };

        let refined = refiner
            .apply_suggestion("SELECT * WHERE { ?s ?p ?o }", &suggestion)
            .expect("should succeed");
        assert!(refined.contains("LIMIT"));
    }

    #[test]
    fn test_refinement_session() {
        let refiner = QueryRefiner::new(RefinementConfig::default());
        let mut session = refiner.start_refinement_session("show me movies");

        let suggestion = RefinementSuggestion {
            suggestion_type: RefinementType::AddLimit,
            original: "show me movies".to_string(),
            suggested: "show me movies LIMIT 100".to_string(),
            reason: "Add limit".to_string(),
            confidence: 0.9,
            example: None,
        };

        session.apply(suggestion, "show me movies LIMIT 100".to_string());
        assert_eq!(session.iteration, 1);
        assert_eq!(session.applied_suggestions.len(), 1);
    }
}