├── README.md └── redis-langchain-ecommerce-chatbot.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # redis langchain e-commerce chatbot 2 | In this tutorial, we will build an e-commerce chatbot that can query Amazon product embeddings using Redis and generate nice responses with Langchain. Our chatbot will take user input, find relevant products, and present the information in a friendly and detailed manner. 3 | 4 | --- 5 | 6 | [![Artificial Intelligence Hackathons, tutorials and Boilerplates](https://storage.googleapis.com/lablab-static-eu/images/github/lablab-banner.jpg)](https://lablab.ai) 7 | 8 | 9 | ## Join the LabLab Discord 10 | 11 | 12 | ![Discord Banner 1](https://discordapp.com/api/guilds/877056448956346408/widget.png?style=banner1) 13 | On lablab discord, we discuss this repo and many other topics related to artificial intelligence! Checkout upcoming [Artificial Intelligence Hackathons](https://lablab.ai) Event 14 | 15 | 16 | [![Acclerating innovation through acceleration](https://storage.googleapis.com/lablab-static-eu/images/github/nn-group-loggos.jpg)](https://newnative.ai) 17 | -------------------------------------------------------------------------------- /redis-langchain-ecommerce-chatbot.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 23, 6 | "metadata": { 7 | "colab": { 8 | "base_uri": "https://localhost:8080/" 9 | }, 10 | "id": "5-h_nDGp3Kdf", 11 | "outputId": "94191443-3844-4c1d-a26f-7619d976a55b" 12 | }, 13 | "outputs": [ 14 | { 15 | "name": "stdout", 16 | "output_type": "stream", 17 | "text": [ 18 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", 19 | "To disable this warning, you can either:\n", 20 | "\t- Avoid using `tokenizers` before the fork if possible\n", 21 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n", 22 | "Requirement already satisfied: redis in ./venv/lib/python3.10/site-packages (4.5.3)\n", 23 | "Requirement already satisfied: pandas in ./venv/lib/python3.10/site-packages (1.5.3)\n", 24 | "Requirement already satisfied: async-timeout>=4.0.2 in ./venv/lib/python3.10/site-packages (from redis) (4.0.2)\n", 25 | "Requirement already satisfied: python-dateutil>=2.8.1 in ./venv/lib/python3.10/site-packages (from pandas) (2.8.2)\n", 26 | "Requirement already satisfied: numpy>=1.21.0 in ./venv/lib/python3.10/site-packages (from pandas) (1.24.2)\n", 27 | "Requirement already satisfied: pytz>=2020.1 in ./venv/lib/python3.10/site-packages (from pandas) (2022.7.1)\n", 28 | "Requirement already satisfied: six>=1.5 in ./venv/lib/python3.10/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n", 29 | "Note: you may need to restart the kernel to use updated packages.\n", 30 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", 31 | "To disable this warning, you can either:\n", 32 | "\t- Avoid using `tokenizers` before the fork if possible\n", 33 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n", 34 | "Requirement already satisfied: sentence-transformers in ./venv/lib/python3.10/site-packages (2.2.2)\n", 35 | "Requirement already satisfied: transformers<5.0.0,>=4.6.0 in ./venv/lib/python3.10/site-packages (from sentence-transformers) (4.27.3)\n", 36 | "Requirement already satisfied: tqdm in ./venv/lib/python3.10/site-packages (from sentence-transformers) (4.65.0)\n", 37 | "Requirement already satisfied: torch>=1.6.0 in ./venv/lib/python3.10/site-packages (from sentence-transformers) (2.0.0)\n", 38 | "Requirement already satisfied: torchvision in ./venv/lib/python3.10/site-packages (from sentence-transformers) (0.15.1)\n", 39 | "Requirement already satisfied: numpy in ./venv/lib/python3.10/site-packages (from sentence-transformers) (1.24.2)\n", 40 | "Requirement already satisfied: scikit-learn in ./venv/lib/python3.10/site-packages (from sentence-transformers) (1.2.2)\n", 41 | "Requirement already satisfied: scipy in ./venv/lib/python3.10/site-packages (from sentence-transformers) (1.10.1)\n", 42 | "Requirement already satisfied: nltk in ./venv/lib/python3.10/site-packages (from sentence-transformers) (3.8.1)\n", 43 | "Requirement already satisfied: sentencepiece in ./venv/lib/python3.10/site-packages (from sentence-transformers) (0.1.97)\n", 44 | "Requirement already satisfied: huggingface-hub>=0.4.0 in ./venv/lib/python3.10/site-packages (from sentence-transformers) (0.13.3)\n", 45 | "Requirement already satisfied: filelock in ./venv/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers) (3.10.3)\n", 46 | "Requirement already satisfied: typing-extensions>=3.7.4.3 in ./venv/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers) (4.5.0)\n", 47 | "Requirement already satisfied: packaging>=20.9 in ./venv/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers) (23.0)\n", 48 | "Requirement already satisfied: requests in ./venv/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers) (2.28.2)\n", 49 | "Requirement already satisfied: pyyaml>=5.1 in ./venv/lib/python3.10/site-packages (from huggingface-hub>=0.4.0->sentence-transformers) (6.0)\n", 50 | "Requirement already satisfied: networkx in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (3.0)\n", 51 | "Requirement already satisfied: sympy in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (1.11.1)\n", 52 | "Requirement already satisfied: nvidia-cuda-runtime-cu11==11.7.99 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.7.99)\n", 53 | "Requirement already satisfied: nvidia-curand-cu11==10.2.10.91 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (10.2.10.91)\n", 54 | "Requirement already satisfied: nvidia-nvtx-cu11==11.7.91 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.7.91)\n", 55 | "Requirement already satisfied: triton==2.0.0 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (2.0.0)\n", 56 | "Requirement already satisfied: nvidia-nccl-cu11==2.14.3 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (2.14.3)\n", 57 | "Requirement already satisfied: nvidia-cudnn-cu11==8.5.0.96 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (8.5.0.96)\n", 58 | "Requirement already satisfied: nvidia-cuda-cupti-cu11==11.7.101 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.7.101)\n", 59 | "Requirement already satisfied: nvidia-cusparse-cu11==11.7.4.91 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.7.4.91)\n", 60 | "Requirement already satisfied: nvidia-cusolver-cu11==11.4.0.1 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.4.0.1)\n", 61 | "Requirement already satisfied: nvidia-cufft-cu11==10.9.0.58 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (10.9.0.58)\n", 62 | "Requirement already satisfied: jinja2 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (3.1.2)\n", 63 | "Requirement already satisfied: nvidia-cublas-cu11==11.10.3.66 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.10.3.66)\n", 64 | "Requirement already satisfied: nvidia-cuda-nvrtc-cu11==11.7.99 in ./venv/lib/python3.10/site-packages (from torch>=1.6.0->sentence-transformers) (11.7.99)\n", 65 | "Requirement already satisfied: wheel in ./venv/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers) (0.40.0)\n", 66 | "Requirement already satisfied: setuptools in ./venv/lib/python3.10/site-packages (from nvidia-cublas-cu11==11.10.3.66->torch>=1.6.0->sentence-transformers) (58.1.0)\n", 67 | "Requirement already satisfied: cmake in ./venv/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.6.0->sentence-transformers) (3.26.1)\n", 68 | "Requirement already satisfied: lit in ./venv/lib/python3.10/site-packages (from triton==2.0.0->torch>=1.6.0->sentence-transformers) (16.0.0)\n", 69 | "Requirement already satisfied: regex!=2019.12.17 in ./venv/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (2023.3.23)\n", 70 | "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in ./venv/lib/python3.10/site-packages (from transformers<5.0.0,>=4.6.0->sentence-transformers) (0.13.2)\n", 71 | "Requirement already satisfied: joblib in ./venv/lib/python3.10/site-packages (from nltk->sentence-transformers) (1.2.0)\n", 72 | "Requirement already satisfied: click in ./venv/lib/python3.10/site-packages (from nltk->sentence-transformers) (8.1.3)\n", 73 | "Requirement already satisfied: threadpoolctl>=2.0.0 in ./venv/lib/python3.10/site-packages (from scikit-learn->sentence-transformers) (3.1.0)\n", 74 | "Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in ./venv/lib/python3.10/site-packages (from torchvision->sentence-transformers) (9.4.0)\n", 75 | "Requirement already satisfied: MarkupSafe>=2.0 in ./venv/lib/python3.10/site-packages (from jinja2->torch>=1.6.0->sentence-transformers) (2.1.2)\n", 76 | "Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence-transformers) (3.1.0)\n", 77 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence-transformers) (1.26.15)\n", 78 | "Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence-transformers) (2022.12.7)\n", 79 | "Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.10/site-packages (from requests->huggingface-hub>=0.4.0->sentence-transformers) (3.4)\n", 80 | "Requirement already satisfied: mpmath>=0.19 in ./venv/lib/python3.10/site-packages (from sympy->torch>=1.6.0->sentence-transformers) (1.3.0)\n", 81 | "Note: you may need to restart the kernel to use updated packages.\n", 82 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", 83 | "To disable this warning, you can either:\n", 84 | "\t- Avoid using `tokenizers` before the fork if possible\n", 85 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n", 86 | "Requirement already satisfied: openai in ./venv/lib/python3.10/site-packages (0.27.2)\n", 87 | "Requirement already satisfied: requests>=2.20 in ./venv/lib/python3.10/site-packages (from openai) (2.28.2)\n", 88 | "Requirement already satisfied: aiohttp in ./venv/lib/python3.10/site-packages (from openai) (3.8.4)\n", 89 | "Requirement already satisfied: tqdm in ./venv/lib/python3.10/site-packages (from openai) (4.65.0)\n", 90 | "Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.10/site-packages (from requests>=2.20->openai) (2022.12.7)\n", 91 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.10/site-packages (from requests>=2.20->openai) (1.26.15)\n", 92 | "Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.10/site-packages (from requests>=2.20->openai) (3.1.0)\n", 93 | "Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.10/site-packages (from requests>=2.20->openai) (3.4)\n", 94 | "Requirement already satisfied: multidict<7.0,>=4.5 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (6.0.4)\n", 95 | "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (4.0.2)\n", 96 | "Requirement already satisfied: frozenlist>=1.1.1 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (1.3.3)\n", 97 | "Requirement already satisfied: aiosignal>=1.1.2 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (1.3.1)\n", 98 | "Requirement already satisfied: attrs>=17.3.0 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (22.2.0)\n", 99 | "Requirement already satisfied: yarl<2.0,>=1.0 in ./venv/lib/python3.10/site-packages (from aiohttp->openai) (1.8.2)\n", 100 | "Note: you may need to restart the kernel to use updated packages.\n", 101 | "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", 102 | "To disable this warning, you can either:\n", 103 | "\t- Avoid using `tokenizers` before the fork if possible\n", 104 | "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n", 105 | "Requirement already satisfied: langchain in ./venv/lib/python3.10/site-packages (0.0.121)\n", 106 | "Requirement already satisfied: pydantic<2,>=1 in ./venv/lib/python3.10/site-packages (from langchain) (1.10.7)\n", 107 | "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in ./venv/lib/python3.10/site-packages (from langchain) (8.2.2)\n", 108 | "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in ./venv/lib/python3.10/site-packages (from langchain) (3.8.4)\n", 109 | "Requirement already satisfied: PyYAML>=5.4.1 in ./venv/lib/python3.10/site-packages (from langchain) (6.0)\n", 110 | "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in ./venv/lib/python3.10/site-packages (from langchain) (0.5.7)\n", 111 | "Requirement already satisfied: requests<3,>=2 in ./venv/lib/python3.10/site-packages (from langchain) (2.28.2)\n", 112 | "Requirement already satisfied: SQLAlchemy<2,>=1 in ./venv/lib/python3.10/site-packages (from langchain) (1.4.47)\n", 113 | "Requirement already satisfied: numpy<2,>=1 in ./venv/lib/python3.10/site-packages (from langchain) (1.24.2)\n", 114 | "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (4.0.2)\n", 115 | "Requirement already satisfied: multidict<7.0,>=4.5 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n", 116 | "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (3.1.0)\n", 117 | "Requirement already satisfied: attrs>=17.3.0 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.2.0)\n", 118 | "Requirement already satisfied: yarl<2.0,>=1.0 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.8.2)\n", 119 | "Requirement already satisfied: aiosignal>=1.1.2 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", 120 | "Requirement already satisfied: frozenlist>=1.1.1 in ./venv/lib/python3.10/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n", 121 | "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in ./venv/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n", 122 | "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in ./venv/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n", 123 | "Requirement already satisfied: typing-inspect>=0.4.0 in ./venv/lib/python3.10/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.8.0)\n", 124 | "Requirement already satisfied: typing-extensions>=4.2.0 in ./venv/lib/python3.10/site-packages (from pydantic<2,>=1->langchain) (4.5.0)\n", 125 | "Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.10/site-packages (from requests<3,>=2->langchain) (1.26.15)\n", 126 | "Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.10/site-packages (from requests<3,>=2->langchain) (3.4)\n", 127 | "Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.10/site-packages (from requests<3,>=2->langchain) (2022.12.7)\n", 128 | "Requirement already satisfied: greenlet!=0.4.17 in ./venv/lib/python3.10/site-packages (from SQLAlchemy<2,>=1->langchain) (2.0.2)\n", 129 | "Requirement already satisfied: packaging>=17.0 in ./venv/lib/python3.10/site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.0)\n", 130 | "Requirement already satisfied: mypy-extensions>=0.3.0 in ./venv/lib/python3.10/site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (1.0.0)\n", 131 | "Note: you may need to restart the kernel to use updated packages.\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "%pip install redis pandas \n", 137 | "%pip install -U sentence-transformers\n", 138 | "%pip install openai\n", 139 | "%pip install langchain" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 24, 145 | "metadata": { 146 | "colab": { 147 | "base_uri": "https://localhost:8080/" 148 | }, 149 | "id": "Yk3jkeRp3Dd-", 150 | "outputId": "2b352d3a-1598-4727-b25e-714fc705d0f1" 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "import numpy as np\n", 155 | "import pandas as pd\n", 156 | "import time\n", 157 | "from redis.commands.search.field import VectorField\n", 158 | "from redis.commands.search.field import TextField\n", 159 | "from redis.commands.search.field import TagField\n", 160 | "from redis.commands.search.query import Query\n", 161 | "import redis\n", 162 | "import time\n", 163 | "\n", 164 | "redis_conn = redis.Redis(\n", 165 | " host='redis-18975.c300.eu-central-1-1.ec2.cloud.redislabs.com',\n", 166 | " port=18975,\n", 167 | " password='xxxxxxxxxxx')" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 25, 173 | "metadata": { 174 | "colab": { 175 | "base_uri": "https://localhost:8080/", 176 | "height": 669 177 | }, 178 | "id": "00_n4VWH7FoB", 179 | "outputId": "f26daa8c-4af9-4def-d5ab-3197777fe2f9" 180 | }, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/html": [ 185 | "
\n", 186 | "\n", 199 | "\n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | "
item_idmarketplacecountrymain_image_iddomain_namebullet_pointitem_keywordsmaterialbrandcoloritem_namemodel_namemodel_numberproduct_typeprimary_key
0B07T6RZ2CMAmazonIN71dZhpsferLamazon.in3D Printed Hard Back Case Mobile Cover for Len...mobile cover back cover mobile case phone case...NaNAmazon Brand - SolimoOthersAmazon Brand - Solimo Designer Couples Sitting...Lenovo K4 Notegz8115-SL40423CELLULAR_PHONE_CASEB07T6RZ2CM-amazon.in
1B07T2JY31YAmazonIN71vX7qIEAILamazon.in3D Printed Hard Back Case Mobile Cover for Son...mobile cover back cover mobile case phone case...WoodAmazon Brand - SolimoothersAmazon Brand - Solimo Designer Leaf on Wood 3D...Sony Xperia Z1 L39Hgz8056-SL40528CELLULAR_PHONE_CASEB07T2JY31Y-amazon.in
2B0849YGSCZAmazonAEA1EZF-2mB5Lamazon.aesmall de fur rooms navidad woven girls shag pa...NaNStone & BeamNaNStone & Beam Contemporary Doily Wool Farmhouse...NaNI59I8044IVYGRYC00-ParentHOME_FURNITURE_AND_DECORB0849YGSCZ-amazon.ae
3B081K6TCMLAmazonIN81o9EyZ-fALamazon.inSolimo Plastic Multipurpose Modular Drawer; sm...drawer modular drawer 3 rack modular drawer ki...PlasticAmazon Brand - SolimoMulticolorAmazon Brand - Solimo Plastic Multipurpose Mod...NaNsol_cujo_13HOMEB081K6TCML-amazon.in
4B0854774X5AmazonIN81xaJCVnl3Lamazon.inSnug fit for Nokia 8.1, with perfect cut-outs ...Back Cover Designer Case Designer Take It Easy...SiliconAmazon Brand - SolimoMulticolorAmazon Brand - Solimo Designer Take It Easy UV...Nokia 8.1UV10714-SL40617CELLULAR_PHONE_CASEB0854774X5-amazon.in
\n", 313 | "
" 314 | ], 315 | "text/plain": [ 316 | " item_id marketplace country main_image_id domain_name \\\n", 317 | "0 B07T6RZ2CM Amazon IN 71dZhpsferL amazon.in \n", 318 | "1 B07T2JY31Y Amazon IN 71vX7qIEAIL amazon.in \n", 319 | "2 B0849YGSCZ Amazon AE A1EZF-2mB5L amazon.ae \n", 320 | "3 B081K6TCML Amazon IN 81o9EyZ-fAL amazon.in \n", 321 | "4 B0854774X5 Amazon IN 81xaJCVnl3L amazon.in \n", 322 | "\n", 323 | " bullet_point \\\n", 324 | "0 3D Printed Hard Back Case Mobile Cover for Len... \n", 325 | "1 3D Printed Hard Back Case Mobile Cover for Son... \n", 326 | "2 \n", 327 | "3 Solimo Plastic Multipurpose Modular Drawer; sm... \n", 328 | "4 Snug fit for Nokia 8.1, with perfect cut-outs ... \n", 329 | "\n", 330 | " item_keywords material \\\n", 331 | "0 mobile cover back cover mobile case phone case... NaN \n", 332 | "1 mobile cover back cover mobile case phone case... Wood \n", 333 | "2 small de fur rooms navidad woven girls shag pa... NaN \n", 334 | "3 drawer modular drawer 3 rack modular drawer ki... Plastic \n", 335 | "4 Back Cover Designer Case Designer Take It Easy... Silicon \n", 336 | "\n", 337 | " brand color \\\n", 338 | "0 Amazon Brand - Solimo Others \n", 339 | "1 Amazon Brand - Solimo others \n", 340 | "2 Stone & Beam NaN \n", 341 | "3 Amazon Brand - Solimo Multicolor \n", 342 | "4 Amazon Brand - Solimo Multicolor \n", 343 | "\n", 344 | " item_name model_name \\\n", 345 | "0 Amazon Brand - Solimo Designer Couples Sitting... Lenovo K4 Note \n", 346 | "1 Amazon Brand - Solimo Designer Leaf on Wood 3D... Sony Xperia Z1 L39H \n", 347 | "2 Stone & Beam Contemporary Doily Wool Farmhouse... NaN \n", 348 | "3 Amazon Brand - Solimo Plastic Multipurpose Mod... NaN \n", 349 | "4 Amazon Brand - Solimo Designer Take It Easy UV... Nokia 8.1 \n", 350 | "\n", 351 | " model_number product_type primary_key \n", 352 | "0 gz8115-SL40423 CELLULAR_PHONE_CASE B07T6RZ2CM-amazon.in \n", 353 | "1 gz8056-SL40528 CELLULAR_PHONE_CASE B07T2JY31Y-amazon.in \n", 354 | "2 I59I8044IVYGRYC00-Parent HOME_FURNITURE_AND_DECOR B0849YGSCZ-amazon.ae \n", 355 | "3 sol_cujo_13 HOME B081K6TCML-amazon.in \n", 356 | "4 UV10714-SL40617 CELLULAR_PHONE_CASE B0854774X5-amazon.in " 357 | ] 358 | }, 359 | "execution_count": 25, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "MAX_TEXT_LENGTH=512\n", 366 | "NUMBER_PRODUCTS=1000\n", 367 | "\n", 368 | "def auto_truncate(val):\n", 369 | " return val[:MAX_TEXT_LENGTH]\n", 370 | "\n", 371 | "#Load Product data and truncate long text fields\n", 372 | "all_prods_df = pd.read_csv(\"product_data.csv\", converters={'bullet_point': auto_truncate,'item_keywords':auto_truncate,'item_name':auto_truncate})\n", 373 | "all_prods_df['primary_key'] = all_prods_df['item_id'] + '-' + all_prods_df['domain_name']\n", 374 | "all_prods_df['item_keywords'].replace('', np.nan, inplace=True)\n", 375 | "all_prods_df.dropna(subset=['item_keywords'], inplace=True)\n", 376 | "all_prods_df.reset_index(drop=True,inplace=True)\n", 377 | "\n", 378 | "#get the first 1000 products with non-empty item keywords\n", 379 | "product_metadata = all_prods_df.head(NUMBER_PRODUCTS).to_dict(orient='index')\n", 380 | "\n", 381 | "all_prods_df.head()\n" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 26, 387 | "metadata": { 388 | "id": "gjouwTqF7ftf" 389 | }, 390 | "outputs": [], 391 | "source": [ 392 | "from sentence_transformers import SentenceTransformer\n", 393 | "model = SentenceTransformer('sentence-transformers/all-distilroberta-v1')\n", 394 | "\n", 395 | "\n", 396 | "item_keywords = [product_metadata[i]['item_keywords'] for i in product_metadata.keys()]\n", 397 | "item_keywords_vectors = [ model.encode(sentence) for sentence in item_keywords]" 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 27, 403 | "metadata": { 404 | "id": "Iw7rlppY8f3a" 405 | }, 406 | "outputs": [ 407 | { 408 | "data": { 409 | "text/plain": [ 410 | "{'item_id': 'B07T6RZ2CM',\n", 411 | " 'marketplace': 'Amazon',\n", 412 | " 'country': 'IN',\n", 413 | " 'main_image_id': '71dZhpsferL',\n", 414 | " 'domain_name': 'amazon.in',\n", 415 | " 'bullet_point': '3D Printed Hard Back Case Mobile Cover for Lenovo K4 Note Easy to put & take off with perfect cutouts for volume buttons, audio & charging ports. Stylish design and appearance, express your unique personality. Extreme precision design allows easy access to all buttons and ports while featuring raised bezel to life screen and camera off flat surface. Slim Hard Back Cover No Warranty None',\n", 416 | " 'item_keywords': 'mobile cover back cover mobile case phone case mobile panel phone panel Lenovo mobile case Lenovo phone cover Lenovo back case hard case 3D printed mobile cover mobile cover back cover mobile case phone case mobile panel phone panel Lenovo mobile case Lenovo phone cover Lenovo back case hard case 3D printed mobile cover mobile cover back cover mobile case phone case mobile panel phone panel Lenovo mobile case Lenovo phone cover Lenovo back case hard case 3D printed mobile cover mobile cover back cover mobil',\n", 417 | " 'material': nan,\n", 418 | " 'brand': 'Amazon Brand - Solimo',\n", 419 | " 'color': 'Others',\n", 420 | " 'item_name': 'Amazon Brand - Solimo Designer Couples Sitting at Dark 3D Printed Hard Back Case Mobile Cover for Lenovo K4 Note',\n", 421 | " 'model_name': 'Lenovo K4 Note',\n", 422 | " 'model_number': 'gz8115-SL40423',\n", 423 | " 'product_type': 'CELLULAR_PHONE_CASE',\n", 424 | " 'primary_key': 'B07T6RZ2CM-amazon.in'}" 425 | ] 426 | }, 427 | "execution_count": 27, 428 | "metadata": {}, 429 | "output_type": "execute_result" 430 | } 431 | ], 432 | "source": [ 433 | "len(item_keywords_vectors)\n", 434 | "len(product_metadata)\n", 435 | "# Check one of the products\n", 436 | "product_metadata[0]" 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 28, 442 | "metadata": { 443 | "id": "ayGF0v-w8mxy" 444 | }, 445 | "outputs": [], 446 | "source": [ 447 | "def load_vectors(client, product_metadata, vector_dict, vector_field_name):\n", 448 | " p = client.pipeline(transaction=False)\n", 449 | " for index in product_metadata.keys(): \n", 450 | " #hash key\n", 451 | " key='product:'+ str(index)+ ':' + product_metadata[index]['primary_key']\n", 452 | " \n", 453 | " #hash values\n", 454 | " item_metadata = product_metadata[index]\n", 455 | " item_keywords_vector = vector_dict[index].astype(np.float32).tobytes()\n", 456 | " item_metadata[vector_field_name]=item_keywords_vector\n", 457 | " \n", 458 | " # HSET\n", 459 | " p.hset(key,mapping=item_metadata)\n", 460 | " \n", 461 | " p.execute()\n", 462 | "\n", 463 | "def create_flat_index (redis_conn,vector_field_name,number_of_vectors, vector_dimensions=512, distance_metric='L2'):\n", 464 | " redis_conn.ft().create_index([\n", 465 | " VectorField(vector_field_name, \"FLAT\", {\"TYPE\": \"FLOAT32\", \"DIM\": vector_dimensions, \"DISTANCE_METRIC\": distance_metric, \"INITIAL_CAP\": number_of_vectors, \"BLOCK_SIZE\":number_of_vectors }),\n", 466 | " TagField(\"product_type\"),\n", 467 | " TextField(\"item_name\"),\n", 468 | " TextField(\"item_keywords\"),\n", 469 | " TagField(\"country\") \n", 470 | " ]) " 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "execution_count": 29, 476 | "metadata": { 477 | "id": "lcjo2rXb8pT9" 478 | }, 479 | "outputs": [ 480 | { 481 | "name": "stdout", 482 | "output_type": "stream", 483 | "text": [ 484 | "Loading and Indexing + 1000 products\n" 485 | ] 486 | } 487 | ], 488 | "source": [ 489 | "\n", 490 | "ITEM_KEYWORD_EMBEDDING_FIELD='item_keyword_vector'\n", 491 | "TEXT_EMBEDDING_DIMENSION=768\n", 492 | "NUMBER_PRODUCTS=1000\n", 493 | "\n", 494 | "print ('Loading and Indexing + ' + str(NUMBER_PRODUCTS) + ' products')\n", 495 | "\n", 496 | "#flush all data\n", 497 | "redis_conn.flushall()\n", 498 | "\n", 499 | "#create flat index & load vectors\n", 500 | "create_flat_index(redis_conn, ITEM_KEYWORD_EMBEDDING_FIELD,NUMBER_PRODUCTS,TEXT_EMBEDDING_DIMENSION,'COSINE')\n", 501 | "load_vectors(redis_conn,product_metadata,item_keywords_vectors,ITEM_KEYWORD_EMBEDDING_FIELD)\n", 502 | " " 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 31, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "name": "stdout", 512 | "output_type": "stream", 513 | "text": [ 514 | "User: im looking for some earrings for my girlfriend\n", 515 | "Bot: Based on your query, I would recommend checking out these Women's Engravable 14k Gold Plated 925 Sterling Silver Polished Disc Charm on 18\" Chain Pendant Necklace. They might make a great gift for your girlfriend if she likes elegant and stylish jewelry. However, if you are looking for shoes, you might want to check out Amazon Brand - The Fix Women's Lizzie Block Heel Ruffled Sandal Heeled, Dove Suede, 9 B US. These shoes are great for spring and summer, and perfect for a date night or a fancy event. Another option for shoes could be Amazon Brand - The Fix Women's Giana Open Toe Bootie with Pearl Buckle, which comes in bright white leather and sizes up to 9.5 B US. I hope this helps!\n", 516 | "User: tell me more about the necklace\n", 517 | "Bot: The Women's Engravable 14k Gold Plated 925 Sterling Silver Polished Disc Charm on 18\" Chain Pendant Necklace is a beautiful and elegant piece of jewelry. The pendant is made of 925 sterling silver and is plated with 14k gold for a luxurious look. The pendant is disc-shaped and can be engraved with a personal message or initials to make it even more special. The chain is 18 inches long and adds a delicate touch to the necklace. This necklace is perfect for everyday wear or for special occasions.\n", 518 | "User: whats the product id\n", 519 | "Bot: The product ID for the Women's Engravable 14k Gold Plated 925 Sterling Silver Polished Disc Charm on 18\" Chain Pendant Necklace is B07CJCSYQF.\n", 520 | "User: \n", 521 | "Bot: Do you have any other questions or queries I can help you with?\n" 522 | ] 523 | }, 524 | { 525 | "ename": "KeyboardInterrupt", 526 | "evalue": "Interrupted by user", 527 | "output_type": "error", 528 | "traceback": [ 529 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 530 | "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", 531 | "Cell \u001b[0;32mIn[31], line 60\u001b[0m\n\u001b[1;32m 57\u001b[0m time\u001b[39m.\u001b[39msleep(\u001b[39m0.5\u001b[39m)\n\u001b[1;32m 59\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m---> 60\u001b[0m follow_up \u001b[39m=\u001b[39m \u001b[39minput\u001b[39;49m(\u001b[39m\"\u001b[39;49m\u001b[39mAnything else you want to ask about this topic?\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n\u001b[1;32m 61\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mUser:\u001b[39m\u001b[39m\"\u001b[39m, follow_up)\n\u001b[1;32m 62\u001b[0m answer \u001b[39m=\u001b[39m llm_chain\u001b[39m.\u001b[39mpredict(\n\u001b[1;32m 63\u001b[0m user_msg\u001b[39m=\u001b[39mfollow_up\n\u001b[1;32m 64\u001b[0m )\n", 532 | "File \u001b[0;32m/workspaces/lablab-api/venv/lib/python3.10/site-packages/ipykernel/kernelbase.py:1191\u001b[0m, in \u001b[0;36mKernel.raw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 1189\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 1190\u001b[0m \u001b[39mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[0;32m-> 1191\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_input_request(\n\u001b[1;32m 1192\u001b[0m \u001b[39mstr\u001b[39;49m(prompt),\n\u001b[1;32m 1193\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_parent_ident[\u001b[39m\"\u001b[39;49m\u001b[39mshell\u001b[39;49m\u001b[39m\"\u001b[39;49m],\n\u001b[1;32m 1194\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mget_parent(\u001b[39m\"\u001b[39;49m\u001b[39mshell\u001b[39;49m\u001b[39m\"\u001b[39;49m),\n\u001b[1;32m 1195\u001b[0m password\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m,\n\u001b[1;32m 1196\u001b[0m )\n", 533 | "File \u001b[0;32m/workspaces/lablab-api/venv/lib/python3.10/site-packages/ipykernel/kernelbase.py:1234\u001b[0m, in \u001b[0;36mKernel._input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 1231\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mKeyboardInterrupt\u001b[39;00m:\n\u001b[1;32m 1232\u001b[0m \u001b[39m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[1;32m 1233\u001b[0m msg \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mInterrupted by user\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m-> 1234\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mKeyboardInterrupt\u001b[39;00m(msg) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[1;32m 1235\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mException\u001b[39;00m:\n\u001b[1;32m 1236\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlog\u001b[39m.\u001b[39mwarning(\u001b[39m\"\u001b[39m\u001b[39mInvalid Message:\u001b[39m\u001b[39m\"\u001b[39m, exc_info\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n", 534 | "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user" 535 | ] 536 | } 537 | ], 538 | "source": [ 539 | "from langchain.prompts import PromptTemplate\n", 540 | "from langchain.llms import OpenAI\n", 541 | "\n", 542 | "llm = OpenAI(model_name=\"gpt-3.5-turbo\", temperature=0.3, openai_api_key=\"sk-....\")\n", 543 | "prompt = PromptTemplate(\n", 544 | " input_variables=[\"product_description\"],\n", 545 | " template=\"Create comma seperated product keywords to perform a query on a amazon dataset for this user input: {product_description}\",\n", 546 | ")\n", 547 | "\n", 548 | "from langchain.chains import LLMChain\n", 549 | "chain = LLMChain(llm=llm, prompt=prompt)\n", 550 | "\n", 551 | "userinput = input(\"Hey im a E-commerce Chatbot, how can i help you today? \")\n", 552 | "print(\"User:\", userinput)\n", 553 | "# Run the chain only specifying the input variable.\n", 554 | "keywords = chain.run(userinput)\n", 555 | "\n", 556 | "\n", 557 | "topK=3\n", 558 | "#vectorize the query\n", 559 | "query_vector = model.encode(keywords).astype(np.float32).tobytes()\n", 560 | "\n", 561 | "#prepare the query\n", 562 | "q = Query(f'*=>[KNN {topK} @{ITEM_KEYWORD_EMBEDDING_FIELD} $vec_param AS vector_score]').sort_by('vector_score').paging(0,topK).return_fields('vector_score','item_name','item_id','item_keywords').dialect(2)\n", 563 | "params_dict = {\"vec_param\": query_vector}\n", 564 | "\n", 565 | "#Execute the query\n", 566 | "results = redis_conn.ft().search(q, query_params = params_dict)\n", 567 | "\n", 568 | "full_result_string = ''\n", 569 | "for product in results.docs:\n", 570 | " full_result_string += product.item_name + ' ' + product.item_keywords + ' ' + product.item_id + \"\\n\\n\\n\"\n", 571 | "\n", 572 | "\n", 573 | "from langchain.memory import ConversationBufferMemory\n", 574 | "\n", 575 | "template = \"\"\"You are a chatbot. Be kind, detailed and nice. Present the given queried search result in a nice way as answer to the user input. dont ask questions back! just take the given context\n", 576 | "\n", 577 | "{chat_history}\n", 578 | "Human: {user_msg}\n", 579 | "Chatbot:\"\"\"\n", 580 | "\n", 581 | "prompt = PromptTemplate(\n", 582 | " input_variables=[\"chat_history\", \"user_msg\"], \n", 583 | " template=template\n", 584 | ")\n", 585 | "memory = ConversationBufferMemory(memory_key=\"chat_history\")\n", 586 | "llm_chain = LLMChain(\n", 587 | " llm=OpenAI(model_name=\"gpt-3.5-turbo\", temperature=0.8, openai_api_key=\"sk-....\"), \n", 588 | " prompt=prompt, \n", 589 | " verbose=False, \n", 590 | " memory=memory,\n", 591 | ")\n", 592 | "\n", 593 | "answer = llm_chain.predict(user_msg= f\"{full_result_string} ---\\n\\n {userinput}\")\n", 594 | "print(\"Bot:\", answer)\n", 595 | "time.sleep(0.5)\n", 596 | "\n", 597 | "while True:\n", 598 | " follow_up = input(\"Anything else you want to ask about this topic?\")\n", 599 | " print(\"User:\", follow_up)\n", 600 | " answer = llm_chain.predict(\n", 601 | " user_msg=follow_up\n", 602 | " )\n", 603 | " print(\"Bot:\", answer)\n", 604 | " time.sleep(0.5)\n", 605 | "\n", 606 | "\n" 607 | ] 608 | } 609 | ], 610 | "metadata": { 611 | "colab": { 612 | "provenance": [] 613 | }, 614 | "kernelspec": { 615 | "display_name": "Python 3", 616 | "name": "python3" 617 | }, 618 | "language_info": { 619 | "codemirror_mode": { 620 | "name": "ipython", 621 | "version": 3 622 | }, 623 | "file_extension": ".py", 624 | "mimetype": "text/x-python", 625 | "name": "python", 626 | "nbconvert_exporter": "python", 627 | "pygments_lexer": "ipython3", 628 | "version": "3.10.4" 629 | } 630 | }, 631 | "nbformat": 4, 632 | "nbformat_minor": 0 633 | } 634 | --------------------------------------------------------------------------------