topic-rag — Topic-Enhanced Retrieval-Augmented Generation
Install it anywhere with:
pip install topic-rag
What it does
Standard RAG systems retrieve documents purely by text similarity (TF-IDF cosine distance). topic-rag adds a second signal — it automatically discovers hidden topics across your document collection and uses those topics to boost retrieval accuracy. A query about "neural networks" will score higher against documents that share that topic cluster, even if the exact words differ.
Quick Start
from topic_rag import DocumentProcessor, TopicEnhancedRAGRetriever
# Build a topic-aware corpus from your documents
processor = DocumentProcessor(n_topics=10)
corpus = processor.process_corpus(documents) # list of {id, text, title}
# Retrieve with topic enhancement
retriever = TopicEnhancedRAGRetriever(corpus)
results = retriever.retrieve("What is transfer learning?", k=5)
for r in results:
print(f"[{r['score']:.4f}] {r['sentence']}")
Two Retrieval Modes
| Retriever | How it works |
|---|---|
StandardRAGRetriever |
Ranks by TF-IDF cosine similarity only |
TopicEnhancedRAGRetriever |
Combines TF-IDF similarity + latent topic overlap for better context-aware ranking |
Use both side by side to compare standard vs topic-enhanced retrieval on your own data:
from topic_rag import DocumentProcessor, StandardRAGRetriever, TopicEnhancedRAGRetriever
processor = DocumentProcessor(n_topics=10)
corpus = processor.process_corpus(documents)
std_retriever = StandardRAGRetriever(corpus)
enh_retriever = TopicEnhancedRAGRetriever(corpus)
std_results = std_retriever.retrieve("my query", k=5)
enh_results = enh_retriever.retrieve("my query", k=5)
Key Design Decisions
- No GPU required — uses TF-IDF and a lightweight NMF-based topic model (no PyTorch, no sentence-transformers)
- Minimal dependencies — only
numpyandscikit-learnat the core - Self-contained — no external API calls or downloads needed
License
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
See the LICENSE file for the full license text.
What AGPL-3.0 means
- Anyone can view, use, and modify the code
- Any modified version used to provide a network service must release its source code
- Companies cannot embed this in proprietary software without open-sourcing their product
For commercial licensing enquiries, please contact the project maintainers.
Metadata
Release files for topic-rag 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| topic_rag-1.0.3.tar.gz | 44.3 kB | Without provenance | Details for topic_rag-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| topic_rag-1.0.3-py3-none-any.whl | Python 3 | none | any | Details for topic_rag-1.0.3-py3-none-any.whl |
Total release size: 85.5 kB
Release files / topic_rag-1.0.3.tar.gz
| Download URL | topic_rag-1.0.3.tar.gz |
|---|---|
| Size | 44.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a863fc9e05fc23014a606badb85c7a769c248ef0da2b331c1d2698afa12b2851
|
|
BLAKE2b-256 checksum How to use checksums |
f292455265c99e037d8ff557d6c8c8e1de3b1b944fd66cbf49417818e9438b62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / topic_rag-1.0.3-py3-none-any.whl
| Download URL | topic_rag-1.0.3-py3-none-any.whl |
|---|---|
| Size | 41.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3931622acc34f80bbe140c282afb8e43b5f7b5d247bc50944387e0c253fc07ed
|
|
BLAKE2b-256 checksum How to use checksums |
da727df773c9241b630699e3fdec2969cd134188dc12c1bef670552d5ab5f29c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Security for topic-rag 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.