This is a simple example demonstrating how to build a RAG system that retrieves relevant information from documents and uses an LLM to generate answers.
RAG (Retrieval-Augmented Generation) combines:
- Retrieval: Finding relevant information from a knowledge base
- Augmentation: Enhancing the LLM prompt with retrieved context
- Generation: Using an LLM to generate accurate answers based on the context
rag-llm-example/
├── README.md # This file
├── requirements.txt # Python dependencies
├── simple_rag.py # Basic RAG implementation
├── advanced_rag.py # Advanced RAG with vector database
├── documents/ # Sample documents directory
│ └── sample_docs.txt # Sample knowledge base
└── .env.example # Environment variables template
pip install -r requirements.txtCopy .env.example to .env and add your API key:
# For GitHub Models (Free to start)
GITHUB_TOKEN=your_github_personal_access_token
# OR for Azure OpenAI
# AZURE_OPENAI_ENDPOINT=your_endpoint
# AZURE_OPENAI_API_KEY=your_keyTo get a GitHub Personal Access Token:
- Go to https://github.com/settings/tokens
- Generate a new token (classic)
- No special scopes needed for GitHub Models
Simple RAG (no vector database):
python simple_rag.pyAdvanced RAG (with ChromaDB vector database):
python advanced_rag.py- Loads documents from the
documents/folder - Chunks documents into smaller pieces
- Uses simple keyword matching to find relevant chunks
- Sends relevant chunks + user question to the LLM
- Returns AI-generated answer based on the context
- Loads and chunks documents
- Creates embeddings (vector representations) of chunks
- Stores embeddings in ChromaDB vector database
- Uses semantic search to find relevant chunks
- Sends relevant chunks + user question to the LLM
- Returns AI-generated answer with sources
- "What is machine learning?"
- "Explain neural networks"
- "What are the benefits of deep learning?"
- Place
.txt,.pdf, or.mdfiles in thedocuments/folder - The system will automatically load and process them
Edit the model name in the Python files:
model = "gpt-4.1-mini" # Change to any supported modelIn advanced_rag.py, modify:
results = collection.query(
query_texts=[question],
n_results=3 # Number of chunks to retrieve
)User Question
↓
[Document Loader]
↓
[Text Chunker]
↓
[Embedding Model] → [Vector Database (ChromaDB)]
↓
[Semantic Search] ← User Question
↓
[Retrieved Chunks]
↓
[LLM (GPT-4.1-mini)] ← Question + Context
↓
Generated Answer
- LLM: OpenAI GPT-4.1-mini (via GitHub Models)
- Embeddings: OpenAI text-embedding-3-small
- Vector Database: ChromaDB
- Document Processing: LangChain
- API Client: OpenAI Python SDK
- Add support for more document types (PDF, DOCX, HTML)
- Implement conversation history
- Add a web interface with Streamlit or Gradio
- Deploy to Azure or other cloud platforms
- Add caching for faster responses
- Implement query rewriting for better retrieval