Skip to content
Home/Resources/Build guides/RAG chatbot with Gemini and Streamlit
Build guide

Build a RAG Chatbot for Free with the Gemini API and Streamlit, Step by Step

By Younus FardeenUpdated 31 Oct 20263 min read
Short answer

Get a free Gemini API key from Google AI Studio, split your PDFs into chunks, embed them with the Gemini embedding model, retrieve the closest chunks for each question and ask the model to answer only from them with citations. Wrap it in Streamlit, test it on 30 questions and deploy to Streamlit Community Cloud.

You will buildA chatbot that answers questions about your own documents with citations, with a measured accuracy, hosted on a public URL.
Time4 to 6 hours
Cost₹0 within the Gemini API free tier and Streamlit Community Cloud
LevelIntermediate (Python basics)

Tools you need

The 10 steps

01

Get a free API key

Open Google AI Studio, create an API key and store it in an environment variable GEMINI_API_KEY. Never paste it into code or a notebook you will share.

02

Pick documents you may use

Public PDFs such as a university handbook or government FAQs. Put them in a docs folder.

03

Install packages

pip install google-genai streamlit pypdf numpy.

04

Chunk the documents

Read each PDF with pypdf and split the text into chunks of roughly 300 to 500 words with some overlap. Keep the file name and page number with every chunk; you will show them as citations.

05

Embed and store

Use the Gemini embedding model through the SDK to embed each chunk, and keep the vectors in a NumPy array with a parallel list of chunk metadata. Save both to disk so you do not re-embed on every run.

06

Retrieve

Embed the user's question, compute cosine similarity against all chunk vectors and take the top 5 chunks.

07

Generate with citations

Build a prompt: the retrieved chunks with their sources, then the question, then an instruction to answer only from the sources, cite them, and say when the answer is not in the documents. Call a Gemini text model and show the answer with the sources underneath.

08

Build the Streamlit UI

A text input, the answer, an expandable list of the chunks used, and a sidebar that shows which documents are loaded. Run with streamlit run app.py.

09

Evaluate

Write 30 questions with reference answers, including 5 the documents cannot answer. Score each run as pass or fail and record the pass rate in your README. Change chunk size once and re-measure.

10

Deploy

Push to GitHub (without the key), sign in to Streamlit Community Cloud, create an app from the repository and add GEMINI_API_KEY under Secrets. Share the URL.

Make it yours

  • Hybrid retrieval: add a keyword score and merge with the vector score.
  • A reranker step before generation.
  • Swap Gemini for a local model through Ollama for private documents.

Troubleshooting

ProblemFix
Answers ignore the documentsStrengthen the instruction and lower the number of chunks; check that retrieval actually returns relevant text.
Rate-limit errorsThe free tier has request limits. Cache embeddings and add a short retry with backoff.
The app forgets the index after restartSave the vectors and metadata to files and load them at start-up.

Frequently asked questions

Is the Gemini API free?

Google AI Studio offers a free tier with rate limits that cover a student project. Check the current limits on the pricing page.

Is Streamlit hosting free?

Streamlit Community Cloud hosts public apps from GitHub for free with resource limits.

Do I need a vector database?

Not for a few hundred chunks; a NumPy array is fine. Move to FAISS, Chroma or Qdrant when you have tens of thousands.

Continue learning

Project guide · AI & ML

AI & ML projects

Churn, fraud, recommenders and RAG, each with data and a metric.

Open guide →24 ideas
Project guide · Agentic AI

Agentic AI projects

MCP servers, text-to-SQL and agentic RAG, each with an eval set.

Open guide →19 ideas
Free tools

Free AI tools for builders

Run, build and test AI projects without paying.

See the list →16 tools