A simple RAG (Retrieval-Augmented Generation) implementation using LlamaIndex and OpenAI. This project demonstrates different approaches to building a RAG system with increasing levels of sophistication.
- Basic RAG implementation with document indexing and chat interface
- Advanced version with text splitting and chat history
- Dynamic data version with automatic reindexing on data changes
- Persistent storage of vector indices
- Streaming responses for better user experience
- Python 3.10+
- OpenAI API key
- Clone the repository:
git clone git@github.com:sajithamma/simplerag.git
cd simplerag- Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root:
OPENAI_API_KEY=your_api_key_hereThe project contains three main implementations:
app.py- Basic RAG implementationapp-advanced.py- Advanced version with text splitting and chat historyapp-dynamic-data.py- Full version with automatic reindexing
simplerag/
├── data/ # Directory for your documents
├── storage/ # Directory for storing vector indices
├── app.py # Basic implementation
├── app-advanced.py # Advanced implementation
├── app-dynamic-data.py # Full implementation
└── requirements.txt # Project dependencies
Simple RAG implementation with basic document indexing:
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
# Load and index documents
data = SimpleDirectoryReader(input_dir="./data").load_data()
index = VectorStoreIndex.from_documents(data)
# Create chat engine
chat_engine = index.as_chat_engine(
llm=OpenAI(model="gpt-4"),
verbose=False
)
# Chat interface
response = chat_engine.stream_chat("Your question here")Includes text splitting and chat history:
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.base.llms.types import ChatMessage, MessageRole
# Configure text splitting
text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=10)
# Create index with text splitting
index = VectorStoreIndex.from_documents(
data,
transformations=[text_splitter]
)
# Chat with history
chat_history = []
user_message = ChatMessage(role=MessageRole.USER, content="Your question")
chat_history.append(user_message)
response = chat_engine.stream_chat(user_message.content, chat_history=chat_history)Includes automatic reindexing when data changes:
# Check if data has changed
needs_reindex = has_data_changed() or not os.path.exists("storage/docstore.json")
if needs_reindex:
# Create new index
index = VectorStoreIndex.from_documents(data)
index.storage_context.persist(persist_dir="storage")
else:
# Load existing index
index = load_index_from_storage(storage_context)- Place your documents in the
data/directory - Run any of the implementations:
# Basic version
python app.py
# Advanced version
python app-advanced.py
# Dynamic data version
python app-dynamic-data.py- Start chatting with your documents!
- Simple document indexing
- Basic chat interface
- Streaming responses
- Text splitting for better context
- Chat history support
- Persistent index storage
- Improved response quality
- All advanced features
- Automatic reindexing on data changes
- File state tracking
- Efficient index management