Implement retrieval and evaluation for a simple RAG
Implement Retrieval and Evaluation for a Simple RAG System
You are provided with a basic "toy RAG" (Retrieval Augmented Generation) setup, including helper utilities and two pandas DataFrames:
docs_df: Contains a corpus of text passages with columnsdoc_id(string/int) andtext(string).queries_df: Contains query information with columnsquery_id,query_text, andrelevant_doc_ids(a list ofdoc_ids considered relevant for that query).
A utility function embed_texts(texts: List[str]) -> np.ndarray is available, which takes a list of strings and returns a 2D NumPy array of embeddings with shape (n, d).
Part A: Build Document Embeddings
Compute embeddings for every text passage in docs_df and store them efficiently for subsequent retrieval operations.
Part B: Retrieve Top-K by Cosine Similarity
For each query in queries_df:
- Compute the embedding for the
query_text. - Calculate the cosine similarity between the query embedding and all stored document embeddings.
- Identify and return the top 10 documents, ranked by their similarity scores.
The output for each query should include:
- The
query_id. - A ranked list of the 10 retrieved
doc_ids. - (Optional) The corresponding similarity scores for the top 10 documents.
Part C: Evaluate Retrieval
Implement an evaluation mechanism across all queries using the following metrics:
- Recall@10: The fraction of queries where at least one relevant document (from
relevant_doc_ids) appears within the top 10 retrieved documents. - MRR@10 (Mean Reciprocal Rank): The mean reciprocal rank of the first relevant document found within the top 10 retrieved documents. If no relevant document is found in the top 10, its reciprocal rank is 0.
Constraints and Notes
- The
docs_dfcan be substantial; prioritize efficient, vectorized operations and avoid explicit Python loops over documents where possible. - Embeddings generated by
embed_textsmay or may not be pre-normalized. Your cosine similarity calculation must correctly handle both scenarios. - You are restricted to using only NumPy and Pandas libraries. External vector databases or other specialized libraries are not permitted.
Describe your proposed approach and implement the necessary code components as if filling in a notebook.
AI interview prep
Core dilemma
Balancing computational efficiency for large-scale numerical operations (vector similarity) with correctness in metric calculation, all within the constraints of NumPy/Pandas.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement retrieval and evaluation for a simple RAG
Implement Retrieval and Evaluation for a Simple RAG System
You are provided with a basic "toy RAG" (Retrieval Augmented Generation) setup, including helper utilities and two pandas DataFrames:
docs_df: Contains a corpus of text passages with columnsdoc_id(string/int) andtext(string).queries_df: Contains query information with columnsquery_id,query_text, andrelevant_doc_ids(a list ofdoc_ids considered relevant for that query).
A utility function embed_texts(texts: List[str]) -> np.ndarray is available, which takes a list of strings and returns a 2D NumPy array of embeddings with shape (n, d).
Part A: Build Document Embeddings
Compute embeddings for every text passage in docs_df and store them efficiently for subsequent retrieval operations.
Part B: Retrieve Top-K by Cosine Similarity
For each query in queries_df:
- Compute the embedding for the
query_text. - Calculate the cosine similarity between the query embedding and all stored document embeddings.
- Identify and return the top 10 documents, ranked by their similarity scores.
The output for each query should include:
- The
query_id. - A ranked list of the 10 retrieved
doc_ids. - (Optional) The corresponding similarity scores for the top 10 documents.
Part C: Evaluate Retrieval
Implement an evaluation mechanism across all queries using the following metrics:
- Recall@10: The fraction of queries where at least one relevant document (from
relevant_doc_ids) appears within the top 10 retrieved documents. - MRR@10 (Mean Reciprocal Rank): The mean reciprocal rank of the first relevant document found within the top 10 retrieved documents. If no relevant document is found in the top 10, its reciprocal rank is 0.
Constraints and Notes
- The
docs_dfcan be substantial; prioritize efficient, vectorized operations and avoid explicit Python loops over documents where possible. - Embeddings generated by
embed_textsmay or may not be pre-normalized. Your cosine similarity calculation must correctly handle both scenarios. - You are restricted to using only NumPy and Pandas libraries. External vector databases or other specialized libraries are not permitted.
Describe your proposed approach and implement the necessary code components as if filling in a notebook.