Skip to content

Commit 9017388

Browse files
committed
fix(search): wire LLM cache into search_with_intelligence
When an orchestrator is present, compute a SHA256 cache key from the query and check the llm_cache table first. On miss, call the orchestrator and store the result. Adds Serialize/Deserialize to QueryIntent and OrchestrationResult for JSON round-tripping. Removes #[allow(dead_code)] from orchestration_cache_key.
1 parent 7c6ba89 commit 9017388

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/llm.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl PromptFormat {
7171
// ── Types ────────────────────────────────────────────────────────────────────
7272

7373
/// Classified intent of an incoming search query.
74-
#[derive(Debug, Clone, PartialEq, Eq)]
74+
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
7575
pub enum QueryIntent {
7676
/// User wants a precise fact or term match.
7777
Exact,
@@ -84,7 +84,7 @@ pub enum QueryIntent {
8484
}
8585

8686
/// Output produced by an orchestrator model for a query.
87-
#[derive(Debug, Clone)]
87+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
8888
pub struct OrchestrationResult {
8989
/// Classified query intent.
9090
pub intent: QueryIntent,
@@ -566,11 +566,15 @@ fn try_external_tokenizer(uri: &HfModelUri, models_dir: &Path) -> Option<tokeniz
566566
};
567567
let tok_path = tok_uri.cache_path(models_dir);
568568

569-
if tok_path.exists() && let Ok(tok) = tokenizers::Tokenizer::from_file(&tok_path) {
569+
if tok_path.exists()
570+
&& let Ok(tok) = tokenizers::Tokenizer::from_file(&tok_path)
571+
{
570572
return Some(tok);
571573
}
572574

573-
if let Ok(p) = ensure_model(&tok_uri, models_dir) && let Ok(tok) = tokenizers::Tokenizer::from_file(&p) {
575+
if let Ok(p) = ensure_model(&tok_uri, models_dir)
576+
&& let Ok(tok) = tokenizers::Tokenizer::from_file(&p)
577+
{
574578
return Some(tok);
575579
}
576580
}
@@ -1473,7 +1477,6 @@ impl CandleRerank {
14731477
no_token_id,
14741478
})
14751479
}
1476-
14771480
}
14781481

14791482
impl RerankModel for CandleRerank {

src/search.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::llm::{self, EmbedModel, OrchestratorModel, RerankModel};
1010
use crate::store::{Store, StoreStats};
1111

1212
/// Compute cache key for orchestration results (SHA256 of query).
13-
#[allow(dead_code)]
1413
fn orchestration_cache_key(query: &str) -> String {
1514
use sha2::{Digest, Sha256};
1615
let hash = Sha256::digest(query.as_bytes());
@@ -85,9 +84,25 @@ pub fn search_with_intelligence(
8584
embedder: &mut impl EmbedModel,
8685
config: &mut SearchConfig<'_>,
8786
) -> Result<SearchOutput> {
88-
// --- Step 1: Orchestrate ---
87+
// --- Step 1: Orchestrate (with LLM cache when orchestrator is present) ---
8988
let orchestration = match &mut config.orchestrator {
90-
Some(orch) => orch.orchestrate(query)?,
89+
Some(orch) => {
90+
let cache_key = orchestration_cache_key(query);
91+
if let Some(cached_json) = config.store.get_llm_cache(&cache_key)? {
92+
serde_json::from_str(&cached_json).unwrap_or_else(|_| {
93+
orch.orchestrate(query)
94+
.unwrap_or_else(|_| llm::heuristic_orchestrate(query))
95+
})
96+
} else {
97+
let result = orch.orchestrate(query)?;
98+
if let Ok(json) = serde_json::to_string(&result) {
99+
let _ = config
100+
.store
101+
.set_llm_cache(&cache_key, &json, "orchestrator");
102+
}
103+
result
104+
}
105+
}
91106
None => llm::heuristic_orchestrate(query),
92107
};
93108
let weights = llm::LaneWeights::from_intent(&orchestration.intent);

0 commit comments

Comments
 (0)