{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hybrid Search with FastEmbed & Qdrant\n", "\n", "Author: [Nirant Kasliwal](https://twitter.com/nirantk)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What will we do?\n", "This notebook demonstrates the usage of Hybrid Search with FastEmbed & Qdrant. \n", "\n", "1. Setup: Download and install the required dependencies\n", "2. Preview data: Load and preview the data\n", "3. Create Sparse Embeddings: Create SPLADE++ embeddings for the data\n", "4. Create Dense Embeddings: Create BGE-Large-en-v1.5 embeddings for the data\n", "5. Indexing: Index the embeddings using Qdrant\n", "6. Search: Perform Hybrid Search using FastEmbed & Qdrant\n", "7. Ranking: Rank the search results with Reciprocal Rank Fusion (RRF)\n", "\n", "## Setup\n", "\n", "In order to get started, you need a few dependencies, and we'll install them next:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "!pip install -qU qdrant-client fastembed datasets transformers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2024-03-30T00:46:59.293555Z", "start_time": "2024-03-30T00:46:59.285419Z" } }, "outputs": [], "source": [ "import json\n", "from typing import List, Tuple\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from datasets import load_dataset\n", "from qdrant_client import QdrantClient\n", "from qdrant_client.models import (\n", " Distance,\n", " NamedSparseVector,\n", " NamedVector,\n", " SparseVector,\n", " PointStruct,\n", " SearchRequest,\n", " SparseIndexParams,\n", " SparseVectorParams,\n", " VectorParams,\n", " ScoredPoint,\n", ")\n", "from transformers import AutoTokenizer\n", "\n", "import fastembed\n", "from fastembed import SparseEmbedding, SparseTextEmbedding, TextEmbedding" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0.2.5'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fastembed.__version__ # 0.2.5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2024-03-30T00:47:06.815264Z", "start_time": "2024-03-30T00:47:00.149649Z" } }, "outputs": [ { "data": { "text/plain": [ "Dataset({\n", " features: ['example_id', 'query', 'query_id', 'product_id', 'product_locale', 'esci_label', 'small_version', 'large_version', 'product_title', 'product_description', 'product_bullet_point', 'product_brand', 'product_color', 'product_text'],\n", " num_rows: 919\n", "})" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset = load_dataset(\"tasksource/esci\", split=\"train\")\n", "# We'll select the first 1000 examples for this demo\n", "dataset = dataset.select(range(1000))\n", "dataset = dataset.filter(lambda x: x[\"product_locale\"] == \"us\")\n", "dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preview Data" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2024-03-30T00:47:06.831216Z", "start_time": "2024-03-30T00:47:06.810740Z" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | example_id | \n", "query | \n", "query_id | \n", "product_id | \n", "product_locale | \n", "esci_label | \n", "small_version | \n", "large_version | \n", "product_title | \n", "product_description | \n", "product_bullet_point | \n", "product_brand | \n", "product_color | \n", "product_text | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "0 | \n", "revent 80 cfm | \n", "0 | \n", "B000MOO21W | \n", "us | \n", "Irrelevant | \n", "0 | \n", "1 | \n", "Panasonic FV-20VQ3 WhisperCeiling 190 CFM Ceil... | \n", "None | \n", "WhisperCeiling fans feature a totally enclosed... | \n", "Panasonic | \n", "White | \n", "Panasonic FV-20VQ3 WhisperCeiling 190 CFM Ceil... | \n", "
| 2 | \n", "1 | \n", "revent 80 cfm | \n", "0 | \n", "B07X3Y6B1V | \n", "us | \n", "Exact | \n", "0 | \n", "1 | \n", "Homewerks 7141-80 Bathroom Fan Integrated LED ... | \n", "None | \n", "OUTSTANDING PERFORMANCE: This Homewerk's bath ... | \n", "Homewerks | \n", "80 CFM | \n", "Homewerks 7141-80 Bathroom Fan Integrated LED ... | \n", "
| 3 | \n", "2 | \n", "revent 80 cfm | \n", "0 | \n", "B07WDM7MQQ | \n", "us | \n", "Exact | \n", "0 | \n", "1 | \n", "Homewerks 7140-80 Bathroom Fan Ceiling Mount E... | \n", "None | \n", "OUTSTANDING PERFORMANCE: This Homewerk's bath ... | \n", "Homewerks | \n", "White | \n", "Homewerks 7140-80 Bathroom Fan Ceiling Mount E... | \n", "
| 4 | \n", "3 | \n", "revent 80 cfm | \n", "0 | \n", "B07RH6Z8KW | \n", "us | \n", "Exact | \n", "0 | \n", "1 | \n", "Delta Electronics RAD80L BreezRadiance 80 CFM ... | \n", "This pre-owned or refurbished product has been... | \n", "Quiet operation at 1.5 sones\\nBuilt-in thermos... | \n", "DELTA ELECTRONICS (AMERICAS) LTD. | \n", "White | \n", "Delta Electronics RAD80L BreezRadiance 80 CFM ... | \n", "
| 5 | \n", "4 | \n", "revent 80 cfm | \n", "0 | \n", "B07QJ7WYFQ | \n", "us | \n", "Exact | \n", "0 | \n", "1 | \n", "Panasonic FV-08VRE2 Ventilation Fan with Reces... | \n", "None | \n", "The design solution for Fan/light combinations... | \n", "Panasonic | \n", "White | \n", "Panasonic FV-08VRE2 Ventilation Fan with Reces... | \n", "