File size: 8,924 Bytes
bc0299d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "081405cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_community.embeddings import HuggingFaceEmbeddings\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3c40840f",
   "metadata": {},
   "outputs": [],
   "source": [
    "MODEL_NAME = \"sentence-transformers/all-MiniLM-L12-v2\"\n",
    "DATA_PATH=\"data/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "90fc0a47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading documents from data/...\n",
      "Loaded 2087 PDF document(s).\n",
      "Split into 25938 chunks.\n",
      "Creating and saving FAISS vector store...\n"
     ]
    }
   ],
   "source": [
    "embeddings = HuggingFaceEmbeddings(model_name=MODEL_NAME)\n",
    "\n",
    "print(f\"Loading documents from {DATA_PATH}...\")\n",
    "loader = DirectoryLoader(\n",
    "    DATA_PATH,\n",
    "    glob='*.pdf',         \n",
    "    loader_cls=PyPDFLoader  \n",
    ")\n",
    "documents = loader.load()\n",
    "\n",
    "if not documents:\n",
    "    print(\"No PDF documents found. Make sure your PDFs are in the /data folder.\")\n",
    "    exit()\n",
    "\n",
    "print(f\"Loaded {len(documents)} PDF document(s).\")\n",
    "\n",
    "# 3. Split Documents\n",
    "text_splitter = RecursiveCharacterTextSplitter(\n",
    "    chunk_size=300, \n",
    "    chunk_overlap=200,\n",
    "    separators=[\"\\n\\n\", \"\\n\", \".\", \"!\", \"?\", \" \", \"\"]\n",
    "    )\n",
    "docs = text_splitter.split_documents(documents)\n",
    "\n",
    "print(f\"Split into {len(docs)} chunks.\")\n",
    "\n",
    "# 4. Create and Save FAISS Vector Store\n",
    "print(\"Creating and saving FAISS vector store...\")\n",
    "db = FAISS.from_documents(docs, embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ca0ee2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading embedding model: sentence-transformers/all-MiniLM-L12-v2...\n",
      "\n",
      "✅ Retriever is ready.\n",
      "   Enter your query to test. Type 'exit' to quit.\n",
      "\n",
      "--- Retrieving docs for: 'who is director' ---\n",
      "\n",
      "--- Document 1 ---\n",
      "Source: data/iiitdmj_crawl_data_1.pdf\n",
      "Page: 133\n",
      "\n",
      "Content:\n",
      "director@iiitdmj.ac.in\n",
      "2.\n",
      "Deputy Director\n",
      "To be nominated on appointment\n",
      "3.\n",
      "Deans (Ex-officio)\n",
      "1. Dr. Mukesh Kumar Roy\n",
      "Faculty-in-Charge (Student Affairs)\n",
      "mkroy@iiitdmj.ac.in\n",
      "2. Prof. V. K. Gupta\n",
      "Professor In-charge (Academic)\n",
      "dean.acad@iiitdmj.ac.in\n",
      "3. Prof. Pritee Khanna\n",
      "--------------------\n",
      "\n",
      "--- Document 2 ---\n",
      "Source: data/IIITDM Jabalpur.pdf\n",
      "Page: 2\n",
      "\n",
      "Content:\n",
      " The Deputy Director  (to be nominated on appointment) \n",
      " The Deans \n",
      " The Heads of various disciplines and \n",
      " The Registrar \n",
      " \n",
      " \n",
      " \n",
      " \n",
      "Building And Works Committee \n",
      "S. No.  Name Designation  \n",
      "1.    Prof. Bhartendu Kumar  Singh \n",
      "Director \n",
      "PDPM-IIITDM Jabalpur \n",
      "director@iiitdmj.ac.in\n",
      "--------------------\n",
      "\n",
      "--- Document 3 ---\n",
      "Source: data/iiitdmj_crawl_data_1.pdf\n",
      "Page: 133\n",
      "\n",
      "Content:\n",
      "S. No.\n",
      "Name\n",
      "Address\n",
      "1.\n",
      "Director as Chairperson (Ex-officio)\n",
      "Prof. Bhartendu K Singh (Director)\n",
      "director@iiitdmj.ac.in\n",
      "2.\n",
      "Deputy Director\n",
      "To be nominated on appointment\n",
      "3.\n",
      "Deans (Ex-officio)\n",
      "1. Dr. Mukesh Kumar Roy\n",
      "Faculty-in-Charge (Student Affairs)\n",
      "mkroy@iiitdmj.ac.in\n",
      "2. Prof. V. K. Gupta\n",
      "--------------------\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain_community.embeddings import HuggingFaceEmbeddings\n",
    "\n",
    "\n",
    "def check_retriever():\n",
    "    \"\"\"\n",
    "    A standalone script to test the FAISS retriever.\n",
    "    \"\"\"\n",
    "    \n",
    "    # 1. Load the Embedding Model\n",
    "    print(f\"Loading embedding model: {MODEL_NAME}...\")\n",
    "    try:\n",
    "        # This line might show a deprecation warning, which is OK.\n",
    "        # It's the same one your agent.py is using.\n",
    "        embeddings = HuggingFaceEmbeddings(model_name=MODEL_NAME)\n",
    "    except Exception as e:\n",
    "        print(f\"Error loading embeddings: {e}\")\n",
    "        print(\"Make sure 'sentence-transformers' is installed: pip install sentence-transformers\")\n",
    "        return\n",
    "\n",
    "    # # 2. Load the FAISS Vector Store\n",
    "    # print(f\"Loading FAISS index from: {DB_FAISS_PATH}...\")\n",
    "    # try:\n",
    "    #     db = FAISS.load_local(\n",
    "    #         DB_FAISS_PATH, \n",
    "    #         embeddings, \n",
    "    #         allow_dangerous_deserialization=True # This is required\n",
    "    #     )\n",
    "    # except Exception as e:\n",
    "    #     print(f\"Error loading FAISS index: {e}\")\n",
    "    #     print(\"Be sure you have run 'python ingest.py' successfully first.\")\n",
    "    #     return\n",
    "\n",
    "    retriever = db.as_retriever(search_kwargs={'k': 3})\n",
    "    \n",
    "    print(\"\\n✅ Retriever is ready.\")\n",
    "    print(\"   Enter your query to test. Type 'exit' to quit.\")\n",
    "    \n",
    "    while True:\n",
    "        try:\n",
    "            query = input(\"\\nQuery> \")\n",
    "            if query.lower() == 'exit':\n",
    "                break\n",
    "            if not query:\n",
    "                continue\n",
    "                \n",
    "            print(f\"\\n--- Retrieving docs for: '{query}' ---\")\n",
    "            \n",
    "            documents = retriever.invoke(query)\n",
    "            \n",
    "            if not documents:\n",
    "                print(\"\\n!!! No documents found. !!!\")\n",
    "            else:\n",
    "                for i, doc in enumerate(documents):\n",
    "                    print(f\"\\n--- Document {i+1} ---\")\n",
    "                    print(f\"Source: {doc.metadata.get('source', 'N/A')}\")\n",
    "                    print(f\"Page: {doc.metadata.get('page', 'N/A')}\")\n",
    "                    print(\"\\nContent:\")\n",
    "                    print(doc.page_content)\n",
    "                    print(\"-\" * 20)\n",
    "                    \n",
    "        except Exception as e:\n",
    "            print(f\"An error occurred: {e}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    check_retriever()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "45430224",
   "metadata": {},
   "outputs": [],
   "source": [
    "DB_FAISS_PATH = \"vectorstore/faiss_index2\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9488f2a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully created and saved FAISS index to vectorstore/faiss_index2\n"
     ]
    }
   ],
   "source": [
    "db = FAISS.from_documents(docs, embeddings)\n",
    "db.save_local(DB_FAISS_PATH)\n",
    "\n",
    "print(f\"Successfully created and saved FAISS index to {DB_FAISS_PATH}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bef0e8c2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}