Seeing context in a vector database

A twelve-sentence demo that shows how sentence embeddings separate four different senses of the same word — and where they still bleed.


The question

Consider four sentences that all use some form of serve or service.

  1. “Federer’s second serve painted the sideline.”
  2. “The waiter served our main course after the appetisers.”
  3. “The municipal corporation serves residents through waste collection and water supply.”
  4. “The income tax department served the company a notice under Section 148.”

A keyword index treats these as neighbours because they share the same surface tokens. A sentence-embedding model does not. Each sentence is compressed into a 384-dimensional vector whose direction is determined by the sentence’s full context, not by any single word. The four vectors end up in four different regions of the space, even though the shared token is what a naive reader would notice first.

This post walks through a small demo that makes that separation visible.


The picture

Twelve sentences and one probe, projected from 384 dimensions to 2D with PCA. The four senses form four visually distinct regions.

The plot above shows all twelve sentences from the demo dataset, embedded with all-MiniLM-L6-v2 and projected from 384 dimensions to 2 with PCA. Each colour is one sense: tennis serves in green, restaurant service in orange, government services in blue, legal or tax notices in purple. The red star is the probe query “the player hit an ace down the line.” It lands next to the tennis cluster even though the probe does not contain the word serve at all. The word never mattered. The context did.

The image was generated by the same script that powers the interactive demo below (python polysemy_demo.py --save-png), so what you see is what any reader will see on their own machine.


The interactive demo

The demo code is not public yet. I will publish it soon and add the link here.

The app opens at http://localhost:8501. Four canned probes are one click away. A text input accepts any sentence you like. Whatever you type is embedded with the same model, projected into the same PCA plane, and drawn on the plot as a red star. The question the plot answers is simple — which region does the probe land in? — and answering it takes about ten seconds per probe.

The four canned probes are chosen to make the effect clear.

ProbeExpected regionWhy it works
the player hit an ace down the lineTennis (green)Player, ace, and line pull the query toward the court-vocabulary cluster.
the waiter brought our plates to the tableRestaurant (orange)Waiter, plates, and table dominate. The word serve is not even in the probe.
citizens can access government schemes onlineGovernment (blue)Citizens and schemes land in the public-services region.
a notice was issued by the tax authoritiesLegal or tax (purple)Notice, issued, and tax authorities pull toward the legal-service region.

Type your own probes too. The most instructive ones are the ambiguous cases — “the corporation serves its customers well” sits between restaurant and government; “a warrant was served at the address” is close to the legal cluster but not quite on top of it. Those near-misses are the honest part of the demo.


The dataset

Twelve short sentences live in seed_polysemy.py, three per sense. The vocabulary overlaps by design. What differs is the surrounding context — court and baseline in one sense, waiter and sommelier in another, ration distribution in a third, Section 148 in the fourth.

# tennis
"Federer's second serve painted the sideline and drew a defensive lob from his opponent."
"A good kick serve bounces high off the court and pushes the returner well behind the baseline."
...

# legal or tax notice
"The income tax department served the company a notice under Section 148 for reassessment of prior returns."
"Legal process was served on the defendant at their registered office address on Monday morning."
...

The full list is in seed_polysemy.py. Twelve sentences is small enough to read on one screen, which is the point — everything the model is working with is visible, and every point on the plot has a sentence you can hover to see.


Reading the plot honestly

The scatter is a linear projection from 384 dimensions down to two. It keeps the two directions of largest variance and discards the other 382. Two points that look close in the picture might still be meaningfully apart in the full space, and two that look far may be closer than they appear. Treat the plot as a hint about what the embeddings are doing, not as proof.

A few observable things in the current image are worth calling out.

The four regions exist without ambiguity. Tennis is clearly separated in the upper left. Restaurant is clearly separated in the lower middle. Government stretches vertically on the right. Legal or tax sits above government in the upper right. A viewer with no background can point at four regions and name them, which is the whole point of the demo.

Government and legal or tax bleed at the boundary. Both use formal institutional language — residents, authorities, notice, served — and share syntactic patterns like X serves Y. The two purple points in the top part of the plot and the blue points at similar heights are the trace of that overlap. On the interactive demo, a probe like “the government issued a public notice” will land somewhere between the two clusters rather than firmly inside either. This is the same failure mode that shows up on real corpora, only small enough to see.

Tennis is the sharpest cluster. Tennis vocabulary — baseline, ace, kick serve, pronation — has almost no overlap with the other three senses in ordinary text, so the model has an easy time putting the three tennis sentences close together and far from everything else.


The caveats worth naming

PCA is not the geometry the model uses. The model compares full 384-dimensional vectors with cosine similarity. PCA to 2D is only for the picture. If you want to see the raw scores that the model actually computes, seed the collection with python seed_polysemy.py and open Vector Search Lab (streamlit run threshold_tuner.py), select the polysemy collection, and run the same probes. The scores agree with the picture in outline but disagree in the details — a 2D projection cannot fully represent a 384-dimensional geometry.

A larger model tightens the clusters. The demo defaults to all-MiniLM-L6-v2 because it is small, fast, and installed everywhere. Re-embedding the same twelve sentences with all-mpnet-base-v2 (768 dimensions) produces cleaner separation, especially between government and legal or tax. That is the standard trade-off — larger models cost more per query and take longer to load, and they buy sharper contextual discrimination.

A bi-encoder is not a cross-encoder. Sentence-transformers models embed the query and each document independently and then compare their vectors. They cannot reason about token-level interactions between the query and a specific document. A cross-encoder re-scores the top few results by feeding query and document through a transformer together, which typically resolves the residual polysemy — at a latency cost that rules it out for the initial retrieval step. Production retrieval stacks the two, it does not swap them.


Why this matters for RAG

A retrieval pipeline that treats serve as a single token cannot distinguish “the notice was served” from “the waiter served the meal.” A retrieval pipeline built on sentence embeddings does distinguish them, most of the time, because the surrounding words drag the vectors apart. The exceptions — the cases where the vectors bleed — are exactly what a threshold slider is for, and exactly what a reranker exists to catch.

This demo is deliberately small and readable. Twelve sentences fit on one screen. Every point can be inspected. The behaviour that surfaces here on a toy corpus is the same behaviour that shows up on a production knowledge base, only harder to see.