A production-grade, local, privacy-first AI assistant and scientific benchmarking suite for Small Language Models (SLMs) running on Ollama.
Every token is evaluated and generated on-device, keeping sensitive data private with zero external cloud dependencies. Designed for repeatable AI performance engineering, dual-clock latency profiling, deterministic structured outputs with auto-repair, and quantization trade-off analysis.
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Server (:8000) β
β - POST /v1/chat & /v1/chat/stream (SSE) β
β - POST /v1/structured (Pydantic + 1-Retry) β
β - POST /v1/benchmark (In-service profiling) β
βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ
β
httpx (Async REST Client with Wire Timing)
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β Local Ollama Daemon β
β (http://127.0.0.1:11434) β
β - Llama 3.2 3B / Phi-4 Mini / Mistral 7B β
β - Grammars / GGUF quantization (Q4, Q5, Q8) β
β - GPU Acceleration (Metal / CUDA / ROCm) β
ββββββββββββββββββββββββββββββββββββββββββββββββββ
β²
β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Benchmarking CLI Suite β
β Phase 1: bench.phase1_latency β Phase 2: bench.phase2_determinism β Phase 3: bench.phase3_compare β
β - TTFT, ITL, Prefill vs Decode β - Strict JSON Validation β - 14-Prompt Standardized Suite β
β - Wall clock vs Server counters β - 1-Shot Error-Feedback Repair β - Peak RSS & /api/ps VRAM Sampling β
β - Student-t 95% Confidence Bounds β - Variance @ Temp 0.0 vs 0.7 β - Pareto Front & Quant Sweeps β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββ app/
β βββ __init__.py # Package marker
β βββ config.py # Settings, env parsing & loopback normalization
β βββ metrics.py # Latency math, percentile interpolation, Student-t CI95
β βββ ollama_client.py # Async HTTP client streaming wire metrics & model lifecycle
β βββ output_schemas.py # Pydantic schemas (extra="forbid" for strict JSON Schema)
β βββ structured.py # Validation layer with single repair retry and graceful fallback
β βββ benchmarking.py # Reusable latency profiling engine with cache-busting
β βββ schemas.py # API request/response models
β βββ main.py # FastAPI application with SSE and structured endpoints
βββ bench/
β βββ __init__.py
β βββ common.py # System hardware capture (OS, CPU, RAM, battery) & tables
β βββ memory.py # Threaded 10 Hz RSS sampler + Ollama /api/ps accounting
β βββ quality.py # Objective test verifiers (regex, code execution, JSON fields)
β βββ prompts.json # 14 standardized test prompts across 6 domains
β βββ phase1_latency.py # Phase 1 CLI: TTFT, ITL, decode tok/s, prefill tok/s
β βββ phase2_determinism.py # Phase 2 CLI: Temp 0.0 vs 0.7 variance & JSON retry tracking
β βββ phase3_compare.py # Phase 3 CLI: Automated multi-model comparison & report gen
βββ docs/
β βββ QUANTIZATION.md # In-depth guide on GGUF quant levels (Q4_K_M vs Q5 vs Q8)
βββ results/
β βββ example/ # Reference benchmark output artifacts
βββ scripts/
β βββ quant_sweep.sh # Automation to pull quants and run baseline comparisons
βββ tests/
β βββ test_api.py # Integration tests for FastAPI endpoints
β βββ test_structured.py # Unit tests for retry mechanism and schema validation
β βββ test_metrics_quality.py # Unit tests for metric calculations and verifiers
βββ .env.example # Template environment variables
βββ .github/workflows/ci.yml # GitHub Actions CI matrix
βββ Makefile # Developer task shortcuts
βββ pyproject.toml # Modern Python package configuration
βββ requirements.txt # Python dependencies
βββ assistant_cli.py # Interactive terminal chat with real-time token telemetry
Install and launch Ollama on your system:
ollama serve
# Pull a small model (e.g. Llama 3.2 3B)
ollama pull llama3.2:3bgit clone https://github.com/prosuvinhere/Local-AI-Assistant-Benchmarking-using-SLMs.git
cd Local-AI-Assistant-Benchmarking-using-SLMs
# Using Makefile
make install
# Or manually
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtAll 23 unit and integration tests run offline without needing Ollama:
make test
# Or: pytest -vChat with your local model in your terminal with live per-token telemetry:
make run-cli
# Or: python assistant_cli.py --model llama3.2:3bLocal assistant on llama3.2:3b (100% on-device). /reset /stats /quit
you βΊ In one sentence, why are SLMs valuable for edge devices?
ai βΊ Small Language Models enable low-latency, privacy-compliant intelligence directly on device without recurring cloud API costs.
[ttft 298 ms Β· 12.3 tok/s Β· 23 tok Β· 2.17 s]
make run-api
# Or: uvicorn app.main:app --host 127.0.0.1 --port 8000 --reloadInteractive OpenAPI documentation is available at http://127.0.0.1:8000/docs.
-
Dual-Clock Telemetry:
- Client Wall Clock: Captures HTTP roundtrip, serialization, OS scheduling, and TTFT.
-
Server-Side Counters: Ollama's
eval_durationandprompt_eval_durationisolate pure hardware inference.
- Prefix-Cache Busting: Generates unique nonces to prevent Ollama KV-cache hits from artificially lowering prefill measurements.
-
Student-t Confidence Intervals: Accurately computes 95% confidence intervals on small sample sizes (
$df = n - 1$ ).
make bench-p1
# Or: python -m bench.phase1_latency --model llama3.2:3b --iterations 10 --cold-start| Prompt Length | Input Tokens | Output Tokens | TTFT p50 | Decode Throughput | Stability (CV) | Prefill Throughput | Total Latency p50 |
|---|---|---|---|---|---|---|---|
| Short | 45 | 73 | 300 ms | 12.2 Β± 0.2 tok/s | 1.6% | 157 tok/s | 6,244 ms |
| Medium | 210 | 128 | 1,636 ms | 12.0 Β± 1.1 tok/s | 7.4% | 132 tok/s | 11,880 ms |
| Long | 1,389 | 128 | 13,507 ms | 11.0 Β± 0.8 tok/s | 5.6% | 104 tok/s | 24,885 ms |
Cold Start: 3,495 ms model load | Cold TTFT: 3,852 ms vs. Warm TTFT: 300 ms.
- Schema Constraints: Pydantic models configured with
model_config = ConfigDict(extra="forbid")output strict"additionalProperties": false. - Strict Validation: Evaluates model JSON via
model_validate_json(strict=True). String booleans ("true") and string floats ("0.9") are rejected. - One-Shot Repair Re-prompt: On validation failure, exact Pydantic diagnostics are packaged into a repair prompt.
- Graceful Fallback: If repair fails, a structured 422 payload with raw output and error history is returned.
make bench-p2
# Or: python -m bench.phase2_determinism --model llama3.2:3b --runs 10 --temperatures 0.0 0.7| Prompt | Temp | Unique Outputs | Mean Similarity | Field Agreement (Enums / Booleans) | Field Agreement (Free Text) |
|---|---|---|---|---|---|
tomato |
0.0 β 0.7 | 1 β 3 | 1.000 β 0.556 | 100% agreement | 33% (wording drift) |
ticket |
0.0 β 0.7 | 1 β 3 | 1.000 β 0.911 | 100% agreement | 33% (summary drift) |
Evaluates models across 14 test prompts in bench/prompts.json spanning 6 domains:
factual, reasoning, coding, summarization, instruction_following, and structured_json.
Every check is objectively verified using regex, AST parsers, or isolated subprocess execution.
make bench-p3
# Or: python -m bench.phase3_compare --models llama3.2:3b phi4-mini mistral:7b --repeats 3 --allow-code-execTo evaluate the speed vs. quality trade-off of quantized variants against a Q8 baseline:
make quant-sweep
# Or: ./scripts/quant_sweep.sh llama3.2See docs/QUANTIZATION.md for theory on memory bandwidth bounds and quantization levels.
Contributions are welcome! Please review CONTRIBUTING.md for details on code style, offline test requirements, and the pull request process.
This project is licensed under the MIT License - see the LICENSE file for details.