use anyhow::{Context, Result}; use momentry_core::{Database, Embedder, PostgresDb, QdrantDb, VectorPayload}; use std::time::{Duration, Instant}; #[tokio::main] async fn main() -> Result<()> { let pg = PostgresDb::init() .await .context("Failed to init PostgreSQL")?; let qdrant = QdrantDb::new(); let embedder = Embedder::new("embeddinggemma-300m".to_string()); let chunk_table = momentry_core::core::db::schema::table_name("chunk"); // Get all files with unembedded sentence chunks let files: Vec<(String,)> = sqlx::query_as(&format!( "SELECT DISTINCT file_uuid FROM {} \ WHERE chunk_type = 'sentence' AND embedding IS NULL \ AND text_content IS NOT NULL AND text_content != ''", chunk_table )) .fetch_all(pg.pool()) .await?; println!( "Found {} files with unembedded sentence chunks", files.len() ); let start = Instant::now(); let mut total_stored = 0usize; let mut total_errors = 0usize; let mut total_chunks = 0usize; for (file_uuid,) in &files { let rows: Vec<(String, String, i64, i64, f64, f64)> = sqlx::query_as(&format!( "SELECT chunk_id, text_content, start_frame, end_frame, start_time, end_time \ FROM {} \ WHERE file_uuid = $1 \ AND chunk_type = 'sentence' \ AND embedding IS NULL \ AND text_content IS NOT NULL AND text_content != '' \ ORDER BY id", chunk_table )) .bind(file_uuid) .fetch_all(pg.pool()) .await?; let count = rows.len(); total_chunks += count; println!(" {}: {} chunks to vectorize", file_uuid, count); if count == 0 { continue; } for (chunk_id, text, start_frame, end_frame, start_time, end_time) in &rows { if text.is_empty() { continue; } match embedder.embed_document(text).await { Ok(vector) => { if let Err(e) = pg.store_vector(&chunk_id, &vector, file_uuid).await { eprintln!("[ERROR] PG store failed for {}: {}", chunk_id, e); total_errors += 1; continue; } let payload = VectorPayload { file_uuid: file_uuid.clone(), chunk_id: chunk_id.clone(), chunk_type: "sentence".to_string(), start_frame: *start_frame, end_frame: *end_frame, start_time: *start_time, end_time: *end_time, text: Some(text.clone()), }; if let Err(e) = qdrant.upsert_vector(&chunk_id, &vector, payload).await { eprintln!("[ERROR] Qdrant upsert failed for {}: {}", chunk_id, e); total_errors += 1; continue; } total_stored += 1; if total_stored % 500 == 0 { let elapsed = start.elapsed(); let rate = total_stored as f64 / elapsed.as_secs_f64(); println!( " [{}/{}] {:.1} vec/s | {} errors", total_stored, total_chunks, rate, total_errors ); } } Err(e) => { eprintln!("[ERROR] Embedding failed for {}: {}", chunk_id, e); total_errors += 1; } } } } let elapsed = start.elapsed(); println!( "\nDone! {}/{} vectors stored ({} errors) in {:.1}s", total_stored, total_chunks, total_errors, elapsed.as_secs_f64(), ); Ok(()) }