0
0
Prompt Engineering / GenAIml~10 mins

Similarity search and retrieval 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 compute cosine similarity between two vectors.

Prompt Engineering / GenAI
from sklearn.metrics.pairwise import [1]

vector_a = [[1, 2, 3]]
vector_b = [[4, 5, 6]]
similarity = [1](vector_a, vector_b)
print(similarity)
Drag options to blanks, or click blank then click option'
Apairwise_distances
Beuclidean_distances
Cmanhattan_distances
Dcosine_similarity
Attempts:
3 left
💡 Hint
Common Mistakes
Using distance functions instead of similarity functions.
Confusing cosine similarity with Euclidean distance.
2fill in blank
medium

Complete the code to find the index of the most similar vector in a list using cosine similarity.

Prompt Engineering / GenAI
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity

vectors = np.array([[1, 0], [0, 1], [1, 1]])
query = np.array([[0.9, 0.1]])
similarities = cosine_similarity(query, vectors)
most_similar_index = np.argmax([1])
print(most_similar_index)
Drag options to blanks, or click blank then click option'
Asimilarities
Bvectors
Cquery
Dnp.array
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original vectors array instead of similarity scores.
Trying to find max on the query vector.
3fill in blank
hard

Fix the error in the code to correctly compute Euclidean distances between vectors.

Prompt Engineering / GenAI
from sklearn.metrics.pairwise import [1]

vectors = [[1, 2], [3, 4], [5, 6]]
distances = [1](vectors)
print(distances)
Drag options to blanks, or click blank then click option'
Acosine_similarity
Beuclidean_distances
Cmanhattan_distances
Dpairwise_kernels
Attempts:
3 left
💡 Hint
Common Mistakes
Using similarity functions instead of distance functions.
Passing a single list instead of a 2D array.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Prompt Engineering / GenAI
words = ['apple', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B<=
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary with uppercase keys and values greater than 2.

Prompt Engineering / GenAI
data = {'a': 1, 'b': 3, 'c': 5, 'd': 2}
filtered = [1]: [2] for k, v in data.items() if v [3] 2}
print(filtered)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase.
Using wrong comparison operator or filtering condition.