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

Install directly via pip:

pip install slm-rag

Or install locally for development:

# 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.1.tar.gz (4.5 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.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: slm_rag-0.1.1.tar.gz
  • Upload date:
  • Size: 4.5 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.1.tar.gz
Algorithm Hash digest
SHA256 fbd58a1032e2f3db22549f1154b03facdf16001f7775c4c9ffc82d891899610e
MD5 35b5a0ece3217af7644c302a6ea8568b
BLAKE2b-256 d7ba8874af73a6798dda050ac60a43ef6c423aac0e799cd8a180dfb24be8b3c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slm_rag-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a185a2459ab501f876b9d7445bde77689373c182d29fab7e81c24d32821ca07f
MD5 6332c8ed83f25bc8d6ddb14049f751a4
BLAKE2b-256 8e6b82e28edc6d847b5b105356afd3da0beebddec8755ba36bf9618f03a3fe17

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