AI Hints
Home›RAG Tutorial›Embeddings
RAG · Lesson 26 of 120

Your First Embedding

Sentence transformers.

⏱ 2 min read▶ Video at 25:50🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

Let's make a real embedding. We'll use a small, free model called all mini LML six v two, from the sentence transformers library. It runs on your own computer, with no API key.

Your First Embedding
From the video: Your First Embedding
  • A small free model
  • Runs locally
  • No API key

Embedding a sentence

We load the model, on the CPU. Encode turns a list of sentences into vectors. Normalise embeddings scales each vector to length one, which makes comparing them easy. Run it. One sentence becomes three hundred and eighty four numbers.

first_embedding.pypython
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")

vectors = model.encode(["How long does charging take?"], normalize_embeddings=True)
print("shape:", vectors.shape)
print("first five numbers:", vectors[0][:5].round(3))
Output
shape: (1, 384)
first five numbers: [ 0.049  0.068 -0.048  0.085  0.037]

Choosing a model

Small local models are free, private, and fast. API models, like Gemini's, are often more accurate, especially for long or technical text, but each call costs a little, and your text leaves your computer. Always embed your chunks and your questions with the same model.

keyvalue
Localfree, private
APIoften more accurate
Same modelfor chunks and questions
Size384 to 3072 numbers

Summary

Sentence transformers turns text into vectors locally. Always use the same model for chunks and questions. Next, measuring how close two meanings are.

Key takeaways
  • SentenceTransformer
  • encode
  • normalize_embeddings
Next up: Cosine Similarity — how close two meanings are