Python / Python Modern Generative AI and Agents Interview Questions
What is Retrieval-Augmented Generation (RAG) and why is it preferred over full fine-tuning for knowledge-intensive tasks?
Retrieval-Augmented Generation (RAG) augments an LLM's response by first retrieving relevant documents from an external knowledge source and injecting them into the prompt as context. Instead of relying solely on knowledge baked into model weights during training, the LLM reasons over dynamically fetched, up-to-date, and verifiable text passages.
RAG is preferred over full fine-tuning for knowledge-intensive tasks for several practical reasons: fine-tuning requires substantial labeled data, significant compute, and retraining whenever the knowledge base changes; RAG's knowledge can be updated instantly by changing the document store. RAG also reduces hallucination — the model is grounded in retrieved text it can cite — and enables attribution of answers to specific sources.
| Aspect | RAG | Fine-tuning |
|---|---|---|
| Knowledge update cost | Instant — add docs to store | Re-train or re-fine-tune |
| Hallucination risk | Lower — grounded in retrieved text | Higher — relies on memorised weights |
| Required training data | None for base RAG | Hundreds to thousands of examples |
| Compute cost | Low (only inference) | High (GPU training hours) |
| Handles private/new data | Yes | Only if re-trained on it |
| Style / tone adaptation | Limited | Strong |
# Conceptual RAG pipeline (full implementation in Q08)
# 1. INDEX: chunk documents, embed each chunk, store in vector DB
# 2. RETRIEVE: embed user query, find k nearest chunks by cosine similarity
# 3. GENERATE: inject retrieved chunks as context, call LLM
SYSTEM = (
'You are a helpful assistant. Answer the user question using ONLY '
'the context provided below. If the answer is not in the context, '
'say you do not know. Always cite the source document.\n\n'
'Context:\n{context}'
)
def rag_answer(query: str, retrieved_docs: list[dict]) -> str:
context = '\n---\n'.join(
f"Source: {d['source']}\n{d['text']}" for d in retrieved_docs
)
from openai import OpenAI
client = OpenAI()
resp = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role': 'system', 'content': SYSTEM.format(context=context)},
{'role': 'user', 'content': query},
],
temperature=0.2,
)
return resp.choices[0].message.content
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
