What is Extractive Question Answering?

Question Answering (QA) is a popular NLP task that aims to provide answers to questions posed in natural language. There are two main types:

  • Abstractive QA: The model generates a new answer in its own words, like a human would.
  • Extractive QA: The model is given a context (a paragraph of text) and a question, and its job is to find and extract the exact span of text from the context that answers the question.

Extractive QA is a more constrained and well-defined problem, and modern transformer models have become incredibly proficient at it, largely thanks to benchmarks like the Stanford Question Answering Dataset (SQuAD).

The Tool: The Hugging Face Transformers pipeline

As with other common NLP tasks, the pipeline function from the Hugging Face Transformers library is the easiest and most effective way to use a powerful, pre-trained QA model.

When you create a question-answering pipeline, it will download a model (like DistilBERT) that has been fine-tuned on the SQuAD dataset. This fine-tuning process specifically teaches the model how to perform the extractive QA task.

How it Works (A High-Level View)

  1. The model receives the question and the context as a combined input. Special tokens are used to separate them.
  2. The input is passed through the transformer architecture (e.g., BERT).
  3. At the final layer, the model doesn't output a class label. Instead, it has two separate output heads:
  • One head outputs a probability for every word in the context being the start of the answer.
  • The other head outputs a probability for every word being the end of the answer.
  1. The pipeline then searches for the (start_word, end_word) pair that has the highest combined probability and extracts that span as the final answer.

Code Example: Building a QA System

Let's build a simple QA system that can answer questions about the Apollo 11 moon landing.

First, install the library: pip install transformers

Code Snippet: Extractive QA with Transformers

Python


from transformers import pipeline

# 1. Create the question-answering pipeline.
# This will download and cache a default model fine-tuned on SQuAD.
qa_pipeline = pipeline("question-answering")

# 2. Define the context (the source of truth).
context = """
The Apollo 11 mission was the first crewed mission to land on the Moon.
The mission was launched by a Saturn V rocket from Kennedy Space Center on Merritt Island, Florida, on July 16, 1969.
The crew consisted of Neil Armstrong as Commander, Michael Collins as Command Module Pilot, and Buzz Aldrin as Lunar Module Pilot.
On July 20, Armstrong and Aldrin landed the Apollo Lunar Module Eagle and became the first two humans to walk on the lunar surface.
"""

# 3. Ask questions!
question_1 = "Who was the commander of the Apollo 11 mission?"
question_2 = "What was the name of the lunar module?"
question_3 = "When was the mission launched?"

# 4. Get the answers from the pipeline.
result_1 = qa_pipeline(question=question_1, context=context)
result_2 = qa_pipeline(question=question_2, context=context)
result_3 = qa_pipeline(question=question_3, context=context)

# 5. Print the results. The output is a dictionary.
print(f"Q: {question_1}")
print(f"A: {result_1['answer']} (Score: {result_1['score']:.4f})\n")

print(f"Q: {question_2}")
print(f"A: {result_2['answer']} (Score: {result_2['score']:.4f})\n")

print(f"Q: {question_3}")
print(f"A: {result_3['answer']} (Score: {result_3['score']:.4f})\n")

# Expected Output:
# Q: Who was the commander of the Apollo 11 mission?
# A: Neil Armstrong (Score: 0.99...)
#
# Q: What was the name of the lunar module?
# A: Eagle (Score: 0.93...)
#
# Q: When was the mission launched?
# A: July 16, 1969 (Score: 0.98...)