Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Text embedding models in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Text Embedding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
What is the main purpose of text embedding models?

Text embedding models convert text into numbers. What is their main purpose?

ATo transform text into fixed-length vectors that capture semantic meaning
BTo translate text from one language to another
CTo generate new text based on a prompt
DTo count the number of words in a sentence
Attempts:
2 left
💡 Hint

Think about how computers understand text in a way that machines can work with.

Model Choice
intermediate
1:30remaining
Which model is best suited for generating text embeddings?

You want to create embeddings for sentences to compare their meanings. Which model type is best?

ATransformer-based model trained on large text corpora
BRecurrent Neural Network (RNN) trained on time series data
CConvolutional Neural Network (CNN) trained on images
DGenerative Adversarial Network (GAN) for image generation
Attempts:
2 left
💡 Hint

Consider models designed to understand language context deeply.

Predict Output
advanced
1:30remaining
What is the output shape of embeddings for 3 sentences using a model that outputs 768-dimensional vectors?

Given this Python code snippet:

sentences = ["Hello world", "Machine learning is fun", "AI helps humans"]
embeddings = model.encode(sentences)
print(embeddings.shape)

What will be printed?

Prompt Engineering / GenAI
sentences = ["Hello world", "Machine learning is fun", "AI helps humans"]
embeddings = model.encode(sentences)
print(embeddings.shape)
A(768, 3)
B(3, 768)
C(3,)
D(768,)
Attempts:
2 left
💡 Hint

Each sentence gets a vector of length 768. How many sentences are there?

Metrics
advanced
1:15remaining
Which metric is best to measure similarity between two text embeddings?

You have two text embeddings and want to measure how similar their meanings are. Which metric is most appropriate?

AEuclidean distance
BAccuracy
CMean squared error
DCosine similarity
Attempts:
2 left
💡 Hint

Think about a metric that measures the angle between two vectors rather than their length.

🔧 Debug
expert
2:00remaining
Why does this embedding code raise a TypeError?

Consider this code snippet:

text = "AI is amazing"
embedding = model.encode(text)
print(embedding.shape)

It raises: TypeError: 'float' object has no attribute 'shape'. Why?

AThe variable 'text' is not defined
BThe model.encode returns a float instead of a vector
CThe model.encode expects a list of strings, not a single string
DThe print statement is missing parentheses
Attempts:
2 left
💡 Hint

Check the input type expected by the encode method.

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