def insert_data_into_qdrant()

in 11-recommendation/upload-to-qdrant.py [0:0]


def insert_data_into_qdrant(data_entries: List[Dict[str, Any]]):
    """
    Inserts data into the Qdrant collection using precomputed embeddings from the CSV.
    """
    points = []
    for idx, entry in enumerate(data_entries):
        embedded_description = entry.get('embedded_description')
        if not isinstance(embedded_description, list):
            if embedded_description:
                try:
                    embedded_description = ast.literal_eval(embedded_description)
                    if not isinstance(embedded_description, list):
                        logger.warning(f"Embedding for row {idx} is not a list. Keeping original value.")
                except (ValueError, SyntaxError) as e:
                    logger.error(f"Error parsing embedding for row {idx}: {e}. Keeping original value.")
            else:
                logger.warning(f"Missing embedding for row {idx}. Converting to empty list.")
                embedded_description = []
        entry['embedded_description'] = embedded_description

        point = PointStruct(
            id=idx,
            vector=embedded_description,
            payload={
                    "product_id": entry.get("id"),
                    "product_name": entry.get("product_name"),
                    "category": entry.get("category"),
                    "price": entry.get("price"),
                    "detailed_description": entry.get("detailed_description"),
                }
            )
        points.append(point)

        # Batch insertion
        if len(points) >= Config.BATCH_SIZE:
            try:
                qdrant_client.upsert(
                    collection_name=Config.COLLECTION_NAME,
                    points=points
                )
                logger.info(f"Inserted batch of {len(points)} points into Qdrant.")
                points = []
            except Exception as e:
                logger.error(f"Failed to upsert batch to Qdrant: {e}")

    # Insert any remaining points
    if points:
        try:
            qdrant_client.upsert(
                collection_name=Config.COLLECTION_NAME,
                points=points
            )
            logger.info(f"Inserted final batch of {len(points)} points into Qdrant.")
        except Exception as e:
            logger.error(f"Failed to upsert final batch to Qdrant: {e}")