Skip to main content

SLM RAG 🧠🔍

slm_rag is a lightweight, local Retrieval-Augmented Generation (RAG) library powered entirely by a Small Language Model (SLM) running on CPU. It allows developers to pass a list of document chunks, a user question, and arbitrary guidelines/instructions to answer queries locally with high privacy, low resource usage, and zero API costs.


Key Features

  • Local & Private: Runs completely on CPU / RAM. Zero API keys, zero network latency, and complete data privacy.
  • Resource Efficient: Uses a 1.5B parameter model (qwen2.5-1.5b-instruct-q4_k_m.gguf), consuming only 1.0 GB to 1.5 GB of RAM.
  • Instruction Adherence: Formats instructions directly into the system template to enforce constraints (e.g. style, safety, or formatting constraints like JSON).

Installation

Create a virtual environment and install the package locally:

# 1. Create a fresh virtual environment
python3 -m venv venv
source venv/bin/activate

# 2. Install the package in editable mode
pip install -e .

Note: Requires llama-cpp-python and huggingface_hub.


Quick Start

from slm_rag import SLMRag

# Initialize the RAG engine (auto-locates or downloads the model)
rag = SLMRag()

# Provide context chunks
chunks = [
    "NebulaCorp was founded in 2024 by Dr. Helena Vance. It specializes in quantum-resistant encryption algorithms.",
    "The flagship product of NebulaCorp is called 'AegisShield'. It is widely used by financial organizations.",
    "In early 2026, NebulaCorp announced a partnership with the European Space Agency."
]

# Run query with a strict instruction
answer = rag.answer(
    chunks=chunks,
    question="What is their flagship product?",
    instruction="Answer like a 17th-century pirate.",
    temperature=0.0
)

print(answer)
# Output: "Ahoy matey! AegisShield be the flagship product of NebulaCorp, savvy?"

Configuration API

SLMRag(
    model_path=None,   # Explicit path to a .gguf file (optional)
    cache_dir=None,    # Cache directory for auto-downloads (defaults to ~/.cache/slm_rag)
    n_ctx=2048,        # Context window size (default: 2048)
    n_threads=4        # Number of CPU threads (default: 4)
)

Answering Queries

rag.answer(
    chunks: list[str],      # Document text chunks
    question: str,          # User query / question
    instruction: str,       # Instruction or constraint the model must follow
    temperature: float = 0.0, # Generation temperature (0.0 for deterministic answers)
    max_tokens: int = 512    # Maximum token limit for the response
)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slm_rag-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slm_rag-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file slm_rag-0.1.0.tar.gz.

File metadata

  • Download URL: slm_rag-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for slm_rag-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bebfbdf352cef257bc9ba8fe864a676b1c918ef7d5e7baf376762b522edad3a
MD5 e849b835135ee43b63422c34e44b1c89
BLAKE2b-256 a9f8411730f6112cdeb693d5587b258cace2c41a17e798b76c4b55185dfea27d

See more details on using hashes here.

File details

Details for the file slm_rag-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slm_rag-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for slm_rag-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15fd8a0382cfe8cc9495858bb082b4d54bf13c2569801869114c40faeccb9f4d
MD5 8627792ff5fc7e4c95acce927a61acfc
BLAKE2b-256 8afef3db242ade52c4354cbfc3ba9c028b79b41b5f3e397a03ec5b884208ba91

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page