A ~9M parameter LLM that talks like a small fish.
Python
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GuppyLM — Chat with a Fish</title>
<style>
/* ── Light theme (default) ── */
:root, [data-theme="light"] {
--bg: #f5f7fa; --surface: #ffffff; --border: #e0e4ea;
--text: #1a1a2e; --dim: #6b7280; --accent: #0891b2;
--bubble-user: #e0f2fe; --bubble-fish: #ffffff;
--logo-shadow: rgba(0,0,0,0.08);
}
/* ── Dark theme ── */
[data-theme="dark"] {
--bg: #0a1628; --surface: #112240; --border: #1e3a5f;
--text: #ccd6f6; --dim: #8892b0; --accent: #64ffda;
--bubble-user: #1e3a5f; --bubble-fish: #0d2137;
--logo-shadow: rgba(0,0,0,0.3);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
background: var(--bg); color: var(--text);
display: flex; flex-direction: column; height: 100vh;
transition: background 0.3s, color 0.3s;
}
/* ── Header ── */
header {
padding: 16px 20px; border-bottom: 1px solid var(--border);
display: flex; align-items: center; gap: 12px;
}
header img.logo { width: 36px; height: 36px; object-fit: contain; }
header h1 { font-size: 16px; font-weight: 600; }
header .tag {
font-size: 11px; color: var(--accent); border: 1px solid var(--accent);
padding: 2px 8px; border-radius: 12px;
}
header .spacer { margin-left: auto; }
header .status { font-size: 12px; color: var(--dim); }
#theme-toggle {
background: none; border: 1px solid var(--border); border-radius: 6px;
padding: 4px 8px; cursor: pointer; font-size: 16px; line-height: 1;
}
/* ── Badges bar ── */
#badges {
padding: 8px 20px; border-bottom: 1px solid var(--border);
display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
#badges a img { height: 20px; }
#badges-toggle {
display: none; background: none; border: 1px solid var(--border);
border-radius: 6px; padding: 2px 8px; cursor: pointer;
font-size: 11px; color: var(--dim); font-family: inherit;
}
#badges .extra { display: inline; }
@media (max-width: 600px) {
#badges-toggle { display: inline-block; }
#badges .extra { display: none; }
#badges .extra.show { display: inline; }
header .tag { display: none; }
header .status { font-size: 11px; }
}
/* ── Chat ── */
#chat {
flex: 1; overflow-y: auto; padding: 20px;
display: flex; flex-direction: column; gap: 12px;
}
.msg {
max-width: 80%; padding: 10px 14px; border-radius: 12px;
font-size: 14px; line-height: 1.5; display: flex; gap: 10px; align-items: flex-start;
}
.msg.user {
align-self: flex-end; background: var(--bubble-user);
border-bottom-right-radius: 4px; flex-direction: row-reverse;
}
.msg.fish {
align-self: flex-start; background: var(--bubble-fish);
border: 1px solid var(--border); border-bottom-left-radius: 4px;
}
.msg .emoji { font-size: 20px; flex-shrink: 0; line-height: 1; }
.msg .body { display: flex; flex-direction: column; }
.msg .label { font-size: 11px; color: var(--dim); margin-bottom: 2px; }
.msg.user .label { text-align: right; }
.msg.fish .label { color: var(--accent); }
.typing { color: var(--dim); font-style: italic; font-size: 13px; padding: 4px 14px; }
/* ── Sample prompts ── */
#samples {
display: flex; flex-direction: column; align-items: center;
justify-content: center; flex: 1; gap: 16px; padding: 40px 20px;
}
#samples img { width: 72px; height: 72px; object-fit: contain; opacity: 0.85; }
#samples .title { font-size: 15px; font-weight: 600; }
#samples .subtitle { font-size: 12px; color: var(--dim); }
#samples .pills {
display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; max-width: 500px;
}
#samples .pill {
background: var(--surface); border: 1px solid var(--border);
padding: 8px 14px; border-radius: 20px; font-size: 13px;
cursor: pointer; transition: border-color 0.2s;
font-family: inherit; color: var(--text);
}
#samples .pill:hover { border-color: var(--accent); }
/* ── Input ── */
#input-row {
padding: 16px 20px; border-top: 1px solid var(--border);
display: flex; gap: 10px;
}
#input-row input {
flex: 1; background: var(--surface); border: 1px solid var(--border);
color: var(--text); padding: 10px 14px; border-radius: 8px;
font-family: inherit; font-size: 14px; outline: none;
}
#input-row input:focus { border-color: var(--accent); }
#input-row input::placeholder { color: var(--dim); }
#input-row button {
background: var(--accent); color: white; border: none;
padding: 10px 20px; border-radius: 8px; font-family: inherit;
font-size: 14px; font-weight: 600; cursor: pointer;
}
[data-theme="dark"] #input-row button { color: #0a1628; }
#input-row button:disabled { opacity: 0.4; cursor: not-allowed; }
#hint-btn {
background: none; border: 1px solid var(--border); border-radius: 8px;
padding: 10px; cursor: pointer; font-size: 16px; line-height: 1;
color: var(--dim); transition: border-color 0.2s;
}
#hint-btn:hover { border-color: var(--accent); }
/* ── Topics popup ── */
#topics-overlay {
position: fixed; inset: 0; background: rgba(0,0,0,0.4);
display: flex; align-items: center; justify-content: center; z-index: 100;
}
#topics-box {
background: var(--surface); border: 1px solid var(--border);
border-radius: 12px; padding: 20px 24px; max-width: 480px; width: 90%;
max-height: 80vh; overflow-y: auto;
}
#topics-box h3 { font-size: 14px; margin-bottom: 12px; }
#topics-box .topics {
display: flex; flex-wrap: wrap; gap: 6px;
}
#topics-box .topic {
font-size: 12px; padding: 4px 10px; border-radius: 14px;
background: var(--bg); border: 1px solid var(--border);
color: var(--text); cursor: pointer; font-family: inherit;
transition: border-color 0.2s;
}
#topics-box .topic:hover { border-color: var(--accent); }
#topics-box .close-btn {
margin-top: 14px; display: block; width: 100%; padding: 8px;
background: none; border: 1px solid var(--border); border-radius: 8px;
font-family: inherit; font-size: 13px; color: var(--dim); cursor: pointer;
}
/* ── Loading ── */
#loading {
display: flex; flex-direction: column; align-items: center;
justify-content: center; flex: 1; gap: 16px; color: var(--dim);
}
#loading img { width: 80px; height: 80px; object-fit: contain; opacity: 0.8; }
#loading .spinner {
width: 32px; height: 32px; border: 3px solid var(--border);
border-top-color: var(--accent); border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }
</style>
</head>
<body>
<header>
<img class="logo" src="guppy.png" alt="Guppy" />
<h1>GuppyLM</h1>
<span class="tag">9M params · WASM</span>
<span class="spacer"></span>
<span class="status" id="status">loading...</span>
<button id="theme-toggle" title="Toggle dark/light mode">☾</button>
</header>
<div id="badges">
<a href="https://github.com/arman-bd/guppylm"><img src="https://img.shields.io/badge/GitHub-guppylm-181717?logo=github" alt="GitHub"/></a>
<button id="badges-toggle">more ▾</button>
<a class="extra" href="https://huggingface.co/arman-bd/guppylm-9M"><img src="https://img.shields.io/badge/🤗_Model-guppylm--9M-orange" alt="Model"/></a>
<a class="extra" href="https://huggingface.co/datasets/arman-bd/guppylm-60k-generic"><img src="https://img.shields.io/badge/🤗_Dataset-guppylm--60k-blue" alt="Dataset"/></a>
<a class="extra" href="https://github.com/arman-bd/guppylm/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-green" alt="License"/></a>
<a class="extra" href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/train_guppylm.ipynb"><img src="https://img.shields.io/badge/Train-Colab-F9AB00?logo=googlecolab" alt="Train"/></a>
<a class="extra" href="https://colab.research.google.com/github/arman-bd/guppylm/blob/main/use_guppylm.ipynb"><img src="https://img.shields.io/badge/Chat-Colab-F9AB00?logo=googlecolab" alt="Chat"/></a>
<a class="extra" href="https://www.linkedin.com/pulse/build-your-own-language-model-5-minutes-i-made-mine-hossain--supif/"><img src="https://img.shields.io/badge/Article-LinkedIn-0A66C2?logo=linkedin" alt="LinkedIn"/></a>
<a class="extra" href="https://arman-bd.medium.com/build-your-own-llm-in-5-minutes-i-made-mine-talk-like-a-fish-e20c338a3d14"><img src="https://img.shields.io/badge/Article-Medium-000000?logo=medium" alt="Medium"/></a>
</div>
<div id="loading">
<img src="guppy.png" alt="Guppy" />
<div class="spinner"></div>
<div>downloading model & tokenizer...</div>
<div id="load-detail" style="font-size: 12px;"></div>
</div>
<div id="samples" class="hidden">
<img src="guppy.png" alt="Guppy" />
<div class="title">Chat with Guppy</div>
<div class="subtitle">9M parameter LLM running locally in your browser</div>
<div class="pills">
<button class="pill" data-msg="hi guppy">hi guppy</button>
<button class="pill" data-msg="are you hungry">are you hungry</button>
<button class="pill" data-msg="do you like bubbles">do you like bubbles</button>
<button class="pill" data-msg="tell me a joke">tell me a joke</button>
<button class="pill" data-msg="what is the meaning of life">meaning of life</button>
<button class="pill" data-msg="the cat is looking at you">the cat is here</button>
<button class="pill" data-msg="do you love me">do you love me</button>
<button class="pill" data-msg="goodnight guppy">goodnight guppy</button>
</div>
</div>
<div id="chat" class="hidden"></div>
<div id="input-row" class="hidden">
<button id="hint-btn" title="What can Guppy talk about?">💡</button>
<input type="text" id="user-input" placeholder="say something to guppy..." autocomplete="off" />
<button id="send-btn" onclick="send()">send</button>
</div>
<div id="topics-overlay" class="hidden"></div>
<script type="module">
// ── Config ──────────────────────────────────────────────────────────
const MODEL_BASE = ".";
const CONFIG = {
vocab_size: 4096, max_seq_len: 128, d_model: 384,
n_layers: 6, n_heads: 6, ffn_hidden: 768,
pad_id: 0, bos_id: 1, eos_id: 2,
};
const GEN = { temperature: 0.7, top_k: 50, max_tokens: 32 };
// ── Globals ─────────────────────────────────────────────────────────
let session = null;
let tokenizer = null;
let chatStarted = false;
// ── Theme toggle ────────────────────────────────────────────────────
const themeBtn = document.getElementById("theme-toggle");
themeBtn.addEventListener("click", () => {
const html = document.documentElement;
const next = html.getAttribute("data-theme") === "dark" ? "light" : "dark";
html.setAttribute("data-theme", next);
themeBtn.textContent = next === "dark" ? "\u2600" : "\u263E";
localStorage.setItem("theme", next);
});
// Restore saved preference
const saved = localStorage.getItem("theme");
if (saved) {
document.documentElement.setAttribute("data-theme", saved);
themeBtn.textContent = saved === "dark" ? "\u2600" : "\u263E";
}
// ── Mobile badges toggle ────────────────────────────────────────────
document.getElementById("badges-toggle").addEventListener("click", function() {
const extras = document.querySelectorAll("#badges .extra");
const open = extras[0]?.classList.contains("show");
extras.forEach(el => el.classList.toggle("show", !open));
this.textContent = open ? "more \u25BE" : "less \u25B4";
});
// ── ONNX Runtime ────────────────────────────────────────────────────
import("https://cdn.jsdelivr.net/npm/[email protected]/dist/ort.min.mjs").then(async (ort) => {
window.ort = ort;
ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/[email protected]/dist/";
await load(ort);
});
// ── Load model + tokenizer ──────────────────────────────────────────
async function load(ort) {
const detail = document.getElementById("load-detail");
detail.textContent = "loading tokenizer...";
const tokResp = await fetch(`${MODEL_BASE}/tokenizer.json`);
const tokJson = await tokResp.json();
tokenizer = buildTokenizer(tokJson);
detail.textContent = "loading model (~10 MB)...";
const modelResp = await fetch(`${MODEL_BASE}/model.onnx`);
const modelBuf = await modelResp.arrayBuffer();
session = await ort.InferenceSession.create(modelBuf, {
executionProviders: ["wasm"],
});
document.getElementById("loading").classList.add("hidden");
document.getElementById("samples").classList.remove("hidden");
document.getElementById("input-row").classList.remove("hidden");
document.getElementById("status").textContent = "ready — running locally in your browser";
document.getElementById("user-input").focus();
// Verify tokenizer
const testIds = tokenizer.encode("<|im_start|>user\nhi guppy<|im_end|>\n<|im_start|>assistant\n");
const expected = [1, 88, 64, 779, 278, 2, 64, 1, 89, 64];
const match = testIds.length === expected.length && testIds.every((v, i) => v === expected[i]);
console.log("Tokenizer check:", match ? "PASS" : "FAIL", testIds, "expected:", expected);
}
// ── Sample prompt pills ─────────────────────────────────────────────
document.querySelectorAll(".pill").forEach(btn => {
btn.addEventListener("click", () => {
document.getElementById("user-input").value = btn.dataset.msg;
send();
});
});
// ── ByteLevel BPE tokenizer (JS port of HuggingFace tokenizers) ─────
function buildTokenizer(json) {
const vocab = json.model.vocab;
const merges = json.model.merges;
const added = {};
for (const t of json.added_tokens) added[t.content] = t.id;
const id2token = {};
for (const [tok, id] of Object.entries(vocab)) id2token[id] = tok;
for (const [tok, id] of Object.entries(added)) id2token[id] = tok;
// Byte-to-unicode mapping (matches HuggingFace bytes_to_unicode)
const byte2char = {};
const char2byte = {};
const ranges = [
[33, 126], [161, 172], [174, 255],
];
const direct = new Set();
for (const [lo, hi] of ranges) for (let b = lo; b <= hi; b++) direct.add(b);
let n = 0;
for (let b = 0; b < 256; b++) {
if (direct.has(b)) {
byte2char[b] = String.fromCharCode(b);
} else {
byte2char[b] = String.fromCharCode(256 + n);
n++;
}
}
for (const [b, c] of Object.entries(byte2char)) char2byte[c] = parseInt(b);
// Merge rank lookup
const mergeRank = {};
for (let i = 0; i < merges.length; i++) {
const key = Array.isArray(merges[i]) ? merges[i].join(" ") : merges[i];
mergeRank[key] = i;
}
function bytesToTokenStr(bytes) {
return Array.from(bytes).map(b => byte2char[b]).join("");
}
function tokenStrToBytes(s) {
return Uint8Array.from([...s].map(c => char2byte[c] ?? c.charCodeAt(0)));
}
function bpe(word) {
if (word.length <= 1) return word;
let pieces = word.slice();
while (pieces.length > 1) {
let bestRank = Infinity, bestIdx = -1;
for (let i = 0; i < pieces.length - 1; i++) {
const pair = pieces[i] + " " + pieces[i + 1];
const rank = mergeRank[pair];
if (rank !== undefined && rank < bestRank) {
bestRank = rank;
bestIdx = i;
}
}
if (bestIdx === -1) break;
const merged = pieces[bestIdx] + pieces[bestIdx + 1];
pieces = [...pieces.slice(0, bestIdx), merged, ...pieces.slice(bestIdx + 2)];
}
return pieces;
}
const PAT = /'(?:[sdmt]|ll|ve|re)| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;
function encode(text) {
const specialPattern = Object.keys(added)
.map(s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
.join("|");
const specialRe = new RegExp(`(${specialPattern})`);
const segments = text.split(specialRe);
const ids = [];
for (const seg of segments) {
if (seg === "") continue;
if (added[seg] !== undefined) {
ids.push(added[seg]);
continue;
}
const words = seg.match(PAT) || [seg];
for (const word of words) {
const bytes = new TextEncoder().encode(word);
const byteChars = [...bytesToTokenStr(bytes)];
const tokens = bpe(byteChars);
for (const tok of tokens) {
const id = vocab[tok];
if (id !== undefined) ids.push(id);
}
}
}
return ids;
}
function decode(ids) {
const parts = [];
for (const id of ids) {
const tok = id2token[id];
if (!tok || added[tok] !== undefined) continue;
parts.push(tok);
}
const allBytes = tokenStrToBytes(parts.join(""));
return new TextDecoder("utf-8", { fatal: false }).decode(allBytes);
}
return { encode, decode };
}
// ── Inference (autoregressive generation) ───────────────────────────
async function generate(inputIds) {
const ort = window.ort;
let ids = inputIds.slice();
for (let i = 0; i < GEN.max_tokens; i++) {
const seq = ids.slice(-CONFIG.max_seq_len);
const tensor = new ort.Tensor("int64",
BigInt64Array.from(seq.map(BigInt)), [1, seq.length]);
const out = await session.run({ input_ids: tensor });
const logits = out.logits.data;
const offset = (seq.length - 1) * CONFIG.vocab_size;
const lastLogits = new Float32Array(CONFIG.vocab_size);
for (let v = 0; v < CONFIG.vocab_size; v++) {
lastLogits[v] = logits[offset + v];
}
if (GEN.temperature !== 1.0) {
for (let v = 0; v < CONFIG.vocab_size; v++) {
lastLogits[v] /= GEN.temperature;
}
}
if (GEN.top_k > 0) {
const sorted = [...lastLogits].sort((a, b) => b - a);
const cutoff = sorted[Math.min(GEN.top_k, sorted.length) - 1];
for (let v = 0; v < CONFIG.vocab_size; v++) {
if (lastLogits[v] < cutoff) lastLogits[v] = -Infinity;
}
}
const maxVal = Math.max(...lastLogits.filter(v => v !== -Infinity));
let sumExp = 0;
const probs = new Float32Array(CONFIG.vocab_size);
for (let v = 0; v < CONFIG.vocab_size; v++) {
probs[v] = Math.exp(lastLogits[v] - maxVal);
sumExp += probs[v];
}
for (let v = 0; v < CONFIG.vocab_size; v++) probs[v] /= sumExp;
let r = Math.random(), acc = 0, nextId = 0;
for (let v = 0; v < CONFIG.vocab_size; v++) {
acc += probs[v];
if (acc >= r) { nextId = v; break; }
}
ids.push(nextId);
if (nextId === CONFIG.eos_id) break;
}
return ids.slice(inputIds.length);
}
// ── Chat logic ──────────────────────────────────────────────────────
async function send() {
const input = document.getElementById("user-input");
const text = input.value.trim();
if (!text || !session) return;
// Switch from sample pills to chat view on first message
if (!chatStarted) {
chatStarted = true;
document.getElementById("samples").classList.add("hidden");
document.getElementById("chat").classList.remove("hidden");
}
input.value = "";
input.disabled = true;
document.getElementById("send-btn").disabled = true;
addMessage("user", text);
const typing = addTyping();
const prompt = `<|im_start|>user\n${text}<|im_end|>\n<|im_start|>assistant\n`;
const inputIds = tokenizer.encode(prompt);
const outputIds = await generate(inputIds);
let reply = tokenizer.decode(outputIds).trim();
if (reply.includes("<|im_end|>")) reply = reply.split("<|im_end|>")[0];
if (reply.includes("<|im_start|>")) reply = reply.split("<|im_start|>")[0];
reply = reply.trim() || "blub.";
typing.remove();
addMessage("fish", reply);
input.disabled = false;
document.getElementById("send-btn").disabled = false;
input.focus();
}
window.send = send;
// ── UI helpers ──────────────────────────────────────────────────────
function addMessage(role, text) {
const chat = document.getElementById("chat");
const div = document.createElement("div");
div.className = `msg ${role}`;
const emoji = document.createElement("span");
emoji.className = "emoji";
emoji.textContent = role === "user" ? "\u{1F9D1}" : "\u{1F420}";
const body = document.createElement("div");
body.className = "body";
const label = document.createElement("div");
label.className = "label";
label.textContent = role === "user" ? "you" : "guppy";
body.appendChild(label);
body.appendChild(document.createTextNode(text));
div.appendChild(emoji);
div.appendChild(body);
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
return div;
}
function addTyping() {
const chat = document.getElementById("chat");
const div = document.createElement("div");
div.className = "typing";
div.textContent = "\u{1F420} guppy is thinking...";
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
return div;
}
document.getElementById("user-input").addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send(); }
});
// ── Topics hint popup ───────────────────────────────────────────────
const TOPICS = [
["greetings", "hi guppy"], ["food", "are you hungry"], ["temperature", "is it cold today"],
["water", "how is the water"], ["bubbles", "do you like bubbles"], ["light", "is it bright"],
["night", "goodnight guppy"], ["dreams", "what do you dream about"], ["jokes", "tell me a joke"],
["love", "do you love me"], ["loneliness", "do you get lonely"], ["meaning of life", "what is the meaning of life"],
["cats", "the cat is looking at you"], ["rain", "it is raining outside"], ["music", "do you like music"],
["feelings", "are you happy"], ["swimming", "do you like swimming"], ["plants", "tell me about your plants"],
["colors", "what is your favorite color"], ["memory", "do you remember me"],
["glass tapping", "sorry i tapped the glass"], ["reflection", "who is that fish in the glass"],
["size", "are you small"], ["scared", "are you scared"], ["bored", "are you bored"],
["intelligence", "are you smart"], ["friends", "do you have friends"], ["sleep", "are you tired"],
["time", "what time is it"], ["outside", "what is outside the tank"],
["noise", "it is loud today"], ["visitors", "someone new is here"], ["singing", "can you sing"],
["age", "how old are you"], ["name", "what is your name"], ["weather", "how is the weather"],
["future", "what will you do tomorrow"], ["past", "what did you do today"],
["filter", "do you hear the filter"], ["algae", "is there algae"],
["snails", "are there snails in the tank"], ["health", "are you feeling ok"],
["excited", "are you excited"], ["curious", "what are you curious about"],
["taste", "what does the water taste like"], ["breathing", "how do you breathe"],
["TV", "do you watch tv"], ["children", "do you like children"],
["seasons", "what season is it"], ["fear", "what are you afraid of"],
];
document.getElementById("hint-btn").addEventListener("click", () => {
const overlay = document.getElementById("topics-overlay");
overlay.innerHTML = "";
overlay.classList.remove("hidden");
const box = document.createElement("div");
box.id = "topics-box";
box.innerHTML = "<h3>🐠 Guppy knows 60 topics — click one to ask</h3>";
const grid = document.createElement("div");
grid.className = "topics";
for (const [label, msg] of TOPICS) {
const btn = document.createElement("button");
btn.className = "topic";
btn.textContent = label;
btn.addEventListener("click", () => {
overlay.classList.add("hidden");
document.getElementById("user-input").value = msg;
send();
});
grid.appendChild(btn);
}
box.appendChild(grid);
const close = document.createElement("button");
close.className = "close-btn";
close.textContent = "close";
close.addEventListener("click", () => overlay.classList.add("hidden"));
box.appendChild(close);
overlay.appendChild(box);
overlay.addEventListener("click", (e) => {
if (e.target === overlay) overlay.classList.add("hidden");
});
});
</script>
</body>
</html>