Skip to content

Instantly share code, notes, and snippets.

@skorotkiewicz
Created September 20, 2026 04:04
Show Gist options
  • Select an option

  • Save skorotkiewicz/dedc3b5a857be7d0f2b378334721713c to your computer and use it in GitHub Desktop.

Select an option

Save skorotkiewicz/dedc3b5a857be7d0f2b378334721713c to your computer and use it in GitHub Desktop.
the smallest LLM
const text = "mama dad mama cat dad mama dog";
const words = text.split(" ");
const model = {};
for (let i = 0; i < words.length - 1; i++) {
const a = words[i];
const b = words[i + 1];
model[a] ??= [];
model[a].push(b);
}
function next(word) {
const choices = model[word] ?? words;
return choices[Math.floor(Math.random() * choices.length)];
}
function generate(start, n = 10) {
let word = start;
const out = [word];
for (let i = 0; i < n; i++) {
word = next(word);
out.push(word);
}
return out.join(" ");
}
console.log(generate("mama"));
@skorotkiewicz

Copy link
Copy Markdown
Author
from random import choices

class TinyLLM:
    def fit(self, text):
        self.text = text

    def generate(self, n):
        return "".join(choices(self.text, k=n)) if self.text else ""

model = TinyLLM()
model.fit("hello world hello tiny language model")
print(model.generate(100))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment