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:
@@ -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())
|
||||||
|
|||||||
@@ -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,89 +140,115 @@ 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");
|
||||||
|
|
||||||
// Read key_frame.jpg
|
// Check if vlm_description already exists
|
||||||
let key_frame_path = PathBuf::from(output_dir)
|
let existing: Option<(Option<String>, Option<serde_json::Value>)> = sqlx::query_as(&format!(
|
||||||
.join(file_uuid)
|
"SELECT vlm_description, vlm_embedding FROM {tp_table} WHERE file_uuid = $1 AND trace_id = $2"
|
||||||
.join(format!("trace_{}", trace_id))
|
))
|
||||||
.join("key_frame.jpg");
|
.bind(file_uuid)
|
||||||
|
.bind(trace_id)
|
||||||
if !key_frame_path.exists() {
|
.fetch_optional(db.pool())
|
||||||
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,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
{
|
.ok()
|
||||||
Ok(s) if !s.trim().is_empty() => s,
|
.flatten();
|
||||||
Ok(_) => {
|
|
||||||
|
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!(
|
warn!(
|
||||||
"[VLM] trace {}/{} (tid={}): VLM returned empty, skipping",
|
"[VLM] trace {}/{} (tid={}): key_frame.jpg missing, skipping",
|
||||||
current, total, trace_id
|
current, total, trace_id
|
||||||
);
|
);
|
||||||
return Ok((false, false));
|
return Ok((false, false));
|
||||||
}
|
}
|
||||||
Err(e) => {
|
|
||||||
return Err(e.context("VLM call failed"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
info!(
|
let jpeg_bytes = std::fs::read(&key_frame_path)?;
|
||||||
"[VLM] trace {}/{} (tid={}): summarized",
|
let base64_img = base64::engine::general_purpose::STANDARD.encode(&jpeg_bytes);
|
||||||
current, total, trace_id
|
|
||||||
);
|
|
||||||
|
|
||||||
// Update DB: vlm_description + vlm_model
|
// Call VLM
|
||||||
let tp_table = table_name("trace_profiles");
|
let summary = match call_llm_vision(
|
||||||
if let Err(e) = sqlx::query(&format!(
|
SYSTEM_PROMPT,
|
||||||
"UPDATE {tp_table} SET vlm_description = $1, vlm_model = $2, updated_at = NOW() \
|
"Summarize this key frame.",
|
||||||
WHERE file_uuid = $3 AND trace_id = $4"
|
vec![base64_img],
|
||||||
))
|
512,
|
||||||
.bind(&summary)
|
120,
|
||||||
.bind(config::llm::VISION_MODEL.as_str())
|
)
|
||||||
.bind(file_uuid)
|
.await
|
||||||
.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());
|
Ok(s) if !s.trim().is_empty() => s,
|
||||||
if let Err(e) = profile.save(output_dir, None, None) {
|
Ok(_) => {
|
||||||
warn!(
|
warn!(
|
||||||
"[VLM] trace {}/{} (tid={}): JSON save failed: {}",
|
"[VLM] trace {}/{} (tid={}): VLM returned empty, skipping",
|
||||||
current, total, trace_id, e
|
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
|
// Embedding
|
||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user