How to clear text_input and fix it in place so user does not scrool up

I got code from following repo: GitHub - alejandro-ao/ask-multiple-pdfs: A Langchain app that allows you to chat with multiple PDFs

The purpose of code is to chat with documents, I have modified some aspects of it to soothe my needs better such as load all files from one folder etc…

The issue I am having is my lack of experience in streamlit.

What I am trying are two things:

  1. After question has been asked clear st.text_input.
  2. In longer chats user will have to scroll to top to enter new question since textbox remains on top while chat goes down, unsure how to fix that issue in streamlit.

Here is the code:

def handle_userinput(user_question):
    if not st.session_state.conversation:
        st.warning("Index not ready. Please wait a moment and try again.")
        return
    preface = "You are a helpful assistant: "
    bot_question = preface + user_question

    response = st.session_state.conversation({'question': bot_question})
    st.session_state.chat_history = response['chat_history']

    for i, message in enumerate(st.session_state.chat_history):
        if i % 2 == 0:  # User's message
            # Display only the original user's question without preface
            st.write(user_template.replace("{{MSG}}", user_question), unsafe_allow_html=True)
        else:  # Bot's response
            st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)

def main():
    user_question = st.text_input("Ask a question:")
    if user_question:
        handle_userinput(user_question)

Consider using chat_input() instead.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.