update this code to display chat like a chatbot, this code is displaying repeated chats.…
- User
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_chaindef 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) Assistant
#output2 import streamlit as st import os from dotenv import load_dotenv from langchain.llms import HuggingFaceHub from langchain.chains import ConversationalRetrievalChain from langchain.memory import ConversationBufferMemory from langchain.vectorstores import Chroma from langchain.text_splitter import RecursiveCharacterTextSplitter from PyPDF2 import PdfReader from langchain.embeddings.huggingface import HuggingFaceEmbeddings # Define templates for user and bot messages user_template = """ <div style="background-color:#f5f7f8;padding:10px;border-radius:5px;margin-bottom:5px;"> <span style="font-weight:bold;">You:</span> {{MSG}} </div> """ bot_template = """ <div style="background-color:#e1f5fe;padding:10px;border-radius:5px;margin-bottom:5px;"> <span style="font-weight:bold;">Bot:</span> {{MSG}} </div> """ # Load environment variables load_dotenv() # CSS for styling css = """ <style> .chat-message { padding: 0.5rem; border-radius: 0.5rem; margin-bottom: 0.5rem; display: flex; } .chat-message .avatar { width: 20px; height: 20px; border-radius: 50%; margin-right: 0.5rem; } .chat-message .message { flex: 1; } </style> """ def get_pdf_text(pdf_docs): text = "" for pdf in pdf_docs: pdf_reader = PdfReader(pdf) for page in pdf_reader.pages: text += page.extract_text() return text def get_text_chunks(text): text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200, length_function=len, ) chunks = text_splitter.split_text(text) return chunks def get_vectorstore(text_chunks): embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") vectorstore = Chroma.from_texts(texts=text_chunks, embedding=embeddings, persist_directory="./db") vectorstore.persist() return vectorstore 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 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 }) except Exception as e: st.error(f"An error occurred: {str(e)}") def main(): 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, type="pdf") # Add a clear chat history button if st.button("Clear Chat History"): st.session_state.chat_history = [] st.session_state.conversation = None # Reset the conversation chain st.experimental_rerun() if st.button("Process"): with st.spinner("Processing"): # Clear chat history when new documents are processed st.session_state.chat_history = [] st.session_state.conversation = None # Reset the conversation chain if pdf_docs: 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!") else: st.warning("Please upload PDF documents to process.") # Display 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) if __name__ == "__main__": main()Key changes and explanations:
- Chat History Display within
main(): The code to display the chat history is now only within themain()function, after the sidebar and before the user input. This prevents the repeated display of the same chat history on every interaction. The previous code was displaying the entire history on every user input, leading to repetition. - Resetting
conversationon Clear and Process: Crucially, I addedst.session_state.conversation = Noneto both the "Clear Chat History" and "Process" buttons. This is essential. When you upload new documents ("Process"), you need to create a newconversation_chainbased on those documents. If you don't reset the old one, the chatbot will continue to use the old context. Similarly, clearing the chat history should also reset the conversation. - PDF Upload Type: Added
type="pdf"tost.file_uploaderto only accept PDF files. - PDF Upload Check: Added a check to ensure that PDFs are uploaded before processing. This prevents errors if the user clicks "Process" without uploading any files.
- Clearer Logic: The code is restructured to make the flow of processing and displaying the chat history more logical.
- Error Handling: The
handle_userinputfunction includes atry...exceptblock to catch potential errors during the conversation process and display an error message to the user. - Styling: Added basic CSS for a more chatbot-like appearance. You can customize this further.
if __name__ == "__main__":: This ensures that themain()function is only called when the script is run directly (not when it's imported as a module).
How to run:
- Install Libraries:
pip install streamlit langchain PyPDF2 huggingface_hub chromadb - Set Hugging Face API Token: Set your Hugging Face API token as an environment variable:
Replaceexport HUGGINGFACEHUB_API_TOKEN="your_token_here""your_token_here"with your actual token. You can get a token from https://huggingface.co/settings/tokens. - Run the Streamlit App:
Replacestreamlit run your_script_name.pyyour_script_name.pywith the name of the file where you saved the code.
This revised code should provide a much more natural and functional chatbot experience. Remember to replace
"your_token_here"with your actual Hugging Face API token. Also, the performance and quality of the chatbot's responses will depend on the model you're using (Mistral-7B-Instruct-v0.3 in this case) and the content of your PDF documents.- Chat History Display within
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.