Question Answering Systems
Document থেকে answer extract করা।
Google এ 'capital of Bangladesh' search করলে শুধু link না — সরাসরি 'Dhaka' answer আসে। কীভাবে? Question Answering (QA) — যেখানে model একটা context (passage) থেকে exact answer extract করে অথবা generate করে। SQuAD benchmark, RAG, ChatGPT — সবার heart এ QA।
QA সিস্টেম দুই ধরনের: (1) Extractive — passage থেকে exact text span extract (BERT-based, SQuAD style), (2) Generative — context থেকে নতুন answer generate (T5, GPT)। Architecture: extractive এ model প্রতিটা token এর জন্য start/end logit দেয় — সবচেয়ে probable span ই answer। Open-domain QA: প্রথমে retriever passage খুঁজে আনে, তারপর reader answer বের করে।
ভাবুন একটা book exam — passage দেওয়া আছে, question দেওয়া আছে, আপনি passage এ একটা phrase underline করে answer। Extractive QA ঠিক তাই। Open-book exam যেখানে আপনি library থেকে relevant book খুঁজে এনে answer লিখছেন — সেটা open-domain QA (retrieval + reading) — modern RAG এর precursor।
from transformers import pipeline
qa = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
context = (
"Bangladesh is a country in South Asia. Its capital and largest city is Dhaka. "
"The official language is Bangla and the currency is the Taka. "
"Bangladesh gained independence from Pakistan in 1971 after a nine month liberation war."
)
questions = [
"What is the capital of Bangladesh?",
"When did Bangladesh gain independence?",
"What is the official language?",
"What is the currency of Bangladesh?",
]
for q in questions:
result = qa(question=q, context=context)
print(f"Q: {q}")
print(f"A: {result['answer']} (score: {result['score']:.3f})\n")pipeline('question-answering') extractive QA model load করে। প্রতিটা question + context pair কে tokenize করে model এ pass করে, best start/end span বের করে answer string return করে। score দিয়ে model এর confidence বোঝা যায়।
একটা CLI tool: user একটা text file (article/report) দেয়, তারপর question জিজ্ঞেস করতে পারে loop এ। distilbert-squad দিয়ে answer + confidence। Confidence < 0.3 হলে 'উত্তর পাওয়া যায়নি' message।