-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
380 lines (299 loc) · 10.9 KB
/
main.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
import uvicorn
from langchain_community.vectorstores import FAISS
from langchain_community.vectorstores.utils import DistanceStrategy
from langchain_huggingface import HuggingFaceEmbeddings
import faiss
import pickle
from langchain_community.docstore.in_memory import InMemoryDocstore
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from fastapi.templating import Jinja2Templates
import datetime
import uuid
from pymongo import MongoClient
force_download = True
torch.random.manual_seed(0)
app = FastAPI()
KNOWLEDGE_VECTOR_DATABASE = None
RAG_PROMPT_TEMPLATE = None
pipe = None
generation_args = None
mongo_client = None
db = None
chats_collection = None
user_collection = None
RAG_PROMPT_TEMPLATE1 = None
RAG_PROMPT_TEMPLATE2 = None
@asynccontextmanager
async def lifespan(app: FastAPI):
print("Initializing models and vector database... from app.main")
init()
yield
print("Cleaning up resources...")
app = FastAPI(lifespan=lifespan)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="static")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def init():
global KNOWLEDGE_VECTOR_DATABASE, RAG_PROMPT_TEMPLATE, pipe, generation_args, mongo_client, db, chats_collection, RAG_PROMPT_TEMPLATE1, RAG_PROMPT_TEMPLATE2, user_collection
mongo_client = MongoClient("mongodb://localhost:27017/")
db = mongo_client["chat_db"]
chats_collection = db["chat_sessions"]
user_collection = db["user_session"]
embedding_model = HuggingFaceEmbeddings(
model_name="thenlper/gte-small",
multi_process=True,
model_kwargs={"device": "cuda:0"},
encode_kwargs={"normalize_embeddings": True},
)
index = faiss.read_index("models/faiss_index_LOL.bin")
with open("models/faiss_metadata_LOL.pkl", "rb") as f:
metadata = pickle.load(f)
docstore = InMemoryDocstore(metadata['docstore'])
index_to_docstore_id = metadata['index_to_docstore_id']
KNOWLEDGE_VECTOR_DATABASE = FAISS(
index=index,
docstore=docstore,
index_to_docstore_id=index_to_docstore_id,
embedding_function=embedding_model
)
print("Models and vector database initialized")
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3.5-mini-instruct", # microsoft/Phi-3-mini-128k-instruct
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
prompt_chat1 = [
{
"role": "system",
"content": """Your are an helpful AI assistant, Using the information contained in the context,
greet the user and Give a comprehensive answer to the Question.
Respond only to the Question asked, response should be concise and relevant to the question.""",
},
{
"role": "user",
"content": """Context:
{context}
---
Now here is the Question you need to answer.
Question:{question}
""",
},
]
prompt_chat2 = [
{
"role": "system",
"content": """You are helpful AI assistant,
assistant is unable to answer the question given by user. inform the user that you cannot answer the question politely and inform the user to ask questions regarding the transport services only.""",
},
{
"role": "user",
"content": """
Now here is the Question.
Question:{question}
""",
},
]
RAG_PROMPT_TEMPLATE1 = tokenizer.apply_chat_template(
prompt_chat1, tokenize=False, add_generation_prompt=True,
)
RAG_PROMPT_TEMPLATE2 = tokenizer.apply_chat_template(
prompt_chat2, tokenize=False, add_generation_prompt=True,
)
print("Microsoft Phi-3 model initialized")
def Retrival_Augmentation(query):
global KNOWLEDGE_VECTOR_DATABASE
# user_query = query
# retrieved_docs = KNOWLEDGE_VECTOR_DATABASE.similarity_search(query=user_query, k=1)
#
# print("======================================\n")
# print(retrieved_docs[0].page_content)
# print("======================================\n")
#
# return retrieved_docs[0].page_content
user_query = query
args = {'score_threshold': 0.70}
retrieved_docs = KNOWLEDGE_VECTOR_DATABASE.similarity_search_with_relevance_scores(user_query, k=3, **args)
if len(retrieved_docs) == 0:
print("no docs retrived")
return ""
print("======================================\n")
print(retrieved_docs[:][:])
print("======================================\n")
result = ""
for i, j in retrieved_docs:
result += i.page_content + "\n"
return result
def generate_answer(context, question):
global RAG_PROMPT_TEMPLATE, pipe, generation_args, RAG_PROMPT_TEMPLATE1, RAG_PROMPT_TEMPLATE2
# final_prompt = RAG_PROMPT_TEMPLATE.format(
# question="greet me by saying hello and answer the question." + question, context=context
# )
#
# output = pipe(final_prompt, **generation_args)
# return output[0]['generated_text']
if (context == ""):
final_prompt = RAG_PROMPT_TEMPLATE2.format(
question="greet me by saying hello and answer the question." + question
)
else:
final_prompt = RAG_PROMPT_TEMPLATE1.format(
question="greet me by saying hello and answer the question." + question, context=context
)
output = pipe(final_prompt, **generation_args)
# Free up GPU memory
torch.cuda.empty_cache()
torch.cuda.synchronize()
return output[0]['generated_text']
def delete_empty_sessions():
try:
result = chats_collection.delete_many({"messages": []})
return result.deleted_count
except Exception as e:
print(f"Failed to delete empty sessions: {str(e)}")
return 0
@app.post("/start_session")
async def start_session(request: Request):
# deleted_count = delete_empty_sessions()
# print(f"Deleted {deleted_count} empty sessions before starting a new session")
#
# session_id = str(uuid.uuid4())
# chats_collection.insert_one({
# "session_id": session_id,
# "messages": [],
# "created_at": datetime.datetime.now()
# })
# return {"session_id": session_id}
data = await request.json()
UID = data.get("UID")
deleted_count = delete_empty_sessions()
print(f"Deleted {deleted_count} empty sessions before starting a new session")
session_id = str(uuid.uuid4())
# Create a new session document
chats_collection.insert_one({
"UID": UID,
"session_id": session_id,
"messages": [],
"created_at": datetime.datetime.now()
})
return {"session_id": session_id}
@app.post("/registration")
async def register_user(request: Request):
data = await request.json()
name = data.get("name")
email = data.get("email")
password = data.get("password")
if not name or not email or not password:
raise HTTPException(status_code=400, detail="Name, email, and password are required")
UID = str(uuid.uuid4())
user = {
"UID": UID,
"name": name,
"email": email,
"password": password,
"created_at": datetime.datetime.now()
}
user_collection.insert_one(user)
return {"message": "User registered successfully"}
# users_collection = db["users"]
# users_collection.insert_one(user)
# return {"message": "User registered successfully"}
@app.post("/login")
async def login_user(request: Request):
data = await request.json()
username = data.get("name")
password = data.get("password")
print(username)
if not username or not password:
raise HTTPException(status_code=400, detail="Email and password are required")
user = user_collection.find_one({"name": username, "password": password})
if user:
return {"message": "Login successful", "UID": user["UID"]}
else:
raise HTTPException(status_code=401, detail="Invalid credentials")
@app.post("/query")
async def receive_query(request: Request):# here query is receieved.
data = await request.json()
query = data.get("query")
session_id = data.get("session_id")
print(session_id)
if not session_id:
raise HTTPException(status_code=400, detail="session_id is required")
print(f"Received query: {query}")
context = Retrival_Augmentation(query)#RAG
answer = generate_answer(context, query)# model generates answer.
print(answer)
chat = {
"user": query,
"context": context,
"chatbot": answer,
"timestamp": datetime.datetime.now()
}# gets saved in mongoDB.
chats_collection.update_one(
{"session_id": session_id},
{"$push": {"messages": chat}}
)
return {"response": answer}
@app.post("/chats")
async def get_chats(request: Request):
# try:
# chats = list(chats_collection.find())
# for chat in chats:
# chat["_id"] = str(chat["_id"])
# return chats[::-1]
# except Exception as e:
# raise HTTPException(status_code=500, detail=f"Failed to fetch chats: {str(e)}")
try:
data = await request.json()
UID = data.get("UID")
chats = list(chats_collection.find({"UID": UID}))
# Convert ObjectId to string for JSON serialization
for chat in chats:
chat["_id"] = str(chat["_id"])
return chats[::-1]
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to fetch chats: {str(e)}")
@app.get("/query")
async def get_query():
# return 200
return {"response": "Hello"}
@app.get("/", response_class=HTMLResponse)
async def get(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/hello")
async def get_name():
return {"message": "Hello"}
@app.get("/chats/{session_id}")
async def get_chats(session_id: str):
session = chats_collection.find_one({"session_id": session_id})
session["_id"] = str(session["_id"])
if session:
return session
else:
raise HTTPException(status_code=404, detail="Session not found")
# if _name_ == "_main_":
# uvicorn.run(app, host="0.0.0.0", port=8000)