import streamlit as st from streamlit_chat import message from langchain.chains import ConversationalRetrievalChain from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import LlamaCpp from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.vectorstores import FAISS from langchain.memory import ConversationBufferMemory from langchain.document_loaders import PyPDFLoader import os import tempfile from langchain.document_loaders import PyPDFDirectoryLoader from langchain.chains import RetrievalQA def initialize_session_state(): if 'history' not in st.session_state: st.session_state['history'] = [] if 'generated' not in st.session_state: st.session_state['generated'] = ["Hello! Ask me anything about 🤗"] if 'past' not in st.session_state: st.session_state['past'] = ["Hey! 👋"] def conversation_chat(query, chain, history): result = chain({"question": query, "chat_history": history}) history.append((query, result["answer"])) return result["answer"] def display_chat_history(chain): reply_container = st.container() container = st.container() with container: with st.form(key='my_form', clear_on_submit=True): user_input = st.text_input("Question:", placeholder="Ask about your PDF", key='input') submit_button = st.form_submit_button(label='Send') if submit_button and user_input: with st.spinner('Generating response...'): output = conversation_chat(user_input, chain, st.session_state['history']) st.session_state['past'].append(user_input) st.session_state['generated'].append(output) if st.session_state['generated']: with reply_container: for i in range(len(st.session_state['generated'])): message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="thumbs") message(st.session_state["generated"][i], key=str(i), avatar_style="fun-emoji") def create_conversational_chain(vector_store): # Create llm # Importing the Model llm = LlamaCpp( streaming = True, model_path = "model/mistral-7b-instruct-v0.2.Q4_K_M.gguf", temperature = 0.75, top_p = 1, verbose = True, n_ctx = 4096 ) memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True) chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff', retriever=vector_store.as_retriever(search_kwargs={"k": 2}), memory=memory) return chain def main(): # Initialize session state initialize_session_state() st.title("Multi-PDF ChatBot using Mistral-7B-Instruct :books:") # Initialize Streamlit # st.sidebar.title("Document Processing") # uploaded_files = st.sidebar.file_uploader("Upload files", accept_multiple_files=True) # Loading the file directory using PyPDF Directory loader = PyPDFDirectoryLoader("data_pdf/") data = loader.load() # Splitting extracted text data into chunks for easier processing text_splitter = RecursiveCharacterTextSplitter(chunk_size = 100000, chunk_overlap = 20) text_chunks = text_splitter.split_documents(data) # Downloading Embessings embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") # Creating Embeddings for each of the text chunk vector_store = FAISS.from_documents(text_chunks, embedding=embeddings) # Create the chain object chain = create_conversational_chain(vector_store) display_chat_history(chain) if __name__ == "__main__": main()