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:
@@ -37,11 +37,12 @@ pub async fn generate_vlm_summaries(
|
||||
) -> VlmSummaryResult {
|
||||
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!(
|
||||
"SELECT trace_id FROM {tp_table} \
|
||||
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"
|
||||
);
|
||||
|
||||
@@ -139,89 +140,115 @@ async fn process_single_trace(
|
||||
total: usize,
|
||||
) -> Result<(bool, bool)> {
|
||||
let trace_id_i64 = trace_id as i64;
|
||||
let tp_table = table_name("trace_profiles");
|
||||
|
||||
// Read key_frame.jpg
|
||||
let key_frame_path = PathBuf::from(output_dir)
|
||||
.join(file_uuid)
|
||||
.join(format!("trace_{}", trace_id))
|
||||
.join("key_frame.jpg");
|
||||
|
||||
if !key_frame_path.exists() {
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): key_frame.jpg missing, skipping",
|
||||
current, total, trace_id
|
||||
);
|
||||
return Ok((false, false));
|
||||
}
|
||||
|
||||
let jpeg_bytes = std::fs::read(&key_frame_path)?;
|
||||
let base64_img = base64::engine::general_purpose::STANDARD.encode(&jpeg_bytes);
|
||||
|
||||
// Call VLM
|
||||
let summary = match call_llm_vision(
|
||||
SYSTEM_PROMPT,
|
||||
"Summarize this key frame.",
|
||||
vec![base64_img],
|
||||
512,
|
||||
120,
|
||||
)
|
||||
// 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(s) if !s.trim().is_empty() => s,
|
||||
Ok(_) => {
|
||||
.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
|
||||
let key_frame_path = PathBuf::from(output_dir)
|
||||
.join(file_uuid)
|
||||
.join(format!("trace_{}", trace_id))
|
||||
.join("key_frame.jpg");
|
||||
|
||||
if !key_frame_path.exists() {
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): VLM returned empty, skipping",
|
||||
"[VLM] trace {}/{} (tid={}): key_frame.jpg missing, skipping",
|
||||
current, total, trace_id
|
||||
);
|
||||
return Ok((false, false));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e.context("VLM call failed"));
|
||||
}
|
||||
};
|
||||
|
||||
info!(
|
||||
"[VLM] trace {}/{} (tid={}): summarized",
|
||||
current, total, trace_id
|
||||
);
|
||||
let jpeg_bytes = std::fs::read(&key_frame_path)?;
|
||||
let base64_img = base64::engine::general_purpose::STANDARD.encode(&jpeg_bytes);
|
||||
|
||||
// Update DB: vlm_description + vlm_model
|
||||
let tp_table = table_name("trace_profiles");
|
||||
if let Err(e) = sqlx::query(&format!(
|
||||
"UPDATE {tp_table} SET vlm_description = $1, vlm_model = $2, updated_at = NOW() \
|
||||
WHERE file_uuid = $3 AND trace_id = $4"
|
||||
))
|
||||
.bind(&summary)
|
||||
.bind(config::llm::VISION_MODEL.as_str())
|
||||
.bind(file_uuid)
|
||||
.bind(trace_id)
|
||||
.execute(db.pool())
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): DB update failed: {}",
|
||||
current, total, trace_id, e
|
||||
);
|
||||
}
|
||||
|
||||
// Update on-disk trace_profile.json
|
||||
let profile_path = crate::core::file_profile::FaceTraceProfile::profile_path(
|
||||
output_dir,
|
||||
file_uuid,
|
||||
trace_id_i64,
|
||||
);
|
||||
if profile_path.exists() {
|
||||
if let Some(mut profile) =
|
||||
crate::core::file_profile::FaceTraceProfile::load(output_dir, file_uuid, trace_id_i64)
|
||||
// Call VLM
|
||||
let summary = match call_llm_vision(
|
||||
SYSTEM_PROMPT,
|
||||
"Summarize this key frame.",
|
||||
vec![base64_img],
|
||||
512,
|
||||
120,
|
||||
)
|
||||
.await
|
||||
{
|
||||
profile.vlm_summary = Some(summary.clone());
|
||||
if let Err(e) = profile.save(output_dir, None, None) {
|
||||
Ok(s) if !s.trim().is_empty() => s,
|
||||
Ok(_) => {
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): JSON save failed: {}",
|
||||
current, total, trace_id, e
|
||||
"[VLM] trace {}/{} (tid={}): VLM returned empty, skipping",
|
||||
current, total, trace_id
|
||||
);
|
||||
return Ok((false, false));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(e.context("VLM call failed"));
|
||||
}
|
||||
};
|
||||
|
||||
info!(
|
||||
"[VLM] trace {}/{} (tid={}): summarized",
|
||||
current, total, trace_id
|
||||
);
|
||||
|
||||
// Update DB: vlm_description + vlm_model
|
||||
if let Err(e) = sqlx::query(&format!(
|
||||
"UPDATE {tp_table} SET vlm_description = $1, vlm_model = $2, updated_at = NOW() \
|
||||
WHERE file_uuid = $3 AND trace_id = $4"
|
||||
))
|
||||
.bind(&summary)
|
||||
.bind(config::llm::VISION_MODEL.as_str())
|
||||
.bind(file_uuid)
|
||||
.bind(trace_id)
|
||||
.execute(db.pool())
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): DB update failed: {}",
|
||||
current, total, trace_id, e
|
||||
);
|
||||
}
|
||||
|
||||
// Update on-disk trace_profile.json
|
||||
let profile_path = crate::core::file_profile::FaceTraceProfile::profile_path(
|
||||
output_dir,
|
||||
file_uuid,
|
||||
trace_id_i64,
|
||||
);
|
||||
if profile_path.exists() {
|
||||
if let Some(mut profile) =
|
||||
crate::core::file_profile::FaceTraceProfile::load(output_dir, file_uuid, trace_id_i64)
|
||||
{
|
||||
profile.vlm_summary = Some(summary.clone());
|
||||
if let Err(e) = profile.save(output_dir, None, None) {
|
||||
warn!(
|
||||
"[VLM] trace {}/{} (tid={}): JSON save failed: {}",
|
||||
current, total, trace_id, e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
summary
|
||||
} else {
|
||||
existing_desc.unwrap_or_default()
|
||||
};
|
||||
|
||||
// Generate embedding and store in Qdrant if needed
|
||||
if !need_embed {
|
||||
return Ok((!need_vlm, true));
|
||||
}
|
||||
|
||||
// Embedding
|
||||
@@ -233,15 +260,13 @@ async fn process_single_trace(
|
||||
"[VLM] trace {}/{} (tid={}): embedding failed: {} (text saved)",
|
||||
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}
|
||||
// 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();
|
||||
// Generate Qdrant point_id: hash of "{file_uuid}_t{trace_id_hex}"
|
||||
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();
|
||||
point_id_str.hash(&mut hasher);
|
||||
let point_id = hasher.finish();
|
||||
|
||||
Reference in New Issue
Block a user