Chapter 0 · Start here
How the portfolio assistant works
Your portfolio now has an AI assistant. A visitor types (or says) a question, and it answers from the portfolio's own content, with links to the pages it used. This guide explains every piece of it, assuming you've never built anything with AI before. Every code excerpt is cut live from the real source files, so what you read is what runs.
A RAG chat assistant
Retrieval-Augmented Generation: search your content first, then let a language model answer using only what the search found.
A voice mode
Speak a question, hear the answer. Speech-to-text and text-to-speech run on open models on your own server, with no API key.
Swappable everything
The language model, embeddings, speech models: each is a line in .env. Gemini today, anything else tomorrow.
One question's journey
Here's everything that happens between a visitor pressing Enter on "How did the CP-SAT scheduler work?" and the answer appearing word by word. The rest of the guide is a zoom into each box.
- The widget sends the question as a
POST /api/chat, with an anonymous id for this browser so the chat lands in this visitor's history. Ch. 6 - Make it searchable. A follow-up like "how fast was it?" means nothing on its own, so it's merged with the previous question before searching. Ch. 3
- Hybrid search ranks ~50 chunks of your portfolio twice: by keywords (BM25) and by meaning (embeddings). The two rankings are merged with Reciprocal Rank Fusion, and the top 5 chunks win. Ch. 2, Ch. 3
- Build the prompt: rules ("answer only from the sources, cite them"), a site map, the recent conversation, the 5 numbered sources, then the question. Ch. 3
- The language model writes the answer and the backend forwards each few-token piece to the browser the moment it arrives. Ch. 1, Ch. 5
- Save and cite. The answer is stored with the sources it actually cited (
[1],[2]…), which the widget turns into links to your pages.
A language model only knows what's in its prompt. "RAG" means you decide what goes in the prompt by searching your own data first. Nearly all the engineering is in that search. The model call itself is one function.
Two repositories
| Repo | What it holds | Language |
|---|---|---|
my-portfolio | The website. Two additions: scripts/export-knowledge.mjs turns src/content/*.ts into knowledge.json on every build, and src/assistant/ is the chat widget and voice mode. | TypeScript, React |
portfolio-assistant | The backend API, the evals, tests, Docker setup, and this guide (served at /guide/). | Python, FastAPI |
Why separate? They're different languages with different deploy cycles: the site is static files; the backend
is a long-running process with models in memory. They talk through exactly two contracts: knowledge.json
(content flows site → backend) and the HTTP API (questions flow widget → backend). Your content invariant still
holds: edit one file in src/content/ and the assistant learns it on the next build.
Map of the code
| File | Job | Chapter |
|---|---|---|
app/config.py | Every setting, read from .env. The swap-anything switchboard. | 1 |
app/llm.py | One interface; LiteLLM's router tries free providers in order, skipping failed ones | 1 |
app/rag/embeddings.py | Text → vectors (local model or API) | 2 |
app/rag/documents.py | Load knowledge.json + extra markdown docs | 3 |
app/rag/chunking.py | Cut documents into searchable chunks | 3 |
app/rag/bm25.py | Keyword search, written from scratch | 3 |
app/rag/index.py | The index: vectors + BM25 + Reciprocal Rank Fusion | 3 |
app/rag/prompts.py | The system prompt and how sources are laid out | 3 |
app/chat.py | One chat turn, end to end (the pipeline) | 3 |
evals/retrieval.py | Does search find the right page? Measured. | 4 |
app/main.py | HTTP routes, streaming, CORS, rate limits | 5 |
app/history.py · app/ratelimit.py | Conversations in SQLite · abuse protection | 5 |
src/assistant/* (portfolio) | Widget, streaming client, markdown, history UI | 6 |
app/voice.py · src/assistant/voice/* | Whisper, Kokoro, mic, VAD, playback, the orb | 7 |
Dockerfile · deploy/ | Shipping it next to your nginx | 8 |
Run it on your machine
# backend (first time: venv + deps + ~320 MB of models) cd C:\dev\portfolio-assistant python -m venv .venv .venv\Scripts\pip install -e ".[voice,dev]" .venv\Scripts\python -m scripts.download_models copy .env.example .env # then paste at least one free key (Groq, Gemini…) .venv\Scripts\uvicorn app.main:app --reload --port 8000 # frontend (another terminal) cd C:\dev\my-portfolio npm run dev # .env.development points the widget at localhost:8000
Then open localhost:5173 and click the ✦ button, or open the Retrieval Lab to watch search work. FastAPI also generates interactive API docs at /docs, where you can call every endpoint by hand.
Set LLM_MODELS=echo and everything runs with a fake model that answers by listing what retrieval
found. The tests use it too, so they never need the network.
The stack, one line each
FastAPI
Python web framework: routes are typed functions, validation and API docs come free.
Gemini via the OpenAI protocol
The LLM. Called through the openai SDK pointed at Google's compatible endpoint.
fastembed + bge-small
A 33M-parameter embedding model running locally on ONNX Runtime. Free and offline.
BM25 + RRF
Classic keyword ranking (50 lines, no library) fused with vector search.
SQLite
Conversation history in one file. No database server.
faster-whisper
OpenAI's open Whisper speech recognizer on a fast CPU engine.
Kokoro-82M
An open text-to-speech model that sounds far bigger than it is.
Web Audio + canvas
Browser APIs that record the mic, play the voice and animate the orb.
How to read this
Chapters 1 to 4 are the AI core: read them in order. 5 to 8 are engineering you can dip into. Each chapter mixes concept boxes, real code (click any excerpt's header to see the whole file), library notes, "what actually happened" stories from building this, and a quiz or two. The Retrieval Lab lets you poke the real search engine and see every score.
A visitor asks about a project you added to projects.ts this morning, and the assistant knows about it. What made that possible?
Nothing about the model changes. RAG keeps knowledge outside the model, in an index you rebuild in seconds. That's its biggest practical advantage over fine-tuning.