A lightweight and efficient solution for retrieval augmented generation
In my last devlog, I introduced you to my chat UI with NiceGUI. Today I would like to show you a new project: QV-RAG, a simple RAG engine for semantic search and document management.
Why your own RAG engine?
Up to now, I have used more complex frameworks for semantic searches. Although these were powerful and worked very well, they were also overloaded with functions that I didn't need. So I decided to develop my own, lean RAG engine that does exactly what I need - no more and no less. Also with the background of really understanding what I was doing.
The structure
The engine consists of three main components:
a text splitter for intelligent document splitting
a storage system based on ChromaDB
a simple API for document management and queries
The code has a modular structure and is easy to extend. This makes the engine not only easy to use, but also easy to maintain and customize.
The implementation
The main class `RAGEngine` is the entry point for all operations. It connects the various components and provides a clear API:
```python
engine = RAGEngine(
collection_name="my_docs",
chunk_size=1000,
chunk_overlap=200
)
```
The engine supports various document types:
Text files
Markdown
HTML
JSON
PDF (works but still needs to be improved)
Add documents
Adding documents is very simple. You can either add individual texts or entire files:
```python
# Add individual texts with metadata
engine.add_texts(
texts=["Python is a popular programming language."],
metadatas=[{"source": "manual", "category": "docs"}]
)
# Add a file
engine.add_file("document.md", metadata={"source": "file"})
```
The engine takes care of this automatically:
Recognizing the file type
Splitting into meaningful chunks
Distributing the metadata to the chunks
Storage in the database
The search function
The semantic search is simple and effective:
```python
results = engine.query(
"What is Python?",
where={"category": "docs"},
top_k=3
)
```
The engine not only provides the relevant text passages, but also metadata and similarity values. This makes it easy to filter and evaluate the results.
Features
What QV-RAG does:
Intelligent text splitting with metadata preservation
Simple API for fast integration
Support for different document types
Flexible metadata management
Persistent storage with ChromaDB
Conclusion
QV-RAG is an example of how to build a good working RAG engine with relatively little code. The engine may not be the ultimate RAG engine, but it is still a useful tool for semantic search and document management.
You can find the complete code in the GitHub repository:
[QV-RAG]
There you will also find examples and documentation that show you how to use the engine in your projects.
See you next time,
Thomas from the Quantyverse
P.S.: Visit my website Quantyverse.ai for products, bonus content, blog posts and more