Hugging Face, End to EndLesson 49 of 75

Module 6 · Embeddings, and search that understands

The library, and the four calls you need

In this lesson

  • 8 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 2 of 9 in this chapter

This chapter’s goalBuild and measure a semantic search over your own documents: choosing an embedding model against your language and text length, chunking deliberately, normalising vectors for the similarity you use, adding a cross-encoder reranker, and reporting recall@k on questions you wrote yourself.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:08Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

Install and encode

Bash
pip install sentence-transformers
Python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("BAAI/bge-small-en-v1.5")

vectors = model.encode(
    ["first document", "second document", "third document"],
    batch_size=64,
    normalize_embeddings=True,
    show_progress_bar=True,
)
print(vectors.shape)     # (3, 384)

Four arguments worth setting every time:

  • batch_size: 32 to 128 on a CPU, larger on a GPU. Encoding a list is far faster than encoding in a loop, for the same reasons as module two.
  • normalize_embeddings=True: scales every vector to unit length, which makes dot product and cosine similarity identical and lets you use the faster one. Set it and then be consistent; mixing normalised and unnormalised vectors in one index gives you nonsense.
  • show_progress_bar: for anything over a few thousand items, so you know whether to make tea.
  • convert_to_tensor=True when you want torch tensors on the GPU rather than NumPy arrays.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘The library, and the four calls you need’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/sentence-transformers-in-practice (Accessed: 29 November 2026).