Complete the code to import the Retriever class used in RAG models.
from transformers import [1]
The RagRetriever class is used to fetch relevant documents in advanced RAG models, improving answer quality by retrieving context.
Complete the code to initialize a RAG model with a retriever.
from transformers import RagTokenizer, RagSequenceForGeneration, RagRetriever tokenizer = RagTokenizer.from_pretrained('facebook/rag-sequence-nq') retriever = RagRetriever.from_pretrained('facebook/rag-sequence-nq') model = RagSequenceForGeneration.from_pretrained('facebook/rag-sequence-nq', retriever=[1])
The retriever object is passed to the RAG model to enable it to fetch relevant documents, which improves answer quality.
Fix the error in this code that generates an answer using the RAG model.
input_dict = tokenizer(question, return_tensors='pt') outputs = model.generate(input_ids=[1], num_beams=5, max_length=50) answer = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
The input_ids key contains the token IDs needed for generation. Using other keys causes errors or wrong input.
Fill both blanks to create a dictionary comprehension that filters retrieved documents with score above 0.5.
filtered_docs = {doc: score for doc, score in retrieved_docs.items() if score [1] 0.5 and len(doc) [2] 0}We want scores greater than 0.5 and documents with length greater than 0 to keep only relevant, non-empty docs.
Fill all three blanks to build a dictionary of document texts and their scores filtered by score > 0.7.
high_score_docs = [1]: [2] for [3], [2] in docs_with_scores.items() if [2] > 0.7
The dictionary comprehension uses doc as key, score as value, iterating over doc, score pairs.