Skip to main content
In this section, you will learn how to wrap your conversational chat loop in a fully functional web-based user interface using Streamlit and LangChain core abstractions.

Objectives

  1. Configure session state memory to retain message history across page reruns.
  2. Render user and assistant chat message containers.
  3. Hook Streamlit chat inputs to LangChain’s invocation pipeline.

Streamlit Chatbot App

Goal

Build a graphical web application where users can type messages and receive chat replies from a Groq-provided Llama model in real-time.

User Interface Layout

  1. Title Banner: Displays a header ”💬 LangChain Streamlit Chatbot”.
  2. Chat Window: Shows user queries in a chat box on the right, and AI assistant answers on the left.
  3. Chat Input Field: Pinned to the bottom of the screen for user inputs.

Plan

  1. Import streamlit, init_chat_model, and message schemas.
  2. Initialize the chat model using init_chat_model("llama-3.3-70b-versatile", model_provider="groq").
  3. Check and initialize st.session_state.messages list with a default SystemMessage.
  4. Loop through existing session messages to render them in containers using st.chat_message.
  5. Capture user inputs using st.chat_input, append to history, invoke the model, render the response, and append the response back to history.

Code Implementation

Let’s build the Streamlit application incrementally step-by-step:

Step 1: Imports and Setup

We start by importing streamlit, the unified init_chat_model initializer, and message schemas. We then load environment variables:

Step 2: Initialize Model and Message State

We set up page headers and check if our model and chat history list exist in st.session_state. This ensures our state objects persist across page reruns:

Step 3: Render Message History

We loop through st.session_state.messages and render human queries and AI answers inside Streamlit’s native bubble containers (st.chat_message). We skip rendering the SystemMessage:

Step 4: Handle Chat Input and Response

We query a text box using st.chat_input. When the user enters a prompt, we render it, append it to st.session_state.messages, query the model with the entire history, display the response inside a spinner, and append the reply to history.
[!NOTE] The Walrus Operator (:=): The syntax if prompt := st.chat_input("What is on your mind?"): uses Python’s assignment expression (walrus operator) to:
  • Assign the user’s input string returned by st.chat_input directly to the prompt variable.
  • Evaluate the condition; if the input is not empty/submitted, the condition resolves to True and executes the code block. If no input is submitted, it resolves to False and skips execution.

Step 5: Complete Application Code

Combining all the steps above gives the final completed script:

Exercise: Custom System Prompt Selector 🎨

Goal

Add a sidebar dropdown (st.sidebar.selectbox) that allows the user to select the chatbot’s persona (e.g., “Math Tutor”, “French Translator”, “Creative Writer”) and updates the initial SystemMessage dynamically.

Sample Input

Sidebar selection: "French Translator"

Sample Output

Assistant greets the user and performs translations accordingly.

Plan

  1. Add a selectbox in the sidebar containing the different persona options.
  2. Based on selection, retrieve the corresponding system instruction text.
  3. If the selection changes, clear the session messages list and re-initialize it with the new SystemMessage to start a fresh context.

Practice & Exercises

To execute the Streamlit chatbot locally, run the script from your terminal: