liveBook · Manning

Vectorization

As explained in LLMs in Production

Vectorization is the transformation of input data into numerical vector representations.

For semantic search, vectorization turns text or code into numerical embeddings that can be compared by distance. Encode stored items and queries with the same embedding model and dimensional space; mismatches make comparisons unreliable. section 4.4.2 section 10.2.2

A practical retrieval workflow:

  • Encode documents or code, then store the vectors alongside useful metadata in a vector database. section 10.2.2
  • Encode a query with the same model and retrieve nearby vectors. In RAG, selected matches can become examples or context in a prompt; filter them to fit the available context window. section 4.4.2 section 10.2.3

Finding related coding instructions

Encode Python coding instructions with a code-search embedding model, index them in Milvus, then encode a new request and retrieve nearby instructions. The matches can provide examples for a coding assistant’s prompt. section 10.2.2 section 10.2.3

Listing 4.11 Example embeddings
import numpy as np
from sentence_transformers import SentenceTransformer
from datasets import load_dataset

model_ckpt = "sentence-transformers/all-MiniLM-L6-v2"     #1
model = SentenceTransformer(model_ckpt)
embs_train = load_dataset("tweet_eval", "emoji", split="train[:1000]")
embs_test = load_dataset("tweet_eval", "emoji", split="test[:100]")


def embed_text(example):                    #2
    embedding = model.encode(example["text"])
    return {"embedding": np.array(embedding, dtype=np.float32)}


print(f"Train 1: {embs_train[0]}")
embs_train = embs_train.map(embed_text, batched=False)
embs_test = embs_test.map(embed_text, batched=False)

embs_train.add_faiss_index("embedding")                 #3

# 
idx, knn = 1, 3  # Select the first query and 3 nearest neighbors    #4

query = np.array(embs_test[idx]["embedding"], dtype=np.float32)
scores, samples = embs_train.get_nearest_examples("embedding", query, k=knn)

print(f"QUERY LABEL: {embs_test[idx]['label']}")                #5
print(f"QUERY TEXT: {embs_test[idx]['text'][:200]} [...]\n")
print("=" * 50)
print("Retrieved Documents:")
for score, label, text in zip(scores, samples["label"], samples["text"]):
    print("=" * 50)
    print(f"TEXT:\n{text[:200]} [...]")
    print(f"SCORE: {score:.2f}")
    print(f"LABEL: {label}")

See a Python example that encodes tweet data with SentenceTransformer and searches for nearest neighbors using FAISS. section 4.4.2

How LLMs in Production covers it

How deep it goes
Connects final-layer model representations to practical encoding, indexing, and nearest-neighbor retrieval.
Examples
Uses tweet data and Python coding instructions to encode records and retrieve nearby matches.
What you can do after
Apply vectorization with SentenceTransformer and store or search vectors using FAISS or Milvus.
How it connects
Links embeddings to similarity search, vector databases, and retrieval-augmented generation.

Also explained in

Compare how 18 books explain vectorization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.