#!/usr/bin/env python3 """ Sync existing PostgreSQL memories to Qdrant vector database """ import asyncio import logging from datetime import datetime, timezone from database.connection import init_database, get_db_session from database.models import Memory, Character from rag.vector_store import VectorStoreManager, VectorMemory, MemoryType from sqlalchemy import select logger = logging.getLogger(__name__) async def sync_memories_to_qdrant(): """Sync all existing memories from PostgreSQL to Qdrant""" # Initialize database await init_database() # Initialize vector store vector_store = VectorStoreManager() print("🔄 Starting memory sync to Qdrant...") async with get_db_session() as session: # Get all memories with character names query = select(Memory, Character.name).join( Character, Memory.character_id == Character.id ).order_by(Memory.timestamp) results = await session.execute(query) memories_with_chars = results.fetchall() print(f"Found {len(memories_with_chars)} memories to sync") synced_count = 0 error_count = 0 for memory, character_name in memories_with_chars: try: # Convert to vector memory format vector_memory = VectorMemory( id=str(memory.id), character_name=character_name, content=memory.content, memory_type=MemoryType.PERSONAL, importance=memory.importance_score, timestamp=memory.timestamp or datetime.now(timezone.utc), metadata={ "tags": memory.tags or [], "memory_id": memory.id, "character_id": memory.character_id, "memory_type": memory.memory_type } ) # Store in vector database await vector_store.store_memory(vector_memory) synced_count += 1 if synced_count % 10 == 0: print(f" Synced {synced_count}/{len(memories_with_chars)} memories...") except Exception as e: error_count += 1 print(f" Error syncing memory {memory.id}: {e}") print(f"✅ Sync complete: {synced_count} synced, {error_count} errors") if __name__ == "__main__": asyncio.run(sync_memories_to_qdrant())