File size: 5,118 Bytes
ef5b171
 
 
 
 
c483373
ef5b171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4341309
ef5b171
 
 
 
 
 
 
 
 
4341309
ef5b171
 
4341309
ef5b171
 
 
 
 
 
 
 
 
790f2e6
ef5b171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebee7b0
4341309
ef5b171
 
 
 
 
 
 
 
 
 
 
 
790f2e6
71fc0b2
ef5b171
 
 
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
import streamlit as st  #Web App
import numpy as np #Image Processing 
import pandas as pd

import time

import os
import tiktoken
from io import StringIO
import time
import json

import requests
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter

#from langchain_community.embeddings import OpenAIEmbeddings
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from dotenv import load_dotenv,find_dotenv

#from langchain_community.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain

from dotenv import load_dotenv
from htmlTemplates import bot_template, user_template, css

load_dotenv()
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

def load_knowledgeBase():
    embeddings=OpenAIEmbeddings(api_key=OPENAI_API_KEY)
    DB_FAISS_PATH = "./vectorstore/db_faiss/"
    db = FAISS.load_local(
            DB_FAISS_PATH, 
            embeddings, 
            allow_dangerous_deserialization=True,
            index_name="njmvc_Index"
        )   
    return db
def load_prompt():
    prompt = """ You are helping students to pass NJMVC Knowledge Test. Provide a Single multiple choice question with 4 options to choose from.
    Use the information from context provided below to provide the question and answer choices.
    context = {context}
    question = {question}
    if the context is not available, say I cannot give Question"
        """
    prompt = ChatPromptTemplate.from_template(prompt)
    return prompt

#function to load the OPENAI LLM
def load_llm():
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, api_key=OPENAI_API_KEY)
    return llm

#knowledgeBase=load_knowledgeBase()
prompt = load_prompt()
llm=load_llm()

def get_conversation_chain(vectorstore, llm):
        llm = llm
        #llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})

        memory = ConversationBufferMemory(memory_key="chat_history")
        conversation_chain = ConversationChain(
                llm=llm,
                verbose=True,
                memory=ConversationBufferMemory(),
        )
        return conversation_chain

def format_docs(docs):
        return "\n\n".join(doc.page_content for doc in docs)

def get_pdf_text(pdf_files):
    
    text = ""
    for pdf_file in pdf_files:
        reader = PdfReader(pdf_file)
        for page in reader.pages:
            text += page.extract_text()
    return text

def get_chunk_text(text):
    text_splitter = CharacterTextSplitter(
    separator = "\n",
    chunk_size = 1000,
    chunk_overlap = 200,
    length_function = len
    )
    chunks = text_splitter.split_text(text)
    return chunks

def handle_user_input(question):
        response = st.session_state.conversation({'question':question})

        st.session_state.chat_history = response['chat_history']

        for i, message in enumerate(st.session_state.chat_history):
                if i % 2 == 0:
                        st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
                else:
                        st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)

def main():
    st.set_page_config(page_title='NJMVC Knowledge Test with RAGAS', page_icon=':cars:')

    st.write(css, unsafe_allow_html=True)

    if "conversation" not in st.session_state:
        st.session_state.conversation = None

    if "chat_history" not in st.session_state:
        st.session_state.chat_history = None

    st.header('NJMVC Knowledge Test with RAGAS :cars:')
    question = st.text_input("Input the Topic you want to test your knowledge: ")

    if question:
        #handle_user_input(question)

        with st.spinner("Get ready..."):  
            text_chunks = get_chunk_text(question)
            
            db = FAISS.load_local(folder_path="./vectorstore/db_faiss/",embeddings=OpenAIEmbeddings(api_key=OPENAI_API_KEY),allow_dangerous_deserialization=True, index_name="njmvc_Index")
            searchDocs = db.similarity_search(question)

            similar_embeddings=FAISS.from_documents(documents=searchDocs, embedding=OpenAIEmbeddings(api_key=OPENAI_API_KEY))
            #creating the chain for integrating llm,prompt,stroutputparser
            retriever = similar_embeddings.as_retriever()
            rag_chain = (
                    {"context": retriever | format_docs, "question": RunnablePassthrough()}
                    | prompt
                    | llm
                    | StrOutputParser()
            )
            #st.session_state.conversation = get_conversation_chain(vector_store)
            response=rag_chain.invoke(question)
            st.write(response)
            st.write(searchDocs)

if __name__ == '__main__':
    main()