use std::sync::Arc;
use async_trait::async_trait;
use crate::vectorstore::VectorStore;
#[async_trait]
pub trait ContextEnhancer: Send + Sync {
async fn enhance(&self, question: &str) -> String;
}
pub struct RagEnhancer {
store: Arc<dyn VectorStore>,
}
impl RagEnhancer {
pub fn new(store: Arc<dyn VectorStore>) -> Self {
Self { store }
}
}
#[async_trait]
impl ContextEnhancer for RagEnhancer {
async fn enhance(&self, question: &str) -> String {
let ddl = self.store.get_related_ddl(question).await.unwrap_or_default();
let docs = self.store.get_related_documentation(question).await.unwrap_or_default();
let examples = self.store.get_similar_question_sql(question).await.unwrap_or_default();
let mut out = String::new();
if !ddl.is_empty() {
out.push_str("\n===Tables\n");
for d in &ddl {
out.push_str(d);
out.push_str("\n\n");
}
}
if !docs.is_empty() {
out.push_str("\n===Additional Context\n\n");
for d in &docs {
out.push_str(d);
out.push_str("\n\n");
}
}
if !examples.is_empty() {
out.push_str("\n===Example question/SQL pairs (for reference)\n\n");
for ex in &examples {
out.push_str(&format!("Q: {}\nSQL: {}\n\n", ex.question, ex.sql));
}
}
out
}
}