Skip to content

Commit 391be40

Browse files
authored
Add local RAG example (#322)
Adds a local RAG example using Encoderfile + Llamafile. Added genius_act.txt (GENIUS Act, S. 919) as a second sample dataset alongside weird_laws.txt, demonstrating window-mode chunking on legislative prose.
1 parent 7b6d625 commit 391be40

7 files changed

Lines changed: 5611 additions & 0 deletions

File tree

examples/local-rag/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Local RAG with Encoderfile + Llamafile
2+
3+
4+
A fully local RAG (Retrieval-Augmented Generation) system. Give it any text file, ask it questions. Nothing leaves your machine.
5+
6+
## Quickstart
7+
8+
### 1. Download the binaries
9+
10+
```bash
11+
# Llama 3.2 3B Instruct (~2.5GB)
12+
wget -O llama3.2-3b.llamafile \
13+
https://huggingface.co/Mozilla/Llama-3.2-3B-Instruct-llamafile/resolve/main/Llama-3.2-3B-Instruct.Q6_K.llamafile
14+
chmod +x llama3.2-3b.llamafile
15+
16+
# all-MiniLM-L6-v2 encoder (~80MB) — pick the file for your architecture:
17+
# macOS (Apple Silicon): all-MiniLM-L6-v2-aarch64-apple-darwin.encoderfile
18+
# macOS (Intel): all-MiniLM-L6-v2-x86_64-apple-darwin.encoderfile
19+
# Linux (x86_64): all-MiniLM-L6-v2-x86_64-unknown-linux-gnu.encoderfile
20+
# Windows (x86_64): all-MiniLM-L6-v2-x86_64-pc-windows-msvc.encoderfile
21+
# Browse all builds: https://huggingface.co/mozilla-ai/encoderfile/tree/main/sentence-transformers/all-MiniLM-L6-v2
22+
wget -O minilm.encoderfile <PASTE_URL_FOR_YOUR_ARCHITECTURE>
23+
chmod +x minilm.encoderfile
24+
```
25+
26+
> **Windows:** Skip `chmod`. Instead, rename each file to add a `.exe` extension (e.g. `llama3.2-3b.llamafile.exe`) and run it directly.
27+
28+
### 2. Start the servers
29+
30+
```bash
31+
# Terminal 1
32+
./minilm.encoderfile serve --http-port 8080
33+
34+
# Terminal 2
35+
./llama3.2-3b.llamafile --server --port 8081 --nobrowser
36+
```
37+
38+
### 3. Run
39+
40+
```bash
41+
uv sync
42+
uv run local_rag.py
43+
```
44+
45+
You'll get an interactive prompt where you can ask questions about the included sample datasets:
46+
47+
- `weird_laws.txt` — a collection of unusual US laws, good for testing separator-mode chunking
48+
- `genius_act.txt` — the GENIUS Act (S. 919, 119th Congress), a stablecoin regulation bill, good for testing window-mode chunking on legislative prose
49+
50+
```bash
51+
# Ask about weird laws (default)
52+
uv run local_rag.py
53+
54+
# Ask about the GENIUS Act
55+
uv run local_rag.py --file genius_act.txt --chunk-mode window --chunk-size 800
56+
```
57+
58+
## Use Your Own Data
59+
60+
Point it at any text file:
61+
62+
```bash
63+
# A congressional bill
64+
uv run local_rag.py --file bill.txt --chunk-mode window --chunk-size 800
65+
66+
# A structured dataset (entries separated by blank lines)
67+
uv run local_rag.py --file my_notes.txt --chunk-mode separator
68+
69+
# Customize everything
70+
uv run local_rag.py \
71+
--file contracts.txt \
72+
--chunk-mode window \
73+
--chunk-size 1000 \
74+
--chunk-overlap 100 \
75+
--top-k 3 \
76+
--system-prompt "You are a legal assistant. Answer using only the provided context."
77+
```
78+
79+
### Chunking modes
80+
81+
- **`separator`** (default) — Splits on blank lines. Best for structured data where each entry is self-contained.
82+
- **`window`** — Splits into overlapping character windows. Best for raw prose like legislation, articles, or books.
83+
84+
## How It Works
85+
86+
1. **Embed** — Each text chunk is converted into a 384-dimensional vector by encoderfile (all-MiniLM-L6-v2).
87+
2. **Retrieve** — Your question is embedded the same way, and the closest chunks are found via cosine similarity.
88+
3. **Generate** — Retrieved chunks are passed as context to Llama 3.2 3B (via llamafile), which generates an answer.
89+
90+
No vector database — numpy handles similarity search in memory. This is fast for datasets up to a few thousand chunks.
91+
92+
## Options
93+
94+
| Flag | Default | Description |
95+
|------|---------|-------------|
96+
| `--file` | `weird_laws.txt` | Path to your text file |
97+
| `--chunk-mode` | `separator` | `separator` or `window` |
98+
| `--chunk-separator` | `\n\n` | String to split on (separator mode) |
99+
| `--chunk-size` | `500` | Characters per chunk (window mode) |
100+
| `--chunk-overlap` | `50` | Overlap between chunks (window mode) |
101+
| `--top-k` | `5` | Number of chunks to retrieve |
102+
| `--encoderfile-url` | `http://localhost:8080` | Encoderfile server URL |
103+
| `--llamafile-url` | `http://localhost:8081/v1` | Llamafile server URL |
104+
| `--system-prompt` | Generic helpful assistant | System prompt for the LLM |
105+
106+
## Going Further
107+
108+
**Better answers:** Use a larger llamafile — [Mistral 7B](https://huggingface.co/mozilla-ai/Mistral-7B-Instruct-v0.2-llamafile) or [Llama 3.1 8B](https://huggingface.co/Mozilla/Meta-Llama-3.1-8B-Instruct-llamafile).
109+
110+
**Better retrieval:** Use a larger encoder via encoderfile, like `all-mpnet-base-v2` or `BGE-small`.
111+
112+
**Swap models freely:** Use [any-llm](https://github.com/mozilla-ai/any-llm) to compare llamafile, OpenAI, Gemini, and Claude with one config change.
113+
114+
**Add orchestration & evals:** Wrap this in [any-agent](https://mozilla-ai.github.io/any-agent/) for tracing and evaluation.
115+
116+
---
117+
118+
*Built with [encoderfile](https://github.com/mozilla-ai/encoderfile) and [llamafile](https://github.com/mozilla-ai/llamafile) from [Mozilla AI](https://www.mozilla.ai/).*

0 commit comments

Comments
 (0)