Sub topic 5
Feeding the Question and Context Together
Assume you know basic Python, tokenization, and the idea of a pretrained language model. This chapter focuses on how a question-answering model receives two pieces of text as one input.
Sentence-pair encoding
Extractive question answering gives the model two strings:
- The question, which asks what information to find.
- The context, which contains the possible answer.
A transformer tokenizer combines them into one token sequence. For a BERT-style model, the sequence usually looks like this:
[CLS] question tokens [SEP] context tokens [SEP][CLS] is a special classification token. Each [SEP] token marks the end of one text segment. The model uses the combined sequence so that attention can connect words in the question with words in the context.
The tokenizer also creates supporting arrays:
| Array | Meaning |
|---|---|
| `input_ids` | Integer IDs representing the tokens |
| `attention_mask` | `1` for real tokens and `0` for padding |
| `token_type_ids` | Segment labels distinguishing question from context when supported |
For BERT, token_type_ids commonly contain 0 for the question segment and 1 for the context segment. This gives the encoder an explicit signal about each token's role. Some architectures, such as RoBERTa and DistilBERT, do not use segment IDs. They still separate the texts with special tokens, and the model architecture knows how to process the pair.
Inspecting the encoded pair
The tokenizer's pair syntax is important: pass the question as the first positional argument and the context as the second.
from transformers import AutoTokenizer
name = "distilbert-base-cased-distilled-squad"
tokenizer = AutoTokenizer.from_pretrained(name)
question = "What does the basin cover?"
context = (
"The Amazon River is the largest river by discharge. "
"Its drainage basin covers about seven million square kilometres."
)
encoded = tokenizer(
question,
context,
truncation="only_second",
max_length=64,
return_attention_mask=True,
return_offsets_mapping=True,
)
tokens = tokenizer.convert_ids_to_tokens(encoded["input_ids"])
sequence_ids = encoded.sequence_ids()
for token, segment, offset in zip(
tokens, sequence_ids, encoded["offset_mapping"]
):
print(f"{token:15} segment={segment} offset={offset}")Here, sequence_ids() normally returns:
Nonefor special tokens such as[CLS]and[SEP]0for question tokens1for context tokens
The sequence IDs are especially useful during answer decoding. A predicted answer must come from segment 1, because the answer is copied from the context. The offset mapping gives each token's (start, end) character positions in its original string. Those positions let you convert a predicted token span back into exact context text.
Truncation and masks
Transformer models have a maximum input length. If the pair is too long, truncation="only_second" removes tokens from the context while preserving the complete question. This is usually the correct choice for question answering.
Padding may be needed when processing several pairs together. The attention mask then prevents padding tokens from influencing the result.
Exercise
Exercise
Why should question-answering code use truncation="only_second" instead of truncating both sequences equally?
Show answer
The question is usually short and must remain intact. Truncating only the second sequence preserves the question while removing excess context tokens when the pair exceeds the model's maximum length.