{ "cells": [ { "cell_type": "code", "execution_count": 4, "id": "4726886c", "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 6, "id": "f2ac860f", "metadata": {}, "outputs": [ { "data": { "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", "
idproduct_search_descriptionmeta_data
2986454800men apparel bottomwear jeans blue summer casua...{'id': 54800, 'name': 'Peter England Men Blue ...
264329140women accessories eyewear sunglasses purple wi...{'id': 29140, 'name': 'Vogue Women Purple Sung...
166835358men footwear shoes sports shoes white summer s...{'id': 5358, 'name': 'Kipsta Calcetto 5 Sr Whi...
2770455004women personal care lips lipstick multi spring...{'id': 55004, 'name': 'Lakme 6 in 1 Travel Kit...
969553444men footwear flip flops flip flops black summe...{'id': 53444, 'name': 'ADIDAS Men Black Duramo...
\n", "
" ], "text/plain": [ " id product_search_description \\\n", "29864 54800 men apparel bottomwear jeans blue summer casua... \n", "2643 29140 women accessories eyewear sunglasses purple wi... \n", "16683 5358 men footwear shoes sports shoes white summer s... \n", "27704 55004 women personal care lips lipstick multi spring... \n", "9695 53444 men footwear flip flops flip flops black summe... \n", "\n", " meta_data \n", "29864 {'id': 54800, 'name': 'Peter England Men Blue ... \n", "2643 {'id': 29140, 'name': 'Vogue Women Purple Sung... \n", "16683 {'id': 5358, 'name': 'Kipsta Calcetto 5 Sr Whi... \n", "27704 {'id': 55004, 'name': 'Lakme 6 in 1 Travel Kit... \n", "9695 {'id': 53444, 'name': 'ADIDAS Men Black Duramo... " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df=pd.read_csv(\"final_filtered_data.csv\")\n", "df.sample(5)" ] }, { "cell_type": "code", "execution_count": 10, "id": "9a02c860", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading weights: 100%|██████████| 103/103 [00:00<00:00, 4989.64it/s]\n", "\u001b[1mBertModel LOAD REPORT\u001b[0m from: sentence-transformers/all-MiniLM-L6-v2\n", "Key | Status | | \n", "------------------------+------------+--+-\n", "embeddings.position_ids | UNEXPECTED | | \n", "\n", "\u001b[3mNotes:\n", "- UNEXPECTED\u001b[3m\t:can be ignored when loading from different task/architecture; not ok if you expect identical arch.\u001b[0m\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "FAISS index saved successfully!\n" ] } ], "source": [ "import faiss\n", "from pathlib import Path\n", "\n", "from langchain_community.docstore.in_memory import InMemoryDocstore\n", "from langchain_community.vectorstores import FAISS\n", "from langchain_huggingface import HuggingFaceEmbeddings\n", "from langchain_core.documents import Document\n", "\n", "EMBEDDING_MODEL = \"sentence-transformers/all-MiniLM-L6-v2\"\n", "\n", "# embedding model\n", "embedding_model = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)\n", "\n", "# dimension\n", "embedding_dim = len(embedding_model.embed_query(\"hello world\"))\n", "\n", "# FAISS index\n", "index = faiss.IndexFlatL2(embedding_dim)\n", "\n", "# docstore\n", "docstore = InMemoryDocstore()\n", "\n", "# mapping\n", "index_to_docstore_id = {}\n", "\n", "# create vector store\n", "vector_store = FAISS(\n", " embedding_function=embedding_model,\n", " index=index,\n", " docstore=docstore,\n", " index_to_docstore_id=index_to_docstore_id,\n", ")\n", "\n", "# example documents\n", "docs = [\n", " Document(page_content=\"Delhi is the capital of India\"),\n", " Document(page_content=\"Python is used for AI\"),\n", "]\n", "\n", "# add docs\n", "vector_store.add_documents(docs)\n", "\n", "# save directory\n", "save_dir = Path(\"vector_data/faiss\")\n", "save_dir.mkdir(parents=True, exist_ok=True)\n", "\n", "# save FAISS\n", "vector_store.save_local(str(save_dir))\n", "\n", "print(\"FAISS index saved successfully!\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "a43532b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "FAISS loaded!\n" ] } ], "source": [ "\n", "\n", "\n", "vector_store = FAISS.load_local(\n", " \"vector_data/faiss\",\n", " embeddings=embedding_model,\n", " allow_dangerous_deserialization=True\n", ")\n", "\n", "print(\"FAISS loaded!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "74cc0de7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 12, "id": "e4e3457e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vector_store" ] }, { "cell_type": "code", "execution_count": 13, "id": "c6cf6af2", "metadata": {}, "outputs": [], "source": [ "retreiver=vector_store.as_retriever()" ] }, { "cell_type": "code", "execution_count": 16, "id": "dc581cc0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Document(id='59c64ee8-a81d-422a-8f73-77df4e13ce7f', metadata={}, page_content='Python is used for AI'),\n", " Document(id='ff18ce3a-2257-4ac8-8b6c-92d239b3269b', metadata={}, page_content='Delhi is the capital of India')]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "retreiver.invoke(\"Hello \",k=3)" ] }, { "cell_type": "code", "execution_count": null, "id": "ed7029d1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "E-Commerce-Chatbot-recommendation (3.13.5)", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }