from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_text(document)
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Chroma.from_texts(chunks, embeddings)
Complete RAG Pipeline
from langchain.chains import RetrievalQA
qa = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
)
result = qa.invoke("What is the capital of France?")
print(result)
RAG Evaluations
Faithfulness — Does the answer match the retrieved context?
Relevance — Is the retrieved context relevant to the query?
LLM-as-Judge — Use an LLM to rate answer quality
Advanced RAG Techniques
Pre-processing — Clean documents before chunking
Re-ranking — Re-rank retrieved chunks with a cross-encoder
Query Expansion — Generate multiple queries from one question
GraphRAG — Build a knowledge graph from documents for better retrieval
✏️ Exercise: Build a complete RAG pipeline with conversation history. Use LangChain, ChromaDB, and a Gradio chat interface. Implement: (1) document upload and chunking with overlap, (2) vector embedding and indexing, (3) hybrid search (keyword + vector), (4) conversation-aware retrieval, and (5) an LLM-as-Judge evaluation step.