Question Answering
Master Question Answering systems from extractive models to Generative AI and RAG.
Asking the Machines
Question Answering (QA) is a computer science discipline within the fields of AI and NLP that focuses on building systems that automatically answer questions posed by humans in natural language.
Extractive QA
The model identifies the exact span of text in the document that contains the answer. (Think: Pointing to the answer in a book)
Generative QA / RAG
The model generates a free-form answer based on retrieved documents or internal knowledge. (Think: Formulating its own sentence)
Level 1 — Extractive QA (Hugging Face)
Using models like BERT or RoBERTa, we can feed a context and a question, and get the exact start/end indices of the answer span.
from transformers import pipeline
qa_model = pipeline("question-answering", model="deepset/bert-base-cased-squad2")
context = "The Nile is the longest river in the world, stretching over 6,650 kilometers through northeastern Africa."
question = "How long is the Nile river?"
result = qa_model(question=question, context=context)
print(f"Answer: {result['answer']}")
print(f"Confidence: {round(result['score'], 4)}")
Level 2 — Generative QA & RAG (OpenAI / LangChain)
Instead of just pointing, Retrieval Augmented Generation (RAG) retrieves relevant documents and then uses an LLM to formulate a clean, natural response.
import openai
# RAG Simple Workflow logic
context_docs = "Solar panels convert sunlight into electrical energy using photovoltaic cells."
user_question = "How do solar panels work?"
prompt_template = f"""
Based tightly on the provided context, answer the user's question clearly.
Context: {context_docs}
Question: {user_question}
Answer:
"""
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt_template}]
)
print(response.choices[0].message.content)