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
Here's the updated code to display chat like a chatbot without repeating messages:
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 }) # No need to display chat history here as it's displayed in main() 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!") # Create a container for chat display chat_container = st.container() # User input at the bottom user_question = st.text_input("Ask a question about your documents:") if user_question: handle_userinput(user_question) # Force a rerun to update the display after adding new messages st.experimental_rerun() # Display chat history in the container (this will show above the input) with chat_container: 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)Would you like me to explain or break down the changes I made to the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.