Question Answering
Question answering systems map a user's natural language query to a precise answer by extracting it from context or generating it from learned knowledge.
Why Does This Exist?
When searching for information, users rarely want a list of ten blue links they must read through. They want an answer to their specific question. Question answering (QA) systems exist to bridge this gap, reading through documents (or relying on internalized knowledge) to provide the exact piece of information requested.
Before modern NLP, QA systems relied on brittle heuristics or structured databases. They required queries in SQL or strict formats. Today, QA models parse natural language questions and surface answers from unstructured text, dramatically reducing the time users spend hunting for information.
Think of It Like This
An open-book exam vs. a closed-book exam
Imagine taking a history test.
In extractive QA (the open-book exam), you are given a specific textbook chapter and asked a question. You scan the chapter, find the exact sentence that contains the answer, and highlight it. You do not invent new words; you just point to the span of text that holds the information.
In generative QA (the closed-book exam), you aren't given a book during the test. Instead, you rely on everything you studied (the model's training data). You synthesize an answer in your own words based on your internal knowledge.
How It Actually Works
Modern QA systems generally fall into three paradigms: extractive, abstractive (generative), and retrieval-augmented.
1. Extractive Question Answering
In extractive QA, the model is given a question and a context document. Its job is to predict the start and end positions of the answer within the context. The model outputs two probability distributions over the tokens in the document: one for the probability of being the start token, and one for the end token.
The answer is the span to that maximizes this joint probability, subject to .
2. Generative (Abstractive) Question Answering
Generative QA models, typically sequence-to-sequence architectures, do not just highlight text. They take the question (and optionally a context) and generate an answer token by token. This allows them to synthesize information from multiple parts of a text or rephrase it for clarity, but it introduces the risk of hallucination—generating plausible but incorrect answers.
3. Retrieval-Augmented Generation (RAG)
When a system needs to answer questions from a vast corpus (like Wikipedia or a company's internal wiki), it cannot feed all documents into the model at once. RAG solves this in two steps:
- Retriever: A lightweight model searches the corpus to find the top documents most relevant to the question.
- Reader/Generator: A more powerful QA model processes these retrieved documents to extract or generate the final answer.
Code
You can run a pre-trained extractive QA model easily using the Hugging Face pipeline.
from transformers import pipeline
# Load a pre-trained QA pipeline (defaults to an extractive model)qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
context = "The AI360Xpert platform was launched in 2026 to teach core ML and Gen AI concepts."question = "When was the platform launched?"
result = qa_pipeline(question=question, context=context)
print(f"Answer: '{result['answer']}'")print(f"Score: {result['score']:.4f}")print(f"Start: {result['start']}, End: {result['end']}")# -> Answer: '2026'# -> Score: 0.9850# -> Start: 44, End: 48Watch Out For
Assuming extraction means truth
An extractive QA model is trained to find the best possible answer in the provided context. If you give it a document that completely contradicts reality, it will still extract an answer. It does not verify the factual accuracy of the text; it only measures how well a text span answers the question grammatically and semantically.
Ignoring the 'No Answer' scenario
Many basic QA models will force an extraction even if the context does not contain the answer. If you ask "Who won the World Series in 2040?" and provide an article from 2020, a naive model will just guess a name. Production systems must be calibrated to predict an empty span or return a low confidence score when the answer is truly missing.
The Quick Version
- QA systems find or generate answers to natural language questions.
- Extractive QA predicts the start and end tokens of an answer within a given context document.
- Generative QA synthesizes an answer token by token, allowing for more flexible responses but risking hallucination.
- Open-domain QA uses a retriever to find relevant documents from a large corpus before applying a reader model.
- Always calibrate models to handle unanswerable questions gracefully rather than forcing an incorrect guess.