Large language models know only what was in their training data. When you need a model to answer questions about your internal documents, recent events, or private knowledge bases, Retrieval-Augmented Generation (RAG) is the standard approach. RAG works in three steps: retrieve the most relevant chunks from your data store, inject those chunks into the prompt as context, then let the model generate an answer grounded in what you provided. The result is accurate, citable responses without the unpredictability of fine-tuning.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/openai/openai-cookbook/llms.txt
Use this file to discover all available pages before exploring further.
Why RAG instead of fine-tuning
Fine-tuning teaches a model a style or task — it is less reliable for factual recall. As the Question Answering cookbook puts it: model weights are like long-term memory, while message inputs are like open notes during an exam. When you inject relevant text into a prompt, the model is far more likely to produce correct, specific answers than when it tries to recall information it saw during training.RAG
- Works immediately — no training required
- Knowledge stays current; update the data store, not the model
- Responses are grounded in retrieved text and can cite sources
- Better for factual, document-heavy use cases
Fine-tuning
- Teaches the model a consistent format or domain style
- Lower inference cost once trained
- Knowledge is baked in and cannot be updated without retraining
- Better for output style, tone, and specialized task formats
The core RAG pipeline
Chunk and embed your documents
Split source documents into chunks that fit within the embedding model’s token limit (8,191 tokens for
text-embedding-3-small). Embed each chunk and store the vector alongside the original text.Embed the user query
At query time, embed the user’s question with the same model used to embed the corpus. Using a different model will produce vectors in a different space and break similarity comparisons.
Retrieve the most relevant chunks
Score every chunk by cosine similarity to the query embedding and take the top-k results.
Chunking PDFs and unstructured documents
PDFs are a common source for RAG pipelines, but their structure varies widely. Slide decks, scanned documents, and text-heavy reports each require different handling.The Parse PDF docs for RAG cookbook example demonstrates two complementary approaches: extracting text with
pdfminer for digitally-born PDFs, and using GPT-4o vision to analyze page images for layout-heavy documents like slide decks.Text extraction with pdfminer
Vision-based extraction with GPT-4o
For PDFs with tables, diagrams, or complex layouts where text extraction loses structure, convert pages to images and describe them with GPT-4o:Vector storage options
For production RAG systems, store embeddings in a vector database rather than in memory. Vector databases support approximate nearest-neighbor (ANN) search that scales to millions of documents.| Option | Type | Best for |
|---|---|---|
| Pinecone | Managed cloud | Fast setup, serverless scaling |
| Weaviate | Open-source / cloud | Hybrid search, GraphQL API |
| Qdrant | Open-source / cloud | High-throughput, Rust-based |
| pgvector | PostgreSQL extension | Teams already on Postgres |
| Chroma | Open-source | Local development and prototyping |
Query reformulation
Raw user questions are not always the best retrieval queries. A few techniques improve recall: HyDE (Hypothetical Document Embeddings): Generate a hypothetical answer to the question, embed that answer, and use it as the query vector. The hypothetical answer tends to be closer in embedding space to real answers than the original question.Hybrid search
Combining embedding similarity with keyword-based scoring (BM25 or full-text search) reduces the chance of missing relevant documents that happen to be far from the query in embedding space. Most production vector databases support hybrid search natively with a configurable weight between the two signals.α = 0.7 is a common starting point; tune on a held-out evaluation set.
When RAG works well vs. when it struggles
RAG works well when...
- The answer exists verbatim or paraphrased in the corpus
- The corpus is updated frequently
- You need citations or source attribution
- Questions are specific and factual
RAG struggles when...
- The question requires synthesizing many distant documents
- The corpus is noisy or poorly chunked
- Questions require multi-hop reasoning across chunks
- Retrieval recall is low (wrong chunks surface for the query)
Next steps
- Semantic text search — deep dive into cosine similarity, chunking, and vector databases
- Embeddings overview — model options, dimensions, and token limits