Hi! I am trying to create a game that allows users to respond to questions asked by a gpt powered source. I want streamlit to wait for the response/input before running through the rest of the code/rerunning completely. The source takes in their answer and uses that to ask more questions. Right now what is happening is that streamlit keeps rerunning even when some fields are blank/haven’t been answered yet. I tried to fix the situation by making the answer textboxes forms, but that still didn’t work. I also tried using time() and a pause function to no avail. Do you have any advice for how to make streamlit operate like terminal, where it waits for my user’s response before moving on to the next parts of the code?
For example, I want to have my user introduce themselves. So I call this function:
you can use the st.text_input and st.button functions instead of the form submission method
intro_dialogue = st.text_input(label='Introduce yourself')
submit_button = st.button(label='Submit')
if submit_button and intro_dialogue:
intro = run_intro_completion()
if intro is not None:
I’ve just completed a similar type of project where the user has a chat session-- in my particular case I’m prompting the AI in such a way that I can extract questions or other commands from the response. When I detect those questions, I dynamically create the correct number of textarea inputs inside a form element. When the user submits I confirm I have all the answers I need (or cards drawn – in this particular case my app is a AI tarot reading).
From there I add the user’s message to the chat history, run it through the Open AI content moderation API endpoint (so I can terminate the session and show mental health resources if something flagged is input) and finally generate a new API response, which starts the loop over:
I’m pretty happy with this flow right now though I’ll be moving more of the logic into the ChatSession class for my next iteration of this.
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.
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.
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.
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.