{ "cells": [ { "cell_type": "markdown", "id": "cbba56a9", "metadata": { "id": "cbba56a9" }, "source": [ "![Redis](https://redis.io/wp-content/uploads/2024/04/Logotype.svg?auto=webp&quality=85,75&width=120)\n", "# Vector Search with RedisVL\n", "\n", "## Let's Begin!\n", "\"Open\n" ] }, { "cell_type": "markdown", "id": "0b80de6b", "metadata": { "id": "0b80de6b" }, "source": [ "## Prepare data\n", "\n", "In this examples we will load a list of movies with the following attributes: `title`, `rating`, `description`, and `genre`.\n", "\n", "We will embed the movie description so that user's can search for movies that best match the kind of movie that they're looking for.\n", "\n", "**If you are running this notebook locally**, FYI you may not need to perform this step at all." ] }, { "cell_type": "code", "execution_count": 1, "id": "b966a9b5", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "b966a9b5", "outputId": "8fb1aed9-94a3-47b2-af50-4eac9b08d7f1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cloning into 'temp_repo'...\n", "remote: Enumerating objects: 669, done.\u001b[K\n", "remote: Counting objects: 100% (320/320), done.\u001b[K\n", "remote: Compressing objects: 100% (207/207), done.\u001b[K\n", "remote: Total 669 (delta 219), reused 141 (delta 112), pack-reused 349 (from 2)\u001b[K\n", "Receiving objects: 100% (669/669), 57.77 MiB | 20.61 MiB/s, done.\n", "Resolving deltas: 100% (287/287), done.\n" ] } ], "source": [ "# NBVAL_SKIP\n", "!git clone https://github.com/redis-developer/redis-ai-resources.git temp_repo\n", "!mv temp_repo/python-recipes/vector-search/resources .\n", "!rm -rf temp_repo" ] }, { "cell_type": "markdown", "id": "19bdc2a5-2192-4f5f-bd6e-7c956fd0e230", "metadata": { "id": "19bdc2a5-2192-4f5f-bd6e-7c956fd0e230" }, "source": [ "## Packages" ] }, { "cell_type": "code", "execution_count": null, "id": "c620286e", "metadata": { "id": "c620286e" }, "outputs": [], "source": [ "%pip install -q \"redisvl>=0.11.0\" sentence-transformers pandas nltk" ] }, { "cell_type": "markdown", "id": "323aec7f", "metadata": { "id": "323aec7f" }, "source": [ "## Install Redis Stack\n", "\n", "Later in this tutorial, Redis will be used to store, index, and query vector\n", "embeddings created from PDF document chunks. **We need to make sure we have a Redis\n", "instance available.\n", "\n", "#### For Colab\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, "id": "2cb85a99", "metadata": { "id": "2cb85a99" }, "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", "id": "7c5dbaaf", "metadata": { "id": "7c5dbaaf" }, "source": [ "#### For Alternative Environments\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", "id": "1d4499ae", "metadata": { "id": "1d4499ae" }, "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": 1, "id": "aefda1d1", "metadata": { "id": "aefda1d1" }, "outputs": [], "source": [ "import os\n", "import warnings\n", "\n", "warnings.filterwarnings('ignore')\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}\"" ] }, { "cell_type": "markdown", "id": "f8c6ef53", "metadata": { "id": "f8c6ef53" }, "source": [ "### Create redis client" ] }, { "cell_type": "code", "execution_count": 48, "id": "370c1fcc", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "370c1fcc", "outputId": "2b5297c6-83b7-468f-b2ac-c47acf13ba2e" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redis import Redis\n", "\n", "client = Redis.from_url(REDIS_URL)\n", "client.ping()" ] }, { "cell_type": "code", "execution_count": 4, "id": "H4w8c3Bevzq4", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "H4w8c3Bevzq4", "outputId": "a4d3b9a4-adda-436e-9aef-b4b0120720ab" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#client.flushall()" ] }, { "cell_type": "markdown", "id": "jCXiuk9ZTN_K", "metadata": { "id": "jCXiuk9ZTN_K" }, "source": [ "### Load Movies Dataset" ] }, { "cell_type": "code", "execution_count": 49, "id": "8d561462", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 223 }, "id": "8d561462", "outputId": "75ae0f32-115f-427e-e426-9a018884e860" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded 20 movie entries\n" ] }, { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"df\",\n \"rows\": 20,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"Explosive Pursuit\",\n \"Despicable Me\",\n \"The Incredibles\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"comedy\",\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 6,\n \"max\": 9,\n \"num_unique_values\": 4,\n \"samples\": [\n 8,\n 9\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"description\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"A daring cop chases a notorious criminal across the city in a high-stakes game of cat and mouse.\",\n \"When a criminal mastermind uses a trio of orphan girls as pawns for a grand scheme, he finds their love is profoundly changing him for the better.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe", "variable_name": "df" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlegenreratingdescription
0Explosive Pursuitaction7A daring cop chases a notorious criminal acros...
1Skyfallaction8James Bond returns to track down a dangerous n...
2Fast & Furious 9action6Dom and his crew face off against a high-tech ...
3Black Widowaction7Natasha Romanoff confronts her dark past and f...
4John Wickaction8A retired hitman seeks vengeance against those...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " title genre rating \\\n", "0 Explosive Pursuit action 7 \n", "1 Skyfall action 8 \n", "2 Fast & Furious 9 action 6 \n", "3 Black Widow action 7 \n", "4 John Wick action 8 \n", "\n", " description \n", "0 A daring cop chases a notorious criminal acros... \n", "1 James Bond returns to track down a dangerous n... \n", "2 Dom and his crew face off against a high-tech ... \n", "3 Natasha Romanoff confronts her dark past and f... \n", "4 A retired hitman seeks vengeance against those... " ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "import json\n", "\n", "df = pd.read_json(\"resources/movies.json\")\n", "print(\"Loaded\", len(df), \"movie entries\")\n", "\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 50, "id": "bfiTJovpQX90", "metadata": { "id": "bfiTJovpQX90" }, "outputs": [], "source": [ "from redisvl.utils.vectorize import HFTextVectorizer\n", "from redisvl.extensions.cache.embeddings import EmbeddingsCache\n", "\n", "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n", "\n", "\n", "hf = HFTextVectorizer(\n", " model=\"sentence-transformers/all-MiniLM-L6-v2\",\n", " cache=EmbeddingsCache(\n", " name=\"embedcache\",\n", " ttl=600,\n", " redis_client=client,\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 51, "id": "Vl3SehnxQvXo", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "Vl3SehnxQvXo", "outputId": "6b9f5555-dee7-4fd6-8dae-628919cfdc74" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"df\",\n \"rows\": 20,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"Explosive Pursuit\",\n \"Despicable Me\",\n \"The Incredibles\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"comedy\",\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 6,\n \"max\": 9,\n \"num_unique_values\": 4,\n \"samples\": [\n 8,\n 9\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"description\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"A daring cop chases a notorious criminal across the city in a high-stakes game of cat and mouse.\",\n \"When a criminal mastermind uses a trio of orphan girls as pawns for a grand scheme, he finds their love is profoundly changing him for the better.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 20,\n \"samples\": [\n \"b'\\\\x9bf|=\\\\na\\\\n;\\\\xbf\\\\x91\\\\xb7;\\\\x19\\\\xcb~\\\\xbd\\\\xd9d\\\\xce\\\\xbb\\\\xda\\\\x16J=X\\\\xa7?=\\\\xd4v\\\\x95\\\\x17\\\\xbe\\\\x14\\\\x11\\\\x05\\\\xb94u\\\\xbf<\\\\xc3\\\\xe0b\\\\xba\\\\xd0\\\\xa6\\\\xa8\\\\xbd\\\\x84\\\\xdc\\\\xec\\\\xbcTc%=\\\\xfe\\\\xe6r\\\\xbb+OG=5(\\\\x85=s@\\\\xa2\\\\xbc.Z\\\\xd0\\\\xbd;%K\\\\xbd\\\\xa5\\\\xed\\\\x94\\\\xbcn\\\\xddH=\\\\xbb&F<\\\\xc8*\\\\xec<\\\\x8d\\\\xd8\\\\x8d\\\\xbd\\\\xc9Z\\\\x98<\\\\r\\\\xa3\\\\xa3=:g3\\\\xbd\\\\x1f\\\\xcd\\\\xbd\\\\xbd\\\\x11%\\\\xf7;\\\\r\\\\xf5z=\\\\x02\\\\xb5\\\\x8c=\\\\x91\\\\x0e\\\\xc6\\\\xbdlI\\\\x90\\\\xbd%\\\\x16\\\\xbd;}\\\\xe7\\\\x0c\\\\xbd!3\\\\xc9\\\\xbct\\\\xf8\\\\xbb\\\\xbc\\\\xd2&u\\\\xbbA\\\\x8f\\\\xca<\\\\xfe\\\\x7fJ=\\\\x0b\\\\xaf*=\\\\x8dOU\\\\xbd\\\\xcd\\\\xf0\\\\x95\\\\xbc\\\\x1d\\\\x02\\\\x19=1\\\\xf4K<\\\\xcf\\\\xc2\\\\t=H\\\\x83\\\\xac=\\\\x9e\\\\xd7\\\\xb8\\\\xbd\\\\xf4\\\\xb5\\\\x9c\\\\xbd9\\\\x85\\\\x18=\\\\x9cd&=93\\\\xf8<\\\\xf2\\\\xf7\\\\x88<5v\\\\xf2\\\\xbb$=[\\\\xbd\\\\xa3\\\\xac\\\\xee\\\\xbb7:A\\\\xbd\\\\xd9d\\\\x19\\\\xbd\\\\xb7c\\\\xf2\\\\xbb\\\\x84\\\\xb9x;\\\\xb0;O<\\\\xc11,\\\\xbc\\\\xe4\\\\xae\\\\xae=\\\\x9f\\\\x00-\\\\xbc\\\\x14\\\\x06\\\\xae\\\\xbdh\\\\xd6\\\\x1a=\\\\xc4\\\\xbf\\\\xcd=\\\\x19\\\\x150=\\\\xe8\\\\xf1\\\\x9d\\\\xbc\\\\xaaGK=\\\\xaf\\\\xb8 =\\\\xb2\\\\xf1I\\\\xbdIe\\\\x9e\\\\xbb/\\\\x89\\\\xf7:\\\\x94\\\\xf8\\\\x1c=\\\\xa2\\\\xba\\\\xde<\\\\xa7o\\\\x16\\\\xbb\\\\t^p\\\\xbb\\\\xef\\\\xd5<<#\\\\xa6\\\\xa3\\\\xb8\\\\xc99s<\\\\xe83&<]\\\\x1c\\\\x18<\\\\x1c\\\\xd9-\\\\xbd\\\\xd3\\\\xe6\\\\x98<\\\\x0f\\\\xa1N=\\\\xa1/\\\\xa5=\\\\x1e\\\\xf3\\\\xddG\\\\xd6\\\\xbc\\\\x91\\\"S=\\\\xd7\\\\xd9^\\\\xbd\\\\xac\\\\xa3\\\\x91<\\\\xe5\\\\xd9\\\\x13<\\\\xbb\\\\xb2y\\\\xbbw\\\\x8d/\\\\xbd\\\\x99\\\\x06p\\\\xbd\\\\x83\\\\x1bF\\\\xbd\\\\xa2?\\\\x14\\\\xbe\\\\xc8\\\\x8f(\\\\xbd\\\\xe7O\\\\x89\\\\xbd\\\\x12\\\\xae\\\\xd4<\\\\xa6\\\\x12\\\\xc3=\\\\xb2\\\\x05O\\\\xbdZ\\\\x8ep\\\\xbc\\\\x1d\\\\xb5\\\\xac\\\\xbc\\\\xcc\\\\x9ee\\\\xbdf\\\\x8es;Ia\\\\xc1;\\\\xe5\\\\xfaB\\\\xbd\\\\x86\\\"\\\\xfe:\\\\x9c\\\\xe6\\\\xf4=\\\\xf6\\\\x15*<\\\\x81\\\\xf8\\\\x1b=\\\\x04\\\\xfcV\\\\xbd\\\\xd1\\\\xd1\\\\r==\\\\xee\\\\x06=\\\\x0cu\\\\xba\\\\xbd\\\\x10\\\\xa4\\\\xd6<\\\\xe3\\\\xeb\\\\xd9;\\\\xbe9/=\\\\xa9\\\\xc2\\\\x85=~\\\\x0b\\\"=\\\\xffi\\\\xef<7\\\\xe8c=\\\\xfb2\\\\x08\\\\xbe\\\\xe1\\\\x12;=YVW;P\\\\xa4b<\\\\xc8\\\\x9d\\\\xb7<\\\\x7fr;\\\\xbdhz\\\\x91\\\\xbcT\\\\x00<\\\\xbd\\\\x00\\\\x1a\\\\xa3<\\\\xca\\\\t\\\\xbb\\\\xa1\\\\xfb\\\\xe7\\\\xa5\\\\x9f\\\\x0c\\\\xbc\\\\x07Q\\\\x9a\\\\xbd\\\\xb3\\\\x08y\\\\xbd\\\\xdaAT;\\\\xddT\\\\xe2<\\\\xfe\\\\xff\\\\x1c\\\\xbd\\\\x8b\\\\xe4\\\\x9e=\\\\x8c-\\\\x0c;\\\\xc3\\\\x0f>;[8\\\\xea=>\\\\xb7\\\\xd5\\\\xbcN\\\\x8c\\\\xf9\\\\xbc\\\\xd7\\\\xc7\\\\xd2\\\\xbaa8\\\\t<\\\\t\\\\x8a\\\\x17\\\\xbdP\\\\x12A\\\\xbd\\\\x90\\\\x89\\\\x82\\\\xbbFy\\\\xc7=,\\\\xddy\\\\xbd\\\\xd2\\\\xf1\\\\x82<\\\\x1c\\\\xe0\\\\xb0<\\\\xdd\\\\x12\\\\xc8<\\\\xd5M\\\\xdf\\\\xbc\\\\x9f\\\\x16\\\\x9a=\\\\xa2W\\\\xb2<\\\\xcbab;\\\\x9di\\\\x96\\\\xbco\\\\x00W<\\\\'\\\\xb6\\\\xe4\\\\xbc\\\\x07 \\\\xb8;^\\\\x0bI\\\\xbdQ\\\\xc0\\\\xbe\\\\xbc\\\\x92n\\\\x95\\\\xbc\\\\x9f\\\\x11\\\\x83=\\\\xd2\\\\xb0\\\\xf5\\\\xbc\\\\xc7g\\\\x8a\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlegenreratingdescriptionvector
0Explosive Pursuitaction7A daring cop chases a notorious criminal acros...b'\\x9bf|=\\na\\n;\\xbf\\x91\\xb7;\\x19\\xcb~\\xbd\\xd9d...
1Skyfallaction8James Bond returns to track down a dangerous n...b'\\x9aD\\x9e\\xbd0\\x9b\\x89\\xbc\\xc3\\x16\\x95\\xbc\\x...
2Fast & Furious 9action6Dom and his crew face off against a high-tech ...b'*\\xa5\\xc7\\xbc\\xf6,\\xa2=?\\x19H\\xbcK\\xc6t\\xbd\\...
3Black Widowaction7Natasha Romanoff confronts her dark past and f...b'u\\xeb\\x85\\xbd\\x0e\\xcdo\\xbd&\\xe8\\xc2\\xbb6\\xcf...
4John Wickaction8A retired hitman seeks vengeance against those...b'\\xaf<x\\xbb\\xfb.\\xc5=B\\x86:;\\xce\\xd0\\x94<\\xf9...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n" ], "text/plain": [ " title genre rating \\\n", "0 Explosive Pursuit action 7 \n", "1 Skyfall action 8 \n", "2 Fast & Furious 9 action 6 \n", "3 Black Widow action 7 \n", "4 John Wick action 8 \n", "\n", " description \\\n", "0 A daring cop chases a notorious criminal acros... \n", "1 James Bond returns to track down a dangerous n... \n", "2 Dom and his crew face off against a high-tech ... \n", "3 Natasha Romanoff confronts her dark past and f... \n", "4 A retired hitman seeks vengeance against those... \n", "\n", " vector \n", "0 b'\\x9bf|=\\na\\n;\\xbf\\x91\\xb7;\\x19\\xcb~\\xbd\\xd9d... \n", "1 b'\\x9aD\\x9e\\xbd0\\x9b\\x89\\xbc\\xc3\\x16\\x95\\xbc\\x... \n", "2 b'*\\xa5\\xc7\\xbc\\xf6,\\xa2=?\\x19H\\xbcK\\xc6t\\xbd\\... \n", "3 b'u\\xeb\\x85\\xbd\\x0e\\xcdo\\xbd&\\xe8\\xc2\\xbb6\\xcf... \n", "4 b'\\xaf\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitlegenre
0movies:01JSHDN7Q4GG029M45HQY8Q5T20.64973795414Fast & Furious 9action
1movies:01JSHDN7Q40QYH6Q6TD7ES4TSG0.763235092163Mad Max: Fury Roadaction
2movies:01JSHDN7Q4AS7C9VT582PWK14J0.792449712753The Lego Moviecomedy
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n" ], "text/plain": [ " id vector_distance title \\\n", "0 movies:01JSHDN7Q4GG029M45HQY8Q5T2 0.64973795414 Fast & Furious 9 \n", "1 movies:01JSHDN7Q40QYH6Q6TD7ES4TSG 0.763235092163 Mad Max: Fury Road \n", "2 movies:01JSHDN7Q4AS7C9VT582PWK14J 0.792449712753 The Lego Movie \n", "\n", " genre \n", "0 action \n", "1 action \n", "2 comedy " ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query import VectorQuery\n", "\n", "user_query = \"High tech and action packed movie\"\n", "\n", "embedded_user_query = hf.embed(user_query)\n", "\n", "vec_query = VectorQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " num_results=3,\n", " return_fields=[\"title\", \"genre\"],\n", " return_score=True,\n", ")\n", "\n", "result = index.query(vec_query)\n", "pd.DataFrame(result)\n" ] }, { "cell_type": "markdown", "id": "ef5e1997", "metadata": { "id": "ef5e1997" }, "source": [ "### Vector search with filters\n", "\n", "Redis allows you to combine filter searches on fields within the index object allowing us to create more specific searches." ] }, { "cell_type": "markdown", "id": "kKCzyMUDDw10", "metadata": { "id": "kKCzyMUDDw10" }, "source": [ "Search for top 3 movies specifically in the action genre:\n" ] }, { "cell_type": "code", "execution_count": 56, "id": "d499dcad", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "d499dcad", "outputId": "ab410048-da42-4b1e-a5fb-fbd6430ba437" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"movies:01JSHDN7Q4GG029M45HQY8Q5T2\",\n \"movies:01JSHDN7Q40QYH6Q6TD7ES4TSG\",\n \"movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"0.64973795414\",\n \"0.763235092163\",\n \"0.796153008938\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Fast & Furious 9\",\n \"Mad Max: Fury Road\",\n \"Explosive Pursuit\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitlegenre
0movies:01JSHDN7Q4GG029M45HQY8Q5T20.64973795414Fast & Furious 9action
1movies:01JSHDN7Q40QYH6Q6TD7ES4TSG0.763235092163Mad Max: Fury Roadaction
2movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG0.796153008938Explosive Pursuitaction
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title \\\n", "0 movies:01JSHDN7Q4GG029M45HQY8Q5T2 0.64973795414 Fast & Furious 9 \n", "1 movies:01JSHDN7Q40QYH6Q6TD7ES4TSG 0.763235092163 Mad Max: Fury Road \n", "2 movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG 0.796153008938 Explosive Pursuit \n", "\n", " genre \n", "0 action \n", "1 action \n", "2 action " ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query.filter import Tag\n", "\n", "tag_filter = Tag(\"genre\") == \"action\"\n", "\n", "vec_query.set_filter(tag_filter)\n", "\n", "result=index.query(vec_query)\n", "pd.DataFrame(result)" ] }, { "cell_type": "markdown", "id": "YAh3GDS4Dudu", "metadata": { "id": "YAh3GDS4Dudu" }, "source": [ "Search for top 3 movies specifically in the action genre with ratings at or above a 7:\n" ] }, { "cell_type": "code", "execution_count": 57, "id": "f59fff2c", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "f59fff2c", "outputId": "d6909c59-a947-4e58-a13a-8d0c2169a6b3" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"movies:01JSHDN7Q40QYH6Q6TD7ES4TSG\",\n \"movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG\",\n \"movies:01JSHDN7Q481PGAEDBX0QG75RP\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"0.763235092163\",\n \"0.796153008938\",\n \"0.87649422884\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Mad Max: Fury Road\",\n \"Explosive Pursuit\",\n \"Inception\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"8\",\n \"7\",\n \"9\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenre
0movies:01JSHDN7Q40QYH6Q6TD7ES4TSG0.763235092163Mad Max: Fury Road8action
1movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG0.796153008938Explosive Pursuit7action
2movies:01JSHDN7Q481PGAEDBX0QG75RP0.87649422884Inception9action
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title \\\n", "0 movies:01JSHDN7Q40QYH6Q6TD7ES4TSG 0.763235092163 Mad Max: Fury Road \n", "1 movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG 0.796153008938 Explosive Pursuit \n", "2 movies:01JSHDN7Q481PGAEDBX0QG75RP 0.87649422884 Inception \n", "\n", " rating genre \n", "0 8 action \n", "1 7 action \n", "2 9 action " ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query.filter import Num\n", "\n", "# build combined filter expressions\n", "tag_filter = Tag(\"genre\") == \"action\"\n", "num_filter = Num(\"rating\") >= 7\n", "combined_filter = tag_filter & num_filter\n", "\n", "# build vector query\n", "vec_query = VectorQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " num_results=3,\n", " return_fields=[\"title\", \"rating\", \"genre\"],\n", " return_score=True,\n", " filter_expression=combined_filter\n", ")\n", "\n", "result = index.query(vec_query)\n", "pd.DataFrame(result)" ] }, { "cell_type": "markdown", "id": "yJ6TkwEVDsbN", "metadata": { "id": "yJ6TkwEVDsbN" }, "source": [ "Search with full text search for movies that directly mention \"criminal mastermind\" in the description:\n" ] }, { "cell_type": "code", "execution_count": 58, "id": "7dab26c2", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 146 }, "id": "7dab26c2", "outputId": "da366f10-d07d-4a1e-8da5-725e6a37827a" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 2,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"movies:01JSHDN7Q4ZG1SBF02A9SMV6DB\",\n \"movies:01JSHDN7Q4W1NYVVAEBTV91X9X\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"0.990856587887\",\n \"0.827254056931\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"The Dark Knight\",\n \"Despicable Me\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"9\",\n \"7\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"action\",\n \"comedy\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"description\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"Batman faces off against the Joker, a criminal mastermind who threatens to plunge Gotham into chaos.\",\n \"When a criminal mastermind uses a trio of orphan girls as pawns for a grand scheme, he finds their love is profoundly changing him for the better.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenredescription
0movies:01JSHDN7Q4W1NYVVAEBTV91X9X0.827254056931Despicable Me7comedyWhen a criminal mastermind uses a trio of orph...
1movies:01JSHDN7Q4ZG1SBF02A9SMV6DB0.990856587887The Dark Knight9actionBatman faces off against the Joker, a criminal...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title rating \\\n", "0 movies:01JSHDN7Q4W1NYVVAEBTV91X9X 0.827254056931 Despicable Me 7 \n", "1 movies:01JSHDN7Q4ZG1SBF02A9SMV6DB 0.990856587887 The Dark Knight 9 \n", "\n", " genre description \n", "0 comedy When a criminal mastermind uses a trio of orph... \n", "1 action Batman faces off against the Joker, a criminal... " ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query.filter import Text\n", "\n", "text_filter = Text(\"description\") % \"criminal mastermind\"\n", "\n", "vec_query = VectorQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " num_results=3,\n", " return_fields=[\"title\", \"rating\", \"genre\", \"description\"],\n", " return_score=True,\n", " filter_expression=text_filter\n", ")\n", "\n", "result = index.query(vec_query)\n", "pd.DataFrame(result)" ] }, { "cell_type": "markdown", "id": "UWQkD69fECJv", "metadata": { "id": "UWQkD69fECJv" }, "source": [ "Vector search with wildcard text match:\n" ] }, { "cell_type": "code", "execution_count": 59, "id": "e39e5e5c", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "id": "e39e5e5c", "outputId": "d9d476dc-8d80-4743-dc14-02e64f9c570d" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG\",\n \"movies:01JSHDN7Q4JARJS5Q4HQZ90RRX\",\n \"movies:01JSHDN7Q4W1NYVVAEBTV91X9X\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"0.796153008938\",\n \"0.807471334934\",\n \"0.827254056931\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Explosive Pursuit\",\n \"The Incredibles\",\n \"Despicable Me\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"8\",\n \"7\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"comedy\",\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"description\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"A daring cop chases a notorious criminal across the city in a high-stakes game of cat and mouse.\",\n \"A family of undercover superheroes, while trying to live the quiet suburban life, are forced into action to save the world. Bob Parr (Mr. Incredible) and his wife Helen (Elastigirl) were among the world's greatest crime fighters, but now they must assume civilian identities and retreat to the suburbs to live a 'normal' life with their three children. However, the family's desire to help the world pulls them back into action when they face a new and dangerous enemy.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenredescription
0movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG0.796153008938Explosive Pursuit7actionA daring cop chases a notorious criminal acros...
1movies:01JSHDN7Q4JARJS5Q4HQZ90RRX0.807471334934The Incredibles8comedyA family of undercover superheroes, while tryi...
2movies:01JSHDN7Q4W1NYVVAEBTV91X9X0.827254056931Despicable Me7comedyWhen a criminal mastermind uses a trio of orph...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title \\\n", "0 movies:01JSHDN7Q4AHFG0J8D7Q8QS1BG 0.796153008938 Explosive Pursuit \n", "1 movies:01JSHDN7Q4JARJS5Q4HQZ90RRX 0.807471334934 The Incredibles \n", "2 movies:01JSHDN7Q4W1NYVVAEBTV91X9X 0.827254056931 Despicable Me \n", "\n", " rating genre description \n", "0 7 action A daring cop chases a notorious criminal acros... \n", "1 8 comedy A family of undercover superheroes, while tryi... \n", "2 7 comedy When a criminal mastermind uses a trio of orph... " ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text_filter = Text(\"description\") % \"crim*\"\n", "\n", "vec_query = VectorQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " num_results=3,\n", " return_fields=[\"title\", \"rating\", \"genre\", \"description\"],\n", " return_score=True,\n", " filter_expression=text_filter\n", ")\n", "\n", "result = index.query(vec_query)\n", "pd.DataFrame(result)" ] }, { "cell_type": "markdown", "id": "CGyNAr70EGLg", "metadata": { "id": "CGyNAr70EGLg" }, "source": [ "Vector search with fuzzy match filter\n", "\n", "> Note: fuzzy match is based on Levenshtein distance. Therefore, \"hero\" might return result for \"her\" as an example.\n", "\n", "See docs for more info https://redis.io/docs/latest/develop/interact/search-and-query/advanced-concepts/query_syntax/\n" ] }, { "cell_type": "code", "execution_count": 60, "id": "3450e07d", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "id": "3450e07d", "outputId": "93b5ea52-3735-4b81-ad51-17c487d1132c" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"movies:01JSHDN7Q4QFMK2MGNKJHTFT9E\",\n \"movies:01JSHDN7Q4031K8AS44WJ3J9ZR\",\n \"movies:01JSHDN7Q4W3ZWAJGWT7YQPKVR\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"0.889985799789\",\n \"0.893866717815\",\n \"0.943198204041\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Black Widow\",\n \"The Avengers\",\n \"The Princess Diaries\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"7\",\n \"8\",\n \"6\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"comedy\",\n \"action\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"description\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Natasha Romanoff confronts her dark past and family ties as she battles a new enemy.\",\n \"Earth's mightiest heroes come together to stop an alien invasion that threatens the entire planet.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenredescription
0movies:01JSHDN7Q4QFMK2MGNKJHTFT9E0.889985799789Black Widow7actionNatasha Romanoff confronts her dark past and f...
1movies:01JSHDN7Q4031K8AS44WJ3J9ZR0.893866717815The Avengers8actionEarth's mightiest heroes come together to stop...
2movies:01JSHDN7Q4W3ZWAJGWT7YQPKVR0.943198204041The Princess Diaries6comedyMia Thermopolis has just found out that she is...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title \\\n", "0 movies:01JSHDN7Q4QFMK2MGNKJHTFT9E 0.889985799789 Black Widow \n", "1 movies:01JSHDN7Q4031K8AS44WJ3J9ZR 0.893866717815 The Avengers \n", "2 movies:01JSHDN7Q4W3ZWAJGWT7YQPKVR 0.943198204041 The Princess Diaries \n", "\n", " rating genre description \n", "0 7 action Natasha Romanoff confronts her dark past and f... \n", "1 8 action Earth's mightiest heroes come together to stop... \n", "2 6 comedy Mia Thermopolis has just found out that she is... " ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "text_filter = Text(\"description\") % \"%hero%\"\n", "\n", "vec_query = VectorQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " num_results=3,\n", " return_fields=[\"title\", \"rating\", \"genre\", \"description\"],\n", " return_score=True,\n", " filter_expression=text_filter\n", ")\n", "\n", "result = index.query(vec_query)\n", "pd.DataFrame(result)" ] }, { "cell_type": "markdown", "id": "6bd27cb3", "metadata": { "id": "6bd27cb3" }, "source": [ "### Range queries\n", "\n", "Range queries allow you to set a pre defined distance \"threshold\" for which we want to return documents. This is helpful when you only want documents with a certain \"radius\" from the search query." ] }, { "cell_type": "code", "execution_count": 61, "id": "cafe1795", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 237 }, "id": "cafe1795", "outputId": "c86063ac-e0e5-4975-c08a-2b8cc71c8f79" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 6,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 6,\n \"samples\": [\n \"movies:01JSHDN7Q4JARJS5Q4HQZ90RRX\",\n \"movies:01JSHDN7Q4QFMK2MGNKJHTFT9E\",\n \"movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ8\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 6,\n \"samples\": [\n \"0.644702494144\",\n \"0.747987031937\",\n \"0.778580129147\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 6,\n \"samples\": [\n \"The Incredibles\",\n \"Black Widow\",\n \"Aladdin\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"7\",\n \"8\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n \"action\",\n \"comedy\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenre
0movies:01JSHDN7Q4JARJS5Q4HQZ90RRX0.644702494144The Incredibles8comedy
1movies:01JSHDN7Q4QFMK2MGNKJHTFT9E0.747987031937Black Widow7action
2movies:01JSHDN7Q4W1NYVVAEBTV91X9X0.750915527344Despicable Me7comedy
3movies:01JSHDN7Q4Y009NXBPM25YDZDV0.751298904419Shrek8comedy
4movies:01JSHDN7Q4W21S96KMMBZ2X6KY0.761669397354Monsters, Inc.8comedy
5movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ80.778580129147Aladdin8comedy
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title rating \\\n", "0 movies:01JSHDN7Q4JARJS5Q4HQZ90RRX 0.644702494144 The Incredibles 8 \n", "1 movies:01JSHDN7Q4QFMK2MGNKJHTFT9E 0.747987031937 Black Widow 7 \n", "2 movies:01JSHDN7Q4W1NYVVAEBTV91X9X 0.750915527344 Despicable Me 7 \n", "3 movies:01JSHDN7Q4Y009NXBPM25YDZDV 0.751298904419 Shrek 8 \n", "4 movies:01JSHDN7Q4W21S96KMMBZ2X6KY 0.761669397354 Monsters, Inc. 8 \n", "5 movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ8 0.778580129147 Aladdin 8 \n", "\n", " genre \n", "0 comedy \n", "1 action \n", "2 comedy \n", "3 comedy \n", "4 comedy \n", "5 comedy " ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query import RangeQuery\n", "\n", "user_query = \"Family friendly fantasy movies\"\n", "\n", "embedded_user_query = hf.embed(user_query)\n", "\n", "range_query = RangeQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " return_fields=[\"title\", \"rating\", \"genre\"],\n", " return_score=True,\n", " distance_threshold=0.8 # find all items with a semantic distance of less than 0.8\n", ")\n", "\n", "result = index.query(range_query)\n", "pd.DataFrame(result)\n" ] }, { "cell_type": "markdown", "id": "a1586ea7", "metadata": { "id": "a1586ea7" }, "source": [ "Like the queries above, we can also chain additional filters and conditional operators with range queries. The following adds an `and` condition that returns vector search within the defined range and with a rating at or above 8." ] }, { "cell_type": "code", "execution_count": 62, "id": "d3110324", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 174 }, "id": "d3110324", "outputId": "dff98df9-60ea-4325-f1c9-1e57c5139014" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"id\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"movies:01JSHDN7Q4Y009NXBPM25YDZDV\",\n \"movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ8\",\n \"movies:01JSHDN7Q4JARJS5Q4HQZ90RRX\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_distance\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"0.751298904419\",\n \"0.778580129147\",\n \"0.644702494144\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"Shrek\",\n \"Aladdin\",\n \"The Incredibles\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"rating\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"8\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"genre\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 1,\n \"samples\": [\n \"comedy\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idvector_distancetitleratinggenre
0movies:01JSHDN7Q4JARJS5Q4HQZ90RRX0.644702494144The Incredibles8comedy
1movies:01JSHDN7Q4Y009NXBPM25YDZDV0.751298904419Shrek8comedy
2movies:01JSHDN7Q4W21S96KMMBZ2X6KY0.761669397354Monsters, Inc.8comedy
3movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ80.778580129147Aladdin8comedy
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " id vector_distance title rating \\\n", "0 movies:01JSHDN7Q4JARJS5Q4HQZ90RRX 0.644702494144 The Incredibles 8 \n", "1 movies:01JSHDN7Q4Y009NXBPM25YDZDV 0.751298904419 Shrek 8 \n", "2 movies:01JSHDN7Q4W21S96KMMBZ2X6KY 0.761669397354 Monsters, Inc. 8 \n", "3 movies:01JSHDN7Q4H6JSC5Y2FKT7SWJ8 0.778580129147 Aladdin 8 \n", "\n", " genre \n", "0 comedy \n", "1 comedy \n", "2 comedy \n", "3 comedy " ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range_query = RangeQuery(\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " return_fields=[\"title\", \"rating\", \"genre\"],\n", " distance_threshold=0.8\n", ")\n", "\n", "numeric_filter = Num(\"rating\") >= 8\n", "\n", "range_query.set_filter(numeric_filter)\n", "\n", "# in this case we want to do a simple filter search or the vector so we execute as a joint filter directly\n", "result = index.query(range_query)\n", "pd.DataFrame(result)\n" ] }, { "cell_type": "markdown", "id": "qABIlUpQE4lT", "metadata": { "id": "qABIlUpQE4lT" }, "source": [ "### Full text search" ] }, { "cell_type": "code", "execution_count": 74, "id": "AOU0Sqx3FCFN", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 174 }, "id": "AOU0Sqx3FCFN", "outputId": "eba96774-147f-4f8f-901f-abc9dc53cf48" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"The Incredibles\",\n \"Toy Story\",\n \"Fast & Furious 9\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"score\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.6009737514689841,\n \"min\": 1.6300968940970675,\n \"max\": 5.157031817943228,\n \"num_unique_values\": 4,\n \"samples\": [\n 4.022877021342021,\n 1.6300968940970675,\n 5.157031817943228\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlescore
0Fast & Furious 95.157032
1The Incredibles4.022877
2Explosive Pursuit2.335427
3Toy Story1.630097
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " title score\n", "0 Fast & Furious 9 5.157032\n", "1 The Incredibles 4.022877\n", "2 Explosive Pursuit 2.335427\n", "3 Toy Story 1.630097" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query import TextQuery\n", "\n", "user_query = \"High tech, action packed, superheros fight scenes\"\n", "\n", "text_query = TextQuery(\n", " text=user_query,\n", " text_field_name=\"description\",\n", " text_scorer=\"BM25STD\",\n", " num_results=20,\n", " return_fields=[\"title\", \"description\"],\n", ")\n", "\n", "result = index.query(text_query)[:4]\n", "pd.DataFrame(result)[[\"title\", \"score\"]]" ] }, { "cell_type": "markdown", "id": "pIZ-RiuyFAJP", "metadata": { "id": "pIZ-RiuyFAJP" }, "source": [ "### Hybrid search" ] }, { "cell_type": "code", "execution_count": 77, "id": "fjJwWyQe02T1", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 174 }, "id": "fjJwWyQe02T1", "outputId": "399a0f70-089c-4d82-968c-1cc0adf0e7fb" }, "outputs": [ { "data": { "application/vnd.google.colaboratory.intrinsic+json": { "summary": "{\n \"name\": \"pd\",\n \"rows\": 4,\n \"fields\": [\n {\n \"column\": \"title\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"Fast & Furious 9\",\n \"Black Widow\",\n \"The Incredibles\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"vector_similarity\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"0.537397742271\",\n \"0.626006484032\",\n \"0.677648752928\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"text_score\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"0.498220622181\",\n \"0\",\n \"0.398671082609\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"hybrid_score\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 4,\n \"samples\": [\n \"0.525644606244\",\n \"0.438204538822\",\n \"0.593955451832\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}", "type": "dataframe" }, "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlevector_similaritytext_scorehybrid_score
0The Incredibles0.6776487529280.3986710826090.593955451832
1Fast & Furious 90.5373977422710.4982206221810.525644606244
2Toy Story0.5530096590520.2135231237920.451163698474
3Black Widow0.62600648403200.438204538822
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "text/plain": [ " title vector_similarity text_score hybrid_score\n", "0 The Incredibles 0.677648752928 0.398671082609 0.593955451832\n", "1 Fast & Furious 9 0.537397742271 0.498220622181 0.525644606244\n", "2 Toy Story 0.553009659052 0.213523123792 0.451163698474\n", "3 Black Widow 0.626006484032 0 0.438204538822" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from redisvl.query import AggregateHybridQuery\n", "\n", "hybrid_query = AggregateHybridQuery(\n", " text=user_query,\n", " text_field_name=\"description\",\n", " text_scorer=\"BM25\",\n", " vector=embedded_user_query,\n", " vector_field_name=\"vector\",\n", " alpha=0.7,\n", " num_results=20,\n", " return_fields=[\"title\", \"description\"],\n", ")\n", "\n", "result = index.query(hybrid_query)[:4]\n", "pd.DataFrame(result)[[\"title\", \"vector_similarity\", \"text_score\", \"hybrid_score\"]]" ] }, { "cell_type": "markdown", "id": "5fa7cdfb", "metadata": { "id": "5fa7cdfb" }, "source": [ "### Next steps\n", "\n", "For more query examples with redisvl: [see here](https://github.com/redis/redis-vl-python/blob/main/docs/user_guide/02_hybrid_queries.ipynb)" ] }, { "cell_type": "code", "execution_count": 78, "id": "915c2cef", "metadata": { "id": "915c2cef" }, "outputs": [], "source": [ "# clean up!\n", "index.delete()" ] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }