A reader is the answer-extraction component in an extractive question-answering system. After a search engine retrieves likely documents, the reader analyzes each document context and predicts the most likely beginning and ending of the answer span. section 14.1.2 section 14.6
A practical reader pipeline works as follows:
- Accept question-and-context pairs from the retriever. section 14.5.1
- Run a fine-tuned question-answering model over every candidate context. section 14.5.2
- Return an answer, confidence score, document identifier, and URL for each context. section 14.5.2
- Compare candidate answers or use reader confidence to rerank retrieved documents. section 14.1.2
Minimalist shoes
For “What are minimalist shoes?”, the retriever supplies several document contexts. The reader identifies “shoes intended to closely approximate barefoot running conditions” as the answer span in the strongest context.
Listing 14.16 Reader function that incorporates our fine-tuned model
from transformers import pipeline
device = get_processor_device()
model_name = "data/question-answering/roberta-base-squad2-fine-tuned"
qa_nlp = pipeline("question-answering", model=model_name, #1
tokenizer=model_name, device=device) #1
def reader(contexts):
answers = []
for _, row in contexts.iterrows(): #2
answer = qa_nlp({"question": row["question"], #2
"context": row["context"]}) #2
answer["id"] = row["id"] #3
answer["url"] = row["url"] #3
answers.append(answer) #2
return answers
The implementation loads a Transformers question-answering pipeline, applies it to each question and context, and attaches document metadata to the resulting answer. section 14.5.2

