update this code to display chat like a chatbot, this code is displaying repeated chats. I should be like I am chatting with a person:


#output2
def handle_userinput(user_question):
    try:
        response = st.session_state.conversation({
            'question': user_question
        })
        
        # Extract the answer
        answer = response['answer']
        
        # Add to chat history
        if 'chat_history' not in st.session_state:
            st.session_state.chat_history = []
            
        st.session_state.chat_history.append({
            "question": user_question,
            "answer": answer
        })
        
        # Display entire chat history
        for chat in st.session_state.chat_history:
            st.write(user_template.replace(
                "{{MSG}}", chat["question"]), unsafe_allow_html=True)
            st.write(bot_template.replace(
                "{{MSG}}", chat["answer"]), unsafe_allow_html=True)
            
    except Exception as e:
        st.error(f"An error occurred: {str(e)}")

def get_conversation_chain(vectorstore):
    model_name = "mistralai/Mistral-7B-Instruct-v0.3"
    token = os.environ['HUGGINGFACEHUB_API_TOKEN']
    
    llm = HuggingFaceHub(
        repo_id=model_name,
        model_kwargs={
            "temperature": 0.5,
            "max_new_tokens": 512,
            "return_full_text": False
        },
        huggingfacehub_api_token=token
    )
    
    memory = ConversationBufferMemory(
        memory_key='chat_history',
        return_messages=True,
        output_key='answer'
    )
    
    conversation_chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vectorstore.as_retriever(),
        memory=memory,
        return_source_documents=True,
        chain_type="stuff"
    )
    
    return conversation_chain

def main():
    load_dotenv()
    st.set_page_config(page_title="Chat with multiple PDFs",
                       page_icon=":books:")
    st.write(css, unsafe_allow_html=True)

    # Initialize session state variables
    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 = []

    st.header("Chat with multiple PDFs :books:")
    
    with st.sidebar:
        st.subheader("Your documents")
        pdf_docs = st.file_uploader(
            "Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
        
        # Add a clear chat history button
        if st.button("Clear Chat History"):
            st.session_state.chat_history = []
            st.experimental_rerun()
            
        if st.button("Process"):
            with st.spinner("Processing"):
                # Clear chat history when new documents are processed
                st.session_state.chat_history = []
                
                raw_text = get_pdf_text(pdf_docs)
                text_chunks = get_text_chunks(raw_text)
                vectorstore = get_vectorstore(text_chunks)
                st.session_state.conversation = get_conversation_chain(vectorstore)
                st.success("Documents processed successfully!")

    # Display existing chat history
    for chat in st.session_state.chat_history:
        st.write(user_template.replace(
            "{{MSG}}", chat["question"]), unsafe_allow_html=True)
        st.write(bot_template.replace(
            "{{MSG}}", chat["answer"]), unsafe_allow_html=True)

    # User input
    user_question = st.text_input("Ask a question about your documents:")
    if user_question:
        handle_userinput(user_question)