0
0
Agentic AIml~20 mins

Retrieval strategies (similarity, MMR, hybrid) in Agentic AI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Retrieval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Similarity-Based Retrieval
Which statement best describes how similarity-based retrieval works in information retrieval systems?
AIt retrieves documents by measuring how closely their content matches the query using a similarity score.
BIt randomly selects documents without considering the query content.
CIt ranks documents solely based on their length compared to the query length.
DIt retrieves documents based on the time they were added to the database.
Attempts:
2 left
💡 Hint
Think about how a search engine finds documents that are most relevant to what you type.
Model Choice
intermediate
2:00remaining
Choosing a Retrieval Strategy for Diverse Results
You want to retrieve documents that are not only relevant but also diverse to avoid redundancy. Which retrieval strategy should you choose?
AMaximal Marginal Relevance (MMR)
BRetrieval based on document age
CRandom sampling of documents
DPure similarity-based retrieval
Attempts:
2 left
💡 Hint
Consider a method that balances relevance and diversity.
Predict Output
advanced
3:00remaining
Output of MMR Score Calculation
Given the following Python code snippet that calculates MMR scores for candidate documents, what is the output of the print statement?
Agentic AI
query_vec = [1, 0]
candidate_vecs = [[0.9, 0.1], [0.1, 0.9], [0.8, 0.2]]
selected = []

import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

lambda_param = 0.5

mmr_scores = []
for i, doc_vec in enumerate(candidate_vecs):
    sim_to_query = cosine_similarity(query_vec, doc_vec)
    sim_to_selected = max([cosine_similarity(doc_vec, candidate_vecs[j]) for j in selected] or [0])
    score = lambda_param * sim_to_query - (1 - lambda_param) * sim_to_selected
    mmr_scores.append(score)

print([round(s, 2) for s in mmr_scores])
A[0.5, 0.5, 0.5]
B[0.95, 0.05, 0.9]
C[0.5, -0.5, 0.5]
D[0.9, -0.05, 0.8]
Attempts:
2 left
💡 Hint
Calculate cosine similarity carefully and apply the MMR formula.
Hyperparameter
advanced
2:00remaining
Effect of Lambda in MMR
In Maximal Marginal Relevance (MMR), what happens when the lambda parameter is set close to 1?
AThe retrieval balances relevance and diversity equally.
BThe retrieval focuses mostly on relevance, ignoring diversity.
CThe retrieval randomly selects documents without any scoring.
DThe retrieval focuses mostly on document diversity, ignoring relevance.
Attempts:
2 left
💡 Hint
Lambda controls the trade-off between relevance and diversity.
🔧 Debug
expert
3:00remaining
Debugging Hybrid Retrieval Code
Consider this hybrid retrieval code combining similarity and MMR. What error will it raise when run?
Agentic AI
def hybrid_retrieval(query_vec, candidate_vecs, lambda_param=0.7):
    import numpy as np
    def cosine_similarity(a, b):
        return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

    selected = []
    scores = []
    for i, doc_vec in enumerate(candidate_vecs):
        sim_query = cosine_similarity(query_vec, doc_vec)
        sim_selected = max([cosine_similarity(doc_vec, candidate_vecs[j]) for j in selected] or [0])
        score = lambda_param * sim_query - (1 - lambda_param) * sim_selected
        scores.append(score)
    return scores

query = [1, 0]
candidates = [[0.9, 0.1], [0.1, 0.9]]
print(hybrid_retrieval(query, candidates))
AZeroDivisionError due to zero vector norm
BTypeError due to unsupported operand types
CValueError because max() arg is an empty sequence
DNo error, outputs a list of scores
Attempts:
2 left
💡 Hint
Look at the max() function call when selected is empty.