HomeInterview QuestionsWrite code for a Retrieval‑Augmented Generation (R…

Write code for a Retrieval‑Augmented Generation (RAG) based chatbot in Python. You may use any PDF processing library for design.

🔴 Hard Coding Senior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

A typical RAG pipeline consists of three stages: (1) document ingestion, (2) retrieval, and (3) generation. Below is a concise implementation using LangChain, OpenAI embeddings, and a local vector store.

python
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

# 1. Load PDF and split into chunks
loader = PyPDFLoader("sample.pdf")
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = text_splitter.split_documents(docs)

# 2. Embed and store in FAISS
embeddings = OpenAIEmbeddings()
vector_store = FAISS.from_documents(chunks, embeddings)

# 3. Build RetrievalQA chain
llm = OpenAI(temperature=0)
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vector_store.as_retriever(search_kwargs={"k": 4})
)

# 4. Query
question = "What is the main conclusion of the report?"
answer = qa_chain.run(question)
print(answer)

Complexity: Ingestion is O(n) in the number of characters. Retrieval is O(log m) per query for FAISS, where m is the number of vectors. Generation cost depends on the LLM token count. The overall latency is dominated by the LLM call. This skeleton can be extended with caching, batching, or a more sophisticated retriever (e.g., BM25).

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500