Streamlit is a Python framework for rapidly prototyping interactive web interfaces, especially machine learning dashboards and chat applications. It produces a modern React-powered interface and supports quick cloud or standalone deployment. section 7.2
A practical Streamlit chat application follows this flow:
- Define interface elements with the st. prefix, including titles, chat messages, inputs, and spinners. section 7.2.1
- Store the model choice and conversation messages in st.session_state, which lasts only for the browser session. section 7.2.1
- Handle submitted prompts, call the API, display the response, and append both sides of the conversation to message history. section 7.2.1
Streaming chat
A responsive chat interface passes stream=True to the API and sends the result to st.write_stream, displaying generated tokens in real time instead of waiting for the complete response. section 7.2.2
Listing 7.4 chatgpt_clone_response.py (bottom section)
if prompt := st.chat_input("What do you need?"): #1
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"): #2
st.markdown(prompt)
with st.spinner(text="The assistant is thinking..."): #3
with st.chat_message("assistant"):
response = client.chat.completions.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
], #4
)
response_content = response.choices[0].message.content
response = st.markdown(response_content,
unsafe_allow_html=True) #5
st.session_state.messages.append(
{"role": "assistant", "content": response_content}) #6
The listing shows prompt submission, session-history updates, a waiting spinner, API completion, and response rendering in one runnable interaction. section 7.2.1








