- Extract text from a PDF and chunk it for embedding
- Create embeddings with a sentence-transformer model
- Persist embeddings and chunk metadata in ChromaDB
- Retrieve embeddings, reduce dimensionality with PCA, and plot an interactive 3D scatter using Plotly

- Load a PDF document.
- Extract and split text into overlapping chunks.
- Embed each chunk with a sentence-transformer.
- Store embeddings and chunk text/metadata in ChromaDB.
- Retrieve embeddings, run PCA → 3 components.
- Render an interactive 3D Plotly scatter with chunk previews on hover.

- PDF loading function This function reads a PDF and concatenates all page text into a single string. It is robust to pages without extractable text.
- Chunking function We split the concatenated text into overlapping character-based chunks. Overlap preserves semantic continuity between adjacent chunks, which helps downstream tasks like retrieval and visualization.
- ChromaDB setup and embedding function
Create a persistent ChromaDB client that stores data on disk (
./chroma_data). Configure the embedding function using theall-MiniLM-L6-v2sentence-transformers model (compact and fast). The first run will download the model weights.
We store each chunk as the document text (and optionally other metadata) alongside its embedding in the vector database. This lets the visualization show a text preview when you hover over a point.
First-time model download and building the embedding index can take time and disk space. Persisting to
./chroma_data helps avoid repeated downloads on subsequent runs.- Load the PDF, chunk it, and add chunks to ChromaDB Load the PDF, create overlapping chunks, and insert them into the ChromaDB collection. The snippet below clears the collection before insertion — useful while iterating. Comment out the deletion code if you want to append instead.
chunk_size and overlap. Adjust chunking parameters to balance granularity and index size.
- Retrieve embeddings and visualize (PCA → 3D scatter) Query the collection to fetch embeddings and metadata, reduce dimensionality with PCA to three components, and render an interactive 3D scatter using Plotly. Hovering shows chunk previews stored in the collection.

- Each point corresponds to a text chunk’s embedding; embeddings encode semantic meaning.
- PCA compresses high-dimensional vectors to three principal components so you can visually inspect structure.
- Nearby points indicate semantically similar chunks; distant points indicate dissimilar content.
- Storing chunk text as
document/metadata lets you validate what each point represents during exploration.
- Vector databases store embeddings (vectors). In this demo we intentionally store the chunk text alongside embeddings so the visualization can display previews — many vector DBs support this pattern.
- Dimensionality reduction (PCA, t-SNE, UMAP) is only used for visualization; original vectors remain high-dimensional in the database and should be used for real retrieval tasks.
- Visual clusters are a qualitative tool to inspect semantic grouping and to debug embedding/model behavior.
- ChromaDB: https://www.trychroma.com
- Sentence-Transformers (SBERT): https://www.sbert.net
- scikit-learn PCA: https://scikit-learn.org/stable/modules/decomposition.html#pca
- Plotly Python docs: https://plotly.com/python/