-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_rag.py
More file actions
350 lines (231 loc) · 11.6 KB
/
main_rag.py
File metadata and controls
350 lines (231 loc) · 11.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from rag import llm_utils, memory , retriever , prompt , merging , embedding
import faiss
from langchain_community.vectorstores import FAISS
from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain
from langchain.chains.history_aware_retriever import create_history_aware_retriever
from artifacts import keys
import google.generativeai as genai
def chatbot_rag(mode):
#mode = 'GEMINI'
# mode = 'HF'
HUGGINGFACEHUB_API_TOKEN = keys.HUGGINGFACEHUB_API_TOKEN
GOOGLE_API_KEY = keys.GOOGLE_API_KEY
LLM_ID = "llama3.2:3b"#"deepseek-r1:1.5b"
try:
if mode == 'GEMINI':
embeddings_g = embedding.gemini_embeddings()
elif mode == 'HF':
embeddings_hf = embedding.hugging_face_embedding()
elif mode == 'OLLAMA':
embeddings_hf = embedding.hugging_face_embedding()
print("embedding loaded")
except Exception as e:
print(f"error loading the embeddings : {e}")
try:
if mode == 'GEMINI':
llm_model = llm_utils.load_llm_gemini(GOOGLE_API_KEY=GOOGLE_API_KEY)
elif mode == 'HF':
llm_model = llm_utils.load_llm_hf(HUGGINGFACEHUB_API_TOKEN = HUGGINGFACEHUB_API_TOKEN)
elif mode == "OLLAMA":
llm_model = llm_utils.load_llm_ollama(LLM_ID)
print("llm loaded")
except Exception as e:
print(f"error loading model : {e}")
#hugging face vectorstores
if mode == 'HF':
try:
try:
pdf_vectorstore = FAISS.load_local(folder_path="artifacts/pdf_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("pdf vectorstore loaded")
except Exception as e:
print(f"error loading pdf vectorstore :{e}")
try:
txt_vectorstore = FAISS.load_local(folder_path="artifacts/txt_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("txt vectorstore loaded")
except Exception as e:
print(f"error loading txt vectorstore :{e}")
try:
csv_vectorstore = FAISS.load_local(folder_path="artifacts/csv_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("csv vectorstore loaded")
except Exception as e:
print(f"error loading csv vectorstore :{e}")
print("all vectorstores loaded successfully")
except Exception as e:
print(f"error loading the vectorstores : {e}")
elif mode == 'OLLAMA':
try:
try:
pdf_vectorstore = FAISS.load_local(folder_path="artifacts/pdf_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("pdf vectorstore loaded")
except Exception as e:
print(f"error loading pdf vectorstore :{e}")
try:
txt_vectorstore = FAISS.load_local(folder_path="artifacts/txt_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("txt vectorstore loaded")
except Exception as e:
print(f"error loading txt vectorstore :{e}")
try:
csv_vectorstore = FAISS.load_local(folder_path="artifacts/csv_vectorstore",embeddings = embeddings_hf ,allow_dangerous_deserialization= True)
print("csv vectorstore loaded")
except Exception as e:
print(f"error loading csv vectorstore :{e}")
print("all vectorstores loaded successfully")
except Exception as e:
print(f"error loading the vectorstores : {e}")
elif mode == 'GEMINI':
# gemini vectorstore
try:
try:
pdf_vectorstore = FAISS.load_local(folder_path="artifacts/pdf_vectorstore_gemini",embeddings = embeddings_g ,allow_dangerous_deserialization= True)
print("pdf vectorstore loaded")
except Exception as e:
print(f"error loading pdf vectorstore :{e}")
try:
txt_vectorstore = FAISS.load_local(folder_path="artifacts/txt_vectorstore_gemini",embeddings = embeddings_g ,allow_dangerous_deserialization= True)
print("txt vectorstore loaded")
except Exception as e:
print(f"error loading txt vectorstore :{e}")
try:
csv_vectorstore = FAISS.load_local(folder_path="artifacts/csv_vectorstore_gemini",embeddings = embeddings_g ,allow_dangerous_deserialization= True)
print("csv vectorstore loaded")
except Exception as e:
print(f"error loading csv vectorstore :{e}")
print("all vectorstores loaded successfully")
except Exception as e:
print(f"error loading the vectorstores : {e}")
# creating retriever instances
try:
try:
txt_retriever = retriever.vector_store_retirever(txt_vectorstore)
print("txt retriever loaded")
except Exception as e:
print(f"error loading txt retriever :{e}")
try:
pdf_retriever = retriever.vector_store_retirever(pdf_vectorstore)
print("pdf retriever loaded")
except Exception as e:
print(f"error loading pdf retriever :{e}")
try:
csv_retriever = retriever.vector_store_retirever(csv_vectorstore)
print("csv retriever loaded")
except Exception as e:
print(f"error loading csv retriever :{e}")
print("all retriever loaded successfully")
except Exception as e:
print(f"error loading the retrievers : {e}")
# merging the 3 retrievers
try:
ensemble_retriever = merging.ensemble_retriever(pdf_retriever=pdf_retriever ,
txt_retriever=txt_retriever ,
csv_retriever=csv_retriever)
print("ensemble retriever loaded successfully")
except Exception as e:
print(f"error loading the ensemble retriever : {e}")
'''try:
rag_memory = memory.ConversationBufferMemory(memory_key='chat_history' , return_messages= True)
print("rag memory func loaded successfully")
except Exception as e:
print(f"error loading rag memory func : {e}")'''
try:
prompt_template = prompt.context_q_prompt()
print("prompt_template loaded successfully")
except Exception as e:
print(f"error loading prompt_template : {e}")
# ConversationalRetrievalChain is depreceated
'''try:
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm_model,
retriever=ensemble_retriever,
memory=rag_memory,
prompt=prompt_template
)
print("conversation_chain loaded successfully")
except Exception as e:
print(f"error loading conversation_chain : {e}")'''
try:
chat_retriever_chain = create_history_aware_retriever(
llm = llm_model, retriever=ensemble_retriever, prompt=prompt_template
)
print("chat_retriever_chain loaded successfully")
except Exception as e:
print(f"error loading chat_retriever_chain : {e}")
"""response = chat_retriever_chain.invoke({"input": query})
"""
'''try:
print("generating response\n")
print(f"response generated successfully \n response : {response}")
except Exception as e:
print(f"error generating response : {e}")'''
return chat_retriever_chain
def hf_response(chat_retriever_chain,query):
chat_retriever_chain = chatbot_rag()
query = query.lower()
response = chat_retriever_chain.invoke({"input": query})
return response
def llm_answer(query , context):
llm_model = llm_utils.load_llm_hf(HUGGINGFACEHUB_API_TOKEN=keys.HUGGINGFACEHUB_API_TOKEN)
prompt = f'''"You are an intelligent assistant capable of answering complex questions using a combination of document retrieval and reasoning. The context provided contains important information extracted from documents, and your task is to use this context to generate a clear, accurate response to the user query.
Context: {context}
User Query: {query}
Based on the context, provide the most relevant and detailed answer to the user query. If the context doesn't have enough information, suggest the next steps or additional information that might be useful."'''
print(prompt)
response = llm_model.invoke(prompt)
return response
def gemini_response(chat_retriever_chain,query):
query = query.lower()
response = chat_retriever_chain.invoke({"input": query})
print('response :',response)
documents = response
response = {}
answer = []
for document in documents:
content = document.page_content.replace('\n', ' ')
content = content.replace('\t',' ')
answer.append(content)
answer = answer[:5]
template=f"Based on the following context:\n{answer}\nAnswer the question:\n{query}"
#print(f"answer : \n {answer}\n answer_len :{len(answer)}")
#llm_response = llm_answer(query=query , context= answer)
print("template : ",template)
genai.configure(api_key = "AIzaSyBKsyvw-c3WFy9tWncfiWIPTC1-e_5XPiQ")
model = genai.GenerativeModel("gemini-1.5-flash")
llm_response = model.generate_content(template)
print("llm_response",llm_response.text)
return llm_response
def ollama_response(chat_retriever_chain,query):
#setting the llm
llm_id = "deepseek-r1:1.5b"
query = query.lower()
response = chat_retriever_chain.invoke({"input": query})
print('response :',response)
documents = response
response = {}
answer = []
for document in documents:
content = document.page_content.replace('\n', ' ')
content = content.replace('\t',' ')
answer.append(content)
context = answer[:5]
template=f'''you are a chat bot helping students and parents for getting information about the college Based on the context and question given below.
try to complete each answer and be friendly as if you are a student guide.
keep the answers brief unless told to give large answers
documents = response
context:\n {context} \n
question:\n{query}'''
#print(f"answer : \n {answer}\n answer_len :{len(answer)}")
#llm_response = llm_answer(query=query , context= answer)
print("template : ",template)
#loadind llama model
llm = llm_utils.load_llm_ollama(llm_id=llm_id)
llm_response = llm.invoke(template)
print("\n \n llm_response :",llm_response)
return llm_response
if __name__ == "__main__":
# query = "What is Document testimonial about?"
chat_retriever_chain = chatbot_rag()
query = "empty"
while(query!="exit"):
query = input("enter your query :")
query = query.lower()
#response = gemini_response(chat_retriever_chain , query)
response = ollama_response(chat_retriever_chain , query)