Retrieval-Augmented Generation (RAG) কী এবং কখন এটি ব্যবহার করবেন?
RAG বা Retrieval-Augmented Generation এমন একটি AI architecture যা external information retrieve করে LLM-কে সেই তথ্যের ভিত্তিতে answer generate করতে সাহায্য করে। এই পোস্টে RAG কী, কীভাবে কাজ করে, কেন গুরুত্বপূর্ণ এবং কখন ব্যবহার করা উচিত তা সহজ উদাহরণসহ ব্যাখ্যা করা হয়েছে।
Large Language Model বা LLM অনেক powerful হলেও এর একটি গুরুত্বপূর্ণ limitation রয়েছে।
আপনার application-এর প্রয়োজনীয় private, current বা domain-specific information model-এর training-এর মধ্যে নাও থাকতে পারে।
এই সমস্যা সমাধানে গুরুত্বপূর্ণ একটি পদ্ধতি হলো:
Retrieval-Augmented Generation বা RAG
RAG এমন একটি architecture যেখানে AI প্রথমে external source থেকে relevant information খুঁজে বের করে এবং তারপর সেই information LLM-কে context হিসেবে দিয়ে answer generate করায়।
RAG কী?
RAG-এর পূর্ণরূপ হলো Retrieval-Augmented Generation।
সহজভাবে RAG-এর দুটি প্রধান অংশ:
Retrieval → প্রয়োজনীয় information খুঁজে বের করা
Generation → সেই information ব্যবহার করে answer তৈরি করা
একটি সাধারণ LLM শুধু নিজের learned information-এর ভিত্তিতে answer দিতে পারে।
কিন্তু RAG system external database, document, knowledge base বা search system থেকে relevant information retrieve করে LLM-এর কাছে পাঠাতে পারে।
একটি সহজ উদাহরণ
ধরুন আপনি একটি company's AI chatbot তৈরি করেছেন।
Company-এর কাছে হাজার হাজার document আছে:
Employee policy
Product information
Pricing
Technical documentation
Company procedure
একজন employee জিজ্ঞেস করল:
"Annual leave কত দিন?"
সাধারণ LLM আপনার company's current policy নাও জানতে পারে।
কিন্তু RAG system:
Company knowledge base search করবে।
Relevant leave policy খুঁজে বের করবে।
সেই information LLM-কে দেবে।
LLM সেই information-এর ভিত্তিতে answer তৈরি করবে।
যেমন:
"Current company leave policy অনুযায়ী employees প্রতি বছর 20 দিন annual leave পান।"
RAG কেন গুরুত্বপূর্ণ?
কারণ একটি LLM সবসময় আপনার application-এর private বা latest information জানে না।
যেমন LLM জানে না:
আপনার internal company policy
Private database data
আপনার latest product price
নতুন uploaded document
আপনার internal knowledge base
RAG একটি bridge হিসেবে কাজ করে এবং LLM-এর সাথে external information যুক্ত করে।
RAG কীভাবে কাজ করে?
একটি সাধারণ RAG system কয়েকটি ধাপে কাজ করে।
ধাপ ১: Documents সংগ্রহ করা
প্রথমে relevant information সংগ্রহ করা হয়।
যেমন:
PDF
Website
Documentation
Word file
Database record
Internal company document
ধাপ ২: Documents ছোট ছোট Chunk-এ ভাগ করা
বড় document সাধারণত ছোট ছোট অংশে ভাগ করা হয়। এগুলোকে Chunk বলা হয়।
কেন?
কারণ প্রতিবার পুরো document LLM-এ পাঠানো inefficient হতে পারে এবং context limit-ও অতিক্রম করতে পারে।
একটি বড় document-কে কয়েকটি paragraph বা section অনুযায়ী ভাগ করা যেতে পারে।
ধাপ ৩: Embedding তৈরি করা
প্রতিটি chunk-কে একটি numerical representation-এ রূপান্তর করা হয়, যাকে Embedding বলা হয়।
Embedding text-এর semantic meaning represent করতে সাহায্য করে।
যেমন:
"How do I reset my password?"
এবং:
"I forgot my account password. How can I change it?"
দুটি বাক্যের wording আলাদা হলেও meaning কাছাকাছি।
তাই তাদের embedding vector-ও সাধারণত semantic space-এ কাছাকাছি হতে পারে।
ধাপ ৪: Embedding Store করা
এই embeddings এমন একটি system-এ সংরক্ষণ করা হয় যেখানে similarity search করা যায়।
যেমন:
Vector database
Search engine
Vector search-enabled database
ধাপ ৫: User Question-এর Embedding তৈরি
User যখন question করে, question-টিও embedding-এ রূপান্তর করা হয়।
যেমন:
"What is the refund policy?"
এই question-এর একটি vector representation তৈরি করা হয়।
ধাপ ৬: Relevant Information খুঁজে বের করা
তারপর question embedding-এর সাথে document embedding-এর similarity compare করা হয়।
সবচেয়ে relevant chunk-গুলো retrieve করা হয়।
যেমন:
Refund policy
Return condition
Refund processing time
ধাপ ৭: Retrieved Information LLM-কে দেওয়া
এখন retrieved information-কে LLM-এর prompt-এর context হিসেবে দেওয়া হয়।
সহজভাবে:
User Question + Retrieved Information → LLM
এখন LLM external information-এর ভিত্তিতে answer তৈরি করতে পারে।
ধাপ ৮: Final Answer Generate করা
LLM প্রশ্ন এবং retrieved context বিশ্লেষণ করে natural-language response তৈরি করে।
অনেক RAG system source citation বা document link-ও দেখায়।
এটি খুব গুরুত্বপূর্ণ, কারণ user চাইলে answer-এর source verify করতে পারে।
RAG-এর Architecture
সহজ workflow:
Documents
↓
Chunking
↓
Embeddings
↓
Vector Database
↓
User Question
↓
Query Embedding
↓
Similarity Search
↓
Relevant Chunks
↓
LLM
↓
Final Answer
সাধারণ LLM বনাম RAG
RAG ছাড়া:
User Question → LLM → Answer
RAG-এর ক্ষেত্রে:
User Question → Retrieval System → Relevant Information → LLM → Answer
দ্বিতীয় পদ্ধতিতে LLM external information ব্যবহার করতে পারে।
কখন RAG ব্যবহার করবেন?
যখন আপনার application-কে external, private, domain-specific অথবা frequently changing information ব্যবহার করতে হবে, তখন RAG খুব ভালো choice হতে পারে।
Company Knowledge Assistant
Employee company policy, internal document বা procedure সম্পর্কে প্রশ্ন করতে পারবে।
Customer Support
AI product manual, troubleshooting guide এবং support article retrieve করে customer-কে answer দিতে পারবে।
Legal Document Search
Contract বা legal document collection থেকে relevant section retrieve করে system answer তৈরিতে সাহায্য করতে পারে।
Research Knowledge System
Approved research বা document repository থেকে relevant information retrieve করে answer তৈরি করা যেতে পারে।
High-stakes domain হলে appropriate safeguards এবং expert review গুরুত্বপূর্ণ।
E-Commerce Assistant
Product price, stock, specification এবং return policy-এর মতো current information retrieve করে customer-কে answer দেওয়া যায়।
Website Chatbot
Website-এর নিজের documentation ব্যবহার করে chatbot visitor-এর প্রশ্নের উত্তর দিতে পারে।
RAG বনাম Fine-Tuning
এটি খুব গুরুত্বপূর্ণ একটি প্রশ্ন:
RAG ব্যবহার করব, নাকি Fine-Tuning?
দুটির উদ্দেশ্য আলাদা।
RAG ব্যবহার করুন যখন model-কে external বা frequently changing information দিতে চান।
Fine-Tuning ব্যবহার করুন যখন model-এর behavior, style বা specific task performance পরিবর্তন বা specialize করতে চান।
উদাহরণ:
Company policy বারবার পরিবর্তন হয় → RAG
একটি নির্দিষ্ট response style consistently follow করাতে চান → Fine-Tuning useful হতে পারে
প্রয়োজনে একই application-এ RAG এবং Fine-Tuning দুটোই ব্যবহার করা যায়।
RAG-এর সুবিধা
External information ব্যবহার করা যায়
নিজস্ব document ও system-এর data LLM-এর কাছে পৌঁছে দেওয়া যায়।
Current information ব্যবহার করা যায়
Knowledge source update করলেই নতুন information ব্যবহার করা সম্ভব; পুরো model retrain করা সবসময় দরকার হয় না।
Domain-specific knowledge
বিশেষ কোনো industry বা company's information নিয়ে AI system তৈরি করা যায়।
Source citation
Answer-এর সাথে source দেখানো সম্ভব।
Hallucination কমাতে সাহায্য করতে পারে
Relevant source information LLM-কে দেওয়া হলে unsupported answer-এর ঝুঁকি কমতে পারে। তবে RAG correctness-এর নিশ্চয়তা দেয় না।
RAG-এর সীমাবদ্ধতা
RAG perfect নয়।
সমস্যা হতে পারে যদি:
ভুল document retrieve হয়
প্রয়োজনীয় information source-এ না থাকে
Document ভুলভাবে chunk করা হয়
Embedding relevant meaning ঠিকভাবে capture না করে
অতিরিক্ত irrelevant information retrieve হয়
LLM retrieved context ভুলভাবে বুঝে
তাই retrieval quality অত্যন্ত গুরুত্বপূর্ণ।
খারাপ information retrieve হলে powerful LLM-ও খারাপ answer দিতে পারে।
RAG মানেই শুধু Vector Database নয়
অনেকের একটি ভুল ধারণা:
RAG = Vector Database
এটি সঠিক নয়।
Vector database RAG-এর একটি component হতে পারে। কিন্তু RAG হলো সম্পূর্ণ একটি architecture।
একটি বাস্তব RAG system-এ থাকতে পারে:
Document ingestion
Document parsing
Chunking
Embeddings
Retrieval
Ranking
Filtering
Prompt construction
LLM generation
Citation
Access control
Monitoring
Evaluation
একটি সহজ বাস্তব উদাহরণ
ধরুন আপনি open-book exam দিচ্ছেন।
আপনি অনেক general knowledge জানেন, কিন্তু teacher আপনাকে textbook ব্যবহার করতে দিয়েছেন।
প্রশ্ন পাওয়ার পরে আপনি:
Textbook search করেন।
Relevant section খুঁজে বের করেন।
সেই অংশ পড়েন।
Information ব্যবহার করে answer দেন।
RAG-এর concept অনেকটা এমন।
Textbook = External Knowledge
Search = Retrieval
Student's Answer = Generation
শেষ কথা
RAG হলো LLM-এর সাথে external knowledge যুক্ত করার একটি পদ্ধতি।
সহজ workflow:
Question → Relevant Information Retrieve → LLM-কে Context দেওয়া → Answer Generate
RAG বিশেষভাবে useful যখন আপনার AI application-এর প্রয়োজন:
Private information
Company documents
Current information
Product data
Technical documentation
Frequently changing knowledge
সবচেয়ে গুরুত্বপূর্ণ পার্থক্য মনে রাখুন:
Fine-Tuning model-এর behavior পরিবর্তন করে।
RAG model-কে answer দেওয়ার সময় অতিরিক্ত information দেয়।
Real-world AI application তৈরি করার সময় এই পার্থক্য বোঝা অত্যন্ত গুরুত্বপূর্ণ।