Adding Forms to a Chatbot

Has anyone had success in implementing forms into a streamlit chat? I am using an LLM to return the parameters, then creating a form for the user to fill out. I have successfully saved the form to session state and can regenerate the form on refresh, but the text_inputs are not being recorded. I have tried saving the names and values (in a dict) to session state and locally. Let me know, thanks.

def build_form(message):
    body = message["body"]
    params = message["content"]
    with st.chat_message("assistant"):
            with st.form(key="form"):
                for name, value in params.items():
                    params[name] = st.text_input(label=name, value=value, key=name)
                st.form_submit_button("Submit", on_click=save_form, args=[params, body])

def save_form(params, body):
    st.session_state.all_messages.append(
        {
            "role": "assistant", 
            "content": params,
            "type": "form",
            "request_body": body,
        }
    )

The return values are not available during the callback, so you still have the old values in params. You have the new values in session state, though. Something like tthis should work (not tested):

"content": {key: st.session_state[key] for key in params.keys()}

It worked! Thank you!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.