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
//! Semantic Query Understanding for NL2SPARQL
//!
//! This module provides advanced semantic understanding for natural language queries,
//! integrating with the NLP pipeline for better SPARQL generation.

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

use crate::nlp::{EntityType, ExtractedEntity, IntentResult, IntentType};
use crate::schema_introspection::DiscoveredSchema;

/// Semantic query understanding result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticUnderstanding {
    /// Recognized intent
    pub intent: IntentType,
    /// Intent confidence
    pub intent_confidence: f32,
    /// Extracted entities with types
    pub entities: Vec<ExtractedEntity>,
    /// Query type (SELECT, ASK, CONSTRUCT, DESCRIBE)
    pub query_type: QueryType,
    /// Required triple patterns
    pub triple_patterns: Vec<TriplePattern>,
    /// Filters to apply
    pub filters: Vec<Filter>,
    /// Aggregations requested
    pub aggregations: Vec<Aggregation>,
    /// Ordering preferences
    pub ordering: Option<Ordering>,
    /// Limit/offset hints
    pub pagination: Option<Pagination>,
    /// Schema elements to use
    pub schema_hints: SchemaHints,
}

/// SPARQL query type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum QueryType {
    Select,
    Ask,
    Construct,
    Describe,
}

/// Triple pattern for SPARQL generation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TriplePattern {
    pub subject: TripleElement,
    pub predicate: TripleElement,
    pub object: TripleElement,
    pub optional: bool,
}

/// Element in a triple pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TripleElement {
    Variable(String),
    URI(String),
    Literal(String),
    BNode(String),
}

/// Filter condition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Filter {
    pub variable: String,
    pub operator: FilterOperator,
    pub value: String,
}

/// Filter operators
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FilterOperator {
    Equals,
    NotEquals,
    GreaterThan,
    LessThan,
    GreaterThanOrEqual,
    LessThanOrEqual,
    Contains,
    Regex,
    Lang,
}

/// Aggregation function
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Aggregation {
    pub function: AggregationFunction,
    pub variable: String,
    pub alias: Option<String>,
}

/// Aggregation functions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AggregationFunction {
    Count,
    Sum,
    Avg,
    Min,
    Max,
    GroupConcat,
    Sample,
}

/// Query ordering
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ordering {
    pub variable: String,
    pub direction: OrderDirection,
}

/// Order direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderDirection {
    Ascending,
    Descending,
}

/// Pagination hints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

/// Schema hints for query generation
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SchemaHints {
    /// Relevant RDF classes
    pub classes: Vec<String>,
    /// Relevant properties
    pub properties: Vec<String>,
    /// Prefixes to use
    pub prefixes: HashMap<String, String>,
}

/// Semantic query analyzer
pub struct SemanticQueryAnalyzer {
    /// Cached schema for schema-aware generation
    schema: Option<DiscoveredSchema>,
}

impl SemanticQueryAnalyzer {
    /// Create a new semantic query analyzer
    pub fn new() -> Self {
        info!("Initialized semantic query analyzer");
        Self { schema: None }
    }

    /// Set the schema for schema-aware analysis
    pub fn set_schema(&mut self, schema: DiscoveredSchema) {
        self.schema = Some(schema);
    }

    /// Analyze a query with NLP results
    pub fn analyze(
        &self,
        query: &str,
        intent: IntentResult,
        entities: Vec<ExtractedEntity>,
    ) -> Result<SemanticUnderstanding> {
        debug!("Analyzing query semantics: {}", query);

        // Determine query type from intent
        let query_type = self.infer_query_type(&intent, query);

        // Build triple patterns from entities and intent
        let triple_patterns = self.build_triple_patterns(&entities, &intent, query);

        // Extract filters from query
        let filters = self.extract_filters(query, &entities);

        // Detect aggregations
        let aggregations = self.detect_aggregations(query, &intent);

        // Detect ordering preferences
        let ordering = self.detect_ordering(query);

        // Extract pagination hints
        let pagination = self.extract_pagination(query);

        // Generate schema hints
        let schema_hints = self.generate_schema_hints(&entities, &triple_patterns);

        Ok(SemanticUnderstanding {
            intent: intent.primary_intent,
            intent_confidence: intent.confidence,
            entities,
            query_type,
            triple_patterns,
            filters,
            aggregations,
            ordering,
            pagination,
            schema_hints,
        })
    }

    /// Infer SPARQL query type from intent
    fn infer_query_type(&self, intent: &IntentResult, query: &str) -> QueryType {
        let lowercase = query.to_lowercase();

        // Explicit query type keywords
        if lowercase.contains("ask") || lowercase.contains("is there") || lowercase.contains("does")
        {
            return QueryType::Ask;
        }

        if lowercase.contains("describe") || lowercase.contains("tell me about") {
            return QueryType::Describe;
        }

        if lowercase.contains("construct") {
            return QueryType::Construct;
        }

        // Intent-based inference
        match intent.primary_intent {
            IntentType::Query | IntentType::Exploration => QueryType::Select,
            IntentType::Analytics => QueryType::Select,
            IntentType::Explanation => QueryType::Describe,
            _ => QueryType::Select,
        }
    }

    /// Build triple patterns from entities and intent
    fn build_triple_patterns(
        &self,
        entities: &[ExtractedEntity],
        intent: &IntentResult,
        query: &str,
    ) -> Vec<TriplePattern> {
        let mut patterns = Vec::new();
        let lowercase = query.to_lowercase();

        // Basic pattern: entity type queries
        for entity in entities {
            match entity.entity_type {
                EntityType::RDFResource | EntityType::Class => {
                    // ?s rdf:type <entity>
                    patterns.push(TriplePattern {
                        subject: TripleElement::Variable("s".to_string()),
                        predicate: TripleElement::URI(
                            "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string(),
                        ),
                        object: TripleElement::URI(entity.text.clone()),
                        optional: false,
                    });
                }
                EntityType::Property => {
                    // ?s <property> ?o
                    patterns.push(TriplePattern {
                        subject: TripleElement::Variable("s".to_string()),
                        predicate: TripleElement::URI(entity.text.clone()),
                        object: TripleElement::Variable("o".to_string()),
                        optional: false,
                    });
                }
                _ => {
                    // General pattern for other entities
                    if intent.primary_intent == IntentType::Analytics && lowercase.contains("count")
                    {
                        // For count queries, add type pattern
                        patterns.push(TriplePattern {
                            subject: TripleElement::Variable("s".to_string()),
                            predicate: TripleElement::URI(
                                "http://www.w3.org/1999/02/22-rdf-syntax-ns#type".to_string(),
                            ),
                            object: TripleElement::Variable("type".to_string()),
                            optional: false,
                        });
                    }
                }
            }
        }

        // If no patterns generated, add a default triple pattern
        if patterns.is_empty() {
            patterns.push(TriplePattern {
                subject: TripleElement::Variable("s".to_string()),
                predicate: TripleElement::Variable("p".to_string()),
                object: TripleElement::Variable("o".to_string()),
                optional: false,
            });
        }

        patterns
    }

    /// Extract filters from query
    fn extract_filters(&self, query: &str, entities: &[ExtractedEntity]) -> Vec<Filter> {
        let mut filters = Vec::new();

        // Number filters
        for entity in entities {
            if entity.entity_type == EntityType::Number {
                // Try to infer filter type from context
                let filter = if query.contains("greater than") || query.contains(">") {
                    Filter {
                        variable: "value".to_string(),
                        operator: FilterOperator::GreaterThan,
                        value: entity.text.clone(),
                    }
                } else if query.contains("less than") || query.contains("<") {
                    Filter {
                        variable: "value".to_string(),
                        operator: FilterOperator::LessThan,
                        value: entity.text.clone(),
                    }
                } else if query.contains("equals") || query.contains("=") {
                    Filter {
                        variable: "value".to_string(),
                        operator: FilterOperator::Equals,
                        value: entity.text.clone(),
                    }
                } else {
                    continue;
                };

                filters.push(filter);
            }
        }

        // DateTime filters
        for entity in entities {
            if entity.entity_type == EntityType::DateTime {
                let lowercase = query.to_lowercase();
                if lowercase.contains("after") {
                    filters.push(Filter {
                        variable: "date".to_string(),
                        operator: FilterOperator::GreaterThan,
                        value: format!("\"{}\"", entity.text),
                    });
                } else if lowercase.contains("before") {
                    filters.push(Filter {
                        variable: "date".to_string(),
                        operator: FilterOperator::LessThan,
                        value: format!("\"{}\"", entity.text),
                    });
                }
            }
        }

        // String filters with contains
        if query.to_lowercase().contains("contains") || query.to_lowercase().contains("includes") {
            filters.push(Filter {
                variable: "label".to_string(),
                operator: FilterOperator::Contains,
                value: "search_term".to_string(),
            });
        }

        filters
    }

    /// Detect aggregations in query
    fn detect_aggregations(&self, query: &str, intent: &IntentResult) -> Vec<Aggregation> {
        let mut aggregations = Vec::new();
        let lowercase = query.to_lowercase();

        // Count
        if lowercase.contains("count") || lowercase.contains("how many") {
            aggregations.push(Aggregation {
                function: AggregationFunction::Count,
                variable: "s".to_string(),
                alias: Some("count".to_string()),
            });
        }

        // Sum
        if lowercase.contains("sum") || lowercase.contains("total") {
            aggregations.push(Aggregation {
                function: AggregationFunction::Sum,
                variable: "value".to_string(),
                alias: Some("sum".to_string()),
            });
        }

        // Average
        if lowercase.contains("average") || lowercase.contains("avg") || lowercase.contains("mean")
        {
            aggregations.push(Aggregation {
                function: AggregationFunction::Avg,
                variable: "value".to_string(),
                alias: Some("avg".to_string()),
            });
        }

        // Min/Max
        if lowercase.contains("minimum") || lowercase.contains("smallest") {
            aggregations.push(Aggregation {
                function: AggregationFunction::Min,
                variable: "value".to_string(),
                alias: Some("min".to_string()),
            });
        }

        if lowercase.contains("maximum")
            || lowercase.contains("largest")
            || lowercase.contains("biggest")
        {
            aggregations.push(Aggregation {
                function: AggregationFunction::Max,
                variable: "value".to_string(),
                alias: Some("max".to_string()),
            });
        }

        // Analytics intent suggests aggregation
        if intent.primary_intent == IntentType::Analytics && aggregations.is_empty() {
            aggregations.push(Aggregation {
                function: AggregationFunction::Count,
                variable: "s".to_string(),
                alias: Some("count".to_string()),
            });
        }

        aggregations
    }

    /// Detect ordering preferences
    fn detect_ordering(&self, query: &str) -> Option<Ordering> {
        let lowercase = query.to_lowercase();

        if lowercase.contains("order by") || lowercase.contains("sort by") {
            let direction = if lowercase.contains("descending") || lowercase.contains("desc") {
                OrderDirection::Descending
            } else {
                OrderDirection::Ascending
            };

            Some(Ordering {
                variable: "s".to_string(),
                direction,
            })
        } else if lowercase.contains("sorted") || lowercase.contains("ordered") {
            Some(Ordering {
                variable: "s".to_string(),
                direction: OrderDirection::Ascending,
            })
        } else {
            None
        }
    }

    /// Extract pagination hints
    fn extract_pagination(&self, query: &str) -> Option<Pagination> {
        let lowercase = query.to_lowercase();
        let mut limit = None;
        let offset = None;

        // Extract limit
        if let Some(limit_match) = lowercase.find("limit") {
            let after_limit = &lowercase[limit_match + 5..];
            let number_str = after_limit
                .split_whitespace()
                .next()
                .and_then(|s| s.parse::<usize>().ok());
            limit = number_str;
        }

        // Extract from patterns like "top 10", "first 5"
        if let Some(top_match) = lowercase.find("top ") {
            let after_top = &lowercase[top_match + 4..];
            let number_str = after_top
                .split_whitespace()
                .next()
                .and_then(|s| s.parse::<usize>().ok());
            limit = number_str.or(limit);
        }

        if let Some(first_match) = lowercase.find("first ") {
            let after_first = &lowercase[first_match + 6..];
            let number_str = after_first
                .split_whitespace()
                .next()
                .and_then(|s| s.parse::<usize>().ok());
            limit = number_str.or(limit);
        }

        // Default limit if none specified
        if limit.is_none() && (lowercase.contains("list") || lowercase.contains("show")) {
            limit = Some(100);
        }

        if limit.is_some() || offset.is_some() {
            Some(Pagination { limit, offset })
        } else {
            None
        }
    }

    /// Generate schema hints from entities and patterns
    fn generate_schema_hints(
        &self,
        entities: &[ExtractedEntity],
        _patterns: &[TriplePattern],
    ) -> SchemaHints {
        let mut hints = SchemaHints::default();

        // Extract classes and properties from entities
        for entity in entities {
            match entity.entity_type {
                EntityType::Class => hints.classes.push(entity.text.clone()),
                EntityType::Property => hints.properties.push(entity.text.clone()),
                EntityType::RDFResource
                    if entity
                        .text
                        .chars()
                        .next()
                        .map(|c| c.is_uppercase())
                        .unwrap_or(false) =>
                {
                    // Try to determine if it's a class or property
                    hints.classes.push(entity.text.clone());
                }
                _ => {}
            }
        }

        // Add common prefixes
        hints.prefixes.insert(
            "rdf".to_string(),
            "http://www.w3.org/1999/02/22-rdf-syntax-ns#".to_string(),
        );
        hints.prefixes.insert(
            "rdfs".to_string(),
            "http://www.w3.org/2000/01/rdf-schema#".to_string(),
        );
        hints.prefixes.insert(
            "xsd".to_string(),
            "http://www.w3.org/2001/XMLSchema#".to_string(),
        );

        // Use schema if available
        if let Some(ref schema) = self.schema {
            for class in &schema.classes {
                if !hints.classes.contains(&class.uri) {
                    hints.classes.push(class.uri.clone());
                }
            }

            for (prefix, uri) in &schema.prefixes {
                if !hints.prefixes.contains_key(prefix as &str) {
                    hints.prefixes.insert(prefix.clone(), uri.clone());
                }
            }
        }

        hints
    }
}

impl Default for SemanticQueryAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_query_type_inference() {
        let analyzer = SemanticQueryAnalyzer::new();
        let intent = crate::nlp::IntentRecognizer::new(IntentRecognitionConfig::default())
            .expect("should succeed")
            .recognize("How many movies are there?")
            .expect("should succeed");

        let query_type = analyzer.infer_query_type(&intent, "How many movies are there?");
        assert_eq!(query_type, QueryType::Select);
    }

    #[test]
    fn test_aggregation_detection() {
        let analyzer = SemanticQueryAnalyzer::new();
        let intent = crate::nlp::IntentRecognizer::new(IntentRecognitionConfig::default())
            .expect("should succeed")
            .recognize("Count all users")
            .expect("should succeed");

        let aggregations = analyzer.detect_aggregations("Count all users", &intent);
        assert!(!aggregations.is_empty());
        assert_eq!(aggregations[0].function, AggregationFunction::Count);
    }

    #[test]
    fn test_pagination_extraction() {
        let analyzer = SemanticQueryAnalyzer::new();

        let pagination = analyzer.extract_pagination("Show me the top 10 results");
        assert!(pagination.is_some());
        assert_eq!(pagination.expect("should succeed").limit, Some(10));
    }
}