Search⌘ K
AI Features

Embeddings and Vector Similarity Fundamentals

Explore how embeddings convert text into numeric vectors to enhance retrieval accuracy by measuring semantic similarity rather than keyword overlap. Understand the process of scoring vector closeness using cosine similarity and learn when to choose vector search over keyword search. This lesson equips you to evaluate and fine-tune retrieval steps in AI systems for better relevance and precision.

A keyword search can miss the right paragraph even when the meaning is a perfect match. In a toy internal docs corpus, a query like Can contractors access the VPN from personal laptops might share almost no words with a policy paragraph that answers it, so keyword overlap ranks it low or not at all.

Now flip the failure. A query like VPN policy laptop might share keywords with a tangential troubleshooting note and retrieve that instead, even though it does not answer the access question. The retrieval step needs a way to score meaning, not just shared strings. This lesson traces the retrieval step’s data movement, not full RAG yet.

The retrieval data flow shows how vector search captures semantic intent and retrieves relevant results that traditional keyword search misses.

Keyword search misses, vector search finds the match
Keyword search misses, vector search finds the match

The key shift is that the pipeline does not compare text to text directly. It turns each text into a vector, scores vector closeness, and returns the top candidates.

From text to vectors

With vector retrieval, every chunk of the internal docs corpus first passes through an embedding function that maps a string to a fixed-length numeric array. That mapping is deterministic for a given model configuration, so identical input strings produce identical vectors, while small wording changes usually produce nearby vectors.

Inside the embedder, tokenization breaks the input string into subword units, then the embedder outputs a single vector for the whole input. The output preserves coarse semantics such as topic and relationships more reliably than surface form, so paraphrases often cluster while exact quotes are not guaranteed to land as nearest neighbors if the surrounding context shifts. A practical implication is that retrieval tends to work better when the query describes the idea, not when it pastes a long excerpt.

Here's what an actual call looks like:

Python 3.14.0
from google import genai
from google.genai import types
client = genai.Client(api_key="{{GEMINI_API_KEY}}")
MODEL_ID = "gemini-embedding-001"
response = client.models.embed_content(
model=MODEL_ID,
contents="Can contractors access the VPN from personal laptops?",
config=types.EmbedContentConfig(
task_type="RETRIEVAL_DOCUMENT"
)
)
vector = response.embeddings[0].values
print(len(vector))
print(MODEL_ID)
  • Line 1 (from google import genai): Imports the real SDK client.

  • Line 2 (from google.genai import types): Imports the config types needed to configure the ...