In Lesson 1, you built a RAG system in Colab. Now let's turn it into a real web app with Streamlit. By the end of this lesson, anyone can upload a document and ask questions — all from a browser.
Step-by-Step Instructions
On your local computer, install Python from python.org first, then run:
pip install streamlit langchain-openai langchain-community langchain-chroma langchain-text-splitters chromadb openaiCopy the complete code below into a file named app.py. This version includes full RAG — chunking, retrieval, AND generation — so the app gives real answers, not just raw document chunks.
streamlit run app.pyPush your project to GitHub → go to share.streamlit.io → Deploy → Get public URL. Lesson 3 covers this in detail.
os.environ["OPENAI_API_KEY"] = openai_api_key with os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"] and remove the text input field. Lesson 3 explains this in full.Full Code — app.py
import streamlit as st
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from openai import OpenAI
import os
st.set_page_config(page_title="PromptPilotPro RAG Chat", page_icon="✈️")
st.title("✈️ PromptPilotPro RAG Assistant")
st.markdown("Upload a Northwind practice file, ask questions, and get answers grounded in the document.")
with st.sidebar:
st.header("Step 1: Upload your document")
uploaded_file = st.file_uploader("Choose a text file", type=["txt"])
openai_api_key = st.text_input("OpenAI API Key", type="password")
st.markdown("[Get your OpenAI API key](https://platform.openai.com/api-keys)")
if uploaded_file and openai_api_key:
os.environ["OPENAI_API_KEY"] = openai_api_key
with open("temp_upload.txt", "wb") as f:
f.write(uploaded_file.getbuffer())
with st.spinner("Loading document and building RAG system..."):
loader = TextLoader("temp_upload.txt")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
st.success("✅ RAG system ready!")
query = st.text_input("Your question:")
if query:
with st.spinner("Retrieving and generating answer..."):
docs = vectorstore.similarity_search(query)
context = "\n\n".join([d.page_content for d in docs])
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer the question based ONLY on the context provided. If the answer is not in the context, say you don't know."},
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
]
)
st.write(response.choices[0].message.content)
else:
st.info("📄 Upload a Northwind practice file and enter your OpenAI API key to begin.")