MFAI-Developer/ComsatsBot
0
1import uuid2from fastapi import FastAPI, HTTPException3from pydantic import BaseModel4from pymongo import MongoClient5import urllib.parse6import os7from langchain_groq import ChatGroq8from langchain_community.embeddings import HuggingFaceBgeEmbeddings9from chatbot import Comsatsbot10# FastAPI app setup11app = FastAPI()12 13model_name = "BAAI/bge-small-en"14model_kwargs = {"device": "cpu"}15encode_kwargs = {"normalize_embeddings": True}16hf = HuggingFaceBgeEmbeddings(17 model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs18)19 20os.environ['GROQ_API_KEY'] = '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'21api_key = os.environ.get("GROQ_API_KEY")22llm = ChatGroq(temperature=0, groq_api_key=api_key, model_name="llama3-70b-8192")23 24# MongoDB setup25username = 'hasnainnaseer987'26password = 'Hasnain'27encoded_username = urllib.parse.quote_plus(username)28encoded_password = urllib.parse.quote_plus(password)29MONGODB_ATLAS_CLUSTER_URI = f'mongodb+srv://{encoded_username}:{encoded_password}@cluster0.jdfp3.mongodb.net/'30client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)31db = client.get_database('chat_db') # Assume this is your database32chats_collection = db.get_collection('chats') # Collection to store chats33paths = ['FYP Supervisor Feedback.csv', 'urdu_data.csv', 'english_data.csv']34 35chatbot = Comsatsbot(hf, llm, api_key, chats_collection, paths)36 37# Endpoint for creating a new chat ID38@app.post("/get_new_chat")39def create_new_chat():40 try:41 chat_id = str(uuid.uuid4())42 message = chatbot.new_chat(chat_id)43 return {"chat_id": chat_id, "message": "Successfully created new chat."}44 except KeyError:45 raise HTTPException(status_code=404, detail="Chat ID already exist try again plz...")46 except Exception as e:47 raise HTTPException(status_code=500, detail=str(e))48 49 50# Request model for response endpoint51class ChatRequest(BaseModel):52 chat_id: str53 question: str54 55# Endpoint for retrieving chat history by chat ID56@app.get("/get_chat/{chat_id}")57def get_chat(chat_id: str):58 try:59 history = chatbot.load_chat(chat_id)60 return {"chat_id": chat_id, "history": history}61 except KeyError:62 raise HTTPException(status_code=404, detail="Chat ID not found")63 except Exception as e:64 raise HTTPException(status_code=500, detail=str(e))65 66 67# Endpoint for deleting a chat by chat ID68@app.delete("/delete_chat/{chat_id}")69def delete_chat(chat_id: str):70 try:71 message = chatbot.delete_chat(chat_id)72 return {"message": f"Chat with ID {chat_id} has been deleted successfully."}73 except KeyError:74 raise HTTPException(status_code=404, detail="Chat ID not found")75 except Exception as e:76 raise HTTPException(status_code=500, detail=str(e))77 78 79# Endpoint for getting a response based on chat ID and question80@app.post("/response")81def response(request: ChatRequest):82 chat_id = request.chat_id83 question = request.question84 85 try:86 answer = chatbot.response(question, chat_id)87 88 return {"answer": answer}89 except KeyError:90 raise HTTPException(status_code=404, detail="Chat ID not found")91 except Exception as e:92 raise HTTPException(status_code=500, detail="GPU Memory Over Loading Please Try Again After Some Time")