Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make st.chat_input show up at the bottom even if used in a tab #8564

Open
3 of 4 tasks
XinyueZ opened this issue Apr 25, 2024 · 4 comments
Open
3 of 4 tasks

Make st.chat_input show up at the bottom even if used in a tab #8564

XinyueZ opened this issue Apr 25, 2024 · 4 comments
Labels
feature:st.chat_input type:enhancement Requests for feature enhancements or new features

Comments

@XinyueZ
Copy link

XinyueZ commented Apr 25, 2024

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

The code shown in https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps
will not work when we use st.tabs to use a tab to host chat ui.

The st.chat_input goes to top instead bottom.

Reproducible Code Example

import os
import streamlit as st
from openai import OpenAI

tab1, tab2 = st.tabs(["Dashboard", "Chat"])
with tab1:
    st.write("Dashboard")
with tab2:
    key_from_export = os.getenv("OPENAI_API_KEY")
    client = OpenAI(api_key=key_from_export)
    if "openai_model" not in st.session_state:
        st.session_state["openai_model"] = "gpt-3.5-turbo"
    if "messages" not in st.session_state:
        st.session_state.messages = []
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])
    if prompt := st.chat_input("What is up?"):
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)
        with st.chat_message("assistant"):
            stream = client.chat.completions.create(
                model=st.session_state["openai_model"],
                messages=[
                    {"role": m["role"], "content": m["content"]}
                    for m in st.session_state.messages
                ],
                stream=True,
            )
            response = st.write_stream(stream)
        st.session_state.messages.append({"role": "assistant", "content": response})

Steps To Reproduce

Just run this

Expected Behavior

No response

Current Behavior

No response

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version:
  • Python version:
  • Operating System:
  • Browser:

Additional Information

No response

@XinyueZ XinyueZ added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels Apr 25, 2024
Copy link

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

@jrieke jrieke added type:enhancement Requests for feature enhancements or new features and removed type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Apr 25, 2024
Copy link

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@jrieke jrieke changed the title st.chat_input goes to top when in tab Make st.chat_input show up at the bottom even if used in a tab Apr 25, 2024
@jrieke
Copy link
Collaborator

jrieke commented Apr 25, 2024

Hey! That's expected behavior. If you're using the chat input in a container (e.g. st.tabs) it will show up inline like any other element. Since you're creating the chat input before the chat messages, it will show up above them.

We will soon add a position parameter that lets you change that behavior manually. So I'm going to convert this to a feature request and adapt the title.

Right now the only ways to work around this are:

  1. Create the chat input outside of the tab. Then it should show up at the bottom of the app, just like in that example. Note that it will only show up on tab 1 though (since it's outside of the normal layout flow).
tab1, tab2 = st.tabs(["Dashboard", "Chat"])
prompt = st.chat_input(...)

with tab1:
   ...
with tab2:
   ...
  1. Put the chat messages in a container above the chat input.
tab1, tab2 = st.tabs(["Dashboard", "Chat"])

with tab1:
   ...
with tab2:
   ...
   chat_container = st.container(height=300)
   prompt = st.chat_input("What is up?")
   ...
   # and replace every `st.chat_message` call with `chat_container.chat_message` 
   # so the chat messages show up in the container
   

@XinyueZ
Copy link
Author

XinyueZ commented Apr 26, 2024

@jrieke
Thank you, but I have to say, putting it at the top is totally anti-UX, which means this should not be the so-called "expected behavior", but still thank you for your reply and info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.chat_input type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

2 participants