Build a RAG app

Chapter 9 of 1125 min

Make it an app

  1. Load
  2. Chunk
  3. Embed
  4. Search
  5. Ask the AI
  6. App

In this chapter you will:

  • turn your terminal program into a chat app in the browser
  • learn how Streamlit runs your code, and why caching matters
  • reuse every function you've written, unchanged

Why Streamlit

A terminal program is fine for you; nobody else will use it. Streamlit turns a Python script into a web page, with no HTML or JavaScript. It's what data scientists and AI engineers use to show their work.

pip install streamlit

How Streamlit runs your code

This is the one idea to understand: Streamlit runs your whole script from top to bottom every time the user does something, such as sending a message. That's what makes it simple. But it also means slow work, like embedding all the chunks, would happen again on every question.

The fix is @st.cache_resource. Put it above a function, and Streamlit runs that function once, remembers what it returned, and hands back the same result every time after.

Write the app

Create app.py. Notice it imports the functions you already wrote; rag.py doesn't change at all.

app.py
import streamlit as st
 
from rag import load_documents, split_into_chunks, embed_chunks, answer
 
 
@st.cache_resource  # do the slow work once, not for every question
def prepare():
    chunks = split_into_chunks(load_documents("data"))
    return chunks, embed_chunks(chunks)
 
 
chunks, chunk_vectors = prepare()
 
st.title("Ask My Docs")
st.write("Ask anything about the Brightpath Studio handbook.")
 
question = st.chat_input("Your question")
if question:
    st.chat_message("user").write(question)
    with st.spinner("Thinking..."):
        reply = answer(question, chunks, chunk_vectors)
    st.chat_message("assistant").write(reply)
  • st.title and st.write put text on the page.
  • st.chat_input draws the message box at the bottom. It gives you the text the user sent, or None if they haven't sent anything yet, which is why the rest sits under if question:.
  • st.chat_message("user") and st.chat_message("assistant") draw chat bubbles.
  • st.spinner shows "Thinking..." while Gemini answers.

Run it

Streamlit apps start with streamlit run, not python:

streamlit run app.py

Your browser opens at http://localhost:8501 with your app. Ask it a question. To stop the app, go back to the terminal and press Ctrl+C.

Predict, then check

Ask a second question. Does the first question and its answer stay on the page?

Show answer

No. Each question replaces the last, because Streamlit reruns the script from the top, and nothing remembers the earlier messages. Real chat apps keep the history in st.session_state, a dictionary that survives reruns. Adding that is one of the ideas in chapter 11.

main.py still works too. Keep it: it's handy for quick tests in the terminal.

Practice

Add a line under the title telling users which documents the app knows, by listing the file names. (Hint: load_documents gives you each document's source.)

Save your progress

git add .
git commit -m "Chapter 9: chat app"

Stuck, or want to compare? The finished code for this chapter is tagged ch09: run git diff ch09, or see it on GitHub.