RAG and Vector Databases Tutorial

Welcome to Kenhuang Academy’s RAG (Retrieval-Augmented Generation) course. This tutorial is framework-agnostic: chunking, embeddings, vector stores, retrieve / rerank, and evaluation. The three runnable stores are Chroma, pgvector, and Qdrant.

LangChain, LlamaIndex, and Dify all implement the same pipeline. Those courses cover product APIs. Principles and storage belong here.


Table of Contents

Principles

  1. Introduction — Why retrieve; RAG vs fine-tuning vs long context
  2. Pipeline — documents → chunk → embed → vector store → retrieve → rerank → LLM
  3. Chunking — Size, overlap, headers, parent-child chunks
  4. Embeddings — OpenAI text-embedding-3-small or local Ollama / BGE (Chinese)

Vector stores

  1. Chroma — Local directory, PersistentClient, add / query
  2. pgvector — PostgreSQL extension, cosine query, existing databases
  3. Qdrant — Docker, HTTP 6333, upsert / search and filters

Quality

  1. Retrieval — top-k, hybrid search, optional rerank, metadata filters
  2. Evaluation — hit@k, faithfulness, “answer only from context”
  3. Practical Examples — Markdown FAQ, existing Postgres, Qdrant filter / hybrid
  4. Resources — Official docs and related courses on this site

Learning path

StageGoalChapters
Day 1Mental modelIntro → Pipeline → Chunking → Embeddings
Day 2A local storeChroma + a FAQ you wrote
Day 3Real storagepgvector or Qdrant
Week 1Measure and fixRetrieval → Evaluation → Examples

Get Chroma green first, then choose Postgres or a dedicated vector service. Do not install all three on day one.


You want to…Go here
Chunk / embed / stores / eval (principles)This tutorial
LangChain loaders, retrievers, agentic RAGLangChain RAG
Index- and query-engine-centric frameworkLlamaIndex
Local embedding models, /api/embedOllama Embeddings
Open-source embedding models on the Hub (BGE)Hugging Face
Console knowledge bases (no-code RAG)Dify Knowledge Base
SQL, tables, and indexesPostgreSQL

Prerequisites

  • Python 3.10+ (functions, lists, virtual environments)
  • One Chat Completions call is enough; you do not need an agent framework yet
  • At least one embedding path: an OpenAI API key, or local Ollama (CPU is fine; no GPU required)
  • PostgreSQL for the pgvector chapter; Docker for Qdrant
  • Windows: PowerShell; bash commands are listed too

You do not need to implement a Transformer, and you do not need a GPU.


Study tips

  1. Use facts the web cannot know (made-up policy numbers). Otherwise you cannot tell retrieval from memorization
  2. Index and query with the same embedding model; switching models means re-embedding the collection
  3. Fix retrieval (hit@k) before rewriting the generation prompt
  4. Take LangChain / LlamaIndex / Dify after this pipeline, so you are not stuck memorizing class names

Next steps

评论