We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Embedding Models

Training a model to convert text into vectors takes a lot of data and computation. The model slurps up a massive amount of text and learns patterns about how words and phrases relate to each other.

For our purposes, there isn't much to gain by training our own model from scratch. We'll use a pre-trained embedding model called all-MiniLM-L6-v2 because it:

  • Runs fast on any computer
  • Has a small size (~80 MB, not including standard dependencies)
  • Provides good quality for most use cases
  • Allows local execution – no internet required after download

Loading a Model

This is all it takes to load the model and convert some text into vectors:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

print(f"Model loaded: {model}")
print(f"Max sequence length: {model.max_seq_length}")

model.encode(text)

The first time you run this, it downloads the model files. Subsequent runs will load from your local cache.

If you get an error about your GPU or hardware acceleration, load the model on your CPU instead by passing device="cpu".

Assignment

Set up and test a pre-trained embedding model for our movie search system.

  1. uv add sentence-transformers
    
  2. Outside of the class definition, create a new function called verify_model that creates an instance of the SemanticSearch class and prints the model information:

Run and submit the CLI tests.