Sub topic 7
Your First Answer with the Question-Answering Pipeline
This chapter assumes that you know basic Python, understand that a tokenizer converts text into model inputs, and have a general idea of tensors and pretrained language models. You will use a ready-made extractive question-answering model. Extractive means that the answer is copied from the context rather than generated as new text.
What the pipeline does
A question-answering pipeline accepts two strings:
question: what you want to knowcontext: the passage that may contain the answer
The pipeline tokenizes both strings, sends them through a transformer encoder, scores possible answer spans, and converts the best span back into text. It does not search the web or retrieve documents. You must provide the context yourself.
from transformers import pipeline
qa = pipeline(
"question-answering",
model="distilbert-base-cased-distilled-squad",
)
context = (
"The Amazon River in South America is the largest river by discharge "
"volume of water in the world. Its drainage basin covers about seven "
"million square kilometres."
)
result = qa(
question="What does the Amazon basin cover?",
context=context,
)
print(result)
print("Answer:", result["answer"])The model name identifies a DistilBERT encoder fine-tuned on SQuAD, a dataset for question answering. The pipeline downloads the model and tokenizer the first time it runs. Later calls can reuse the loaded qa object.
A typical result is a dictionary like this:
| Field | Meaning |
|---|---|
| `answer` | Text selected from the context |
| `score` | Model confidence for the selected span |
| `start` | Character index where the answer begins |
| `end` | Character index where the answer ends |
The answer may be about seven million square kilometres. The character positions refer to the original context string, not to token positions.
How the answer is selected
Internally, the model reads the question and context as one paired input. Its question-answering head produces two scores for every input token:
- A start score: how likely the token is to begin the answer
- An end score: how likely the token is to finish the answer
The pipeline searches for a valid span whose start comes before its end. It then uses the tokenizer's mapping back to the original characters to return the answer string.
The score is useful for ranking answers, but it is not a guaranteed probability that the answer is correct. A model can assign a high score to a plausible but incorrect span, especially when the context is ambiguous or the question requires reasoning beyond the passage.
> callout note > Keep the question and context separate in your code. The model needs the question to identify the desired information and the context to constrain where the answer can come from.
Checking the returned span
Because start and end are character offsets, you can verify the result directly:
answer = context[result["start"]:result["end"]]
assert answer == result["answer"]
print(answer)This check is useful when integrating the pipeline into an application. It confirms that the reported text actually matches the reported positions in the source context.
A question may also be poorly matched to its context. For example, asking “Who discovered the Amazon River?” cannot be answered from the passage above. Basic pipeline usage may still return the most plausible span unless you use a model and decoding method that support unanswerable questions.
Exercise
What must you provide to an extractive question-answering pipeline, and what do the start and end fields identify?
Show answer
You must provide a question and a context passage. The start and end fields identify the answer's character positions in the original context string.