yvann-ba / Robby-chatbot

AI chatbot πŸ€– for chat with CSV, PDF, TXT files πŸ“„ and YTB videos πŸŽ₯ | using Langchain🦜 | OpenAI | Streamlit ⚑
MIT License
766 stars 287 forks source link

how to have chat before query not after (in 2_πŸ“Š Robby-Sheet (beta).py)? #49

Closed al-yakubovich closed 9 months ago

al-yakubovich commented 1 year ago

Hi. Currently 2_πŸ“Š Robby-Sheet (beta).py has UI elements in the following order:

  1. Query
  2. Agent's thoughts
  3. Chat history
  4. Current dataframe

The problem with such a structure is that if a user asks a lot of questions, the chat history becomes too long, and to see each new answer, the user needs to scroll down and then go back to query. Is there a way to reorder it as:

  1. Chat history
  2. Query
  3. Agent's thoughts
  4. Current dataframe

I tried to do it on my own, but I am having a bug. When I provide my first query, no chat is shown. When I provide a second query, chat appears with the first query. If I provide a third chat, it appears with the first and second queries, and so on. I'm not sure how to fix it. My code:

if not user_api_key:
    layout.show_api_key_missing()
else:
    st.session_state.setdefault("reset_chat", False)

    uploaded_file = utils.handle_upload(["csv", "xlsx"])

    if uploaded_file:
        sidebar.about_()

        uploaded_file_content = BytesIO(uploaded_file.getvalue())
        if uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" or uploaded_file.type == "application/vnd.ms-excel":
            df = pd.read_excel(uploaded_file_content)
        else:
            df = pd.read_csv(uploaded_file_content)

        st.session_state.df = df

        if "chat_history" not in st.session_state:
            st.session_state["chat_history"] = []
        csv_agent = PandasAgent()

        form_submitted = False

        if st.session_state.df is not None:
            if form_submitted:
                result, captured_output = csv_agent.get_agent_response(df, query)
                cleaned_thoughts = csv_agent.process_agent_thoughts(captured_output)
                csv_agent.display_agent_thoughts(cleaned_thoughts)
                csv_agent.update_chat_history(query, result)
                csv_agent.display_chat_history()

        csv_agent.display_chat_history()

        with st.form(key="query"):
            query = st.text_input("", value="", type="default", 
                placeholder="e-g : How many rows ? "
                )
            submitted_query = st.form_submit_button("Submit")
            reset_chat_button = st.form_submit_button("Reset Chat")
            if reset_chat_button:
                st.session_state["chat_history"] = []
            if submitted_query:
                form_submitted = True

        if form_submitted:
            result, captured_output = csv_agent.get_agent_response(df, query)
            cleaned_thoughts = csv_agent.process_agent_thoughts(captured_output)
            csv_agent.display_agent_thoughts(cleaned_thoughts)
            csv_agent.update_chat_history(query, result)
            # csv_agent.display_chat_history()

        if st.session_state.df is not None:
            st.subheader("Current dataframe:")
            st.write(st.session_state.df)