218 lines
7.0 KiB
Rust
218 lines
7.0 KiB
Rust
|
|
//! Example: RAG Query Optimization
|
||
|
|
//!
|
||
|
|
//! Demonstrates intelligent query enhancement through:
|
||
|
|
//! - Intent detection (factual, comparison, how-to, etc)
|
||
|
|
//! - Query rewriting and normalization
|
||
|
|
//! - Synonym expansion
|
||
|
|
//! - Key term extraction
|
||
|
|
//! - Context injection
|
||
|
|
|
||
|
|
use provisioning_rag::{IntentDetector, QueryOptimizer};
|
||
|
|
|
||
|
|
fn main() -> anyhow::Result<()> {
|
||
|
|
println!("=== RAG Query Optimization Example ===\n");
|
||
|
|
|
||
|
|
// Initialize query optimizer
|
||
|
|
let optimizer = QueryOptimizer::new();
|
||
|
|
|
||
|
|
println!("1. Intent Detection\n");
|
||
|
|
println!("The system can detect 6 types of query intents:\n");
|
||
|
|
|
||
|
|
let test_queries = vec![
|
||
|
|
("What is Kubernetes?", "Factual"),
|
||
|
|
("How do I deploy Kubernetes?", "How-to"),
|
||
|
|
("Why is scaling important?", "Why"),
|
||
|
|
(
|
||
|
|
"What's the difference between Docker and Kubernetes?",
|
||
|
|
"Comparison",
|
||
|
|
),
|
||
|
|
("Can you explain how services work?", "Explanation"),
|
||
|
|
("Tell me random stuff", "General"),
|
||
|
|
];
|
||
|
|
|
||
|
|
for (query, expected_intent) in test_queries {
|
||
|
|
let intent = IntentDetector::detect(query);
|
||
|
|
println!("Query: \"{}\"", query);
|
||
|
|
println!(
|
||
|
|
"Detected: {} (expected: {})\n",
|
||
|
|
intent.name(),
|
||
|
|
expected_intent
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n2. Query Optimization Pipeline\n");
|
||
|
|
|
||
|
|
let test_query = "How can I deploy kubernetes applications?";
|
||
|
|
println!("Original query: \"{}\"\n", test_query);
|
||
|
|
|
||
|
|
let optimized = optimizer.optimize(test_query)?;
|
||
|
|
|
||
|
|
println!("Optimization Results:");
|
||
|
|
println!(" Intent: {}", optimized.intent.name());
|
||
|
|
println!(
|
||
|
|
" Intent confidence: {:.1}%",
|
||
|
|
optimized.intent_confidence * 100.0
|
||
|
|
);
|
||
|
|
println!(" Key terms: {:?}", optimized.key_terms);
|
||
|
|
println!(" Optimized query: \"{}\"", optimized.optimized);
|
||
|
|
println!(" Context injected: {}\n", optimized.context_injected);
|
||
|
|
|
||
|
|
println!("\n3. Key Term Extraction\n");
|
||
|
|
|
||
|
|
let queries_to_analyze = vec![
|
||
|
|
"What is Kubernetes?",
|
||
|
|
"How do I deploy applications?",
|
||
|
|
"Compare Docker and Kubernetes",
|
||
|
|
"Explain networking concepts",
|
||
|
|
];
|
||
|
|
|
||
|
|
for query in queries_to_analyze {
|
||
|
|
let terms = optimizer.extract_key_terms(query);
|
||
|
|
println!("Query: \"{}\"", query);
|
||
|
|
println!("Key terms: {:?}\n", terms);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n4. Query Normalization\n");
|
||
|
|
|
||
|
|
let messy_queries = vec![
|
||
|
|
" What IS kubernetes? ",
|
||
|
|
"HOW DO I DEPLOY???",
|
||
|
|
"TELL me ABOUT kubernetes",
|
||
|
|
];
|
||
|
|
|
||
|
|
for query in messy_queries {
|
||
|
|
let normalized = optimizer.normalize_query(query);
|
||
|
|
println!("Original: \"{}\"", query);
|
||
|
|
println!("Normalized: \"{}\"\n", normalized);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n5. Synonym Expansion\n");
|
||
|
|
|
||
|
|
let queries_with_synonyms = vec![
|
||
|
|
"What is kubernetes?",
|
||
|
|
"Deploy docker containers",
|
||
|
|
"Kubernetes services",
|
||
|
|
];
|
||
|
|
|
||
|
|
for query in queries_with_synonyms {
|
||
|
|
let expanded = optimizer.rewrite_query(query);
|
||
|
|
println!("Original: \"{}\"", query);
|
||
|
|
println!("Expanded: \"{}\"\n", expanded);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n6. Context Injection\n");
|
||
|
|
|
||
|
|
let conversation_context =
|
||
|
|
"We previously discussed container orchestration and Kubernetes deployment strategies";
|
||
|
|
let followup_query = "Tell me more about networking";
|
||
|
|
|
||
|
|
let context_result = optimizer.inject_context(followup_query, conversation_context);
|
||
|
|
|
||
|
|
println!("Follow-up query: \"{}\"", followup_query);
|
||
|
|
println!("Conversation context: \"{}\"", conversation_context);
|
||
|
|
println!("\nEnriched query:");
|
||
|
|
println!("{}\n", context_result);
|
||
|
|
|
||
|
|
println!("\n7. Full Optimization with Context\n");
|
||
|
|
|
||
|
|
let optimized_with_context = optimizer.optimize_with_context(
|
||
|
|
"Tell me more about services",
|
||
|
|
Some("We discussed Kubernetes deployment earlier"),
|
||
|
|
)?;
|
||
|
|
|
||
|
|
println!("Original query: \"Tell me more about services\"");
|
||
|
|
println!("Context: \"We discussed Kubernetes deployment earlier\"\n");
|
||
|
|
println!("Full optimization result:");
|
||
|
|
println!(" Intent: {}", optimized_with_context.intent.name());
|
||
|
|
println!(" Key terms: {:?}", optimized_with_context.key_terms);
|
||
|
|
println!(
|
||
|
|
" Context injected: {}",
|
||
|
|
optimized_with_context.context_injected
|
||
|
|
);
|
||
|
|
println!(" Optimized: \"{}\"", optimized_with_context.optimized);
|
||
|
|
println!();
|
||
|
|
|
||
|
|
println!("\n8. Intent Confidence Levels\n");
|
||
|
|
|
||
|
|
let confidence_examples = vec![
|
||
|
|
("What is it?", "High confidence factual query"),
|
||
|
|
("How do I?", "High confidence how-to query"),
|
||
|
|
("Tell me stuff", "Lower confidence general query"),
|
||
|
|
];
|
||
|
|
|
||
|
|
for (query, description) in confidence_examples {
|
||
|
|
let optimized = optimizer.optimize(query)?;
|
||
|
|
println!("Query: \"{}\"", query);
|
||
|
|
println!("Description: {}", description);
|
||
|
|
println!("Confidence: {:.1}%\n", optimized.intent_confidence * 100.0);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n9. Use Case Recommendations\n");
|
||
|
|
|
||
|
|
let use_cases = vec![
|
||
|
|
(
|
||
|
|
"What is Kubernetes?",
|
||
|
|
"Factual",
|
||
|
|
"Search for definitions and overview docs",
|
||
|
|
),
|
||
|
|
(
|
||
|
|
"How do I deploy?",
|
||
|
|
"How-to",
|
||
|
|
"Search for step-by-step guides and procedures",
|
||
|
|
),
|
||
|
|
(
|
||
|
|
"Why is it important?",
|
||
|
|
"Why",
|
||
|
|
"Search for explanations and rationale",
|
||
|
|
),
|
||
|
|
(
|
||
|
|
"Docker vs Kubernetes",
|
||
|
|
"Comparison",
|
||
|
|
"Search for comparison documents",
|
||
|
|
),
|
||
|
|
(
|
||
|
|
"Explain services",
|
||
|
|
"Explanation",
|
||
|
|
"Search for detailed explanations",
|
||
|
|
),
|
||
|
|
];
|
||
|
|
|
||
|
|
for (query, intent, recommendation) in use_cases {
|
||
|
|
println!("Query: \"{}\"", query);
|
||
|
|
println!("Intent: {}", intent);
|
||
|
|
println!("Recommendation: {}\n", recommendation);
|
||
|
|
}
|
||
|
|
|
||
|
|
println!("\n10. Best Practices for Query Optimization\n");
|
||
|
|
|
||
|
|
println!("✓ Let the optimizer detect intent automatically");
|
||
|
|
println!("✓ Use extracted key terms for focused searching");
|
||
|
|
println!("✓ Inject conversation context for follow-ups");
|
||
|
|
println!("✓ Customize synonyms for your domain");
|
||
|
|
println!("✓ Monitor intent confidence scores");
|
||
|
|
println!("✓ Use intent-specific search strategies");
|
||
|
|
println!("✓ Cache optimized queries for repeated use\n");
|
||
|
|
|
||
|
|
println!("\n11. Performance Characteristics\n");
|
||
|
|
|
||
|
|
println!("Intent detection: <2ms");
|
||
|
|
println!("Query normalization: <1ms");
|
||
|
|
println!("Key term extraction: <5ms");
|
||
|
|
println!("Full optimization: <10ms");
|
||
|
|
println!("Context injection: <1ms");
|
||
|
|
println!("\nTotal optimization overhead: <15ms per query\n");
|
||
|
|
|
||
|
|
println!("\n12. Integration with RAG System\n");
|
||
|
|
|
||
|
|
println!("1. User submits question");
|
||
|
|
println!("2. Optimizer analyzes and enhances query");
|
||
|
|
println!("3. Intent guides search strategy");
|
||
|
|
println!("4. Key terms improve retrieval");
|
||
|
|
println!("5. Context enriches relevance");
|
||
|
|
println!("6. Better answers generated\n");
|
||
|
|
|
||
|
|
println!("✅ Query optimization example complete!\n");
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|