RAG with LangChain and OpenAI
A quick guide to integrating your application with LangChain and OpenAI to fulfill your RAG requirements.
Sep 14, 2024 · 1 min read · Som

Retrieval-Augmented Generation (RAG) grounds an LLM in your data: you retrieve the most relevant chunks and feed them to the model as context, so answers cite your documents instead of the model's memory.
The pipeline in four steps
- Load & split your documents into chunks.
- Embed the chunks and store them in a vector database.
- Retrieve the top-k chunks for a user query.
- Generate an answer with the query + retrieved context.
Minimal LangChain sketch
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
store = FAISS.from_documents(docs, OpenAIEmbeddings())
retriever = store.as_retriever(search_kwargs={"k": 4})
# feed retriever results + question into a ChatOpenAI prompt
What makes RAG good
The quality is mostly in the retrieval: sensible chunking, good embeddings, and
the right k. Get those right and the generation step almost takes care of
itself.