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:
However, it keeps saying that my intro_dialogue variable is Nonetype, because streamlit continues the execution without waiting for the submit button tobe pressed/the intro field to be filled out.
you can use the st.text_input and st.button functions instead of the form submission method
def run_intro_completion():
intro_dialogue = st.text_input(label='Introduce yourself')
submit_button = st.button(label='Submit')
if submit_button and intro_dialogue:
return 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.