feat: VLM summary vector storage in Qdrant for semantic search
- Add upsert_vlm_embedding to QdrantDb with trace_id in payload
- chunk_id format: {file_uuid}_t{trace_id_hex} for search enrichment
- Update enrich_vlm_from_pg to extract trace_id from chunk_id
- VLM summary now searchable via semantic search with source_type=vlm
This commit is contained in:
+19
-10
@@ -143,25 +143,34 @@ async fn enrich_from_pg(
|
||||
}
|
||||
}
|
||||
|
||||
/// Enrich a VLM summary search result from trace_profiles, looked up by vlm_point_id.
|
||||
/// Enrich a VLM summary search result from trace_profiles.
|
||||
/// The chunk_id from Qdrant is in format "{file_uuid}_t{trace_id_hex}".
|
||||
async fn enrich_vlm_from_pg(
|
||||
db: &crate::core::db::PostgresDb,
|
||||
file_uuid: &str,
|
||||
point_id: &str,
|
||||
chunk_id: &str,
|
||||
qdrant_score: f32,
|
||||
) -> Option<SearchResult> {
|
||||
// Extract trace_id from chunk_id format: "{file_uuid}_t{trace_id_hex}"
|
||||
let trace_id: i32 = if chunk_id.len() > 34 && chunk_id.starts_with(file_uuid) {
|
||||
let hex_part = &chunk_id[33..]; // after "{file_uuid}_t"
|
||||
i32::from_str_radix(hex_part, 16).ok()?
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
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"
|
||||
let rows: Vec<(i64, i64, Option<String>)> = sqlx::query_as(&format!(
|
||||
"SELECT start_frame, end_frame, vlm_description FROM {tp_table} \
|
||||
WHERE file_uuid = $1 AND trace_id = $2"
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.bind(point_id)
|
||||
.bind(trace_id)
|
||||
.fetch_all(db.pool())
|
||||
.await
|
||||
.ok()?;
|
||||
|
||||
let (tid, start_frame, end_frame, vlm_desc) = rows.into_iter().next()?;
|
||||
let (start_frame, end_frame, vlm_desc) = rows.into_iter().next()?;
|
||||
let text = vlm_desc.unwrap_or_default();
|
||||
|
||||
Some(SearchResult {
|
||||
@@ -175,16 +184,16 @@ async fn enrich_vlm_from_pg(
|
||||
start_time: 0.0,
|
||||
end_time: 0.0,
|
||||
raw_text: None,
|
||||
summary: Some(format!("VLM trace_{}", tid)),
|
||||
summary: Some(format!("VLM trace_{}", trace_id)),
|
||||
text_content: Some(text),
|
||||
metadata: Some(serde_json::json!({
|
||||
"type": "vlm_summary",
|
||||
"trace_id": tid,
|
||||
"trace_id": trace_id,
|
||||
})),
|
||||
similarity: Some(qdrant_score as f64),
|
||||
file_name: None,
|
||||
serve_url: None,
|
||||
thumbnail_url: Some(format!("/api/v1/file/{}/trace/{}/thumbnail", file_uuid, tid)),
|
||||
thumbnail_url: Some(format!("/api/v1/file/{}/trace/{}/thumbnail", file_uuid, trace_id)),
|
||||
source_type: Some("vlm".to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -887,6 +887,50 @@ impl QdrantDb {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store VLM summary embedding with trace_id in payload
|
||||
pub async fn upsert_vlm_embedding(
|
||||
&self,
|
||||
point_id: u64,
|
||||
vector: &[f32],
|
||||
file_uuid: &str,
|
||||
trace_id: i32,
|
||||
text: &str,
|
||||
) -> Result<()> {
|
||||
let url = format!(
|
||||
"{}/collections/{}/points?wait=true",
|
||||
self.base_url, self.collection_name
|
||||
);
|
||||
// chunk_id in format "{file_uuid}_t{trace_id_hex}" for search enrichment
|
||||
let chunk_id = format!("{}_t{:x}", file_uuid, trace_id);
|
||||
let mut payload_map = std::collections::HashMap::new();
|
||||
payload_map.insert("file_uuid".to_string(), serde_json::json!(file_uuid));
|
||||
payload_map.insert("chunk_id".to_string(), serde_json::json!(chunk_id));
|
||||
payload_map.insert("chunk_type".to_string(), serde_json::json!("vlm_summary"));
|
||||
payload_map.insert("text".to_string(), serde_json::json!(text));
|
||||
payload_map.insert("trace_id".to_string(), serde_json::json!(trace_id));
|
||||
payload_map.insert("start_time".to_string(), serde_json::json!(0.0));
|
||||
payload_map.insert("end_time".to_string(), serde_json::json!(0.0));
|
||||
|
||||
let point = serde_json::json!({
|
||||
"points": [{
|
||||
"id": point_id,
|
||||
"vector": vector,
|
||||
"payload": payload_map
|
||||
}]
|
||||
});
|
||||
let resp = self
|
||||
.client
|
||||
.put(&url)
|
||||
.header("api-key", &self.api_key)
|
||||
.json(&point)
|
||||
.send()
|
||||
.await?;
|
||||
if !resp.status().is_success() {
|
||||
anyhow::bail!("Qdrant upsert VLM failed: {}", resp.status());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Scroll points matching a filter, returning payload data (single page)
|
||||
pub async fn scroll_points(
|
||||
&self,
|
||||
|
||||
@@ -237,11 +237,13 @@ async fn process_single_trace(
|
||||
}
|
||||
};
|
||||
|
||||
// Generate Qdrant point_id (unique u64 hash)
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
// 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();
|
||||
use std::hash::{Hash, Hasher};
|
||||
let mut hasher = DefaultHasher::new();
|
||||
format!("{}_{}", file_uuid, trace_id).hash(&mut hasher);
|
||||
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
||||
point_id_str.hash(&mut hasher);
|
||||
let point_id = hasher.finish();
|
||||
|
||||
// Store embedding in DB
|
||||
@@ -261,17 +263,15 @@ async fn process_single_trace(
|
||||
);
|
||||
}
|
||||
|
||||
// Store in Qdrant (using u64 ID)
|
||||
// Store in Qdrant (using u64 ID) with trace_id in payload
|
||||
let qdrant = QdrantDb::new();
|
||||
if let Err(e) = qdrant
|
||||
.upsert_chunk_embedding_u64(
|
||||
.upsert_vlm_embedding(
|
||||
point_id,
|
||||
&vector,
|
||||
"vlm_summary",
|
||||
file_uuid,
|
||||
trace_id,
|
||||
&summary,
|
||||
0.0,
|
||||
0.0,
|
||||
)
|
||||
.await
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user