Skip to content

Complete RAG implementation in Python with document loading, embeddings, vector storage, and LLM integration

Notifications You must be signed in to change notification settings

jmoliugp/rag-playground

Repository files navigation

πŸ€– RAG Playground

An educational Python project to demonstrate how RAG (Retrieval-Augmented Generation) systems work.

πŸ“‹ Description

This project implements a complete RAG system from scratch, showing each component of the process:

  • Document loading
  • Processing and chunking
  • Embedding generation
  • Vector storage
  • Information retrieval
  • Answer generation with LLMs

πŸš€ Quick Start

Prerequisites

  • Python 3.10 or higher
  • Node.js 18+ (for the frontend)
  • OpenAI API Key (to use GPT models)

Backend Installation

  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
pip install -r backend/requirements.txt
  1. Configure environment variables

Copy the .env.example file to .env and configure your API key:

cp .env.example .env

Edit .env and add your OpenAI API Key:

OPENAI_API_KEY=your_api_key_here

Frontend Installation

cd frontend
npm install

🎯 Usage

Option 1: Web Frontend (Recommended)

  1. Start Backend:
# In one terminal
source venv/bin/activate
cd backend
python main.py

The backend will be at http://localhost:8000

  1. Start Frontend:
# In another terminal
cd frontend
npm run dev

The frontend will be at http://localhost:5173

Option 2: Web Interface (Streamlit)

Run the Streamlit application:

streamlit run app/main.py

Option 3: Jupyter Notebooks

Explore the educational notebooks in the notebooks/ folder:

  1. 01_data_loading.ipynb - Document loading
  2. 02_embeddings.ipynb - Embedding generation
  3. 03_retrieval.ipynb - Retrieval system
  4. 04_full_rag.ipynb - Complete RAG system

Option 4: Programmatic Usage

from src.document_loader import DocumentLoader
from src.text_splitter import TextSplitter
from src.embeddings import EmbeddingGenerator
from src.vector_store import VectorStore
from src.retriever import Retriever
from src.rag_chain import RAGChain

# 1. Load documents
loader = DocumentLoader()
documents = loader.load_directory("./data/documents")

# 2. Split into chunks
splitter = TextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(documents)

# 3. Generate embeddings
embedding_gen = EmbeddingGenerator()
chunks_with_embeddings = embedding_gen.generate_embeddings_for_chunks(chunks)

# 4. Store in vector store
vector_store = VectorStore(collection_name="my_rag", reset=True)
vector_store.add_chunks(chunks_with_embeddings)

# 5. Create retriever and RAG chain
retriever = Retriever(vector_store, embedding_gen, top_k=3)
rag_chain = RAGChain(retriever, model_name="gpt-3.5-turbo")

# 6. Ask a question
result = rag_chain.query("What is RAG?")
print(result['answer'])

πŸ“ Project Structure

rag-playground/
β”œβ”€β”€ src/                    # Main source code
β”‚   β”œβ”€β”€ document_loader.py  # Document loading
β”‚   β”œβ”€β”€ text_splitter.py    # Chunk splitting
β”‚   β”œβ”€β”€ embeddings.py       # Embedding generation
β”‚   β”œβ”€β”€ vector_store.py     # Vector database
β”‚   β”œβ”€β”€ retriever.py        # Retrieval system
β”‚   β”œβ”€β”€ rag_chain.py        # Complete RAG chain
β”‚   └── utils.py            # Utilities
β”œβ”€β”€ backend/                # FastAPI Backend
β”‚   β”œβ”€β”€ main.py             # API server
β”‚   └── requirements.txt    # Backend dependencies
β”œβ”€β”€ frontend/               # React Frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/     # React components
β”‚   β”‚   └── App.jsx         # Main app
β”‚   └── package.json        # Frontend dependencies
β”œβ”€β”€ app/                    # Streamlit application
β”‚   └── main.py             # Web interface
β”œβ”€β”€ data/                   # Data
β”‚   β”œβ”€β”€ documents/          # Example documents
β”‚   └── vector_db/          # Vector database (generated)
β”œβ”€β”€ notebooks/              # Educational notebooks
β”œβ”€β”€ tests/                  # Unit tests
└── requirements.txt        # Python dependencies

πŸ”§ Configuration

You can configure various aspects of the system by editing the .env file:

  • OPENAI_API_KEY: Your OpenAI API key
  • LLM_MODEL: Model to use (default: gpt-3.5-turbo)
  • LLM_TEMPERATURE: Temperature for generation (0.0-1.0)
  • EMBEDDING_MODEL: Embedding model (default: sentence-transformers/all-MiniLM-L6-v2)
  • CHUNK_SIZE: Chunk size (default: 1000)
  • CHUNK_OVERLAP: Overlap between chunks (default: 200)
  • TOP_K: Number of chunks to retrieve (default: 3)

πŸ§ͺ Testing

Run tests with pytest:

pytest tests/

πŸ“š Example Documents

The project includes example documents in data/documents/:

  • sample1.txt - Introduction to RAG
  • sample2.txt - Embeddings and vector representations
  • sample3.md - Chunking and processing

You can add your own documents to experiment.

πŸŽ“ Concepts Learned

This project demonstrates:

  1. Document Loading: How to load different formats (PDF, TXT, MD, DOCX)
  2. Chunking: Strategies for splitting documents into manageable fragments
  3. Embeddings: How to convert text into vector representations
  4. Vector Stores: Storage and search of embeddings
  5. Retrieval: How to find relevant information using similarity
  6. RAG Chain: Combination of retrieval and generation with LLMs

πŸ› οΈ Technologies Used

Backend

  • Python 3.10+
  • LangChain - Framework for LLM applications
  • ChromaDB - Vector database
  • Sentence Transformers - Embedding models
  • OpenAI API - Language models
  • FastAPI - REST API

Frontend

  • React 19 - UI framework
  • Tailwind CSS - Styling
  • Vite - Build tool

Others

  • Streamlit - Alternative web interface
  • PyPDF2 - PDF processing

πŸ“ License

This is an open-source educational project. Feel free to use and modify it.

🀝 Contributions

Contributions are welcome! If you find bugs or have suggestions, please open an issue or submit a pull request.

πŸ“– Additional Resources

⚠️ Notes

  • This project requires an OpenAI API key to function completely
  • Embedding models are downloaded automatically the first time
  • The vector database is created automatically in data/vector_db/

Enjoy exploring RAG! πŸš€

About

Complete RAG implementation in Python with document loading, embeddings, vector storage, and LLM integration

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published