-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoreOpenSearch.py
More file actions
327 lines (279 loc) · 10.6 KB
/
storeOpenSearch.py
File metadata and controls
327 lines (279 loc) · 10.6 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from openai import OpenAI
import numpy as np
from opensearchpy import OpenSearch
import mariadb
import urllib3
from opensearchpy.connection import RequestsHttpConnection
import logging
from elasticsearch.helpers import bulk
# Disable SSL warnings (use cautiously in production)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# ----------------------------
# OpenAI Configuration
# ----------------------------
OPENAI_API_KEY = "sk-proj-" # Store securely in environment variables or secret managers
openAI_client = OpenAI(api_key=OPENAI_API_KEY)
# ----------------------------
# OpenSearch Configuration
# ----------------------------
OPENSEARCH_HOST = "172.31.30.137"
OPENSEARCH_AUTH = ("admin", "H@RTn311_ROCKS")
# OPENSEARCH_INDEX = "knn_vector_index"
OPENSEARCH_INDEX = "mock_knn_vector_index"
client = OpenSearch(
hosts=[OPENSEARCH_HOST],
http_auth=OPENSEARCH_AUTH,
http_compress=True,
use_ssl=True,
verify_certs=False, # Set to True with proper certs in production
connection_class=RequestsHttpConnection,
timeout=60, # Set timeout to 30 seconds or longer
retries=10, # Increase retries
max_retries=10 # Allow multiple retries
)
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ----------------------------
# Create OpenSearch k-NN Index
# ----------------------------
def makeIndexNamed(indexName):
index_body = {
"settings": {
"index": {
"knn": True,
"number_of_shards": 1,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"url": {"type": "text"},
"content": {"type": "text"},
"vector": {
"type": "knn_vector",
"dimension": 1536,
"method": {
"name": "hnsw",
"space_type": "cosinesimil",
"engine": "nmslib"
}
}
}
}
}
if not client.indices.exists(index=indexName):
client.indices.create(index=indexName, body=index_body)
print(f"Index '{indexName}' created successfully.")
else:
print(f"Index '{indexName}' already exists.")
# ----------------------------
# MariaDB Configuration
# ----------------------------
mariadb_config = {
"host": "172.31.30.137",
"user": "root",
"password": "H*W7]nD-C(4:#EfsV?MA5G$bQ",
"port": 3306,
"database": "hartnell_scraped_data"
}
# Fetch scraped data from MariaDB
def fetch_scraped_data():
try:
conn = mariadb.connect(**mariadb_config)
cursor = conn.cursor()
cursor.execute("SELECT id, url, content FROM scraped_data")
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows
except mariadb.Error as e:
print(f"Error fetching data: {e}")
return []
# Function to fetch specific records by IDs
def fetch_scraped_data_by_ids(ids):
try:
conn = mariadb.connect(**mariadb_config)
cursor = conn.cursor()
format_strings = ','.join(['%s'] * len(ids))
cursor.execute(f"SELECT id, url, content FROM scraped_data WHERE id IN ({format_strings})", tuple(ids))
rows = cursor.fetchall()
cursor.close()
conn.close()
return rows
except mariadb.Error as e:
logger.error(f"Error fetching specific data: {e}")
return []
# Retry mechanism for fetching data from MariaDB
# def fetch_scraped_data_with_retry(max_retries=3):
# retries = 0
# while retries < max_retries:
# try:
# return fetch_scraped_data()
# except mariadb.Error as e:
# retries += 1
# logger.error(f"Error fetching data (attempt {retries}): {e}")
# time.sleep(2 ** retries) # Exponential backoff
# logger.error("Failed to fetch data after multiple retries.")
# return []
# Generate OpenAI embedding
def generate_openai_embedding(text):
response = openAI_client.embeddings.create(
model="text-embedding-ada-002",
input=text
)
return np.array(response.data[0].embedding, dtype=np.float32)
# Store single document embedding
def store_embedding(doc_id, text, url):
embedding = generate_openai_embedding(text)
document = {
"url": url,
"content": text,
"vector": embedding.tolist()
}
response = client.index(index=OPENSEARCH_INDEX, id=doc_id, body=document)
print(f"Stored document {doc_id}: {response['result']}")
# Store multiple embeddings from database to OpenSearch
def store_embeddings_in_opensearch():
# data = fetch_scraped_data()
data =fetch_mock_data()
if not data:
print("No data fetched from MariaDB.")
return
for doc_id, url, text in data:
embedding = generate_openai_embedding(text)
document = {
"url": url,
"content": text,
"vector": embedding.tolist()
}
response = client.index(index=OPENSEARCH_INDEX, id=str(doc_id), body=document)
print(response)
print(f"Number of embeddings stored: {len(data)}")
# Store documents in OpenSearch with retry and error handling
def bulk_store_embeddings_in_opensearch():
# data = fetch_scraped_data_with_retry()
# data = fetch_scraped_data()
data=fetch_mock_data()
if not data:
logger.error("No data fetched from MariaDB.")
return
actions = []
failed_docs = []
# Prepare documents for bulk indexing
for doc_id, url, text in data:
embedding = generate_openai_embedding(text)
document = {
"_op_type": "index",
"_index": OPENSEARCH_INDEX,
"_id": str(doc_id),
"_source": {
"url": url,
"content": text,
"vector": embedding.tolist()
}
}
actions.append(document)
# Perform bulk indexing
try:
success, failed = bulk(client, actions)
logger.info(f"Successfully indexed {success} documents.")
if failed > 0:
logger.error(f"Failed to index {failed} documents. Storing failed IDs.")
# Track failed documents
failed_docs= [error['index']['_id'] for error in failed if 'index' in error]
except (ConnectionError, TimeoutError) as e:
logger.error(f"Error during bulk indexing: {e}")
# Retry indexing for failed documents
if failed_docs:
logger.info(f"Retrying failed documents: {failed_docs}")
store_failed_documents(failed_docs)
# Retry failed documents by fetching them and re-indexing
def store_failed_documents(failed_ids, max_retries=1):
retry_count = 0
failed_docs = []
while retry_count < max_retries:
try:
# Fetch specific failed documents from the database
data = fetch_scraped_data_by_ids(failed_ids)
if not data:
logger.error("No failed documents to retry.")
return
actions = []
for doc_id, url, text in data:
embedding = generate_openai_embedding(text)
document = {
"_op_type": "index",
"_index": OPENSEARCH_INDEX,
"_id": str(doc_id),
"_source": {
"url": url,
"content": text,
"vector": embedding.tolist()
}
}
actions.append(document)
# Perform bulk indexing for failed documents
success, failed = bulk(client, actions)
logger.info(f"Successfully re-indexed {success} documents.")
if failed > 0:
logger.error(f"Failed to index {failed} documents. Storing failed IDs.")
# Track failed documents
failed_docs= [error['index']['_id'] for error in failed if 'index' in error]
else:
# If no failures, break the loop
logger.info("All documents successfully re-indexed.")
return
except (ConnectionError, TimeoutError) as e:
logger.error(f"Error during bulk indexing: {e}")
# Increment the retry count
retry_count += 1
logger.info(f"Retry attempt {retry_count}/{max_retries}")
# If there are still failed documents, we retry
if failed_docs:
logger.info(f"Retrying failed documents: {failed_docs}")
# Call the function recursively for retry
store_failed_documents(failed_docs, max_retries)
else:
break # Exit if no failed documents remain after retry
if retry_count == max_retries and failed_docs:
logger.error(f"Failed to re-index {len(failed_docs)} documents after {max_retries} retries.")
print(failed_docs)
# Delete a single document by ID
def delDocId(index, id):
client.delete(index=index, id=id)
# Delete entire index
def delIndexNamed(indexName):
if client.indices.exists(index=indexName):
client.indices.delete(index=indexName)
print(f"Index '{indexName}' deleted.")
else:
print(f"Index '{indexName}' not found. Nothing to delete.")
def delIndex():
delIndexNamed(OPENSEARCH_INDEX)
def makeIndex():
makeIndexNamed(OPENSEARCH_INDEX)
def fetch_mock_data():
return [
(1, "https://example.com/1", "Hartnell College is a public community college in Salinas, California."),
(2, "https://example.com/2", "The college offers associate degrees and certificates across a variety of fields."),
(3, "https://example.com/3", "Students benefit from smaller class sizes and personalized attention."),
(4, "https://example.com/4", "Hartnell has a strong transfer program to California State Universities."),
(5, "https://example.com/5", "Online and hybrid classes are available to meet student needs.")
]
# ----------------------------
# Main execution
# ----------------------------
if __name__ == "__main__":
try:
response = client.info()
logger.info(f"Connected to OpenSearch: {response}")
except Exception as e:
logger.error(f"Error connecting to OpenSearch: {str(e)}")
print("Removing Index")
delIndexNamed(OPENSEARCH_INDEX)
print("Making Index")
makeIndexNamed(OPENSEARCH_INDEX) # Create index if it doesn't exist
print("Storing Embeddings")
# bulk_store_embeddings_in_opensearch() # Store all embeddings
store_embeddings_in_opensearch()