feat: VLM summary vector search integration

- Add vlm_point_id column to trace_profiles (stores Qdrant point ID)
- Add upsert_chunk_embedding_u64 to QdrantDb (numeric ID support)
- Add chunk_id to VLM payload in Qdrant for search enrichment
- Update enrich_from_pg to fallback to VLM lookup when chunk not found
- Add /api/v1/file/:uuid/vlm-summary endpoint for manual trigger
- Fix VLM point_id: use u64 hash instead of string (Qdrant requirement)
- Fix VLM timeout: was 0 (immediate timeout), now 120s

Verified: semantic search returns [vlm] results with proper enrichment
This commit is contained in:
Accusys
2026-08-22 12:10:47 +08:00
parent 476a31ecc6
commit 054545bccd
5 changed files with 184 additions and 10 deletions
+71
View File
@@ -878,6 +878,73 @@ async fn verify_file_handler(
Json(report)
}
/// Manually trigger VLM summary generation for an existing file.
/// Non-blocking: returns immediately, VLM runs in background.
async fn trigger_vlm_summary(
State(state): State<AppState>,
Path(file_uuid): Path<String>,
) -> Json<serde_json::Value> {
let db = state.db.clone();
let output_dir = crate::core::config::OUTPUT_DIR.clone();
// Check if file has face traces
let tp_table = crate::core::db::schema::table_name("trace_profiles");
let face_count: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1"
))
.bind(&file_uuid)
.fetch_one(db.pool())
.await
.unwrap_or(0);
if face_count == 0 {
return Json(serde_json::json!({
"success": false,
"message": "No face traces found for this file",
"file_uuid": file_uuid,
}));
}
// Check how many traces already have vlm_description
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 != ''"
))
.bind(&file_uuid)
.fetch_one(db.pool())
.await
.unwrap_or(0);
let pending = face_count - done_count;
if pending == 0 {
return Json(serde_json::json!({
"success": true,
"message": "All traces already have VLM summaries",
"file_uuid": file_uuid,
"total_traces": face_count,
"already_done": done_count,
}));
}
// Spawn VLM generation in background
let file_uuid_clone = file_uuid.clone();
tokio::spawn(async move {
let redis = crate::core::db::RedisClient::new().ok();
crate::core::processor::vlm_summary::generate_vlm_summaries(
&db, &file_uuid_clone, &output_dir, redis.map(std::sync::Arc::new),
)
.await;
});
Json(serde_json::json!({
"success": true,
"message": format!("VLM summary generation started for {} traces", pending),
"file_uuid": file_uuid,
"total_traces": face_count,
"already_done": done_count,
"pending": pending,
}))
}
pub fn processing_routes() -> Router<AppState> {
Router::new()
.route("/api/v1/file/:file_uuid/process", post(trigger_processing))
@@ -903,4 +970,8 @@ pub fn processing_routes() -> Router<AppState> {
get(get_processor_counts),
)
.route("/api/v1/file/:file_uuid/verify", get(verify_file_handler))
.route(
"/api/v1/file/:file_uuid/vlm-summary",
post(trigger_vlm_summary),
)
}
+52 -2
View File
@@ -103,7 +103,8 @@ fn map_chunk_type_to_source(chunk_type: &str) -> String {
}
}
/// Enrich a Qdrant search result with full data from PostgreSQL
/// Enrich a Qdrant search result with full data from PostgreSQL.
/// Falls back to trace_profiles lookup for VLM summary results (matched by vlm_point_id).
async fn enrich_from_pg(
db: &crate::core::db::PostgresDb,
file_uuid: &str,
@@ -131,7 +132,10 @@ async fn enrich_from_pg(
thumbnail_url: None,
source_type: Some(map_chunk_type_to_source(&p.chunk_type)),
}),
Ok(None) => None,
Ok(None) => {
// Chunk not found — try VLM summary lookup by vlm_point_id
enrich_vlm_from_pg(db, file_uuid, chunk_id, qdrant_score).await
}
Err(e) => {
tracing::warn!("PG enrichment failed for {} {}: {}", file_uuid, chunk_id, e);
None
@@ -139,6 +143,52 @@ async fn enrich_from_pg(
}
}
/// Enrich a VLM summary search result from trace_profiles, looked up by vlm_point_id.
async fn enrich_vlm_from_pg(
db: &crate::core::db::PostgresDb,
file_uuid: &str,
point_id: &str,
qdrant_score: f32,
) -> Option<SearchResult> {
let tp_table = crate::core::db::schema::table_name("trace_profiles");
let rows: Vec<(i32, i64, i64, Option<String>)> = sqlx::query_as(&format!(
"SELECT trace_id, start_frame, end_frame, vlm_description FROM {tp_table} \
WHERE file_uuid = $1 AND vlm_point_id = $2"
))
.bind(file_uuid)
.bind(point_id)
.fetch_all(db.pool())
.await
.ok()?;
let (tid, start_frame, end_frame, vlm_desc) = rows.into_iter().next()?;
let text = vlm_desc.unwrap_or_default();
Some(SearchResult {
id: 0,
file_uuid: Some(file_uuid.to_string()),
parent_id: 0,
scene_order: None,
start_frame,
end_frame,
fps: 0.0,
start_time: 0.0,
end_time: 0.0,
raw_text: None,
summary: Some(format!("VLM trace_{}", tid)),
text_content: Some(text),
metadata: Some(serde_json::json!({
"type": "vlm_summary",
"trace_id": tid,
})),
similarity: Some(qdrant_score as f64),
file_name: None,
serve_url: None,
thumbnail_url: Some(format!("/api/v1/file/{}/trace/{}/thumbnail", file_uuid, tid)),
source_type: Some("vlm".to_string()),
})
}
fn pg_result_to_search(p: &SemanticSearchResult) -> SearchResult {
SearchResult {
id: 0,