Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Text embedding models in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a text embedding using a simple model.

Prompt Engineering / GenAI
embedding = model.[1](text)
Drag options to blanks, or click blank then click option'
Atransform
Bembed
Cfit
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'predict' instead of 'transform' causes errors because embeddings are not predictions.
Using 'fit' tries to train the model, not create embeddings.
2fill in blank
medium

Complete the code to normalize the embedding vector.

Prompt Engineering / GenAI
normalized_embedding = embedding / [1](embedding)
Drag options to blanks, or click blank then click option'
Anp.linalg.norm
Blen
Csum
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' adds all elements but does not give vector length.
Using 'len' returns number of elements, not vector magnitude.
3fill in blank
hard

Fix the error in the code to compute cosine similarity between two embeddings.

Prompt Engineering / GenAI
similarity = np.dot(embedding1, embedding2) / ([1](embedding1) * np.linalg.norm(embedding2))
Drag options to blanks, or click blank then click option'
Amax
Bsum
Clen
Dnp.linalg.norm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' or 'len' causes incorrect similarity calculation.
Forgetting to normalize both vectors leads to wrong results.
4fill in blank
hard

Fill both blanks to create a dictionary of word embeddings for words longer than 3 letters.

Prompt Engineering / GenAI
word_embeddings = {word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Amodel.transform(word)
Bmodel.fit(word)
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fit' instead of 'transform' tries to train the model on single words.
Using '<' filters words shorter than 3 letters, which is incorrect here.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary of embeddings where embedding norm is greater than 0.5.

Prompt Engineering / GenAI
filtered_embeddings = {word: emb for word, emb in embeddings.items() if [1](emb) [2] 0.5 and len(word) [3] 4}
Drag options to blanks, or click blank then click option'
Anp.linalg.norm
B>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes the filter logic incorrectly.
Not using norm function causes errors in filtering.

Practice

(1/5)
1. What is the main purpose of a text embedding model?
easy
A. To convert text into numbers that capture its meaning
B. To translate text from one language to another
C. To generate images from text descriptions
D. To count the number of words in a text

Solution

  1. Step 1: Understand what text embedding models do

    Text embedding models turn words or sentences into number arrays that represent their meaning.
  2. Step 2: Compare options with this understanding

    Only To convert text into numbers that capture its meaning describes converting text into meaningful numbers. Other options describe different tasks.
  3. Final Answer:

    To convert text into numbers that capture its meaning -> Option A
  4. Quick Check:

    Text embedding = convert text to meaningful numbers [OK]
Hint: Remember: embeddings turn text into numbers for meaning [OK]
Common Mistakes:
  • Confusing embeddings with translation
  • Thinking embeddings generate images
  • Assuming embeddings just count words
2. Which of the following is the correct way to get an embedding vector from a text using a Python function get_embedding(text)?
easy
A. embedding = get_embedding->text
B. embedding = get_embedding[text]
C. embedding = get_embedding{text}
D. embedding = get_embedding(text)

Solution

  1. Step 1: Recall Python function call syntax

    In Python, functions are called with parentheses and arguments inside, like func(arg).
  2. Step 2: Match syntax with options

    Only embedding = get_embedding(text) uses parentheses correctly. Options A, B, and C use invalid syntax for function calls.
  3. Final Answer:

    embedding = get_embedding(text) -> Option D
  4. Quick Check:

    Function call uses parentheses () [OK]
Hint: Use parentheses () to call functions in Python [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Using curly braces {} instead of parentheses
  • Using arrow -> instead of parentheses
3. Given the code below, what will be the output?
def dummy_embedding(text):
    return [len(text), sum(ord(c) for c in text) % 100]

result = dummy_embedding('cat')
print(result)
medium
A. [3, 12]
B. [3, 15]
C. [4, 30]
D. [3, 30]

Solution

  1. Step 1: Calculate length of 'cat'

    The word 'cat' has 3 characters, so first element is 3.
  2. Step 2: Calculate sum of ASCII codes modulo 100

    ord('c')=99, ord('a')=97, ord('t')=116; sum=99+97+116=312; 312 % 100 = 12.
  3. Step 3: Determine output

    return [3, 12], so print([3, 12]).
  4. Final Answer:

    [3, 12] -> Option A
  5. Quick Check:

    len('cat')=3, (99+97+116)%100=12 [OK]
Hint: Calculate length and ASCII sum mod 100 carefully [OK]
Common Mistakes:
  • Wrong ASCII sum calculation
  • Miscounting string length
  • Mixing uppercase and lowercase ASCII codes
4. The following code tries to get embeddings for two texts but doesn't work as intended. What is the problem?
def get_embedding(text):
    return [len(text)]

texts = ['hello', 'world']
embeddings = []
for t in texts:
    embeddings.append(get_embedding)
print(embeddings)
medium
A. The list texts is empty
B. The function is not called; it appends the function itself
C. The variable embeddings is not defined
D. The function get_embedding has wrong syntax

Solution

  1. Step 1: Check the loop appending embeddings

    The code appends get_embedding without parentheses, so it adds the function object, not the result.
  2. Step 2: Understand the problem

    Appending the function itself causes the list to hold function references, not embedding lists like [5] and [5].
  3. Final Answer:

    The function is not called; it appends the function itself -> Option B
  4. Quick Check:

    Missing () calls function, else appends function object [OK]
Hint: Add () to call function, not just reference it [OK]
Common Mistakes:
  • Forgetting parentheses to call function
  • Assuming list is empty causes error
  • Thinking variable is undefined
5. You want to find the most similar sentence to 'I love apples' from a list using embeddings. Which approach is best?
hard
A. Count common words between 'I love apples' and each sentence
B. Translate all sentences to another language and compare lengths
C. Compute embeddings for all sentences, then find the one with smallest distance to 'I love apples' embedding
D. Randomly pick a sentence from the list

Solution

  1. Step 1: Understand similarity with embeddings

    Embeddings turn sentences into number arrays capturing meaning, so comparing distances between embeddings finds similar sentences.
  2. Step 2: Evaluate options for similarity search

    Compute embeddings for all sentences, then find the one with smallest distance to 'I love apples' embedding uses embeddings and distance, which is the correct method. Options A, C, and D do not use embeddings or meaningful similarity measures.
  3. Final Answer:

    Compute embeddings for all sentences, then find the one with smallest distance to 'I love apples' embedding -> Option C
  4. Quick Check:

    Use embeddings + distance for similarity [OK]
Hint: Use embedding distances to find similar texts [OK]
Common Mistakes:
  • Using word count instead of embeddings
  • Ignoring embeddings for similarity
  • Random selection instead of comparison