Ardaarslan02 commited on
Commit
0987c0b
·
verified ·
1 Parent(s): 27522fd

Upload setup_qdrant.ipynb

Browse files
Files changed (1) hide show
  1. setup_qdrant.ipynb +211 -0
setup_qdrant.ipynb ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "data": {
10
+ "text/plain": [
11
+ "'/Users/arda/Desktop/A.I./Projects/FinanceChatbot'"
12
+ ]
13
+ },
14
+ "execution_count": 1,
15
+ "metadata": {},
16
+ "output_type": "execute_result"
17
+ }
18
+ ],
19
+ "source": [
20
+ "%pwd"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 2,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "# Import libraries\n",
30
+ "import os\n",
31
+ "import warnings\n",
32
+ "from dotenv import load_dotenv\n",
33
+ "from langchain.document_loaders import PyPDFLoader, DirectoryLoader\n",
34
+ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
35
+ "from langchain_qdrant import QdrantVectorStore\n",
36
+ "from langchain.embeddings import HuggingFaceEmbeddings\n",
37
+ "\n",
38
+ "# Ignore all warnings\n",
39
+ "warnings.filterwarnings(\"ignore\")"
40
+ ]
41
+ },
42
+ {
43
+ "cell_type": "code",
44
+ "execution_count": 3,
45
+ "metadata": {},
46
+ "outputs": [
47
+ {
48
+ "name": "stdout",
49
+ "output_type": "stream",
50
+ "text": [
51
+ "Environment variables loaded successfully.\n"
52
+ ]
53
+ }
54
+ ],
55
+ "source": [
56
+ "# Load environment variables from .env file\n",
57
+ "load_dotenv()\n",
58
+ "\n",
59
+ "# Check if .env file exists and API keys are loaded\n",
60
+ "if not os.path.exists('.env'):\n",
61
+ " print(\"Warning: .env file not found!\")\n",
62
+ "elif not os.getenv(\"QDRANT_API_KEY\") or not os.getenv(\"QDRANT_URL\"):\n",
63
+ " print(\"Warning: QDRANT_API_KEY or QDRANT_URL not found in .env file!\")\n",
64
+ "else:\n",
65
+ " print(\"Environment variables loaded successfully.\")\n",
66
+ "\n",
67
+ "# Settings\n",
68
+ "QDRANT_API_KEY = os.getenv(\"QDRANT_API_KEY\")\n",
69
+ "QDRANT_URL = os.getenv(\"QDRANT_URL\")\n",
70
+ "COLLECTION_NAME = \"finance-chatbot\"\n",
71
+ "DATA_DIR = \"Data\""
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "execution_count": 4,
77
+ "metadata": {},
78
+ "outputs": [
79
+ {
80
+ "name": "stdout",
81
+ "output_type": "stream",
82
+ "text": [
83
+ "Number of unique PDF files loaded: 3\n",
84
+ "Loaded files:\n",
85
+ "File 1: Data/Basics.pdf\n",
86
+ "File 2: Data/Financialterms.pdf\n",
87
+ "File 3: Data/Statementanalysis.pdf\n",
88
+ "Success: All 3 PDFs (Basics.pdf, Statementanalysis.pdf, Financialterms.pdf) have been loaded.\n",
89
+ "Total number of pages loaded: 547\n"
90
+ ]
91
+ }
92
+ ],
93
+ "source": [
94
+ "# Load and extract data from PDFs\n",
95
+ "def load_pdf_file(data_dir):\n",
96
+ " loader = DirectoryLoader(\n",
97
+ " data_dir,\n",
98
+ " glob=\"*.pdf\",\n",
99
+ " loader_cls=PyPDFLoader\n",
100
+ " )\n",
101
+ " documents = loader.load()\n",
102
+ " return documents\n",
103
+ "\n",
104
+ "extracted_data = load_pdf_file(DATA_DIR)\n",
105
+ "\n",
106
+ "# Verify the number of loaded PDFs by checking unique file sources\n",
107
+ "unique_files = set(doc.metadata.get('source', 'Unknown') for doc in extracted_data)\n",
108
+ "print(f\"Number of unique PDF files loaded: {len(unique_files)}\")\n",
109
+ "print(\"Loaded files:\")\n",
110
+ "for i, file in enumerate(unique_files, 1):\n",
111
+ " print(f\"File {i}: {file}\")\n",
112
+ "\n",
113
+ "# Check if the expected number of PDFs (3) were loaded\n",
114
+ "if len(unique_files) == 3:\n",
115
+ " print(\"Success: All 3 PDFs (Basics.pdf, Statementanalysis.pdf, Financialterms.pdf) have been loaded.\")\n",
116
+ "else:\n",
117
+ " print(f\"Warning: Expected 3 PDFs, but {len(unique_files)} unique files were loaded. Check the Data directory.\")\n",
118
+ "\n",
119
+ "# Additional info: Total number of pages (documents)\n",
120
+ "print(f\"Total number of pages loaded: {len(extracted_data)}\")"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": 5,
126
+ "metadata": {},
127
+ "outputs": [
128
+ {
129
+ "name": "stdout",
130
+ "output_type": "stream",
131
+ "text": [
132
+ "Length of Text Chunks: 2756\n"
133
+ ]
134
+ }
135
+ ],
136
+ "source": [
137
+ "# Split the data into text chunks\n",
138
+ "def text_split(extracted_data):\n",
139
+ " text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)\n",
140
+ " text_chunks = text_splitter.split_documents(extracted_data)\n",
141
+ " return text_chunks\n",
142
+ "\n",
143
+ "text_chunks = text_split(extracted_data)\n",
144
+ "print(\"Length of Text Chunks:\", len(text_chunks))"
145
+ ]
146
+ },
147
+ {
148
+ "cell_type": "code",
149
+ "execution_count": 6,
150
+ "metadata": {},
151
+ "outputs": [
152
+ {
153
+ "name": "stdout",
154
+ "output_type": "stream",
155
+ "text": [
156
+ "Embedding Dimension: 384\n"
157
+ ]
158
+ }
159
+ ],
160
+ "source": [
161
+ "# Download embeddings from Hugging Face\n",
162
+ "embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')\n",
163
+ "\n",
164
+ "# Verify embedding dimension\n",
165
+ "query_result = embeddings.embed_query(\"Hello world\")\n",
166
+ "print(\"Embedding Dimension:\", len(query_result))"
167
+ ]
168
+ },
169
+ {
170
+ "cell_type": "code",
171
+ "execution_count": null,
172
+ "metadata": {},
173
+ "outputs": [],
174
+ "source": [
175
+ "# Initialize Qdrant client and create/upload to collection\n",
176
+ "try:\n",
177
+ " qdrant = QdrantVectorStore.from_documents(\n",
178
+ " documents=text_chunks,\n",
179
+ " embedding=embeddings,\n",
180
+ " url=QDRANT_URL,\n",
181
+ " api_key=QDRANT_API_KEY,\n",
182
+ " collection_name=COLLECTION_NAME\n",
183
+ " )\n",
184
+ " print(\"Qdrant collection created and populated successfully.\")\n",
185
+ "except Exception as e:\n",
186
+ " print(f\"Error creating Qdrant collection: {e}\")"
187
+ ]
188
+ }
189
+ ],
190
+ "metadata": {
191
+ "kernelspec": {
192
+ "display_name": "finance_chatbot",
193
+ "language": "python",
194
+ "name": "python3"
195
+ },
196
+ "language_info": {
197
+ "codemirror_mode": {
198
+ "name": "ipython",
199
+ "version": 3
200
+ },
201
+ "file_extension": ".py",
202
+ "mimetype": "text/x-python",
203
+ "name": "python",
204
+ "nbconvert_exporter": "python",
205
+ "pygments_lexer": "ipython3",
206
+ "version": "3.10.16"
207
+ }
208
+ },
209
+ "nbformat": 4,
210
+ "nbformat_minor": 2
211
+ }