Python / Python Modern Generative AI and Agents Interview Questions
How do you build a complete RAG pipeline using LangChain?
LangChain provides composable abstractions for every component of a RAG pipeline — document loaders, text splitters, embedding models, vector stores, retrievers, and LLM chains — making it straightforward to assemble a production-quality system without boilerplate.
The pipeline follows the standard RAG pattern: load and split documents into chunks, embed and index the chunks, then at query time retrieve the top-k relevant chunks and pass them with the question to an LLM for answer generation. LangChain's LCEL (LangChain Expression Language) uses the pipe operator | to compose these steps into a clean, readable chain.
# pip install langchain langchain-openai langchain-chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_chroma import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
# ── Step 1: Load and chunk documents
loader = WebBaseLoader('https://lilianweng.github.io/posts/2023-06-23-agent/')
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)
print(f'Created {len(chunks)} chunks')
# ── Step 2: Embed and index
embeddings = OpenAIEmbeddings(model='text-embedding-3-small')
vectorstore = Chroma.from_documents(chunks, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={'k': 4})
# ── Step 3: Define the RAG prompt and chain
prompt = ChatPromptTemplate.from_template("""
Answer the question using ONLY the following context.
If the answer is not in the context, say 'I don't know'.
Context:
{context}
Question: {question}
""")
def format_docs(docs):
return '\n\n'.join(d.page_content for d in docs)
llm = ChatOpenAI(model='gpt-4o-mini', temperature=0)
# LCEL chain: retriever | format | prompt | llm | parse
rag_chain = (
{'context': retriever | format_docs, 'question': RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
answer = rag_chain.invoke('What are the main components of an AI agent?')
print(answer)
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...
