fix: VLM re-embedding support + API checks vlm_embedding

- vlm_summary now skips VLM call if description exists, but still generates embedding + Qdrant
- API endpoint checks both vlm_description AND vlm_embedding before saying 'already done'
- Fixed point_id hash generation (was creating 0, now properly hashes)
This commit is contained in:
Accusys
2026-08-25 08:11:48 +08:00
parent 29f14e83c4
commit 8a34dcb8f2
2 changed files with 105 additions and 78 deletions
+4 -2
View File
@@ -905,9 +905,11 @@ async fn trigger_vlm_summary(
})); }));
} }
// Check how many traces already have vlm_description // Check how many traces need VLM and/or embedding
let done_count: i64 = sqlx::query_scalar(&format!( let done_count: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1 AND vlm_description IS NOT NULL AND vlm_description != ''" "SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1 \
AND vlm_description IS NOT NULL AND vlm_description != '' \
AND vlm_embedding IS NOT NULL"
)) ))
.bind(&file_uuid) .bind(&file_uuid)
.fetch_one(db.pool()) .fetch_one(db.pool())
+33 -8
View File
@@ -37,11 +37,12 @@ pub async fn generate_vlm_summaries(
) -> VlmSummaryResult { ) -> VlmSummaryResult {
let tp_table = table_name("trace_profiles"); let tp_table = table_name("trace_profiles");
// Find traces without vlm_description // Find traces that need VLM and/or embedding
// Traces need processing if: no vlm_description, OR no vlm_embedding (need Qdrant vector)
let sql = format!( let sql = format!(
"SELECT trace_id FROM {tp_table} \ "SELECT trace_id FROM {tp_table} \
WHERE file_uuid = $1 \ WHERE file_uuid = $1 \
AND (vlm_description IS NULL OR vlm_description = '') \ AND (vlm_description IS NULL OR vlm_description = '' OR vlm_embedding IS NULL) \
ORDER BY trace_id" ORDER BY trace_id"
); );
@@ -139,7 +140,24 @@ async fn process_single_trace(
total: usize, total: usize,
) -> Result<(bool, bool)> { ) -> Result<(bool, bool)> {
let trace_id_i64 = trace_id as i64; let trace_id_i64 = trace_id as i64;
let tp_table = table_name("trace_profiles");
// Check if vlm_description already exists
let existing: Option<(Option<String>, Option<serde_json::Value>)> = sqlx::query_as(&format!(
"SELECT vlm_description, vlm_embedding FROM {tp_table} WHERE file_uuid = $1 AND trace_id = $2"
))
.bind(file_uuid)
.bind(trace_id)
.fetch_optional(db.pool())
.await
.ok()
.flatten();
let (existing_desc, existing_embed) = existing.unwrap_or((None, None));
let need_vlm = existing_desc.is_none() || existing_desc.as_ref().map(|s| s.is_empty()).unwrap_or(true);
let need_embed = existing_embed.is_none();
let summary = if need_vlm {
// Read key_frame.jpg // Read key_frame.jpg
let key_frame_path = PathBuf::from(output_dir) let key_frame_path = PathBuf::from(output_dir)
.join(file_uuid) .join(file_uuid)
@@ -186,7 +204,6 @@ async fn process_single_trace(
); );
// Update DB: vlm_description + vlm_model // Update DB: vlm_description + vlm_model
let tp_table = table_name("trace_profiles");
if let Err(e) = sqlx::query(&format!( if let Err(e) = sqlx::query(&format!(
"UPDATE {tp_table} SET vlm_description = $1, vlm_model = $2, updated_at = NOW() \ "UPDATE {tp_table} SET vlm_description = $1, vlm_model = $2, updated_at = NOW() \
WHERE file_uuid = $3 AND trace_id = $4" WHERE file_uuid = $3 AND trace_id = $4"
@@ -224,6 +241,16 @@ async fn process_single_trace(
} }
} }
summary
} else {
existing_desc.unwrap_or_default()
};
// Generate embedding and store in Qdrant if needed
if !need_embed {
return Ok((!need_vlm, true));
}
// Embedding // Embedding
let embedder = Embedder::new("embeddinggemma-300m".to_string()); let embedder = Embedder::new("embeddinggemma-300m".to_string());
let vector = match embedder.embed_document(&summary).await { let vector = match embedder.embed_document(&summary).await {
@@ -233,15 +260,13 @@ async fn process_single_trace(
"[VLM] trace {}/{} (tid={}): embedding failed: {} (text saved)", "[VLM] trace {}/{} (tid={}): embedding failed: {} (text saved)",
current, total, trace_id, e current, total, trace_id, e
); );
return Ok((true, false)); return Ok((!need_vlm, false));
} }
}; };
// Generate Qdrant point_id in format: {file_uuid}_t{trace_id_hex} // Generate Qdrant point_id: hash of "{file_uuid}_t{trace_id_hex}"
// This matches the format expected by enrich_vlm_from_pg in search.rs
let point_id_str = format!("{}_t{:x}", file_uuid, trace_id);
let point_id: u64 = std::collections::hash_map::DefaultHasher::new().finish();
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
let point_id_str = format!("{}_t{:x}", file_uuid, trace_id);
let mut hasher = std::collections::hash_map::DefaultHasher::new(); let mut hasher = std::collections::hash_map::DefaultHasher::new();
point_id_str.hash(&mut hasher); point_id_str.hash(&mut hasher);
let point_id = hasher.finish(); let point_id = hasher.finish();