Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the main components of a RAG model.
Prompt Engineering / GenAI
from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration tokenizer = RagTokenizer.from_pretrained('facebook/rag-token-nq') retriever = RagRetriever.from_pretrained('facebook/rag-token-nq') model = RagSequenceForGeneration.from_pretrained('facebook/rag-token-nq', retriever=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the tokenizer instead of the retriever to the model.
Passing None or forgetting to pass the retriever.
✗ Incorrect
The retriever component must be passed to the RAG model to enable document retrieval during generation.
2fill in blank
mediumComplete the code to tokenize the input question for the RAG model.
Prompt Engineering / GenAI
question = "What is RAG architecture?" inputs = tokenizer(question, return_tensors=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tf' which returns TensorFlow tensors instead of PyTorch tensors.
Using unsupported return_tensors values like 'np' or 'pd'.
✗ Incorrect
The tokenizer returns PyTorch tensors when 'pt' is specified as the return_tensors argument.
3fill in blank
hardFix the error in generating answers with the RAG model.
Prompt Engineering / GenAI
outputs = model.generate(input_ids=inputs['input_ids'], [1]=inputs['attention_mask'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect argument names like 'mask' or 'input_mask' causes errors.
Omitting the attention mask leads to wrong model behavior.
✗ Incorrect
The generate method expects the argument 'attention_mask' to specify which tokens to attend to.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps retrieved document titles to their scores.
Prompt Engineering / GenAI
doc_scores = {doc['title']: doc[1] for doc in retrieved_docs if doc[1] [2] 0.5} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'id' instead of 'score'.
Using incorrect comparison operators like '<' instead of '>'.
✗ Incorrect
We access the 'score' key for each document and filter documents with score greater than 0.5.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps document IDs to their text if the text length is greater than 100.
Prompt Engineering / GenAI
filtered_docs = {doc[1]: doc[2] for doc in docs if len(doc[3]) > 100} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'title' instead of 'text' or 'id'.
Not using len() to check text length.
✗ Incorrect
We map document IDs to their text and filter documents where the text length is greater than 100 characters.