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
//! Query Suggestions System
//!
//! Provides intelligent query suggestions based on conversation context,
//! schema information, and user patterns.

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

/// Query suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuerySuggestion {
    /// Suggestion text
    pub text: String,
    /// Suggestion type
    pub suggestion_type: SuggestionType,
    /// Relevance score (0.0 - 1.0)
    pub relevance: f32,
    /// Category
    pub category: String,
    /// Example SPARQL query (if applicable)
    pub sparql_example: Option<String>,
    /// Metadata
    pub metadata: HashMap<String, String>,
}

/// Type of suggestion
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SuggestionType {
    /// Complete query suggestion
    Query,
    /// Query continuation
    Continuation,
    /// Entity suggestion
    Entity,
    /// Property/predicate suggestion
    Property,
    /// Filter suggestion
    Filter,
    /// Aggregation suggestion
    Aggregation,
    /// Follow-up question
    FollowUp,
}

/// Suggestion configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuggestionConfig {
    /// Maximum number of suggestions to return
    pub max_suggestions: usize,
    /// Minimum relevance score
    pub min_relevance: f32,
    /// Enable schema-based suggestions
    pub enable_schema_suggestions: bool,
    /// Enable pattern-based suggestions
    pub enable_pattern_suggestions: bool,
    /// Enable history-based suggestions
    pub enable_history_suggestions: bool,
    /// Enable follow-up suggestions
    pub enable_followup_suggestions: bool,
}

impl Default for SuggestionConfig {
    fn default() -> Self {
        Self {
            max_suggestions: 5,
            min_relevance: 0.3,
            enable_schema_suggestions: true,
            enable_pattern_suggestions: true,
            enable_history_suggestions: true,
            enable_followup_suggestions: true,
        }
    }
}

/// Query suggestion engine
pub struct SuggestionEngine {
    config: SuggestionConfig,
    store: Arc<dyn Store>,
    query_patterns: Vec<QueryPattern>,
    user_history: Vec<String>,
}

/// Predefined query pattern
#[derive(Debug, Clone)]
struct QueryPattern {
    template: String,
    category: String,
    keywords: Vec<String>,
    sparql_template: Option<String>,
}

impl SuggestionEngine {
    /// Create a new suggestion engine
    pub fn new(config: SuggestionConfig, store: Arc<dyn Store>) -> Result<Self> {
        let query_patterns = Self::load_query_patterns();

        info!(
            "Initialized suggestion engine with {} patterns",
            query_patterns.len()
        );

        Ok(Self {
            config,
            store,
            query_patterns,
            user_history: Vec::new(),
        })
    }

    /// Load predefined query patterns
    fn load_query_patterns() -> Vec<QueryPattern> {
        vec![
            QueryPattern {
                template: "Show me all {entities} from {time_range}".to_string(),
                category: "Exploration".to_string(),
                keywords: vec!["show", "all", "list"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some(
                    "SELECT * WHERE { ?s a ?type . FILTER(?time > {start}) }".to_string(),
                ),
            },
            QueryPattern {
                template: "Find {entities} related to {topic}".to_string(),
                category: "Search".to_string(),
                keywords: vec!["find", "search", "related"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some("SELECT * WHERE { ?s ?p {topic} }".to_string()),
            },
            QueryPattern {
                template: "Count the total number of {entities}".to_string(),
                category: "Analytics".to_string(),
                keywords: vec!["count", "total", "number"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some(
                    "SELECT (COUNT(?s) as ?count) WHERE { ?s a ?type }".to_string(),
                ),
            },
            QueryPattern {
                template: "What are the properties of {entity}?".to_string(),
                category: "Exploration".to_string(),
                keywords: vec!["properties", "attributes", "fields"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some("SELECT ?p ?o WHERE { {entity} ?p ?o }".to_string()),
            },
            QueryPattern {
                template: "Show me {entities} ordered by {property}".to_string(),
                category: "Sorting".to_string(),
                keywords: vec!["sorted", "ordered", "ranked"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some(
                    "SELECT * WHERE { ?s a ?type } ORDER BY ?property".to_string(),
                ),
            },
            QueryPattern {
                template: "Which {entities} have {property} equal to {value}?".to_string(),
                category: "Filter".to_string(),
                keywords: vec!["which", "where", "filter"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some("SELECT * WHERE { ?s ?p {value} }".to_string()),
            },
            QueryPattern {
                template: "Compare {entity1} and {entity2}".to_string(),
                category: "Comparison".to_string(),
                keywords: vec!["compare", "difference", "versus"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: None,
            },
            QueryPattern {
                template: "What is the average {property} of {entities}?".to_string(),
                category: "Analytics".to_string(),
                keywords: vec!["average", "mean", "aggregate"]
                    .into_iter()
                    .map(String::from)
                    .collect(),
                sparql_template: Some(
                    "SELECT (AVG(?value) as ?avg) WHERE { ?s ?p ?value }".to_string(),
                ),
            },
        ]
    }

    /// Generate suggestions based on partial input
    pub fn suggest(
        &self,
        partial_query: &str,
        context: &SuggestionContext,
    ) -> Result<Vec<QuerySuggestion>> {
        debug!("Generating suggestions for: {}", partial_query);

        let mut suggestions = Vec::new();

        // Pattern-based suggestions
        if self.config.enable_pattern_suggestions {
            suggestions.extend(self.pattern_based_suggestions(partial_query)?);
        }

        // Schema-based suggestions
        if self.config.enable_schema_suggestions {
            suggestions.extend(self.schema_based_suggestions(partial_query, context)?);
        }

        // History-based suggestions
        if self.config.enable_history_suggestions {
            suggestions.extend(self.history_based_suggestions(partial_query)?);
        }

        // Follow-up suggestions
        if self.config.enable_followup_suggestions && !context.last_query.is_empty() {
            suggestions.extend(self.followup_suggestions(&context.last_query)?);
        }

        // Filter by relevance
        suggestions.retain(|s| s.relevance >= self.config.min_relevance);

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

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

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

        Ok(suggestions)
    }

    /// Generate pattern-based suggestions
    fn pattern_based_suggestions(&self, partial: &str) -> Result<Vec<QuerySuggestion>> {
        let mut suggestions = Vec::new();
        let lowercase = partial.to_lowercase();

        for pattern in &self.query_patterns {
            // Calculate relevance based on keyword matching
            let mut relevance: f32 = 0.0;
            for keyword in &pattern.keywords {
                if lowercase.contains(keyword) {
                    relevance += 0.3;
                }
            }

            if relevance > 0.0 {
                suggestions.push(QuerySuggestion {
                    text: pattern.template.clone(),
                    suggestion_type: SuggestionType::Query,
                    relevance: relevance.min(1.0),
                    category: pattern.category.clone(),
                    sparql_example: pattern.sparql_template.clone(),
                    metadata: HashMap::new(),
                });
            }
        }

        Ok(suggestions)
    }

    /// Generate schema-based suggestions
    fn schema_based_suggestions(
        &self,
        partial: &str,
        context: &SuggestionContext,
    ) -> Result<Vec<QuerySuggestion>> {
        let mut suggestions = Vec::new();

        // Suggest entity types from schema
        if let Some(classes) = &context.available_classes {
            for class in classes {
                if class.to_lowercase().contains(&partial.to_lowercase()) {
                    suggestions.push(QuerySuggestion {
                        text: format!("Show me all {}", class),
                        suggestion_type: SuggestionType::Entity,
                        relevance: 0.8,
                        category: "Entity Type".to_string(),
                        sparql_example: Some(format!("SELECT * WHERE {{ ?s a {} }}", class)),
                        metadata: HashMap::new(),
                    });
                }
            }
        }

        // Suggest properties
        if let Some(properties) = &context.available_properties {
            for property in properties {
                if property.to_lowercase().contains(&partial.to_lowercase()) {
                    suggestions.push(QuerySuggestion {
                        text: format!("Filter by {}", property),
                        suggestion_type: SuggestionType::Property,
                        relevance: 0.7,
                        category: "Property".to_string(),
                        sparql_example: None,
                        metadata: HashMap::new(),
                    });
                }
            }
        }

        Ok(suggestions)
    }

    /// Generate history-based suggestions
    fn history_based_suggestions(&self, partial: &str) -> Result<Vec<QuerySuggestion>> {
        let mut suggestions = Vec::new();

        for historical_query in &self.user_history {
            if historical_query
                .to_lowercase()
                .starts_with(&partial.to_lowercase())
            {
                suggestions.push(QuerySuggestion {
                    text: historical_query.clone(),
                    suggestion_type: SuggestionType::Query,
                    relevance: 0.6,
                    category: "Recent".to_string(),
                    sparql_example: None,
                    metadata: [("source".to_string(), "history".to_string())]
                        .into_iter()
                        .collect(),
                });
            }
        }

        Ok(suggestions)
    }

    /// Generate follow-up suggestions
    fn followup_suggestions(&self, _last_query: &str) -> Result<Vec<QuerySuggestion>> {
        let suggestions = vec![
            // Suggest refinements
            QuerySuggestion {
                text: "Refine the previous query".to_string(),
                suggestion_type: SuggestionType::FollowUp,
                relevance: 0.7,
                category: "Follow-up".to_string(),
                sparql_example: None,
                metadata: HashMap::new(),
            },
            // Suggest related queries
            QuerySuggestion {
                text: "Show me related entities".to_string(),
                suggestion_type: SuggestionType::FollowUp,
                relevance: 0.6,
                category: "Follow-up".to_string(),
                sparql_example: None,
                metadata: HashMap::new(),
            },
            // Suggest aggregations
            QuerySuggestion {
                text: "Count the results".to_string(),
                suggestion_type: SuggestionType::FollowUp,
                relevance: 0.5,
                category: "Follow-up".to_string(),
                sparql_example: None,
                metadata: HashMap::new(),
            },
        ];

        Ok(suggestions)
    }

    /// Add a query to user history
    pub fn add_to_history(&mut self, query: String) {
        self.user_history.push(query);

        // Keep only recent history
        if self.user_history.len() > 100 {
            self.user_history.remove(0);
        }
    }

    /// Clear user history
    pub fn clear_history(&mut self) {
        self.user_history.clear();
    }
}

/// Context for generating suggestions
#[derive(Debug, Clone, Default)]
pub struct SuggestionContext {
    /// Last query executed
    pub last_query: String,
    /// Available entity classes in the dataset
    pub available_classes: Option<Vec<String>>,
    /// Available properties in the dataset
    pub available_properties: Option<Vec<String>>,
    /// Current conversation topic
    pub conversation_topic: Option<String>,
    /// User preferences
    pub user_preferences: HashMap<String, String>,
}

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

    #[test]
    fn test_pattern_based_suggestions() {
        let store = Arc::new(ConcreteStore::new().expect("should succeed"));
        let engine =
            SuggestionEngine::new(SuggestionConfig::default(), store).expect("should succeed");

        let context = SuggestionContext::default();
        let suggestions = engine
            .suggest("show me all", &context)
            .expect("should succeed");

        assert!(!suggestions.is_empty());
        assert!(suggestions.iter().any(|s| s.text.contains("Show me all")));
    }

    #[test]
    fn test_history_suggestions() {
        let store = Arc::new(ConcreteStore::new().expect("should succeed"));
        let mut engine =
            SuggestionEngine::new(SuggestionConfig::default(), store).expect("should succeed");

        engine.add_to_history("What movies were released in 2023?".to_string());

        let context = SuggestionContext::default();
        let suggestions = engine.suggest("What", &context).expect("should succeed");

        assert!(suggestions.iter().any(|s| s.text.contains("movies")));
    }

    #[test]
    fn test_followup_suggestions() {
        let store = Arc::new(ConcreteStore::new().expect("should succeed"));
        let engine =
            SuggestionEngine::new(SuggestionConfig::default(), store).expect("should succeed");

        let context = SuggestionContext {
            last_query: "Show me all movies".to_string(),
            ..Default::default()
        };

        let suggestions = engine.suggest("", &context).expect("should succeed");

        assert!(suggestions
            .iter()
            .any(|s| s.suggestion_type == SuggestionType::FollowUp));
    }
}