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)
More Related questions...