Title: Mini LLM in Python - Runnable Code Snippet
Open Graph Title: Mini LLM in Python - Runnable Code Snippet
X Title: Mini LLM in Python - Runnable Code Snippet
Description: Demonstrate the core mechanism behind LLMs by predicting the next token, appending it, and repeating. Runnable Python snippet with live execution.
Open Graph Description: Demonstrate the core mechanism behind LLMs by predicting the next token, appending it, and repeating. Runnable Python snippet with live execution.
X Description: Demonstrate the core mechanism behind LLMs by predicting the next token, appending it, and repeating. Runnable Python snippet with live execution.
Keywords:
Opengraph URL: https://tempercode.dev/snippets/mini-llm-python
Domain: tempercode.dev
{"@context":"https://schema.org","@type":"SoftwareSourceCode","name":"Mini LLM","description":"Demonstrate the core mechanism behind LLMs by predicting the next token, appending it, and repeating.","programmingLanguage":"Python","text":"# LLM is a next-token predictor. That's the whole trick.\n# It looks at text, predicts what comes next, appends it, and repeats.\n\n# These patterns simulate what a model \"learned\" from training.\n# Real LLM learned from trillions of words - same idea, bigger scale.\nlearned = {\n 'hel': {'l': 0.9},\n 'ell': {'o': 0.9},\n 'llo': {' ': 0.6, '!': 0.3},\n 'lo ': {'w': 0.5, 't': 0.3},\n 'o w': {'o': 0.8},\n ' wo': {'r': 0.9},\n 'wor': {'l': 0.7},\n 'orl': {'d': 0.9},\n 'the': {' ': 0.9},\n 'he ': {'c': 0.4},\n 'e c': {'a': 0.8},\n ' ca': {'t': 0.7},\n 'cat': {' ': 0.6, 's': 0.3},\n 'at ': {'s': 0.5},\n 't s': {'a': 0.6},\n ' sa': {'t': 0.7},\n}\n\ntext = prompt.lower()\nsteps = [f'Generating from \"{text}\":']\n\n# Generate up to 12 more characters\nfor _ in range(12):\n if len(text) >= 20:\n break\n context = text[-3:]\n probs = learned.get(context)\n\n if not probs:\n break # Unknown pattern\n\n # Pick highest probability (greedy decoding)\n next_char, prob = max(probs.items(), key=lambda x: x[1])\n steps.append(f' \"{context}\" → \"{next_char}\" ({int(prob*100)}%)')\n text += next_char\n\nsteps.append(f'Result: \"{text}\"')\nreturn '\\n'.join(steps)"}
| theme-color | #0c0d12 |
| color-scheme | dark |
| author | Temper |
| creator | Temper |
| publisher | Temper |
| og:image | https://tempercode.dev/og-image.png |
| og:type | article |
| twitter:card | summary_large_image |
| twitter:image | https://tempercode.dev/og-image.png |
Links:
Viewport: width=device-width, initial-scale=1