From ba175dac94718fc7e0ba4b05f121c4a3311d3070 Mon Sep 17 00:00:00 2001 From: Nitin Kanukolanu Date: Fri, 17 Oct 2025 15:10:40 -0400 Subject: [PATCH 1/3] - Example notebooks - readme update - add them to ignore-notebooks.txt --- .github/ignore-notebooks.txt | 4 +- README.md | 2 + .../06_hnsw_to_svs_vamana_migration.ipynb | 1314 +++++++++++++++++ .../07_flat_to_svs_vamana_migration.ipynb | 1192 +++++++++++++++ 4 files changed, 2511 insertions(+), 1 deletion(-) create mode 100644 python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb create mode 100644 python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb diff --git a/.github/ignore-notebooks.txt b/.github/ignore-notebooks.txt index 55052688..61ba17de 100644 --- a/.github/ignore-notebooks.txt +++ b/.github/ignore-notebooks.txt @@ -7,4 +7,6 @@ 02_semantic_cache_optimization spring_ai_redis_rag.ipynb 00_litellm_proxy_redis.ipynb -04_redisvl_benchmarking_basics.ipynb \ No newline at end of file +04_redisvl_benchmarking_basics.ipynb +06_hnsw_to_svs_vamana_migration.ipynb +07_flat_to_svs_vamana_migration.ipynb \ No newline at end of file diff --git a/README.md b/README.md index a01de17f..6425baf0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Need quickstarts to begin your Redis AI journey? | ๐Ÿ”ข **Data Type Support** - Shows how to convert a float32 index to float16 or integer dataypes | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/03_dtype_support.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/03_dtype_support.ipynb) | | ๐Ÿ“Š **Benchmarking Basics** - Overview of search benchmarking basics with RedisVL and Python multiprocessing | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/04_redisvl_benchmarking_basics.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/04_redisvl_benchmarking_basics.ipynb) | | ๐Ÿ“Š **Multi Vector Search** - Overview of multi vector queries with RedisVL | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/05_multivector_search.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/05_multivector_search.ipynb) | +| ๐Ÿ—œ๏ธ **HNSW to SVS-VAMANA Migration** - Showcase how to migrate HNSW indices to SVS-VAMANA and demonstrate 50-75% memory savings benefits | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | +| ๐Ÿ—œ๏ธ **FLAT to SVS-VAMANA Migration** - Showcase how to migrate FLAT indices to SVS-VAMANA and demonstrate significant memory reduction benefits | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | ### Retrieval Augmented Generation (RAG) diff --git a/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb b/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb new file mode 100644 index 00000000..dbe20a7a --- /dev/null +++ b/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb @@ -0,0 +1,1314 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Redis](https://redis.io/wp-content/uploads/2024/04/Logotype.svg?auto=webp&quality=85,75&width=120)\n", + "# Migrating from HNSW to SVS-VAMANA\n", + "\n", + "## Let's Begin!\n", + "\"Open\n", + "\n", + "This notebook demonstrates how to migrate existing HNSW vector indices to SVS-VAMANA for improved memory efficiency while maintaining search quality.\n", + "\n", + "## What You'll Learn\n", + "\n", + "- How to assess your current HNSW index for migration\n", + "- Step-by-step migration from HNSW to SVS-VAMANA\n", + "- Memory usage comparison and cost analysis\n", + "- Search quality validation between HNSW and SVS-VAMANA\n", + "- Performance benchmarking and recall comparison\n", + "- Migration decision framework for production systems\n", + "\n", + "## Prerequisites\n", + "\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", + "- Existing HNSW index with substantial data (1000+ documents recommended)\n", + "- High-dimensional vectors (768+ dimensions for best compression benefits)\n", + "\n", + "## HNSW vs SVS-VAMANA\n", + "\n", + "**HNSW (Hierarchical Navigable Small World):**\n", + "- Excellent search quality and recall\n", + "- Fast query performance\n", + "- Higher memory usage (stores full-precision vectors)\n", + "- Good for applications prioritizing search quality\n", + "\n", + "**SVS-VAMANA:**\n", + "- Competitive search quality with compression\n", + "- Significant memory savings (50-75% reduction)\n", + "- Built-in vector compression (LeanVec, quantization)\n", + "- Ideal for large-scale deployments with cost constraints" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ๐Ÿ“ฆ Installation & Setup\n", + "\n", + "This notebook requires **sentence-transformers** for generating embeddings and **Redis Stack** running in Docker.\n", + "\n", + "**Requirements:**\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", + "- sentence-transformers (for generating embeddings)\n", + "- numpy (for vector operations)\n", + "- redisvl (should be available in your environment)\n", + "\n", + "**๐Ÿณ Docker Setup (Required):**\n", + "\n", + "Before running this notebook, make sure Redis Stack is running in Docker:\n", + "\n", + "```bash\n", + "# Start Redis Stack with Docker\n", + "docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest\n", + "```\n", + "\n", + "Or if you prefer using docker-compose, create a `docker-compose.yml` file:\n", + "\n", + "```yaml\n", + "version: '3.8'\n", + "services:\n", + " redis:\n", + " image: redis/redis-stack:latest\n", + " ports:\n", + " - \"6379:6379\"\n", + " - \"8001:8001\"\n", + "```\n", + "\n", + "Then run: `docker-compose up -d`\n", + "\n", + "**๐Ÿ“š Python Dependencies Installation:**\n", + "\n", + "Install the required Python packages:\n", + "\n", + "```bash\n", + "# Install core dependencies\n", + "pip install redisvl numpy sentence-transformers\n", + "\n", + "# Or install with specific versions for compatibility\n", + "pip install redisvl>=0.2.0 numpy>=1.21.0 sentence-transformers>=2.2.0\n", + "```\n", + "\n", + "**For Google Colab users, run this cell:**\n", + "\n", + "```python\n", + "!pip install redisvl sentence-transformers numpy\n", + "```\n", + "\n", + "**For Conda users:**\n", + "\n", + "```bash\n", + "conda install numpy\n", + "pip install redisvl sentence-transformers\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# # Install dependencies if needed\n", + "# import sys\n", + "# import subprocess\n", + "\n", + "# def install_if_missing(package):\n", + "# try:\n", + "# __import__(package)\n", + "# except ImportError:\n", + "# print(f\"Installing {package}...\")\n", + "# subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", package])\n", + "\n", + "# # Check and install required packages\n", + "# install_if_missing(\"sentence-transformers\")\n", + "# install_if_missing(\"redisvl\")\n", + "\n", + "# print(\"โœ… All dependencies are ready!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“š Libraries imported successfully!\n" + ] + } + ], + "source": [ + "# Import required libraries\n", + "import os\n", + "import json\n", + "import numpy as np\n", + "import time\n", + "from typing import List, Dict, Any\n", + "\n", + "# Redis and RedisVL imports\n", + "import redis\n", + "from redisvl.index import SearchIndex\n", + "from redisvl.query import VectorQuery\n", + "from redisvl.redis.utils import array_to_buffer, buffer_to_array\n", + "from redisvl.utils import CompressionAdvisor\n", + "from redisvl.redis.connection import supports_svs\n", + "\n", + "# Configuration\n", + "REDIS_URL = \"redis://localhost:6379\"\n", + "\n", + "print(\"๐Ÿ“š Libraries imported successfully!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Verify Redis and SVS Support\n", + "\n", + "First, let's ensure Redis Stack is running and supports SVS-VAMANA." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "โœ… Redis connection successful\n", + "๐Ÿ“Š Redis version: 8.2.2\n", + "โœ… SVS-VAMANA supported\n" + ] + } + ], + "source": [ + "# Test Redis connection and SVS support\n", + "try:\n", + " client = redis.Redis.from_url(REDIS_URL)\n", + " client.ping()\n", + " print(\"โœ… Redis connection successful\")\n", + " \n", + " # Check Redis version\n", + " redis_info = client.info()\n", + " redis_version = redis_info['redis_version']\n", + " print(f\"๐Ÿ“Š Redis version: {redis_version}\")\n", + " \n", + " # Check SVS support\n", + " if supports_svs(client):\n", + " print(\"โœ… SVS-VAMANA supported\")\n", + " else:\n", + " print(\"โŒ SVS-VAMANA not supported\")\n", + " print(\"Please ensure you're using Redis Stack 8.2.0+ with RediSearch 2.8.10+\")\n", + " \n", + "except Exception as e:\n", + " print(f\"โŒ Redis connection failed: {e}\")\n", + " print(\"Please ensure Redis Stack is running on localhost:6379\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Load Sample Data\n", + "\n", + "We'll use the movie dataset to demonstrate the migration process." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“ฝ๏ธ Loaded 20 movie records\n", + "Sample movie: Explosive Pursuit\n", + "Genres available: {'comedy', 'action'}\n", + "\n", + "๐Ÿ”ง Configuration:\n", + "Vector dimensions: 1024\n", + "Dataset size: 20 movie documents\n" + ] + } + ], + "source": [ + "# Load the movies dataset\n", + "with open('resources/movies.json', 'r') as f:\n", + " movies_data = json.load(f)\n", + "\n", + "print(\n", + " f\"๐Ÿ“ฝ๏ธ Loaded {len(movies_data)} movie records\",\n", + " f\"Sample movie: {movies_data[0]['title']}\",\n", + " f\"Genres available: {set(movie['genre'] for movie in movies_data)}\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "# Configuration for demonstration \n", + "dims = 1024 # sentence-transformers/all-roberta-large-v1 - 1024 dims\n", + "num_docs = len(movies_data) # Use actual dataset size\n", + "\n", + "print(\n", + " f\"\\n๐Ÿ”ง Configuration:\",\n", + " f\"Vector dimensions: {dims}\",\n", + " f\"Dataset size: {num_docs} movie documents\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Create HNSW Index\n", + "\n", + "First, we'll create an HNSW index with typical production settings." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Creating HNSW index with optimized settings...\n", + "โœ… Created HNSW index: hnsw_demo_index\n", + "\n", + "๐Ÿ”ง HNSW Configuration:\n", + "M (connections per node): 16\n", + "EF Construction: 200\n", + "EF Runtime: 10\n", + "Distance metric: cosine\n", + "Data type: float32\n" + ] + } + ], + "source": [ + "# Create HNSW schema with production-like settings\n", + "hnsw_schema = {\n", + " \"index\": {\n", + " \"name\": \"hnsw_demo_index\",\n", + " \"prefix\": \"demo:hnsw:\",\n", + " },\n", + " \"fields\": [\n", + " {\"name\": \"movie_id\", \"type\": \"tag\"},\n", + " {\"name\": \"title\", \"type\": \"text\"},\n", + " {\"name\": \"genre\", \"type\": \"tag\"},\n", + " {\"name\": \"rating\", \"type\": \"numeric\"},\n", + " {\"name\": \"description\", \"type\": \"text\"},\n", + " {\n", + " \"name\": \"embedding\",\n", + " \"type\": \"vector\",\n", + " \"attrs\": {\n", + " \"dims\": dims,\n", + " \"algorithm\": \"hnsw\",\n", + " \"datatype\": \"float32\",\n", + " \"distance_metric\": \"cosine\",\n", + " \"m\": 16, # Number of bi-directional links for each node\n", + " \"ef_construction\": 200, # Size of dynamic candidate list\n", + " \"ef_runtime\": 10 # Size of dynamic candidate list during search\n", + " }\n", + " }\n", + " ]\n", + "}\n", + "\n", + "print(\"Creating HNSW index with optimized settings...\")\n", + "hnsw_index = SearchIndex.from_dict(hnsw_schema, redis_url=REDIS_URL)\n", + "hnsw_index.create(overwrite=True)\n", + "print(f\"โœ… Created HNSW index: {hnsw_index.name}\")\n", + "\n", + "# Display HNSW configuration\n", + "print(\n", + " \"\\n๐Ÿ”ง HNSW Configuration:\",\n", + " f\"M (connections per node): 16\",\n", + " f\"EF Construction: 200\",\n", + " f\"EF Runtime: 10\",\n", + " f\"Distance metric: cosine\",\n", + " f\"Data type: float32\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Generate Embeddings and Load HNSW Index\n", + "\n", + "Generate embeddings for movie descriptions and populate the HNSW index." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ”„ Generating embeddings for movie descriptions...\n", + "14:40:35 sentence_transformers.SentenceTransformer INFO Use pytorch device_name: mps\n", + "14:40:35 sentence_transformers.SentenceTransformer INFO Load pretrained SentenceTransformer: all-MiniLM-L6-v2\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "dfa2af21d4904b58845f57a9786706e3", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Batches: 0%| | 0/1 [00:00 0:\n", + " for i in range(0, len(svs_data), batch_size):\n", + " batch = svs_data[i:i+batch_size]\n", + " svs_index.load(batch)\n", + " print(f\" Migrated {min(i+batch_size, len(svs_data))}/{len(svs_data)} documents\")\n", + "\n", + " # Wait for indexing to complete\n", + " print(\"โณ Waiting for SVS-VAMANA indexing to complete...\")\n", + " time.sleep(5)\n", + "\n", + " svs_info = svs_index.info()\n", + " print(\n", + " f\"\\nโœ… Migration complete! SVS index has {svs_info['num_docs']} documents\",\n", + " f\"Index size: {svs_info.get('vector_index_sz_mb', 'N/A')} MB\",\n", + " sep=\"\\n\"\n", + " )\n", + "else:\n", + " print(\"โš ๏ธ No data to migrate. Make sure the HNSW index was populated first.\")\n", + " print(\" Run the previous cells to load data into the HNSW index.\")\n", + " svs_info = svs_index.info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 8: Compare Memory Usage\n", + "\n", + "Analyze the memory savings achieved through the HNSW to SVS-VAMANA migration." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“Š Memory Usage Comparison\n", + "========================================\n", + "Original HNSW index: 4.23 MB\n", + "SVS-VAMANA index: 1.02 MB\n", + "\n", + "๐Ÿ’ฐ Memory savings: 75.9%\n", + "Absolute reduction: 3.21 MB\n", + "\n", + "๐Ÿ’ต Cost Impact Analysis:\n", + "Monthly cost reduction: $0.23\n", + "Annual cost reduction: $2.71\n" + ] + } + ], + "source": [ + "# Helper function to extract memory info\n", + "def get_memory_mb(index_info):\n", + " \"\"\"Extract memory usage in MB from index info\"\"\"\n", + " memory = index_info.get('vector_index_sz_mb', 0)\n", + " if isinstance(memory, str):\n", + " try:\n", + " return float(memory)\n", + " except ValueError:\n", + " return 0.0\n", + " return float(memory)\n", + "\n", + "# Get memory usage\n", + "hnsw_memory = get_memory_mb(hnsw_info)\n", + "svs_memory = get_memory_mb(svs_info)\n", + "\n", + "print(\n", + " \"๐Ÿ“Š Memory Usage Comparison\",\n", + " \"=\" * 40,\n", + " f\"Original HNSW index: {hnsw_memory:.2f} MB\",\n", + " f\"SVS-VAMANA index: {svs_memory:.2f} MB\",\n", + " \"\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "if hnsw_memory > 0:\n", + " if svs_memory > 0:\n", + " savings = ((hnsw_memory - svs_memory) / hnsw_memory) * 100\n", + " print(\n", + " f\"๐Ÿ’ฐ Memory savings: {savings:.1f}%\",\n", + " f\"Absolute reduction: {hnsw_memory - svs_memory:.2f} MB\",\n", + " sep=\"\\n\"\n", + " )\n", + " else:\n", + " print(\"โณ SVS index still indexing - memory comparison pending\")\n", + " \n", + " # Cost analysis\n", + " print(\"\\n๐Ÿ’ต Cost Impact Analysis:\")\n", + " cost_per_gb_hour = 0.10 # Example cloud pricing\n", + " hours_per_month = 24 * 30\n", + " \n", + " hnsw_monthly_cost = (hnsw_memory / 1024) * cost_per_gb_hour * hours_per_month\n", + " if svs_memory > 0:\n", + " svs_monthly_cost = (svs_memory / 1024) * cost_per_gb_hour * hours_per_month\n", + " monthly_savings = hnsw_monthly_cost - svs_monthly_cost\n", + " print(\n", + " f\"Monthly cost reduction: ${monthly_savings:.2f}\",\n", + " f\"Annual cost reduction: ${monthly_savings * 12:.2f}\",\n", + " sep=\"\\n\"\n", + " )\n", + " else:\n", + " print(\n", + " f\"Current monthly cost: ${hnsw_monthly_cost:.2f}\",\n", + " \"Projected savings: Available after indexing completes\",\n", + " sep=\"\\n\"\n", + " )\n", + "else:\n", + " print(\"โš ๏ธ Memory information not available\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 9: Validate Search Quality\n", + "\n", + "Compare search quality between HNSW and SVS-VAMANA to ensure the migration maintains acceptable recall." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ” Generating test queries for quality validation...\n", + "Generated 10 test queries\n" + ] + } + ], + "source": [ + "# Generate test queries\n", + "print(\"๐Ÿ” Generating test queries for quality validation...\")\n", + "\n", + "np.random.seed(123) # For reproducible test queries\n", + "num_test_queries = 10\n", + "test_queries = []\n", + "\n", + "for i in range(num_test_queries):\n", + " # Create test query vectors\n", + " query_vec = np.random.random(dims).astype(np.float32)\n", + " query_vec = query_vec / np.linalg.norm(query_vec) # Normalize\n", + " test_queries.append(query_vec)\n", + "\n", + "print(f\"Generated {len(test_queries)} test queries\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ” Testing HNSW search quality...\n", + "HNSW search completed in 0.007 seconds\n" + ] + } + ], + "source": [ + "# Test HNSW search quality\n", + "print(\"๐Ÿ” Testing HNSW search quality...\")\n", + "\n", + "hnsw_results = []\n", + "hnsw_start = time.time()\n", + "\n", + "for query_vec in test_queries:\n", + " query = VectorQuery(\n", + " vector=query_vec,\n", + " vector_field_name=\"embedding\",\n", + " return_fields=[\"movie_id\", \"title\", \"genre\"],\n", + " dtype=\"float32\",\n", + " num_results=10\n", + " )\n", + " results = hnsw_index.query(query)\n", + " hnsw_results.append([doc[\"movie_id\"] for doc in results])\n", + "\n", + "hnsw_time = time.time() - hnsw_start\n", + "print(f\"HNSW search completed in {hnsw_time:.3f} seconds\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ” Testing SVS-VAMANA search quality...\n", + "SVS-VAMANA search completed in 0.006 seconds\n" + ] + } + ], + "source": [ + "# Test SVS-VAMANA search quality\n", + "print(\"๐Ÿ” Testing SVS-VAMANA search quality...\")\n", + "\n", + "svs_results = []\n", + "svs_start = time.time()\n", + "\n", + "for i, query_vec in enumerate(test_queries):\n", + " # Adjust query vector for SVS index (handle dimensionality reduction)\n", + " if target_dims < dims:\n", + " svs_query_vec = query_vec[:target_dims]\n", + " else:\n", + " svs_query_vec = query_vec\n", + " \n", + " if target_dtype == 'float16':\n", + " svs_query_vec = svs_query_vec.astype(np.float16)\n", + " \n", + " query = VectorQuery(\n", + " vector=svs_query_vec,\n", + " vector_field_name=\"embedding\",\n", + " return_fields=[\"movie_id\", \"title\", \"genre\"],\n", + " dtype=target_dtype,\n", + " num_results=10\n", + " )\n", + " results = svs_index.query(query)\n", + " svs_results.append([doc[\"movie_id\"] for doc in results])\n", + "\n", + "svs_time = time.time() - svs_start\n", + "print(f\"SVS-VAMANA search completed in {svs_time:.3f} seconds\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“Š Search Quality Comparison\n", + "========================================\n", + "Recall@5: 1.000 (100.0%)\n", + "Recall@10: 0.990 (99.0%)\n", + "\n", + "โฑ๏ธ Performance Comparison:\n", + "HNSW query time: 0.007s (0.7ms per query)\n", + "SVS-VAMANA query time: 0.006s (0.6ms per query)\n", + "Speed difference: +10.4%\n", + "\n", + "๐ŸŽฏ Quality Assessment: ๐ŸŸข Excellent - Minimal quality loss\n" + ] + } + ], + "source": [ + "# Calculate recall and performance metrics\n", + "def calculate_recall(reference_results, test_results, k=10):\n", + " \"\"\"Calculate recall@k between two result sets\"\"\"\n", + " if not reference_results or not test_results:\n", + " return 0.0\n", + " \n", + " total_recall = 0.0\n", + " for ref, test in zip(reference_results, test_results):\n", + " ref_set = set(ref[:k])\n", + " test_set = set(test[:k])\n", + " if len(ref_set) > 0:\n", + " recall = len(ref_set.intersection(test_set)) / len(ref_set)\n", + " total_recall += recall\n", + " \n", + " return total_recall / len(reference_results)\n", + "\n", + "# Calculate metrics\n", + "recall_at_5 = calculate_recall(hnsw_results, svs_results, k=5)\n", + "recall_at_10 = calculate_recall(hnsw_results, svs_results, k=10)\n", + "\n", + "print(\n", + " \"๐Ÿ“Š Search Quality Comparison\",\n", + " \"=\" * 40,\n", + " f\"Recall@5: {recall_at_5:.3f} ({recall_at_5*100:.1f}%)\",\n", + " f\"Recall@10: {recall_at_10:.3f} ({recall_at_10*100:.1f}%)\",\n", + " \"\",\n", + " \"โฑ๏ธ Performance Comparison:\",\n", + " f\"HNSW query time: {hnsw_time:.3f}s ({hnsw_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"SVS-VAMANA query time: {svs_time:.3f}s ({svs_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"Speed difference: {((hnsw_time - svs_time) / hnsw_time * 100):+.1f}%\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "# Quality assessment\n", + "if recall_at_10 >= 0.95:\n", + " quality_assessment = \"๐ŸŸข Excellent - Minimal quality loss\"\n", + "elif recall_at_10 >= 0.90:\n", + " quality_assessment = \"๐ŸŸก Good - Acceptable quality for most applications\"\n", + "elif recall_at_10 >= 0.80:\n", + " quality_assessment = \"๐ŸŸ  Fair - Consider if quality requirements are flexible\"\n", + "else:\n", + " quality_assessment = \"๐Ÿ”ด Poor - Migration not recommended\"\n", + "\n", + "print(f\"\\n๐ŸŽฏ Quality Assessment: {quality_assessment}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 10: Migration Decision Framework\n", + "\n", + "Based on the analysis, determine if migration is recommended." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿค” Migration Decision Analysis\n", + "========================================\n", + "\n", + "๐Ÿ“Š Criteria Evaluation:\n", + "Memory savings: 75.9% โœ… (threshold: 20%)\n", + "Search quality: 0.990 โœ… (threshold: 0.85)\n", + "\n", + "๐ŸŽฏ Migration Recommendation: ๐ŸŸข RECOMMENDED\n", + "๐Ÿ’ญ Reasoning: Migration provides significant memory savings while maintaining good search quality.\n" + ] + } + ], + "source": [ + "# Migration decision logic\n", + "memory_savings_threshold = 20 # Minimum 20% memory savings\n", + "recall_threshold = 0.85 # Minimum 85% recall@10\n", + "\n", + "memory_savings_pct = ((hnsw_memory - svs_memory) / hnsw_memory * 100) if hnsw_memory > 0 and svs_memory > 0 else 0\n", + "meets_memory_threshold = memory_savings_pct >= memory_savings_threshold\n", + "meets_quality_threshold = recall_at_10 >= recall_threshold\n", + "\n", + "print(\n", + " \"๐Ÿค” Migration Decision Analysis\",\n", + " \"=\" * 40,\n", + " \"\",\n", + " \"๐Ÿ“Š Criteria Evaluation:\",\n", + " f\"Memory savings: {memory_savings_pct:.1f}% {'โœ…' if meets_memory_threshold else 'โŒ'} (threshold: {memory_savings_threshold}%)\",\n", + " f\"Search quality: {recall_at_10:.3f} {'โœ…' if meets_quality_threshold else 'โŒ'} (threshold: {recall_threshold})\",\n", + " \"\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "if meets_memory_threshold and meets_quality_threshold:\n", + " recommendation = \"๐ŸŸข RECOMMENDED\"\n", + " reasoning = \"Migration provides significant memory savings while maintaining good search quality.\"\n", + "elif meets_memory_threshold and not meets_quality_threshold:\n", + " recommendation = \"๐ŸŸก CONDITIONAL\"\n", + " reasoning = \"Good memory savings but reduced search quality. Consider if your application can tolerate lower recall.\"\n", + "elif not meets_memory_threshold and meets_quality_threshold:\n", + " recommendation = \"๐ŸŸ  LIMITED BENEFIT\"\n", + " reasoning = \"Search quality is maintained but memory savings are minimal. Migration may not be worth the effort.\"\n", + "else:\n", + " recommendation = \"๐Ÿ”ด NOT RECOMMENDED\"\n", + " reasoning = \"Insufficient memory savings and/or poor search quality. Consider alternative optimization strategies.\"\n", + "\n", + "print(\n", + " f\"๐ŸŽฏ Migration Recommendation: {recommendation}\",\n", + " f\"๐Ÿ’ญ Reasoning: {reasoning}\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 11: Production Migration Checklist\n", + "\n", + "If migration is recommended, follow this checklist for production deployment." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“‹ HNSW to SVS-VAMANA Migration Checklist\n", + "==================================================\n", + "\n", + "PRE-MIGRATION:\n", + "โ–ก Backup existing HNSW index data\n", + "โ–ก Test migration on staging environment\n", + "โ–ก Validate search quality with real queries\n", + "โ–ก Measure baseline HNSW performance metrics\n", + "โ–ก Plan rollback strategy\n", + "โ–ก Document current HNSW parameters (M, EF_construction, EF_runtime)\n", + "\n", + "MIGRATION:\n", + "โ–ก Create SVS-VAMANA index with tested configuration\n", + "โ–ก Migrate data in batches during low-traffic periods\n", + "โ–ก Monitor memory usage and indexing progress\n", + "โ–ก Validate data integrity after migration\n", + "โ–ก Test search functionality thoroughly\n", + "โ–ก Compare recall metrics with baseline\n", + "\n", + "POST-MIGRATION:\n", + "โ–ก Monitor search performance and quality\n", + "โ–ก Track memory usage and cost savings\n", + "โ–ก Update application configuration\n", + "โ–ก Document new SVS-VAMANA settings\n", + "โ–ก Clean up old HNSW index after validation period\n", + "โ–ก Update monitoring and alerting thresholds\n", + "\n", + "๐Ÿ’ก HNSW-SPECIFIC TIPS:\n", + "โ€ข HNSW indices are more complex to rebuild than FLAT\n", + "โ€ข Consider the impact on applications using EF_runtime tuning\n", + "โ€ข SVS-VAMANA may have different optimal query parameters\n", + "โ€ข Test with your specific HNSW configuration (M, EF values)\n", + "โ€ข Monitor for 48-72 hours before removing HNSW index\n", + "โ€ข Keep compression settings documented for future reference\n" + ] + } + ], + "source": [ + "print(\n", + " \"๐Ÿ“‹ HNSW to SVS-VAMANA Migration Checklist\",\n", + " \"=\" * 50,\n", + " \"\\nPRE-MIGRATION:\",\n", + " \"โ–ก Backup existing HNSW index data\",\n", + " \"โ–ก Test migration on staging environment\",\n", + " \"โ–ก Validate search quality with real queries\",\n", + " \"โ–ก Measure baseline HNSW performance metrics\",\n", + " \"โ–ก Plan rollback strategy\",\n", + " \"โ–ก Document current HNSW parameters (M, EF_construction, EF_runtime)\",\n", + " \"\\nMIGRATION:\",\n", + " \"โ–ก Create SVS-VAMANA index with tested configuration\",\n", + " \"โ–ก Migrate data in batches during low-traffic periods\",\n", + " \"โ–ก Monitor memory usage and indexing progress\",\n", + " \"โ–ก Validate data integrity after migration\",\n", + " \"โ–ก Test search functionality thoroughly\",\n", + " \"โ–ก Compare recall metrics with baseline\",\n", + " \"\\nPOST-MIGRATION:\",\n", + " \"โ–ก Monitor search performance and quality\",\n", + " \"โ–ก Track memory usage and cost savings\",\n", + " \"โ–ก Update application configuration\",\n", + " \"โ–ก Document new SVS-VAMANA settings\",\n", + " \"โ–ก Clean up old HNSW index after validation period\",\n", + " \"โ–ก Update monitoring and alerting thresholds\",\n", + " \"\\n๐Ÿ’ก HNSW-SPECIFIC TIPS:\",\n", + " \"โ€ข HNSW indices are more complex to rebuild than FLAT\",\n", + " \"โ€ข Consider the impact on applications using EF_runtime tuning\",\n", + " \"โ€ข SVS-VAMANA may have different optimal query parameters\",\n", + " \"โ€ข Test with your specific HNSW configuration (M, EF values)\",\n", + " \"โ€ข Monitor for 48-72 hours before removing HNSW index\",\n", + " \"โ€ข Keep compression settings documented for future reference\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 12: Cleanup\n", + "\n", + "Clean up the demonstration indices." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿงน Cleaning up demonstration indices...\n", + "โœ… Deleted HNSW demonstration index\n", + "โœ… Deleted SVS-VAMANA demonstration index\n", + "\n", + "๐ŸŽ‰ HNSW to SVS-VAMANA migration demonstration complete!\n", + "\n", + "Next steps:\n", + "1. Apply learnings to your production HNSW indices\n", + "2. Test with your actual query patterns and data\n", + "3. Monitor performance in your environment\n", + "4. Consider gradual rollout strategy\n", + "5. Evaluate impact on applications using HNSW-specific features\n" + ] + } + ], + "source": [ + "print(\"๐Ÿงน Cleaning up demonstration indices...\")\n", + "\n", + "# Clean up HNSW index\n", + "try:\n", + " hnsw_index.delete(drop=True)\n", + " print(\"โœ… Deleted HNSW demonstration index\")\n", + "except Exception as e:\n", + " print(f\"โš ๏ธ Failed to delete HNSW index: {e}\")\n", + "\n", + "# Clean up SVS index\n", + "try:\n", + " svs_index.delete(drop=True)\n", + " print(\"โœ… Deleted SVS-VAMANA demonstration index\")\n", + "except Exception as e:\n", + " print(f\"โš ๏ธ Failed to delete SVS index: {e}\")\n", + "\n", + "print(\n", + " \"\\n๐ŸŽ‰ HNSW to SVS-VAMANA migration demonstration complete!\",\n", + " \"\\nNext steps:\",\n", + " \"1. Apply learnings to your production HNSW indices\",\n", + " \"2. Test with your actual query patterns and data\",\n", + " \"3. Monitor performance in your environment\",\n", + " \"4. Consider gradual rollout strategy\",\n", + " \"5. Evaluate impact on applications using HNSW-specific features\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb b/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb new file mode 100644 index 00000000..e52879c9 --- /dev/null +++ b/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb @@ -0,0 +1,1192 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Redis](https://redis.io/wp-content/uploads/2024/04/Logotype.svg?auto=webp&quality=85,75&width=120)\n", + "# Migrating from FLAT to SVS-VAMANA\n", + "\n", + "## Let's Begin!\n", + "\"Open\n", + "\n", + "This notebook demonstrates how to migrate existing FLAT vector indices to SVS-VAMANA for improved memory efficiency and cost savings.\n", + "\n", + "## What You'll Learn\n", + "\n", + "- How to assess your current FLAT index for migration\n", + "- Step-by-step migration from FLAT to SVS-VAMANA\n", + "- Memory usage comparison and cost analysis\n", + "- Search quality validation\n", + "- Performance benchmarking\n", + "- Migration decision framework\n", + "\n", + "## Prerequisites\n", + "\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", + "- Existing vector index with substantial data (1000+ documents recommended)\n", + "- Vector embeddings (768 dimensions using sentence-transformers/all-mpnet-base-v2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ๐Ÿ“ฆ Installation & Setup\n", + "\n", + "This notebook requires **sentence-transformers** for generating embeddings and **Redis Stack** running in Docker.\n", + "\n", + "**Requirements:**\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", + "- sentence-transformers (for generating embeddings)\n", + "- numpy (for vector operations)\n", + "- redisvl (should be available in your environment)\n", + "\n", + "**๐Ÿณ Docker Setup (Required):**\n", + "\n", + "Before running this notebook, make sure Redis Stack is running in Docker:\n", + "\n", + "```bash\n", + "# Start Redis Stack with Docker\n", + "docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest\n", + "```\n", + "\n", + "Or if you prefer using docker-compose, create a `docker-compose.yml` file:\n", + "\n", + "```yaml\n", + "version: '3.8'\n", + "services:\n", + " redis:\n", + " image: redis/redis-stack:latest\n", + " ports:\n", + " - \"6379:6379\"\n", + " - \"8001:8001\"\n", + "```\n", + "\n", + "Then run: `docker-compose up -d`\n", + "\n", + "**๐Ÿ“š Python Dependencies Installation:**\n", + "\n", + "Install the required Python packages:\n", + "\n", + "```bash\n", + "# Install core dependencies\n", + "pip install redisvl numpy sentence-transformers\n", + "\n", + "# Or install with specific versions for compatibility\n", + "pip install redisvl>=0.2.0 numpy>=1.21.0 sentence-transformers>=2.2.0\n", + "```\n", + "\n", + "**For Google Colab users, run this cell:**\n", + "\n", + "```python\n", + "!pip install redisvl sentence-transformers numpy\n", + "```\n", + "\n", + "**For Conda users:**\n", + "\n", + "```bash\n", + "conda install numpy\n", + "pip install redisvl sentence-transformers\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [], + "source": [ + "# Setup redis-vl environment\n", + "import os\n", + "import sys\n", + "import subprocess\n", + "# Required imports from redis-vl\n", + "import numpy as np\n", + "import time\n", + "from redisvl.index import SearchIndex\n", + "from redisvl.query import VectorQuery\n", + "from redisvl.redis.utils import array_to_buffer, buffer_to_array\n", + "from redisvl.utils import CompressionAdvisor\n", + "from redisvl.redis.connection import supports_svs\n", + "import redis\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Verify SVS-VAMANA Support\n", + "\n", + "First, let's ensure your Redis environment supports SVS-VAMANA." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "โœ… Redis connection successful\n", + "โœ… SVS-VAMANA supported\n", + " Ready for migration!\n" + ] + } + ], + "source": [ + "# Check Redis connection and SVS support\n", + "REDIS_URL = \"redis://localhost:6379\"\n", + "\n", + "try:\n", + " client = redis.Redis.from_url(REDIS_URL)\n", + " client.ping()\n", + " print(\"โœ… Redis connection successful\")\n", + " \n", + " if supports_svs(client):\n", + " print(\"โœ… SVS-VAMANA supported\")\n", + " print(\" Ready for migration!\")\n", + " else:\n", + " print(\"โŒ SVS-VAMANA not supported\")\n", + " print(\" Requires Redis >= 8.2.0 with RediSearch >= 2.8.10\")\n", + " print(\" Please upgrade Redis Stack before proceeding\")\n", + " \n", + "except Exception as e:\n", + " print(f\"โŒ Redis connection failed: {e}\")\n", + " print(\" Please ensure Redis is running and accessible\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Assess Your Current Index\n", + "\n", + "For this demonstration, we'll create a sample FLAT index. In practice, you would analyze your existing index." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“ฅ Loading sample movie data...\n", + "Loaded 20 movie records\n", + "Sample movie: Explosive Pursuit - A daring cop chases a notorious criminal across the city in a high-stakes game of cat and mouse.\n" + ] + } + ], + "source": [ + "# Download sample data from redis-ai-resources\n", + "print(\"๐Ÿ“ฅ Loading sample movie data...\")\n", + "import os\n", + "import json\n", + "\n", + "# Load the movies dataset\n", + "url = \"resources/movies.json\"\n", + "with open(\"resources/movies.json\", \"r\") as f:\n", + " movies_data = json.load(f)\n", + "\n", + "print(f\"Loaded {len(movies_data)} movie records\")\n", + "print(f\"Sample movie: {movies_data[0]['title']} - {movies_data[0]['description']}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“Š Migration Assessment\n", + "Vector dimensions: 768 (sentence-transformers/all-mpnet-base-v2)\n", + "Dataset size: 20 movie documents\n", + "Data includes: title, genre, rating, description\n" + ] + } + ], + "source": [ + "# Configuration for demonstration \n", + "dims = 768 # sentence-transformers/all-mpnet-base-v2 - 768 dims\n", + "\n", + "num_docs = len(movies_data) # Use actual dataset size\n", + "\n", + "print(\n", + " \"๐Ÿ“Š Migration Assessment\",\n", + " f\"Vector dimensions: {dims} (sentence-transformers/all-mpnet-base-v2)\",\n", + " f\"Dataset size: {num_docs} movie documents\",\n", + " \"Data includes: title, genre, rating, description\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "Next, let's configure a smaple FLAT index. Notice the algorithm value, dims value, and datatype value under fields." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Creating sample FLAT index...\n", + "โœ… Created FLAT index: migration_demo_flat\n" + ] + } + ], + "source": [ + "flat_schema = {\n", + " \"index\": {\n", + " \"name\": \"migration_demo_flat\",\n", + " \"prefix\": \"demo:flat:\",\n", + " },\n", + " \"fields\": [\n", + " {\"name\": \"movie_id\", \"type\": \"tag\"},\n", + " {\"name\": \"title\", \"type\": \"text\"},\n", + " {\"name\": \"genre\", \"type\": \"tag\"},\n", + " {\"name\": \"rating\", \"type\": \"numeric\"},\n", + " {\"name\": \"description\", \"type\": \"text\"},\n", + " {\n", + " \"name\": \"embedding\",\n", + " \"type\": \"vector\",\n", + " \"attrs\": {\n", + " \"dims\": dims,\n", + " \"algorithm\": \"flat\",\n", + " \"datatype\": \"float32\",\n", + " \"distance_metric\": \"cosine\"\n", + " }\n", + " }\n", + " ]\n", + "}\n", + "\n", + "# Create and populate FLAT index\n", + "print(\"Creating sample FLAT index...\")\n", + "flat_index = SearchIndex.from_dict(flat_schema, redis_url=REDIS_URL)\n", + "flat_index.create(overwrite=True)\n", + "print(f\"โœ… Created FLAT index: {flat_index.name}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "---\n", + "Generate embeddings for movie descriptions\n" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ”„ Generating embeddings for movie descriptions...\n", + "๐Ÿ“ฆ Loading sentence transformer model...\n", + "14:45:27 sentence_transformers.SentenceTransformer INFO Use pytorch device_name: mps\n", + "14:45:27 sentence_transformers.SentenceTransformer INFO Load pretrained SentenceTransformer: sentence-transformers/all-mpnet-base-v2\n", + "โœ… Loaded embedding model with 768 dimensions\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "0e06f2f860ec443e802a3fbf3961487c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Batches: 0%| | 0/1 [00:00 0:\n", + " for i in range(0, len(svs_data), batch_size):\n", + " batch = svs_data[i:i+batch_size]\n", + " svs_index.load(batch)\n", + " print(f\" Migrated {min(i+batch_size, len(svs_data))}/{len(svs_data)} documents\")\n", + "\n", + " # Wait for indexing to complete\n", + " print(\"Waiting for indexing to complete...\")\n", + " time.sleep(5)\n", + "\n", + " svs_info = svs_index.info()\n", + " print(f\"\\nโœ… Migration complete! SVS index has {svs_info['num_docs']} documents\")\n", + "else:\n", + " print(\"โš ๏ธ No data to migrate. Make sure the FLAT index was populated first.\")\n", + " print(\" Run the previous cells to load data into the FLAT index.\")\n", + " svs_info = svs_index.info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 6: Compare Memory Usage\n", + "\n", + "Let's analyze the memory savings achieved through compression. This is just an example on the small sample data. Use a larger dataset before deciding." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“Š Memory Usage Comparison\n", + "========================================\n", + "Original FLAT index: 3.02 MB\n", + "SVS-VAMANA index: 3.02 MB\n", + "\n", + "๐Ÿ’ฐ Memory savings: -0.0%\n", + "Absolute reduction: -0.00 MB\n", + "\n", + "๐Ÿ’ต Cost Impact Analysis:\n", + "Monthly cost reduction: $-0.00\n", + "Annual cost reduction: $-0.00\n" + ] + } + ], + "source": [ + "# Helper function to extract memory info\n", + "def get_memory_mb(index_info):\n", + " \"\"\"Extract memory usage in MB from index info\"\"\"\n", + " memory = index_info.get('vector_index_sz_mb', 0)\n", + " if isinstance(memory, str):\n", + " try:\n", + " return float(memory)\n", + " except ValueError:\n", + " return 0.0\n", + " return float(memory)\n", + "\n", + "# Get memory usage\n", + "flat_memory = get_memory_mb(flat_info)\n", + "svs_memory = get_memory_mb(svs_info)\n", + "\n", + "print(\n", + " \"๐Ÿ“Š Memory Usage Comparison\",\n", + " \"=\" * 40,\n", + " f\"Original FLAT index: {flat_memory:.2f} MB\",\n", + " f\"SVS-VAMANA index: {svs_memory:.2f} MB\",\n", + " \"\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "if flat_memory > 0:\n", + " if svs_memory > 0:\n", + " savings = ((flat_memory - svs_memory) / flat_memory) * 100\n", + " print(\n", + " f\"๐Ÿ’ฐ Memory savings: {savings:.1f}%\",\n", + " f\"Absolute reduction: {flat_memory - svs_memory:.2f} MB\",\n", + " sep=\"\\n\"\n", + " )\n", + " else:\n", + " print(\"โณ SVS index still indexing - memory comparison pending\")\n", + " \n", + " # Cost analysis\n", + " print(\"\\n๐Ÿ’ต Cost Impact Analysis:\")\n", + " cost_per_gb_hour = 0.10 # Example cloud pricing\n", + " hours_per_month = 24 * 30\n", + " \n", + " flat_monthly_cost = (flat_memory / 1024) * cost_per_gb_hour * hours_per_month\n", + " if svs_memory > 0:\n", + " svs_monthly_cost = (svs_memory / 1024) * cost_per_gb_hour * hours_per_month\n", + " monthly_savings = flat_monthly_cost - svs_monthly_cost\n", + " print(\n", + " f\"Monthly cost reduction: ${monthly_savings:.2f}\",\n", + " f\"Annual cost reduction: ${monthly_savings * 12:.2f}\",\n", + " sep=\"\\n\"\n", + " )\n", + " else:\n", + " print(\n", + " f\"Current monthly cost: ${flat_monthly_cost:.2f}\",\n", + " \"Projected savings: Available after indexing completes\",\n", + " sep=\"\\n\"\n", + " )\n", + "else:\n", + " print(\"โš ๏ธ Memory information not available\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 7: Validate Search Quality\n", + "\n", + "Test that the compressed index maintains good search quality." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ” Validating search quality...\n", + "Generated 5 test queries\n", + "\n", + "Testing original FLAT index...\n", + "FLAT search time: 0.012s (0.002s per query)\n", + "\n", + "Testing SVS-VAMANA index...\n", + "SVS search time: 0.017s (0.003s per query)\n", + "\n", + "๐Ÿ“ˆ Average recall@10: 1.000 (100.0%)\n", + "โœ… Excellent search quality maintained\n" + ] + } + ], + "source": [ + "print(\"๐Ÿ” Validating search quality...\")\n", + "\n", + "# Create test queries\n", + "num_test_queries = 5\n", + "test_queries = []\n", + "\n", + "for i in range(num_test_queries):\n", + " # Generate normalized test vector\n", + " query_vec = np.random.random(dims).astype(np.float32)\n", + " query_vec = query_vec / np.linalg.norm(query_vec)\n", + " test_queries.append(query_vec)\n", + "\n", + "print(f\"Generated {num_test_queries} test queries\")\n", + "\n", + "# Test FLAT index (ground truth)\n", + "print(\"\\nTesting original FLAT index...\")\n", + "flat_results = []\n", + "flat_start = time.time()\n", + "\n", + "for query_vec in test_queries:\n", + " query = VectorQuery(\n", + " vector=query_vec,\n", + " vector_field_name=\"embedding\",\n", + " return_fields=[\"movie_id\", \"title\", \"genre\"],\n", + " dtype=\"float32\",\n", + " num_results=10\n", + " )\n", + " results = flat_index.query(query)\n", + " flat_results.append([doc[\"movie_id\"] for doc in results])\n", + "\n", + "flat_time = time.time() - flat_start\n", + "print(f\"FLAT search time: {flat_time:.3f}s ({flat_time/num_test_queries:.3f}s per query)\")\n", + "\n", + "# Test SVS-VAMANA index\n", + "print(\"\\nTesting SVS-VAMANA index...\")\n", + "svs_results = []\n", + "svs_start = time.time()\n", + "\n", + "for i, query_vec in enumerate(test_queries):\n", + " # Adjust query vector for SVS index (handle dimensionality reduction)\n", + " if target_dims < dims:\n", + " svs_query_vec = query_vec[:target_dims]\n", + " else:\n", + " svs_query_vec = query_vec\n", + " \n", + " if target_dtype == 'float16':\n", + " svs_query_vec = svs_query_vec.astype(np.float16)\n", + " \n", + " query = VectorQuery(\n", + " vector=svs_query_vec,\n", + " vector_field_name=\"embedding\",\n", + " return_fields=[\"movie_id\", \"title\", \"genre\"],\n", + " dtype=target_dtype,\n", + " num_results=10\n", + " )\n", + " \n", + " try:\n", + " results = svs_index.query(query)\n", + " svs_results.append([doc[\"movie_id\"] for doc in results])\n", + " except Exception as e:\n", + " print(f\"Query {i+1} failed: {e}\")\n", + " svs_results.append([])\n", + "\n", + "svs_time = time.time() - svs_start\n", + "print(f\"SVS search time: {svs_time:.3f}s ({svs_time/num_test_queries:.3f}s per query)\")\n", + "\n", + "# Calculate recall if we have results\n", + "if svs_results and any(svs_results):\n", + " recalls = []\n", + " for flat_res, svs_res in zip(flat_results, svs_results):\n", + " if flat_res and svs_res:\n", + " intersection = set(flat_res).intersection(set(svs_res))\n", + " recall = len(intersection) / len(flat_res)\n", + " recalls.append(recall)\n", + " \n", + " if recalls:\n", + " avg_recall = np.mean(recalls)\n", + " print(f\"\\n๐Ÿ“ˆ Average recall@10: {avg_recall:.3f} ({avg_recall*100:.1f}%)\")\n", + " \n", + " if avg_recall >= 0.9:\n", + " print(\"โœ… Excellent search quality maintained\")\n", + " elif avg_recall >= 0.8:\n", + " print(\"โœ… Good search quality maintained\")\n", + " else:\n", + " print(\"โš ๏ธ Search quality may be impacted - consider adjusting compression\")\n", + "else:\n", + " print(\"โš ๏ธ SVS index may still be indexing - search quality test pending\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 8: Migration Decision Framework\n", + "\n", + "Based on the results, let's determine if migration is recommended." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐ŸŽฏ Migration Analysis & Recommendation\n", + "==================================================\n", + "Dataset: 20 documents, 768-dimensional vectors\n", + "Compression: LVQ4\n", + "Datatype: float32 โ†’ float32\n", + "\n", + "Memory savings: -0.0% (Modest)\n", + "Search quality: 1.0% recall (Acceptable)\n", + "Performance: 1.4x vs original (Acceptable)\n", + "\n", + "๐Ÿ† RECOMMENDATION:\n", + "โŒ MIGRATION NOT RECOMMENDED\n", + " โ€ข Insufficient benefits for current dataset\n", + " โ€ข Consider larger dataset or different compression\n", + " โ€ข SVS-VAMANA works best with high-dimensional data\n" + ] + } + ], + "source": [ + "print(\"๐ŸŽฏ Migration Analysis & Recommendation\")\n", + "print(\"=\" * 50)\n", + "\n", + "# Fallback configuration if not defined (for CI/CD compatibility)\n", + "if 'selected_config' not in locals():\n", + " from redisvl.utils import CompressionAdvisor\n", + " selected_config = CompressionAdvisor.recommend(dims=dims, priority=\"memory\")\n", + "\n", + "# Summarize configuration\n", + "print(f\"Dataset: {num_docs} documents, {dims}-dimensional vectors\")\n", + "print(f\"Compression: {selected_config.get('compression', 'None')}\")\n", + "print(f\"Datatype: float32 โ†’ {selected_config['datatype']}\")\n", + "if 'reduce' in selected_config:\n", + " reduction = ((dims - selected_config['reduce']) / dims) * 100\n", + " print(f\"Dimensions: {dims} โ†’ {selected_config['reduce']} ({reduction:.1f}% reduction)\")\n", + "print()\n", + "\n", + "# Decision criteria\n", + "memory_savings_significant = False\n", + "search_quality_acceptable = True\n", + "performance_acceptable = True\n", + "\n", + "if flat_memory > 0 and svs_memory > 0:\n", + " savings_pct = ((flat_memory - svs_memory) / flat_memory) * 100\n", + " memory_savings_significant = savings_pct > 25 # 25%+ savings considered significant\n", + " print(f\"Memory savings: {savings_pct:.1f}% ({'Significant' if memory_savings_significant else 'Modest'})\")\n", + "else:\n", + " print(\"Memory savings: Pending (SVS index still indexing)\")\n", + "\n", + "if 'recalls' in locals() and recalls:\n", + " avg_recall = np.mean(recalls)\n", + " search_quality_acceptable = avg_recall >= 0.8 # 80%+ recall considered acceptable\n", + " print(f\"Search quality: {avg_recall:.1f}% recall ({'Acceptable' if search_quality_acceptable else 'Needs improvement'})\")\n", + "else:\n", + " print(\"Search quality: Pending validation\")\n", + "\n", + "if 'flat_time' in locals() and 'svs_time' in locals():\n", + " performance_ratio = svs_time / flat_time if flat_time > 0 else 1\n", + " performance_acceptable = performance_ratio <= 2.0 # Allow up to 2x slower\n", + " print(f\"Performance: {performance_ratio:.1f}x vs original ({'Acceptable' if performance_acceptable else 'Slower than expected'})\")\n", + "else:\n", + " print(\"Performance: Pending comparison\")\n", + "\n", + "\n", + "# Final recommendation\n", + "print(\"\\n๐Ÿ† RECOMMENDATION:\")\n", + "if memory_savings_significant and search_quality_acceptable and performance_acceptable:\n", + " print(\"โœ… MIGRATE TO SVS-VAMANA\")\n", + " print(\" โ€ข Significant memory savings achieved\")\n", + " print(\" โ€ข Search quality maintained\")\n", + " print(\" โ€ข Performance impact acceptable\")\n", + " print(\" โ€ข Cost reduction benefits clear\")\n", + "elif memory_savings_significant and search_quality_acceptable:\n", + " print(\"โš ๏ธ CONSIDER MIGRATION WITH MONITORING\")\n", + " print(\" โ€ข Good memory savings and search quality\")\n", + " print(\" โ€ข Monitor performance in production\")\n", + " print(\" โ€ข Consider gradual rollout\")\n", + "elif memory_savings_significant:\n", + " print(\"โš ๏ธ MIGRATION NEEDS TUNING\")\n", + " print(\" โ€ข Memory savings achieved\")\n", + " print(\" โ€ข Search quality or performance needs improvement\")\n", + " print(\" โ€ข Try different compression settings\")\n", + "else:\n", + " print(\"โŒ MIGRATION NOT RECOMMENDED\")\n", + " print(\" โ€ข Insufficient benefits for current dataset\")\n", + " print(\" โ€ข Consider larger dataset or different compression\")\n", + " print(\" โ€ข SVS-VAMANA works best with high-dimensional data\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 9: Production Migration Checklist\n", + "\n", + "If migration is recommended, follow this checklist for production deployment." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿ“‹ Production Migration Checklist\n", + "========================================\n", + "\n", + "PRE-MIGRATION:\n", + "โ–ก Backup existing index data\n", + "โ–ก Test migration on staging environment\n", + "โ–ก Validate search quality with real queries\n", + "โ–ก Measure baseline performance metrics\n", + "โ–ก Plan rollback strategy\n", + "\n", + "MIGRATION:\n", + "โ–ก Create SVS-VAMANA index with tested configuration\n", + "โ–ก Migrate data in batches during low-traffic periods\n", + "โ–ก Monitor memory usage and indexing progress\n", + "โ–ก Validate data integrity after migration\n", + "โ–ก Test search functionality thoroughly\n", + "\n", + "POST-MIGRATION:\n", + "โ–ก Monitor search performance and quality\n", + "โ–ก Track memory usage and cost savings\n", + "โ–ก Update application configuration\n", + "โ–ก Document new index settings\n", + "โ–ก Clean up old index after validation period\n", + "\n", + "๐Ÿ’ก TIPS:\n", + "โ€ข Start with a subset of data for initial validation\n", + "โ€ข Use blue-green deployment for zero-downtime migration\n", + "โ€ข Monitor for 24-48 hours before removing old index\n", + "โ€ข Keep compression settings documented for future reference\n" + ] + } + ], + "source": [ + "print(\n", + " \"๐Ÿ“‹ Production Migration Checklist\",\n", + " \"=\" * 40,\n", + " \"\\nPRE-MIGRATION:\",\n", + " \"โ–ก Backup existing index data\",\n", + " \"โ–ก Test migration on staging environment\",\n", + " \"โ–ก Validate search quality with real queries\",\n", + " \"โ–ก Measure baseline performance metrics\",\n", + " \"โ–ก Plan rollback strategy\",\n", + " \"\\nMIGRATION:\",\n", + " \"โ–ก Create SVS-VAMANA index with tested configuration\",\n", + " \"โ–ก Migrate data in batches during low-traffic periods\",\n", + " \"โ–ก Monitor memory usage and indexing progress\",\n", + " \"โ–ก Validate data integrity after migration\",\n", + " \"โ–ก Test search functionality thoroughly\",\n", + " \"\\nPOST-MIGRATION:\",\n", + " \"โ–ก Monitor search performance and quality\",\n", + " \"โ–ก Track memory usage and cost savings\",\n", + " \"โ–ก Update application configuration\",\n", + " \"โ–ก Document new index settings\",\n", + " \"โ–ก Clean up old index after validation period\",\n", + " \"\\n๐Ÿ’ก TIPS:\",\n", + " \"โ€ข Start with a subset of data for initial validation\",\n", + " \"โ€ข Use blue-green deployment for zero-downtime migration\",\n", + " \"โ€ข Monitor for 24-48 hours before removing old index\",\n", + " \"โ€ข Keep compression settings documented for future reference\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 10: Cleanup\n", + "\n", + "Clean up the demonstration indices." + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "๐Ÿงน Cleaning up demonstration indices...\n", + "โœ… Deleted FLAT demonstration index\n", + "โœ… Deleted SVS-VAMANA demonstration index\n", + "\n", + "๐ŸŽ‰ Migration demonstration complete!\n", + "\n", + "Next steps:\n", + "1. Apply learnings to your production data\n", + "2. Test with your actual query patterns\n", + "3. Monitor performance in your environment\n", + "4. Consider gradual rollout strategy\n" + ] + } + ], + "source": [ + "print(\"๐Ÿงน Cleaning up demonstration indices...\")\n", + "\n", + "# Clean up FLAT index\n", + "try:\n", + " flat_index.delete(drop=True)\n", + " print(\"โœ… Deleted FLAT demonstration index\")\n", + "except Exception as e:\n", + " print(f\"โš ๏ธ Failed to delete FLAT index: {e}\")\n", + "\n", + "# Clean up SVS index\n", + "try:\n", + " svs_index.delete(drop=True)\n", + " print(\"โœ… Deleted SVS-VAMANA demonstration index\")\n", + "except Exception as e:\n", + " print(f\"โš ๏ธ Failed to delete SVS index: {e}\")\n", + "\n", + "print(\n", + " \"\\n๐ŸŽ‰ Migration demonstration complete!\",\n", + " \"\\nNext steps:\",\n", + " \"1. Apply learnings to your production data\",\n", + " \"2. Test with your actual query patterns\",\n", + " \"3. Monitor performance in your environment\",\n", + " \"4. Consider gradual rollout strategy\",\n", + " sep=\"\\n\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} From 0b2f58c954265db36d9b4fff9dda507a7579f521 Mon Sep 17 00:00:00 2001 From: Nitin Kanukolanu Date: Fri, 17 Oct 2025 15:19:27 -0400 Subject: [PATCH 2/3] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6425baf0..ca5c7394 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,8 @@ Need quickstarts to begin your Redis AI journey? | ๐Ÿ”ข **Data Type Support** - Shows how to convert a float32 index to float16 or integer dataypes | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/03_dtype_support.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/03_dtype_support.ipynb) | | ๐Ÿ“Š **Benchmarking Basics** - Overview of search benchmarking basics with RedisVL and Python multiprocessing | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/04_redisvl_benchmarking_basics.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/04_redisvl_benchmarking_basics.ipynb) | | ๐Ÿ“Š **Multi Vector Search** - Overview of multi vector queries with RedisVL | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/05_multivector_search.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/05_multivector_search.ipynb) | -| ๐Ÿ—œ๏ธ **HNSW to SVS-VAMANA Migration** - Showcase how to migrate HNSW indices to SVS-VAMANA and demonstrate 50-75% memory savings benefits | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | -| ๐Ÿ—œ๏ธ **FLAT to SVS-VAMANA Migration** - Showcase how to migrate FLAT indices to SVS-VAMANA and demonstrate significant memory reduction benefits | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | +| ๐Ÿ—œ๏ธ **HNSW to SVS-VAMANA Migration** - Showcase how to migrate HNSW indices to SVS-VAMANA | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb) | +| ๐Ÿ—œ๏ธ **FLAT to SVS-VAMANA Migration** - Showcase how to migrate FLAT indices to SVS-VAMANA | [![Open In GitHub](https://img.shields.io/badge/View-GitHub-green)](python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb) | ### Retrieval Augmented Generation (RAG) From acb02d8643376cf6bae5903c9676edf41f78957d Mon Sep 17 00:00:00 2001 From: Nitin Kanukolanu Date: Wed, 22 Oct 2025 12:15:06 -0400 Subject: [PATCH 3/3] Update notebooks with: - colab supported install and setup - use redisvl vectorizers --- .../06_hnsw_to_svs_vamana_migration.ipynb | 549 ++++++--------- .../07_flat_to_svs_vamana_migration.ipynb | 658 ++++++++---------- 2 files changed, 496 insertions(+), 711 deletions(-) diff --git a/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb b/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb index dbe20a7a..8b2cee73 100644 --- a/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb +++ b/python-recipes/vector-search/06_hnsw_to_svs_vamana_migration.ipynb @@ -42,96 +42,140 @@ "- Ideal for large-scale deployments with cost constraints" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ๐Ÿ“‹ HNSW to SVS-VAMANA Migration Checklist\n", + "\n", + "**PRE-MIGRATION:**\n", + "- โ˜ Backup existing HNSW index data\n", + "- โ˜ Test migration on staging environment\n", + "- โ˜ Validate search quality with real queries\n", + "- โ˜ Measure baseline HNSW performance metrics\n", + "- โ˜ Plan rollback strategy\n", + "- โ˜ Document current HNSW parameters (M, EF_construction, EF_runtime)\n", + "\n", + "**MIGRATION:**\n", + "- โ˜ Create SVS-VAMANA index with tested configuration\n", + "- โ˜ Migrate data in batches during low-traffic periods\n", + "- โ˜ Monitor memory usage and indexing progress\n", + "- โ˜ Validate data integrity after migration\n", + "- โ˜ Test search functionality thoroughly\n", + "- โ˜ Compare recall metrics with baseline\n", + "\n", + "**POST-MIGRATION:**\n", + "- โ˜ Monitor search performance and quality\n", + "- โ˜ Track memory usage and cost savings\n", + "- โ˜ Update application configuration\n", + "- โ˜ Document new SVS-VAMANA settings\n", + "- โ˜ Clean up old HNSW index after validation period\n", + "- โ˜ Update monitoring and alerting thresholds\n", + "\n", + "**๐Ÿ’ก HNSW-SPECIFIC TIPS:**\n", + "- HNSW indices are more complex to rebuild than FLAT\n", + "- Consider the impact on applications using EF_runtime tuning\n", + "- SVS-VAMANA may have different optimal query parameters\n", + "- Test with your specific HNSW configuration (M, EF values)\n", + "- Monitor for 48-72 hours before removing HNSW index\n", + "- Keep compression settings documented for future reference" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ๐Ÿ“ฆ Installation & Setup\n", "\n", - "This notebook requires **sentence-transformers** for generating embeddings and **Redis Stack** running in Docker.\n", + "This notebook uses **RedisVL vectorizers** for generating embeddings and **Redis Stack** for vector search.\n", "\n", "**Requirements:**\n", - "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", - "- sentence-transformers (for generating embeddings)\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+ (for SVS-VAMANA support)\n", + "- redisvl>=0.11.0 (required for SVS-VAMANA migration features and vectorizers)\n", + "- redis-py>=6.4.0 (required for compatibility with RedisVL 0.11.0+)\n", "- numpy (for vector operations)\n", - "- redisvl (should be available in your environment)\n", - "\n", - "**๐Ÿณ Docker Setup (Required):**\n", - "\n", - "Before running this notebook, make sure Redis Stack is running in Docker:\n", - "\n", - "```bash\n", - "# Start Redis Stack with Docker\n", - "docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest\n", - "```\n", - "\n", - "Or if you prefer using docker-compose, create a `docker-compose.yml` file:\n", "\n", - "```yaml\n", - "version: '3.8'\n", - "services:\n", - " redis:\n", - " image: redis/redis-stack:latest\n", - " ports:\n", - " - \"6379:6379\"\n", - " - \"8001:8001\"\n", - "```\n", - "\n", - "Then run: `docker-compose up -d`\n", - "\n", - "**๐Ÿ“š Python Dependencies Installation:**\n", - "\n", - "Install the required Python packages:\n", - "\n", - "```bash\n", - "# Install core dependencies\n", - "pip install redisvl numpy sentence-transformers\n", - "\n", - "# Or install with specific versions for compatibility\n", - "pip install redisvl>=0.2.0 numpy>=1.21.0 sentence-transformers>=2.2.0\n", - "```\n", - "\n", - "**For Google Colab users, run this cell:**\n", - "\n", - "```python\n", - "!pip install redisvl sentence-transformers numpy\n", - "```\n", + "**โš ๏ธ Important:** If you encounter Redis connection errors, upgrade redis-py: `pip install -U \"redis>=6.4.0\"`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Install Packages" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/nitin.kanukolanu/workspace/redis-vl-python/.venv/bin/python3: No module named pip\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install git+https://github.com/redis/redis-vl-python.git \"redis>=6.4.0\" \"numpy>=1.21.0\" \"sentence-transformers>=2.2.0\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Install Redis Stack\n", "\n", - "**For Conda users:**\n", + "Later in this tutorial, Redis will be used to store, index, and query vector\n", + "embeddings and full text fields. **We need to have a Redis\n", + "instance available.**\n", "\n", - "```bash\n", - "conda install numpy\n", - "pip install redisvl sentence-transformers\n", - "```" + "#### Local Redis\n", + "Use the shell script below to download, extract, and install [Redis Stack](https://redis.io/docs/getting-started/install-stack/) directly from the Redis package archive." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "# # Install dependencies if needed\n", - "# import sys\n", - "# import subprocess\n", - "\n", - "# def install_if_missing(package):\n", - "# try:\n", - "# __import__(package)\n", - "# except ImportError:\n", - "# print(f\"Installing {package}...\")\n", - "# subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", package])\n", - "\n", - "# # Check and install required packages\n", - "# install_if_missing(\"sentence-transformers\")\n", - "# install_if_missing(\"redisvl\")\n", - "\n", - "# print(\"โœ… All dependencies are ready!\")" + "# NBVAL_SKIP\n", + "%%sh\n", + "curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg\n", + "echo \"deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main\" | sudo tee /etc/apt/sources.list.d/redis.list\n", + "sudo apt-get update > /dev/null 2>&1\n", + "sudo apt-get install redis-stack-server > /dev/null 2>&1\n", + "redis-stack-server --daemonize yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Alternative Redis Access (Cloud, Docker, other)\n", + "There are many ways to get the necessary redis-stack instance running\n", + "1. On cloud, deploy a [FREE instance of Redis in the cloud](https://redis.com/try-free/). Or, if you have your\n", + "own version of Redis Enterprise running, that works too!\n", + "2. Per OS, [see the docs](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack/)\n", + "3. With docker: `docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define the Redis Connection URL\n", + "\n", + "By default this notebook connects to the local instance of Redis Stack. **If you have your own Redis Enterprise instance** - replace REDIS_PASSWORD, REDIS_HOST and REDIS_PORT values with your own." ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -143,7 +187,6 @@ } ], "source": [ - "# Import required libraries\n", "import os\n", "import json\n", "import numpy as np\n", @@ -158,8 +201,16 @@ "from redisvl.utils import CompressionAdvisor\n", "from redisvl.redis.connection import supports_svs\n", "\n", - "# Configuration\n", - "REDIS_URL = \"redis://localhost:6379\"\n", + "# RedisVL Vectorizer imports\n", + "from redisvl.utils.vectorize import HFTextVectorizer\n", + "\n", + "# Replace values below with your own if using Redis Cloud instance\n", + "REDIS_HOST = os.getenv(\"REDIS_HOST\", \"localhost\") # ex: \"redis-18374.c253.us-central1-1.gce.cloud.redislabs.com\"\n", + "REDIS_PORT = os.getenv(\"REDIS_PORT\", \"6379\") # ex: 18374\n", + "REDIS_PASSWORD = os.getenv(\"REDIS_PASSWORD\", \"\") # ex: \"1TNxTEdYRDgIDKM2gDfasupCADXXXX\"\n", + "\n", + "# If SSL is enabled on the endpoint, use rediss:// as the URL prefix\n", + "REDIS_URL = f\"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}\"\n", "\n", "print(\"๐Ÿ“š Libraries imported successfully!\")" ] @@ -175,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -223,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -235,8 +286,9 @@ "Genres available: {'comedy', 'action'}\n", "\n", "๐Ÿ”ง Configuration:\n", - "Vector dimensions: 1024\n", - "Dataset size: 20 movie documents\n" + "Vector dimensions: 768\n", + "Dataset size: 20 movie documents\n", + "Vectorizer: RedisVL HFTextVectorizer\n" ] } ], @@ -253,13 +305,14 @@ ")\n", "\n", "# Configuration for demonstration \n", - "dims = 1024 # sentence-transformers/all-roberta-large-v1 - 1024 dims\n", + "dims = 768 # Using all-mpnet-base-v2 model (768 dimensions)\n", "num_docs = len(movies_data) # Use actual dataset size\n", "\n", "print(\n", " f\"\\n๐Ÿ”ง Configuration:\",\n", " f\"Vector dimensions: {dims}\",\n", " f\"Dataset size: {num_docs} movie documents\",\n", + " f\"Vectorizer: RedisVL HFTextVectorizer\",\n", " sep=\"\\n\"\n", ")" ] @@ -275,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -351,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -359,83 +412,37 @@ "output_type": "stream", "text": [ "๐Ÿ”„ Generating embeddings for movie descriptions...\n", - "14:40:35 sentence_transformers.SentenceTransformer INFO Use pytorch device_name: mps\n", - "14:40:35 sentence_transformers.SentenceTransformer INFO Load pretrained SentenceTransformer: all-MiniLM-L6-v2\n" - ] - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "dfa2af21d4904b58845f57a9786706e3", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Batches: 0%| | 0/1 [00:00 0:\n", - " svs_monthly_cost = (svs_memory / 1024) * cost_per_gb_hour * hours_per_month\n", - " monthly_savings = hnsw_monthly_cost - svs_monthly_cost\n", - " print(\n", - " f\"Monthly cost reduction: ${monthly_savings:.2f}\",\n", - " f\"Annual cost reduction: ${monthly_savings * 12:.2f}\",\n", - " sep=\"\\n\"\n", - " )\n", - " else:\n", - " print(\n", - " f\"Current monthly cost: ${hnsw_monthly_cost:.2f}\",\n", - " \"Projected savings: Available after indexing completes\",\n", - " sep=\"\\n\"\n", - " )\n", "else:\n", " print(\"โš ๏ธ Memory information not available\")" ] @@ -872,7 +855,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -903,7 +886,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -911,7 +894,7 @@ "output_type": "stream", "text": [ "๐Ÿ” Testing HNSW search quality...\n", - "HNSW search completed in 0.007 seconds\n" + "HNSW search completed in 0.010 seconds\n" ] } ], @@ -939,7 +922,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -947,7 +930,7 @@ "output_type": "stream", "text": [ "๐Ÿ” Testing SVS-VAMANA search quality...\n", - "SVS-VAMANA search completed in 0.006 seconds\n" + "SVS-VAMANA search completed in 0.009 seconds\n" ] } ], @@ -984,7 +967,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -993,13 +976,14 @@ "text": [ "๐Ÿ“Š Search Quality Comparison\n", "========================================\n", - "Recall@5: 1.000 (100.0%)\n", - "Recall@10: 0.990 (99.0%)\n", + "HNSW (baseline): 100% recall (exact graph-based search)\n", + "SVS-VAMANA Recall@5: 100.0% (vs HNSW baseline)\n", + "SVS-VAMANA Recall@10: 100.0% (vs HNSW baseline)\n", "\n", "โฑ๏ธ Performance Comparison:\n", - "HNSW query time: 0.007s (0.7ms per query)\n", - "SVS-VAMANA query time: 0.006s (0.6ms per query)\n", - "Speed difference: +10.4%\n", + "HNSW query time: 0.010s (1.0ms per query)\n", + "SVS-VAMANA query time: 0.009s (0.9ms per query)\n", + "Speed difference: +5.9%\n", "\n", "๐ŸŽฏ Quality Assessment: ๐ŸŸข Excellent - Minimal quality loss\n" ] @@ -1029,13 +1013,14 @@ "print(\n", " \"๐Ÿ“Š Search Quality Comparison\",\n", " \"=\" * 40,\n", - " f\"Recall@5: {recall_at_5:.3f} ({recall_at_5*100:.1f}%)\",\n", - " f\"Recall@10: {recall_at_10:.3f} ({recall_at_10*100:.1f}%)\",\n", + " \"HNSW (baseline): 100% recall (exact graph-based search)\",\n", + " f\"SVS-VAMANA Recall@5: {recall_at_5*100:.1f}% (vs HNSW baseline)\",\n", + " f\"SVS-VAMANA Recall@10: {recall_at_10*100:.1f}% (vs HNSW baseline)\",\n", " \"\",\n", " \"โฑ๏ธ Performance Comparison:\",\n", - " f\"HNSW query time: {hnsw_time:.3f}s ({hnsw_time/num_test_queries*1000:.1f}ms per query)\",\n", - " f\"SVS-VAMANA query time: {svs_time:.3f}s ({svs_time/num_test_queries*1000:.1f}ms per query)\",\n", - " f\"Speed difference: {((hnsw_time - svs_time) / hnsw_time * 100):+.1f}%\",\n", + " f\"HNSW query time: {hnsw_time:.3f}s ({hnsw_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"SVS-VAMANA query time: {svs_time:.3f}s ({svs_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"Speed difference: {((hnsw_time - svs_time) / hnsw_time * 100):+.1f}%\",\n", " sep=\"\\n\"\n", ")\n", "\n", @@ -1063,7 +1048,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -1074,8 +1059,8 @@ "========================================\n", "\n", "๐Ÿ“Š Criteria Evaluation:\n", - "Memory savings: 75.9% โœ… (threshold: 20%)\n", - "Search quality: 0.990 โœ… (threshold: 0.85)\n", + "Memory savings: 6.5% โœ… (threshold: 5%)\n", + "Search quality: 1.000 โœ… (threshold: 0.85)\n", "\n", "๐ŸŽฏ Migration Recommendation: ๐ŸŸข RECOMMENDED\n", "๐Ÿ’ญ Reasoning: Migration provides significant memory savings while maintaining good search quality.\n" @@ -1084,7 +1069,7 @@ ], "source": [ "# Migration decision logic\n", - "memory_savings_threshold = 20 # Minimum 20% memory savings\n", + "memory_savings_threshold = 5 # Minimum % memory savings\n", "recall_threshold = 0.85 # Minimum 85% recall@10\n", "\n", "memory_savings_pct = ((hnsw_memory - svs_memory) / hnsw_memory * 100) if hnsw_memory > 0 and svs_memory > 0 else 0\n", @@ -1122,97 +1107,6 @@ ")" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Step 11: Production Migration Checklist\n", - "\n", - "If migration is recommended, follow this checklist for production deployment." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "๐Ÿ“‹ HNSW to SVS-VAMANA Migration Checklist\n", - "==================================================\n", - "\n", - "PRE-MIGRATION:\n", - "โ–ก Backup existing HNSW index data\n", - "โ–ก Test migration on staging environment\n", - "โ–ก Validate search quality with real queries\n", - "โ–ก Measure baseline HNSW performance metrics\n", - "โ–ก Plan rollback strategy\n", - "โ–ก Document current HNSW parameters (M, EF_construction, EF_runtime)\n", - "\n", - "MIGRATION:\n", - "โ–ก Create SVS-VAMANA index with tested configuration\n", - "โ–ก Migrate data in batches during low-traffic periods\n", - "โ–ก Monitor memory usage and indexing progress\n", - "โ–ก Validate data integrity after migration\n", - "โ–ก Test search functionality thoroughly\n", - "โ–ก Compare recall metrics with baseline\n", - "\n", - "POST-MIGRATION:\n", - "โ–ก Monitor search performance and quality\n", - "โ–ก Track memory usage and cost savings\n", - "โ–ก Update application configuration\n", - "โ–ก Document new SVS-VAMANA settings\n", - "โ–ก Clean up old HNSW index after validation period\n", - "โ–ก Update monitoring and alerting thresholds\n", - "\n", - "๐Ÿ’ก HNSW-SPECIFIC TIPS:\n", - "โ€ข HNSW indices are more complex to rebuild than FLAT\n", - "โ€ข Consider the impact on applications using EF_runtime tuning\n", - "โ€ข SVS-VAMANA may have different optimal query parameters\n", - "โ€ข Test with your specific HNSW configuration (M, EF values)\n", - "โ€ข Monitor for 48-72 hours before removing HNSW index\n", - "โ€ข Keep compression settings documented for future reference\n" - ] - } - ], - "source": [ - "print(\n", - " \"๐Ÿ“‹ HNSW to SVS-VAMANA Migration Checklist\",\n", - " \"=\" * 50,\n", - " \"\\nPRE-MIGRATION:\",\n", - " \"โ–ก Backup existing HNSW index data\",\n", - " \"โ–ก Test migration on staging environment\",\n", - " \"โ–ก Validate search quality with real queries\",\n", - " \"โ–ก Measure baseline HNSW performance metrics\",\n", - " \"โ–ก Plan rollback strategy\",\n", - " \"โ–ก Document current HNSW parameters (M, EF_construction, EF_runtime)\",\n", - " \"\\nMIGRATION:\",\n", - " \"โ–ก Create SVS-VAMANA index with tested configuration\",\n", - " \"โ–ก Migrate data in batches during low-traffic periods\",\n", - " \"โ–ก Monitor memory usage and indexing progress\",\n", - " \"โ–ก Validate data integrity after migration\",\n", - " \"โ–ก Test search functionality thoroughly\",\n", - " \"โ–ก Compare recall metrics with baseline\",\n", - " \"\\nPOST-MIGRATION:\",\n", - " \"โ–ก Monitor search performance and quality\",\n", - " \"โ–ก Track memory usage and cost savings\",\n", - " \"โ–ก Update application configuration\",\n", - " \"โ–ก Document new SVS-VAMANA settings\",\n", - " \"โ–ก Clean up old HNSW index after validation period\",\n", - " \"โ–ก Update monitoring and alerting thresholds\",\n", - " \"\\n๐Ÿ’ก HNSW-SPECIFIC TIPS:\",\n", - " \"โ€ข HNSW indices are more complex to rebuild than FLAT\",\n", - " \"โ€ข Consider the impact on applications using EF_runtime tuning\",\n", - " \"โ€ข SVS-VAMANA may have different optimal query parameters\",\n", - " \"โ€ข Test with your specific HNSW configuration (M, EF values)\",\n", - " \"โ€ข Monitor for 48-72 hours before removing HNSW index\",\n", - " \"โ€ข Keep compression settings documented for future reference\",\n", - " sep=\"\\n\"\n", - ")" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -1224,7 +1118,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -1275,13 +1169,6 @@ ")" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "code", "execution_count": null, diff --git a/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb b/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb index e52879c9..6b7789f5 100644 --- a/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb +++ b/python-recipes/vector-search/07_flat_to_svs_vamana_migration.ipynb @@ -28,76 +28,134 @@ "- Vector embeddings (768 dimensions using sentence-transformers/all-mpnet-base-v2)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ๐Ÿ“‹ FLAT to SVS-VAMANA Migration Checklist\n", + "\n", + "**PRE-MIGRATION:**\n", + "- โ˜ Backup existing FLAT index data\n", + "- โ˜ Test migration on staging environment\n", + "- โ˜ Validate search quality with real queries\n", + "- โ˜ Measure baseline FLAT performance metrics\n", + "- โ˜ Plan rollback strategy\n", + "- โ˜ Document current FLAT index configuration\n", + "\n", + "**MIGRATION:**\n", + "- โ˜ Create SVS-VAMANA index with tested configuration\n", + "- โ˜ Migrate data in batches during low-traffic periods\n", + "- โ˜ Monitor memory usage and indexing progress\n", + "- โ˜ Validate data integrity after migration\n", + "- โ˜ Test search functionality thoroughly\n", + "- โ˜ Compare recall metrics with baseline\n", + "\n", + "**POST-MIGRATION:**\n", + "- โ˜ Monitor search performance and quality\n", + "- โ˜ Track memory usage and cost savings\n", + "- โ˜ Update application configuration\n", + "- โ˜ Document new SVS-VAMANA settings\n", + "- โ˜ Clean up old FLAT index after validation period\n", + "- โ˜ Update monitoring and alerting thresholds\n", + "\n", + "**๐Ÿ’ก FLAT-SPECIFIC TIPS:**\n", + "- FLAT indices are simpler to migrate than HNSW (no graph structure)\n", + "- FLAT provides 100% recall, so focus on acceptable recall threshold for SVS-VAMANA\n", + "- SVS-VAMANA will be faster than FLAT for large datasets\n", + "- Memory savings are most significant with FLAT migrations\n", + "- Consider using compression for maximum memory reduction\n", + "- Test query performance improvements with your dataset size" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ๐Ÿ“ฆ Installation & Setup\n", "\n", - "This notebook requires **sentence-transformers** for generating embeddings and **Redis Stack** running in Docker.\n", + "This notebook uses **RedisVL vectorizers** for generating embeddings and **Redis Stack** for vector search.\n", "\n", "**Requirements:**\n", - "- Redis Stack 8.2.0+ with RediSearch 2.8.10+\n", - "- sentence-transformers (for generating embeddings)\n", + "- Redis Stack 8.2.0+ with RediSearch 2.8.10+ (for SVS-VAMANA support)\n", + "- redisvl>=0.11.0 (required for SVS-VAMANA migration features and vectorizers)\n", + "- redis-py>=6.4.0 (required for compatibility with RedisVL 0.11.0+)\n", "- numpy (for vector operations)\n", - "- redisvl (should be available in your environment)\n", - "\n", - "**๐Ÿณ Docker Setup (Required):**\n", - "\n", - "Before running this notebook, make sure Redis Stack is running in Docker:\n", - "\n", - "```bash\n", - "# Start Redis Stack with Docker\n", - "docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest\n", - "```\n", "\n", - "Or if you prefer using docker-compose, create a `docker-compose.yml` file:\n", - "\n", - "```yaml\n", - "version: '3.8'\n", - "services:\n", - " redis:\n", - " image: redis/redis-stack:latest\n", - " ports:\n", - " - \"6379:6379\"\n", - " - \"8001:8001\"\n", - "```\n", - "\n", - "Then run: `docker-compose up -d`\n", - "\n", - "**๐Ÿ“š Python Dependencies Installation:**\n", - "\n", - "Install the required Python packages:\n", - "\n", - "```bash\n", - "# Install core dependencies\n", - "pip install redisvl numpy sentence-transformers\n", - "\n", - "# Or install with specific versions for compatibility\n", - "pip install redisvl>=0.2.0 numpy>=1.21.0 sentence-transformers>=2.2.0\n", - "```\n", - "\n", - "**For Google Colab users, run this cell:**\n", + "**โš ๏ธ Important:** If you encounter Redis connection errors, upgrade redis-py: `pip install -U \"redis>=6.4.0\"`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Install Packages" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%pip install git+https://github.com/redis/redis-vl-python.git \"redis>=6.4.0\" \"numpy>=1.21.0\" \"sentence-transformers>=2.2.0\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Install Redis Stack\n", "\n", - "```python\n", - "!pip install redisvl sentence-transformers numpy\n", - "```\n", + "Later in this tutorial, Redis will be used to store, index, and query vector\n", + "embeddings and full text fields. **We need to have a Redis\n", + "instance available.**\n", "\n", - "**For Conda users:**\n", + "#### Local Redis\n", + "Use the shell script below to download, extract, and install [Redis Stack](https://redis.io/docs/getting-started/install-stack/) directly from the Redis package archive." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# NBVAL_SKIP\n", + "%%sh\n", + "curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg\n", + "echo \"deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main\" | sudo tee /etc/apt/sources.list.d/redis.list\n", + "sudo apt-get update > /dev/null 2>&1\n", + "sudo apt-get install redis-stack-server > /dev/null 2>&1\n", + "redis-stack-server --daemonize yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Alternative Redis Access (Cloud, Docker, other)\n", + "There are many ways to get the necessary redis-stack instance running\n", + "1. On cloud, deploy a [FREE instance of Redis in the cloud](https://redis.com/try-free/). Or, if you have your\n", + "own version of Redis Enterprise running, that works too!\n", + "2. Per OS, [see the docs](https://redis.io/docs/latest/operate/oss_and_stack/install/install-stack/)\n", + "3. With docker: `docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Define the Redis Connection URL\n", "\n", - "```bash\n", - "conda install numpy\n", - "pip install redisvl sentence-transformers\n", - "```" + "By default this notebook connects to the local instance of Redis Stack. **If you have your own Redis Enterprise instance** - replace REDIS_PASSWORD, REDIS_HOST and REDIS_PORT values with your own." ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Setup redis-vl environment\n", "import os\n", "import sys\n", "import subprocess\n", @@ -109,7 +167,18 @@ "from redisvl.redis.utils import array_to_buffer, buffer_to_array\n", "from redisvl.utils import CompressionAdvisor\n", "from redisvl.redis.connection import supports_svs\n", - "import redis\n" + "import redis\n", + "\n", + "# RedisVL Vectorizer imports\n", + "from redisvl.utils.vectorize import HFTextVectorizer\n", + "\n", + "# Replace values below with your own if using Redis Cloud instance\n", + "REDIS_HOST = os.getenv(\"REDIS_HOST\", \"localhost\") # ex: \"redis-18374.c253.us-central1-1.gce.cloud.redislabs.com\"\n", + "REDIS_PORT = os.getenv(\"REDIS_PORT\", \"6379\") # ex: 18374\n", + "REDIS_PASSWORD = os.getenv(\"REDIS_PASSWORD\", \"\") # ex: \"1TNxTEdYRDgIDKM2gDfasupCADXXXX\"\n", + "\n", + "# If SSL is enabled on the endpoint, use rediss:// as the URL prefix\n", + "REDIS_URL = f\"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}\"" ] }, { @@ -123,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -169,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -199,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -207,23 +276,25 @@ "output_type": "stream", "text": [ "๐Ÿ“Š Migration Assessment\n", - "Vector dimensions: 768 (sentence-transformers/all-mpnet-base-v2)\n", + "Vector dimensions: 768\n", "Dataset size: 20 movie documents\n", - "Data includes: title, genre, rating, description\n" + "Data includes: title, genre, rating, description\n", + "Vectorizer: RedisVL HFTextVectorizer\n" ] } ], "source": [ "# Configuration for demonstration \n", - "dims = 768 # sentence-transformers/all-mpnet-base-v2 - 768 dims\n", + "dims = 768 # Using all-mpnet-base-v2 model (768 dimensions)\n", "\n", "num_docs = len(movies_data) # Use actual dataset size\n", "\n", "print(\n", " \"๐Ÿ“Š Migration Assessment\",\n", - " f\"Vector dimensions: {dims} (sentence-transformers/all-mpnet-base-v2)\",\n", + " f\"Vector dimensions: {dims}\",\n", " f\"Dataset size: {num_docs} movie documents\",\n", " \"Data includes: title, genre, rating, description\",\n", + " f\"Vectorizer: RedisVL HFTextVectorizer\",\n", " sep=\"\\n\"\n", ")" ] @@ -238,7 +309,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -246,6 +317,7 @@ "output_type": "stream", "text": [ "Creating sample FLAT index...\n", + "12:00:03 redisvl.index.index INFO Index already exists, overwriting.\n", "โœ… Created FLAT index: migration_demo_flat\n" ] } @@ -292,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -300,74 +372,30 @@ "output_type": "stream", "text": [ "๐Ÿ”„ Generating embeddings for movie descriptions...\n", - "๐Ÿ“ฆ Loading sentence transformer model...\n", - "14:45:27 sentence_transformers.SentenceTransformer INFO Use pytorch device_name: mps\n", - "14:45:27 sentence_transformers.SentenceTransformer INFO Load pretrained SentenceTransformer: sentence-transformers/all-mpnet-base-v2\n", - "โœ… Loaded embedding model with 768 dimensions\n" - ] - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "0e06f2f860ec443e802a3fbf3961487c", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Batches: 0%| | 0/1 [00:00 0:\n", - " svs_monthly_cost = (svs_memory / 1024) * cost_per_gb_hour * hours_per_month\n", - " monthly_savings = flat_monthly_cost - svs_monthly_cost\n", - " print(\n", - " f\"Monthly cost reduction: ${monthly_savings:.2f}\",\n", - " f\"Annual cost reduction: ${monthly_savings * 12:.2f}\",\n", - " sep=\"\\n\"\n", - " )\n", - " else:\n", - " print(\n", - " f\"Current monthly cost: ${flat_monthly_cost:.2f}\",\n", - " \"Projected savings: Available after indexing completes\",\n", - " sep=\"\\n\"\n", - " )\n", "else:\n", " print(\"โš ๏ธ Memory information not available\")" ] @@ -797,50 +802,58 @@ "source": [ "## Step 7: Validate Search Quality\n", "\n", - "Test that the compressed index maintains good search quality." + "Compare search quality and performance between FLAT and SVS-VAMANA indices." ] }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "๐Ÿ” Validating search quality...\n", - "Generated 5 test queries\n", - "\n", - "Testing original FLAT index...\n", - "FLAT search time: 0.012s (0.002s per query)\n", + "๐Ÿ“Š Search Quality Comparison\n", + "========================================\n", + "Recall@5: 0.667 (66.7%)\n", + "Recall@10: 0.800 (80.0%)\n", "\n", - "Testing SVS-VAMANA index...\n", - "SVS search time: 0.017s (0.003s per query)\n", + "โฑ๏ธ Performance Comparison:\n", + "FLAT query time: 0.018s (1.8ms per query)\n", + "SVS-VAMANA query time: 0.013s (1.3ms per query)\n", + "Speed difference: +28.3%\n", "\n", - "๐Ÿ“ˆ Average recall@10: 1.000 (100.0%)\n", - "โœ… Excellent search quality maintained\n" + "๐ŸŽฏ Quality Assessment: ๐ŸŸ  Fair - Consider if quality requirements are flexible\n" ] } ], "source": [ - "print(\"๐Ÿ” Validating search quality...\")\n", + "# Calculate recall and performance metrics\n", + "def calculate_recall(reference_results, test_results, k=10):\n", + " \"\"\"Calculate recall@k between two result sets\"\"\"\n", + " if not reference_results or not test_results:\n", + " return 0.0\n", + " \n", + " ref_ids = set([doc['movie_id'] for doc in reference_results[:k]])\n", + " test_ids = set([doc['movie_id'] for doc in test_results[:k]])\n", + " \n", + " if not ref_ids:\n", + " return 0.0\n", + " \n", + " return len(ref_ids.intersection(test_ids)) / len(ref_ids)\n", "\n", "# Create test queries\n", - "num_test_queries = 5\n", + "num_test_queries = 10\n", "test_queries = []\n", "\n", "for i in range(num_test_queries):\n", - " # Generate normalized test vector\n", " query_vec = np.random.random(dims).astype(np.float32)\n", " query_vec = query_vec / np.linalg.norm(query_vec)\n", " test_queries.append(query_vec)\n", "\n", - "print(f\"Generated {num_test_queries} test queries\")\n", - "\n", "# Test FLAT index (ground truth)\n", - "print(\"\\nTesting original FLAT index...\")\n", - "flat_results = []\n", + "flat_results_list = []\n", "flat_start = time.time()\n", "\n", "for query_vec in test_queries:\n", @@ -852,17 +865,15 @@ " num_results=10\n", " )\n", " results = flat_index.query(query)\n", - " flat_results.append([doc[\"movie_id\"] for doc in results])\n", + " flat_results_list.append(results)\n", "\n", "flat_time = time.time() - flat_start\n", - "print(f\"FLAT search time: {flat_time:.3f}s ({flat_time/num_test_queries:.3f}s per query)\")\n", "\n", "# Test SVS-VAMANA index\n", - "print(\"\\nTesting SVS-VAMANA index...\")\n", - "svs_results = []\n", + "svs_results_list = []\n", "svs_start = time.time()\n", "\n", - "for i, query_vec in enumerate(test_queries):\n", + "for query_vec in test_queries:\n", " # Adjust query vector for SVS index (handle dimensionality reduction)\n", " if target_dims < dims:\n", " svs_query_vec = query_vec[:target_dims]\n", @@ -879,223 +890,110 @@ " dtype=target_dtype,\n", " num_results=10\n", " )\n", - " \n", - " try:\n", - " results = svs_index.query(query)\n", - " svs_results.append([doc[\"movie_id\"] for doc in results])\n", - " except Exception as e:\n", - " print(f\"Query {i+1} failed: {e}\")\n", - " svs_results.append([])\n", + " results = svs_index.query(query)\n", + " svs_results_list.append(results)\n", "\n", "svs_time = time.time() - svs_start\n", - "print(f\"SVS search time: {svs_time:.3f}s ({svs_time/num_test_queries:.3f}s per query)\")\n", - "\n", - "# Calculate recall if we have results\n", - "if svs_results and any(svs_results):\n", - " recalls = []\n", - " for flat_res, svs_res in zip(flat_results, svs_results):\n", - " if flat_res and svs_res:\n", - " intersection = set(flat_res).intersection(set(svs_res))\n", - " recall = len(intersection) / len(flat_res)\n", - " recalls.append(recall)\n", - " \n", - " if recalls:\n", - " avg_recall = np.mean(recalls)\n", - " print(f\"\\n๐Ÿ“ˆ Average recall@10: {avg_recall:.3f} ({avg_recall*100:.1f}%)\")\n", - " \n", - " if avg_recall >= 0.9:\n", - " print(\"โœ… Excellent search quality maintained\")\n", - " elif avg_recall >= 0.8:\n", - " print(\"โœ… Good search quality maintained\")\n", - " else:\n", - " print(\"โš ๏ธ Search quality may be impacted - consider adjusting compression\")\n", - "else:\n", - " print(\"โš ๏ธ SVS index may still be indexing - search quality test pending\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Step 8: Migration Decision Framework\n", - "\n", - "Based on the results, let's determine if migration is recommended." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "๐ŸŽฏ Migration Analysis & Recommendation\n", - "==================================================\n", - "Dataset: 20 documents, 768-dimensional vectors\n", - "Compression: LVQ4\n", - "Datatype: float32 โ†’ float32\n", - "\n", - "Memory savings: -0.0% (Modest)\n", - "Search quality: 1.0% recall (Acceptable)\n", - "Performance: 1.4x vs original (Acceptable)\n", - "\n", - "๐Ÿ† RECOMMENDATION:\n", - "โŒ MIGRATION NOT RECOMMENDED\n", - " โ€ข Insufficient benefits for current dataset\n", - " โ€ข Consider larger dataset or different compression\n", - " โ€ข SVS-VAMANA works best with high-dimensional data\n" - ] - } - ], - "source": [ - "print(\"๐ŸŽฏ Migration Analysis & Recommendation\")\n", - "print(\"=\" * 50)\n", "\n", - "# Fallback configuration if not defined (for CI/CD compatibility)\n", - "if 'selected_config' not in locals():\n", - " from redisvl.utils import CompressionAdvisor\n", - " selected_config = CompressionAdvisor.recommend(dims=dims, priority=\"memory\")\n", + "# Calculate recall metrics\n", + "recall_at_5 = np.mean([calculate_recall(flat_res, svs_res, k=5) \n", + " for flat_res, svs_res in zip(flat_results_list, svs_results_list)])\n", + "recall_at_10 = np.mean([calculate_recall(flat_res, svs_res, k=10) \n", + " for flat_res, svs_res in zip(flat_results_list, svs_results_list)])\n", "\n", - "# Summarize configuration\n", - "print(f\"Dataset: {num_docs} documents, {dims}-dimensional vectors\")\n", - "print(f\"Compression: {selected_config.get('compression', 'None')}\")\n", - "print(f\"Datatype: float32 โ†’ {selected_config['datatype']}\")\n", - "if 'reduce' in selected_config:\n", - " reduction = ((dims - selected_config['reduce']) / dims) * 100\n", - " print(f\"Dimensions: {dims} โ†’ {selected_config['reduce']} ({reduction:.1f}% reduction)\")\n", - "print()\n", - "\n", - "# Decision criteria\n", - "memory_savings_significant = False\n", - "search_quality_acceptable = True\n", - "performance_acceptable = True\n", - "\n", - "if flat_memory > 0 and svs_memory > 0:\n", - " savings_pct = ((flat_memory - svs_memory) / flat_memory) * 100\n", - " memory_savings_significant = savings_pct > 25 # 25%+ savings considered significant\n", - " print(f\"Memory savings: {savings_pct:.1f}% ({'Significant' if memory_savings_significant else 'Modest'})\")\n", - "else:\n", - " print(\"Memory savings: Pending (SVS index still indexing)\")\n", + "print(\n", + " \"๐Ÿ“Š Search Quality Comparison\",\n", + " \"=\" * 40,\n", + " \"FLAT (baseline): 100% recall (brute-force exact search)\",\n", + " f\"SVS-VAMANA Recall@5: {recall_at_5*100:.1f}% (vs FLAT baseline)\",\n", + " f\"SVS-VAMANA Recall@10: {recall_at_10*100:.1f}% (vs FLAT baseline)\",\n", + " \"\",\n", + " \"โฑ๏ธ Performance Comparison:\",\n", + " f\"FLAT query time: {flat_time:.3f}s ({flat_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"SVS-VAMANA query time: {svs_time:.3f}s ({svs_time/num_test_queries*1000:.1f}ms per query)\",\n", + " f\"Speed difference: {((flat_time - svs_time) / flat_time * 100):+.1f}%\",\n", + " sep=\"\\n\"\n", + ")\n", "\n", - "if 'recalls' in locals() and recalls:\n", - " avg_recall = np.mean(recalls)\n", - " search_quality_acceptable = avg_recall >= 0.8 # 80%+ recall considered acceptable\n", - " print(f\"Search quality: {avg_recall:.1f}% recall ({'Acceptable' if search_quality_acceptable else 'Needs improvement'})\")\n", + "# Quality assessment\n", + "if recall_at_10 >= 0.95:\n", + " quality_assessment = \"๐ŸŸข Excellent - Minimal quality loss\"\n", + "elif recall_at_10 >= 0.90:\n", + " quality_assessment = \"๐ŸŸก Good - Acceptable quality for most applications\"\n", + "elif recall_at_10 >= 0.80:\n", + " quality_assessment = \"๐ŸŸ  Fair - Consider if quality requirements are flexible\"\n", "else:\n", - " print(\"Search quality: Pending validation\")\n", + " quality_assessment = \"๐Ÿ”ด Poor - Migration not recommended\"\n", "\n", - "if 'flat_time' in locals() and 'svs_time' in locals():\n", - " performance_ratio = svs_time / flat_time if flat_time > 0 else 1\n", - " performance_acceptable = performance_ratio <= 2.0 # Allow up to 2x slower\n", - " print(f\"Performance: {performance_ratio:.1f}x vs original ({'Acceptable' if performance_acceptable else 'Slower than expected'})\")\n", - "else:\n", - " print(\"Performance: Pending comparison\")\n", - "\n", - "\n", - "# Final recommendation\n", - "print(\"\\n๐Ÿ† RECOMMENDATION:\")\n", - "if memory_savings_significant and search_quality_acceptable and performance_acceptable:\n", - " print(\"โœ… MIGRATE TO SVS-VAMANA\")\n", - " print(\" โ€ข Significant memory savings achieved\")\n", - " print(\" โ€ข Search quality maintained\")\n", - " print(\" โ€ข Performance impact acceptable\")\n", - " print(\" โ€ข Cost reduction benefits clear\")\n", - "elif memory_savings_significant and search_quality_acceptable:\n", - " print(\"โš ๏ธ CONSIDER MIGRATION WITH MONITORING\")\n", - " print(\" โ€ข Good memory savings and search quality\")\n", - " print(\" โ€ข Monitor performance in production\")\n", - " print(\" โ€ข Consider gradual rollout\")\n", - "elif memory_savings_significant:\n", - " print(\"โš ๏ธ MIGRATION NEEDS TUNING\")\n", - " print(\" โ€ข Memory savings achieved\")\n", - " print(\" โ€ข Search quality or performance needs improvement\")\n", - " print(\" โ€ข Try different compression settings\")\n", - "else:\n", - " print(\"โŒ MIGRATION NOT RECOMMENDED\")\n", - " print(\" โ€ข Insufficient benefits for current dataset\")\n", - " print(\" โ€ข Consider larger dataset or different compression\")\n", - " print(\" โ€ข SVS-VAMANA works best with high-dimensional data\")" + "print(f\"\\n๐ŸŽฏ Quality Assessment: {quality_assessment}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Step 9: Production Migration Checklist\n", + "## Step 8: Migration Decision Framework\n", "\n", - "If migration is recommended, follow this checklist for production deployment." + "Analyze the migration results and provide a recommendation based on memory savings and search quality." ] }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "๐Ÿ“‹ Production Migration Checklist\n", + "๐Ÿค” Migration Decision Analysis\n", "========================================\n", "\n", - "PRE-MIGRATION:\n", - "โ–ก Backup existing index data\n", - "โ–ก Test migration on staging environment\n", - "โ–ก Validate search quality with real queries\n", - "โ–ก Measure baseline performance metrics\n", - "โ–ก Plan rollback strategy\n", - "\n", - "MIGRATION:\n", - "โ–ก Create SVS-VAMANA index with tested configuration\n", - "โ–ก Migrate data in batches during low-traffic periods\n", - "โ–ก Monitor memory usage and indexing progress\n", - "โ–ก Validate data integrity after migration\n", - "โ–ก Test search functionality thoroughly\n", + "๐Ÿ“Š Criteria Evaluation:\n", + "Memory savings: -0.1% โŒ (threshold: 5%)\n", + "Search quality: 0.800 โŒ (threshold: 0.85)\n", "\n", - "POST-MIGRATION:\n", - "โ–ก Monitor search performance and quality\n", - "โ–ก Track memory usage and cost savings\n", - "โ–ก Update application configuration\n", - "โ–ก Document new index settings\n", - "โ–ก Clean up old index after validation period\n", - "\n", - "๐Ÿ’ก TIPS:\n", - "โ€ข Start with a subset of data for initial validation\n", - "โ€ข Use blue-green deployment for zero-downtime migration\n", - "โ€ข Monitor for 24-48 hours before removing old index\n", - "โ€ข Keep compression settings documented for future reference\n" + "๐ŸŽฏ Migration Recommendation: ๐Ÿ”ด NOT RECOMMENDED\n", + "๐Ÿ’ญ Reasoning: Insufficient memory savings and/or poor search quality. Consider alternative optimization strategies.\n" ] } ], "source": [ + "# Migration decision logic\n", + "memory_savings_threshold = 5 # Minimum % memory savings\n", + "recall_threshold = 0.85 # Minimum 85% recall@10\n", + "\n", + "memory_savings_pct = ((flat_memory - svs_memory) / flat_memory * 100) if flat_memory > 0 and svs_memory > 0 else 0\n", + "meets_memory_threshold = memory_savings_pct >= memory_savings_threshold\n", + "meets_quality_threshold = recall_at_10 >= recall_threshold\n", + "\n", "print(\n", - " \"๐Ÿ“‹ Production Migration Checklist\",\n", + " \"๐Ÿค” Migration Decision Analysis\",\n", " \"=\" * 40,\n", - " \"\\nPRE-MIGRATION:\",\n", - " \"โ–ก Backup existing index data\",\n", - " \"โ–ก Test migration on staging environment\",\n", - " \"โ–ก Validate search quality with real queries\",\n", - " \"โ–ก Measure baseline performance metrics\",\n", - " \"โ–ก Plan rollback strategy\",\n", - " \"\\nMIGRATION:\",\n", - " \"โ–ก Create SVS-VAMANA index with tested configuration\",\n", - " \"โ–ก Migrate data in batches during low-traffic periods\",\n", - " \"โ–ก Monitor memory usage and indexing progress\",\n", - " \"โ–ก Validate data integrity after migration\",\n", - " \"โ–ก Test search functionality thoroughly\",\n", - " \"\\nPOST-MIGRATION:\",\n", - " \"โ–ก Monitor search performance and quality\",\n", - " \"โ–ก Track memory usage and cost savings\",\n", - " \"โ–ก Update application configuration\",\n", - " \"โ–ก Document new index settings\",\n", - " \"โ–ก Clean up old index after validation period\",\n", - " \"\\n๐Ÿ’ก TIPS:\",\n", - " \"โ€ข Start with a subset of data for initial validation\",\n", - " \"โ€ข Use blue-green deployment for zero-downtime migration\",\n", - " \"โ€ข Monitor for 24-48 hours before removing old index\",\n", - " \"โ€ข Keep compression settings documented for future reference\",\n", + " \"\",\n", + " \"๐Ÿ“Š Criteria Evaluation:\",\n", + " f\"Memory savings: {memory_savings_pct:.1f}% {'โœ…' if meets_memory_threshold else 'โŒ'} (threshold: {memory_savings_threshold}%)\",\n", + " f\"Search quality: {recall_at_10:.3f} {'โœ…' if meets_quality_threshold else 'โŒ'} (threshold: {recall_threshold})\",\n", + " \"\",\n", + " sep=\"\\n\"\n", + ")\n", + "\n", + "if meets_memory_threshold and meets_quality_threshold:\n", + " recommendation = \"๐ŸŸข RECOMMENDED\"\n", + " reasoning = \"Migration provides significant memory savings while maintaining good search quality.\"\n", + "elif meets_memory_threshold and not meets_quality_threshold:\n", + " recommendation = \"๐ŸŸก CONDITIONAL\"\n", + " reasoning = \"Good memory savings but reduced search quality. Consider if your application can tolerate lower recall.\"\n", + "elif not meets_memory_threshold and meets_quality_threshold:\n", + " recommendation = \"๐ŸŸ  LIMITED BENEFIT\"\n", + " reasoning = \"Search quality is maintained but memory savings are minimal. Migration may not be worth the effort.\"\n", + "else:\n", + " recommendation = \"๐Ÿ”ด NOT RECOMMENDED\"\n", + " reasoning = \"Insufficient memory savings and/or poor search quality. Consider alternative optimization strategies.\"\n", + "\n", + "print(\n", + " f\"๐ŸŽฏ Migration Recommendation: {recommendation}\",\n", + " f\"๐Ÿ’ญ Reasoning: {reasoning}\",\n", " sep=\"\\n\"\n", ")" ] @@ -1104,14 +1002,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Step 10: Cleanup\n", + "## Step 9: Cleanup\n", "\n", "Clean up the demonstration indices." ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 16, "metadata": {}, "outputs": [ {