A chatbot needs a place for people to send messages. `st.chat_input` creates a text box with a Send icon, pinned to the bottom of the screen or its containing widget. Unlike `st.text_input`, it returns a message only when the user clicks Send or presses Enter—not when they click away. section 10.2.3
Send a message to a bot
With a `bot` already available, collect the message, send it to the backend, and show both sides of the exchange: ```python human_message_text = st.chat_input("Chat with me!") if human_message_text: st.chat_message("human").markdown(human_message_text) ai_message_text = bot.chat(human_message_text) st.chat_message("ai").markdown(ai_message_text) ``` The condition keeps the response code tied to a submitted message. `st.chat_message` provides the corresponding human and AI display containers. section 10.2.3
Listing 10.4 A simple chatbot frontend in frontend.py
import streamlit as st
from bot import Bot
if "bot" not in st.session_state:
api_keys = st.secrets["api_keys"]
st.session_state.bot = Bot(api_keys)
bot = st.session_state.bot
if human_message_text := st.chat_input("Chat with me!"):
st.chat_message("human").markdown(human_message_text)
ai_message_text = bot.chat(human_message_text)
st.chat_message("ai").markdown(ai_message_text)
The complete small frontend also stores the bot in `st.session_state` and obtains its API keys from `st.secrets`. section 10.2.3
