-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_manager.py
301 lines (240 loc) · 10.9 KB
/
database_manager.py
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
import sqlite3
import chromadb
from sentence_transformers import SentenceTransformer
import csv
import fitz
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.metrics.pairwise import cosine_similarity
embedder = SentenceTransformer('all-MiniLM-L6-v2')
IDX = 0
# Function to generate embeddings
def generate_embeddings(texts):
if isinstance(texts, str):
texts = [texts]
embeddings = embedder.encode(texts, convert_to_tensor=False).tolist()
return embeddings
# Function to add questions to ChromaDB with full metadata
def add_questions_from_csv_to_db(csv_file_path, db_connection, theme):
global IDX
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for idx, row in enumerate(reader):
question_text = row['question']
answer_text = row['answer']
level = row['level']
status = row['status']
unique_id = str(IDX)
# embedding = generate_embeddings(question_text)[0]
IDX += 1
# Store question and answer with full metadata in ChromaDB
# collection.add(
# ids=[unique_id],
# metadatas=[{
# 'theme': theme,
# 'question_text': question_text,
# 'answer_text': answer_text,
# 'level': level,
# 'status': status
# }],
# embeddings=[embedding]
# )
# Add the metadata to SQLite database
cursor = db_connection.cursor()
cursor.execute("INSERT INTO questions (id, theme, question, answer, level, status) VALUES (?, ?, ?, ?, ?, ?)",
(unique_id, theme, question_text, answer_text, level, status))
db_connection.commit()
print(f"Added question '{question_text}' with level '{level}' and status '{status}' to ChromaDB.")
# Function to find relevant questions by theme
def get_relevant_question_by_theme(theme, collection, n_results):
# Generate embedding for the theme provided by the user
theme_embedding = generate_embeddings(theme)[0]
# Search for the closest matching question embeddings in ChromaDB
results = collection.query(query_embeddings=[theme_embedding], n_results=n_results) # Get n most relevant documents
# Print debug info for the results
print(f"ChromaDB query results: {results}") # Debug print
# Retrieve the list of IDs from the query results
retrieved_ids = [int(id_str) for id_str in results['ids'][0]] # Assuming ids are in a list inside another list
print(f"Retrieved IDs: {retrieved_ids}") # Debug print
# Create a list to store the relevant questions and metadata
relevant_questions = []
# Retrieve the corresponding question, answer, level, and status from ChromaDB using the retrieved IDs
for retrieved_id in retrieved_ids:
theme_name = results['metadatas'][0][retrieved_ids.index(retrieved_id)]['theme']
metadata = results['metadatas'][0][retrieved_ids.index(retrieved_id)] # Get metadata for the matching ID
question_text = metadata['question_text']
answer_text = metadata['answer_text']
level = metadata['level']
status = metadata['status']
# Add the found question and metadata to the list
relevant_questions.append({
'id': retrieved_id,
'theme': theme_name,
'question': question_text,
'answer': answer_text,
'level': level,
'status': status
})
# If no relevant questions found, return a default message
if not relevant_questions:
return "No relevant questions found for this theme."
return relevant_questions
# Function to query SQLite for questions by level
def get_questions_by_level_from_sqlite(theme, level, db_connection, n_results):
cursor = db_connection.cursor()
# Base query to fetch questions by level
query = "SELECT id, theme, question, answer, level, status FROM questions WHERE theme=? AND level=? and status='none'"
# If n_results is specified, add LIMIT to the query
if n_results:
query += " LIMIT ?"
cursor.execute(query, (theme, level, n_results))
else:
cursor.execute(query, (theme, level,))
rows = cursor.fetchall()
# If no questions are found
if not rows:
return f"No relevant questions found for theme '{theme}' and level '{level}'."
relevant_questions = []
for row in rows:
relevant_questions.append({
'id': row[0],
'theme': row[1],
'question': row[2],
'answer': row[3],
'level': row[4],
'status': row[5]
})
return relevant_questions
def change_status(id, status, db_connection):
cursor = db_connection.cursor()
# Debug print: Display the ID and status being updated
print(f"Attempting to update status for ID: {id} to '{status}'")
cursor.execute("UPDATE questions SET status=? WHERE id=?", (status, id))
db_connection.commit()
# Debug print: Check the number of rows affected
if cursor.rowcount > 0:
print(f"Status updated successfully for ID: {id}")
else:
print(f"No rows were updated. Check if ID: {id} exists in the database.")
return True
# Function to extract text from a PDF and chunk it into semantically meaningful pieces
def extract_text_from_pdf(pdf_file_path):
doc = fitz.open(pdf_file_path)
all_text = []
# Iterate over the pages in the PDF and extract text
for page_num in range(doc.page_count):
page = doc.load_page(page_num)
text = page.get_text("text")
all_text.append(text)
return "\n".join(all_text)
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn.metrics.pairwise import cosine_similarity
def semantic_chunking(text, min_samples=2, eps=0.5, max_sentences_per_chunk=3):
# Split the text into sentences
sentences = text.split('. ') # Simple sentence splitting, you can use a more advanced tokenizer like spaCy
# Generate embeddings for each sentence
sentence_embeddings = generate_embeddings(sentences)
# Compute pairwise cosine similarities
similarity_matrix = cosine_similarity(sentence_embeddings)
# Convert cosine similarity to distance matrix (1 - similarity)
distance_matrix = 1 - similarity_matrix
# Ensure no negative values in the distance matrix (clip them to 0)
distance_matrix = np.maximum(distance_matrix, 0)
# Apply DBSCAN clustering to identify semantically similar sentences
clustering_model = DBSCAN(eps=eps, min_samples=min_samples, metric="precomputed")
clusters = clustering_model.fit_predict(distance_matrix)
# Group sentences into chunks based on clustering results
chunks = []
for cluster_id in np.unique(clusters):
if cluster_id == -1: # -1 is the label for noise in DBSCAN
continue
chunk_sentences = [sentences[i] for i in range(len(sentences)) if clusters[i] == cluster_id]
# Ensure the chunk does not exceed the max number of sentences
while len(chunk_sentences) > max_sentences_per_chunk:
# Split the chunk into smaller parts if it's too large
chunk = " ".join(chunk_sentences[:max_sentences_per_chunk])
chunks.append(chunk)
chunk_sentences = chunk_sentences[max_sentences_per_chunk:]
# Add the remaining sentences as one chunk
if chunk_sentences:
chunks.append(" ".join(chunk_sentences))
return chunks
# Function to add PDF content to ChromaDB
def add_pdf_to_chromadb(pdf_file_path, collection, theme):
global IDX
text = extract_text_from_pdf(pdf_file_path)
chunks = semantic_chunking(text)
for chunk in chunks:
unique_id = str(IDX)
embedding = generate_embeddings(chunk)[0]
IDX += 1
# Store the text chunk with metadata in ChromaDB
collection.add(
ids=[unique_id],
metadatas=[{
'theme': theme,
'text_chunk': chunk
}],
embeddings=[embedding]
)
print(f"Added chunk to ChromaDB with ID '{unique_id}'.")
# Function to retrieve relevant context from ChromaDB based on a query
def retrieve_context_from_chromadb(query, collection, n_results=3):
query_embedding = generate_embeddings(query)[0]
# Search for the closest matching embeddings in ChromaDB
results = collection.query(query_embeddings=[query_embedding], n_results=n_results)
# Print debug info for the results
print(f"ChromaDB query results: {results}") # Debug print
# Retrieve the list of IDs from the query results
retrieved_ids = results['ids'][0]
# Create a list to store the relevant text chunks
relevant_context = []
for idx in range(len(retrieved_ids)):
metadata = results['metadatas'][0][idx]
text_chunk = metadata['text_chunk']
# Add the relevant chunk and metadata to the list
relevant_context.append({
'id': retrieved_ids[idx],
'theme': metadata['theme'],
'text_chunk': text_chunk
})
# If no relevant context found, return a default message
if not relevant_context:
return "No relevant context found."
return relevant_context
# # Example usage
# # Assuming you have a ChromaDB collection and a SQLite database connection set up
# collection = chromadb.Client().get_or_create_collection('pdf_collection4')
# # Add PDF to ChromaDB
# add_pdf_to_chromadb('datasets/PDF_ML.pdf', collection, theme='ML_EN')
# # Retrieve context based on a query
# retrieved_context = retrieve_context_from_chromadb('Clustering', collection)
# print(retrieved_context)
# Change the status of question with ID 3 to 'reviewed'
# Example usage
# client = chromadb.HttpClient(host='localhost', port=8000)
# collection = client.get_or_create_collection(name="chromadb_demo")
# def create_sqlite_db(path):
# connection = sqlite3.connect(path)
# cursor = connection.cursor()
# cursor.execute('''CREATE TABLE IF NOT EXISTS questions (
# id TEXT PRIMARY KEY,
# theme TEXT,
# question TEXT,
# answer TEXT,
# level TEXT,
# status TEXT)''')
# connection.commit()
# return connection
# db_path = 'sglite_demo.db'
# sqlite_connection = create_sqlite_db(db_path)
# ## IOS_RU
# theme_name = "IOS_RU"
# csv_file_path = 'datasets/ios_interview_questions_ru.csv'
# add_questions_from_csv_to_db(csv_file_path, collection, sqlite_connection, theme_name)
# ## ML_EN
# theme_name = "ML_EN"
# csv_file_path = 'datasets/ml_interview_questions_en.csv'
# add_questions_from_csv_to_db(csv_file_path, collection, sqlite_connection, theme_name)
# print(IDX) #903