I have an app that is (presently) designed to be a ChatGPT clone using st.chat_input and st.chat_message. It has a dropdown of predefined prompts that the user could choose from, and once they select one from the dropdown, the prompt would go into the chat_input box automatically as if the user typed out that prompt manually (just like example prompts in ChatGPT). In order to do so, I inserted a piece of hacky JS code into the app (see below):
init_prompt = st.selectbox(
'You might want to try these prompts...',
['<Click Me to Expand>',
'How to socialize?',
'How to focus on tasks?',
'How to find peace in daily work?']
)
INIT_PROMPT_HTML = """
<script>
const doc = window.parent.document;
const dropdown = doc.querySelector('[data-baseweb="select"]');
const watcher = dropdown.firstChild.firstChild.firstChild;
const origSetAttr = watcher.setAttribute;
watcher.setAttribute = (key, value) => {
const input = doc.querySelector('[type="textarea"]'); // This is the chat_input element
input.click();
input.innerText = value;
origSetAttr.call(watcher, key, value);
};
</script>
"""
html(INIT_PROMPT_HTML)
I was able to update the chat_input element with the line input.innerText = value; everytime the user selects from the dropdown, causing its value to change. However, the change goes away almost instantly (e.g., the chat_input would hold the updated value for 1 second and then resume to its previous state). I doubt that streamlit somehow overwrote the change but couldn’t figure how it does that. Or maybe this is a XY problem?
It seems you’d like to use the selectbox to provide some starter prompts, however the st.chat_input() does not return these as entered strings and rather uses it as a placeholder text that serves as a visual display of text such as “Enter your text here”.
Rather, if you’re using your selectbox with st.text_input() this may accept the selectbox text as entered text.
Thus, perhaps you can use this instead:
import streamlit as st
init_prompt = st.selectbox(
'You might want to try these prompts...',
['<Click Me to Expand>',
'How to socialize?',
'How to focus on tasks?',
'How to find peace in daily work?']
)
st.text_input('Enter you text', init_prompt)
Hi, thank you for the help! However, with st.text_input, while it does put the starter prompt on, it automatically triggers a ChatGPT response when the user selects an item from the dropdown without them manually sending it out. Any ideas for addressing this issue?
Right now the code looks like this:
init_prompt = st.selectbox(
'You might want to try these prompts...',
['<Click Me to Expand>',
'How to socialize?',
'How to focus on tasks?',
'How to find peace in daily work?']
)
instr = 'Hi there! Enter what you want to let me know here.'
if prompt := st.text_input(
instr,
value=init_prompt,
placeholder=instr, # Instruct the user to enter sth
label_visibility='collapsed' # Hide the label
):
# Get the ChatGPT response and put it on the screen (this is auto-triggered
# whenever the user chooses something from the dropdown, which is unwanted)
Hi,
You can use st.form() and add a Submit button. This would allow the LLM generated response to occur only after the initial prompt is selected and the Submit button is clicked.
You could also use the disabled parameter of st.text_input() so that the text input box is deactivated and after the initial prompt is selected, this would trigger the activation of the text_input.
Hi, I think I got it working, thanks a lot! One final question: Right now the form submit button is below rather than horizontally next to the input, which is a little strange for a Chat app. Is there any way to have st.text_input and st.form_submit_button in the same line?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.