0
0
Prompt Engineering / GenAIml~10 mins

Sentence transformers in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a pre-trained sentence transformer model.

Prompt Engineering / GenAI
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('[1]')
Drag options to blanks, or click blank then click option'
Abert-base-uncased
Ball-MiniLM-L6-v2
Cresnet50
Dgpt-3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a model name from a different library like GPT or ResNet.
Using a base BERT model which is not fine-tuned for sentence embeddings.
2fill in blank
medium

Complete the code to encode a list of sentences into embeddings.

Prompt Engineering / GenAI
sentences = ['Hello world', 'Machine learning is fun']
embeddings = model.[1](sentences)
Drag options to blanks, or click blank then click option'
Aencode
Btransform
Cpredict
Dfit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fit' which is for training models, not encoding sentences.
Using 'predict' which is for classification or regression outputs.
3fill in blank
hard

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

Prompt Engineering / GenAI
from sklearn.metrics.pairwise import [1]
similarity = cosine_similarity([emb1], [emb2])[0][0]
Drag options to blanks, or click blank then click option'
Acosine_similarity
Beuclidean_distances
Cpairwise_distances
Dmanhattan_distances
Attempts:
3 left
💡 Hint
Common Mistakes
Using distance functions which give dissimilarity, not similarity.
Misspelling the function name.
4fill in blank
hard

Fill both blanks to create a dictionary of sentence embeddings for given sentences.

Prompt Engineering / GenAI
sentences = ['AI is amazing', 'I love coding']
embeddings = model.encode(sentences)
embedding_dict = {sentences[[1]]: embeddings[[2]] for i in range(len(sentences))}
Drag options to blanks, or click blank then click option'
Ai
B0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for sentence and embedding indices.
Using fixed indices instead of the loop variable.
5fill in blank
hard

Fill all three blanks to filter sentences with embeddings having norm greater than 1.0.

Prompt Engineering / GenAI
import numpy as np
filtered = {sent: emb for sent, emb in zip(sentences, embeddings) if np.linalg.[1](emb) [2] [3]
Drag options to blanks, or click blank then click option'
Anorm
B>
C1.0
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' instead of 'norm' which sums elements but does not compute vector length.
Using wrong comparison operators.