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.

WHAT YOU BUILT

A RAG chat assistant

Retrieval-Augmented Generation: search your content first, then let a language model answer using only what the search found.

PLUS

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.

AND

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.

BROWSER Chat widget React · my-portfolio BACKEND · FastAPI · portfolio-assistant ① Save the questionSQLite · history.py ② Make it searchablequery rewrite · chat.py ③ Hybrid search BM25 keywords · bm25.py embeddings · embeddings.py fused by RRF · index.py ④ Build the promptrules + sources + question ⑥ Save the answer + citationsSQLite Every step is ~1 ms except ⑤: the model. ⑤ LANGUAGE MODEL Gemini (free tier) or Groq, Ollama, any OpenAI-compatible API POST answer tokens stream back to the browser as Server-Sent Events
Text mode. Voice mode wraps the same pipeline with speech-to-text before it and text-to-speech after it (chapter 7).
  1. 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
  2. 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
  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
  4. 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
  5. 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
  6. Save and cite. The answer is stored with the sources it actually cited ([1], [2]…), which the widget turns into links to your pages.
The one idea to keep

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

RepoWhat it holdsLanguage
my-portfolioThe 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-assistantThe 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

FileJobChapter
app/config.pyEvery setting, read from .env. The swap-anything switchboard.1
app/llm.pyOne interface; LiteLLM's router tries free providers in order, skipping failed ones1
app/rag/embeddings.pyText → vectors (local model or API)2
app/rag/documents.pyLoad knowledge.json + extra markdown docs3
app/rag/chunking.pyCut documents into searchable chunks3
app/rag/bm25.pyKeyword search, written from scratch3
app/rag/index.pyThe index: vectors + BM25 + Reciprocal Rank Fusion3
app/rag/prompts.pyThe system prompt and how sources are laid out3
app/chat.pyOne chat turn, end to end (the pipeline)3
evals/retrieval.pyDoes search find the right page? Measured.4
app/main.pyHTTP routes, streaming, CORS, rate limits5
app/history.py · app/ratelimit.pyConversations in SQLite · abuse protection5
src/assistant/* (portfolio)Widget, streaming client, markdown, history UI6
app/voice.py · src/assistant/voice/*Whisper, Kokoro, mic, VAD, playback, the orb7
Dockerfile · deploy/Shipping it next to your nginx8

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.

No key? No problem

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.