#[cfg(feature = "embeddings")]
use tracing::{debug, info};
const HYDE_PROMPT: &str = "Write a short, factual paragraph that would be a perfect answer to \
this question. Do not include any preamble or meta-commentary. \
Just write the answer directly.\n\nQuestion: {query}\n\nAnswer:";
pub fn should_use_hyde(query: &str, intent_confidence: f64) -> bool {
let word_count = query.split_whitespace().count();
intent_confidence < 0.6 && word_count < 6
}
pub fn hyde_prompt(query: &str) -> String {
HYDE_PROMPT.replace("{query}", query)
}
#[cfg(feature = "embeddings")]
pub async fn hyde_embed(
query: &str,
ollama_response: Option<&str>,
) -> Result<Vec<f32>, crate::error::FetchiumError> {
let text_to_embed = ollama_response.unwrap_or(query);
debug!(
query = query,
using_hypothetical = ollama_response.is_some(),
"HyDE embed"
);
crate::embeddings::embed(text_to_embed)
}
#[cfg(feature = "embeddings")]
pub async fn smart_embed(
query: &str,
intent_confidence: f64,
ollama_response: Option<&str>,
) -> Result<Vec<f32>, crate::error::FetchiumError> {
if should_use_hyde(query, intent_confidence) && ollama_response.is_some() {
info!(query = query, "Applying HyDE for ambiguous query");
hyde_embed(query, ollama_response).await
} else {
debug!(query = query, "Direct query embedding (no HyDE)");
crate::embeddings::embed(query)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_low_confidence_uses_hyde() {
assert!(should_use_hyde("Apple", 0.3));
}
#[test]
fn long_query_no_hyde() {
assert!(!should_use_hyde(
"What is the difference between Rust and Go for systems programming",
0.4
));
}
#[test]
fn high_confidence_no_hyde() {
assert!(!should_use_hyde("Paris", 0.9));
}
#[test]
fn hyde_prompt_contains_query() {
let prompt = hyde_prompt("What is Rust?");
assert!(prompt.contains("What is Rust?"));
assert!(prompt.contains("Answer:"));
}
}