Lesson Complete!

Generate Answers and Finish the App

What you built

A command-line RAG application that:

  • Loads any PDF from your file system
  • Extracts and chunks its text
  • Embeds every chunk using the Gemini API — once, then caches to disk
  • Finds the most relevant chunks for any question
  • Generates a grounded, accurate answer using Gemini Flash
  • Shows the source passages alongside the answer

The functions you wrote

FunctionLessonPurpose
load_pdf1Count pages in a PDF
extract_text2Pull all text from a PDF
chunk_text2Split text into overlapping pieces
preview_chunks2Inspect chunking output
create_client3Authenticate with the Gemini API
embed_text3Convert one chunk to a vector
embed_all_chunks3Embed an entire list of chunks
cosine_similarity4Score two vectors for closeness
search4Retrieve the top matching chunks
build_prompt5Assemble context + question into a prompt
generate_answer5Call Gemini and return the answer
print_result5Display the answer and sources
save_embeddings5Write chunks and vectors to disk
load_embeddings5Read chunks and vectors from disk
main5Wire the full pipeline from CLI arguments

Where to go next

  • Add a web UI — replace the CLI with a gradio interface in about 10 lines
  • Support multiple PDFs — index a folder of files and search across all of them
  • Use a local model — swap Gemini for a model running on your machine with ollama