TL;DR — This project delivers a pure‑Python adaptive nucleus sampler that dynamically tunes temperature and top‑p based on token entropy, giving you a runnable artifact that showcases real systems skill. It includes real code, tests, and extension paths, making it a strong talking point on a CV.

In the race to stand out as a machine‑learning engineer, a polished portfolio piece that demonstrates end‑to‑end system design is invaluable. The following guide walks you through building a compact, dependency‑light sampler that reacts to the uncertainty of each token, a technique that bridges research and production.

Why This Project Stands Out on a CV

  • Algorithmic depth – You implement Shannon entropy, softmax, and nucleus sampling from scratch, proving you can translate theory into code.
  • Systems thinking – The sampler is designed as a reusable component with clear inputs/outputs, mirroring how production ML services are structured.
  • Performance awareness – By keeping the implementation in pure Python with optional NumPy fallbacks, you demonstrate an ability to balance speed and simplicity.
  • Testing & reliability – Including unit tests and a CLI runner shows you care about correctness and reproducibility, traits hiring managers value.
  • Scalability roadmap – The extension section outlines how to add persistence, observability, and horizontal scaling, signaling you can grow the project beyond a toy.
  • Role alignment – This artifact is ideal for positions such as ML Engineer, Data Infrastructure Engineer, or AI Research Engineer where hands‑on model serving expertise is required.

Architecture Overview

The system is composed of four loosely coupled components:

  1. Tokenizer / Input Layer – Accepts raw text and produces a sequence of token IDs (for demonstration we use a simple word‑level tokenizer, but the interface can be swapped with any tokenizer).
  2. Entropy Estimator – Computes the Shannon entropy of the probability distribution generated by the model for the next token.
  3. Adaptive Sampler – Uses the entropy value to adjust temperature ( τ ) and top‑p ( p ) on the fly, then performs nucleus sampling.
  4. CLI / Driver – Exposes the sampler as a command‑line tool, allowing users to input text and observe the generated continuation.

A high‑level flow:

Input Text → Tokenizer → Model Logits → Entropy Estimator → Adaptive Sampler → Output Tokens

Each component is a pure Python class or function, making the code easy to read, test, and replace.

Building It Step by Step

Step 1 – Project Scaffold

Create a directory adaptive_nucleus/ with the following files:

adaptive_nucleus/
│   __init__.py
│   sampler.py
│   entropy.py
│   cli.py
│   tests/
│       test_sampler.py

Step 2 – Entropy Calculation

In entropy.py, implement a function that computes Shannon entropy from a probability distribution.

import math
from typing import List

def shannon_entropy(probs: List[float]) -> float:
    """Calculate Shannon entropy H = -Σ p_i * log(p_i)."""
    entropy = 0.0
    for p in probs:
        if p > 0.0:
            entropy -= p * math.log(p)
    return entropy

Step 3 – Softmax and Logits Handling

Add a helper to convert raw logits into probabilities (avoiding overflow).

import numpy as np

def softmax(logits: np.ndarray) -> np.ndarray:
    """Stable softmax."""
    logits = logits - np.max(logits)
    exp_logits = np.exp(logits)
    return exp_logits / np.sum(exp_logits)

Step 4 – Adaptive Nucleus Sampler

In sampler.py, create the core class that ties entropy to temperature and top‑p.

import random
from typing import Tuple

import numpy as np

from .entropy import shannon_entropy
from . import softmax

class AdaptiveNucleusSampler:
    def __init__(self, base_temp: float = 1.0, base_p: float = 0.9,
                 entropy_threshold: float = 1.5):
        self.base_temp = base_temp
        self.base_p = base_p
        self.entropy_threshold = entropy_threshold

    def _adjust_params(self, entropy: float) -> Tuple[float, float]:
        """Dynamically scale temperature and top‑p based on entropy."""
        # If entropy is high, increase temperature and lower top‑p
        if entropy > self.entropy_threshold:
            temp = self.base_temp * (entropy / self.entropy_threshold)
            p = max(0.5, self.base_p - 0.2)
        else:
            temp = self.base_temp
            p = self.base_p
        return temp, p

    def sample(self, logits: np.ndarray) -> int:
        probs = softmax(logits)
        entropy = shannon_entropy(probs.tolist())
        temp, p = self._adjust_params(entropy)

        # Apply temperature
        scaled_logits = logits / temp
        probs = softmax(scaled_logits)

        # Nucleus sampling
        sorted_indices = np.argsort(probs)[::-1]
        cumulative = np.cumsum(probs[sorted_indices])
        cutoff = cumulative >= p
        if not np.any(cutoff):
            cutoff[-1] = True
        nucleus_indices = sorted_indices[cutoff]
        nucleus_probs = probs[nucleus_indices]
        nucleus_probs = nucleus_probs / nucleus_probs.sum()

        chosen_idx = np.random.choice(nucleus_indices, p=nucleus_probs)
        return int(chosen_idx)

Step 5 – CLI Driver

cli.py provides a simple way to run the sampler from the command line.

import argparse
import numpy as np
from .sampler import AdaptiveNucleusSampler

def dummy_model_logits(vocab_size: int = 100) -> np.ndarray:
    """Simulate model logits for demonstration."""
    return np.random.randn(vocab_size)

def main():
    parser = argparse.ArgumentParser(description="Adaptive Nucleus Sampler Demo")
    parser.add_argument("--steps", type=int, default=10,
                        help="Number of tokens to generate")
    args = parser.parse_args()

    sampler = AdaptiveNucleusSampler()
    for _ in range(args.steps):
        logits = dummy_model_logits()
        token_id = sampler.sample(logits)
        print(f"Token {token_id}")

if __name__ == "__main__":
    main()

Step 6 – Tests

In tests/test_sampler.py, add a basic unit test.

import numpy as np
from adaptive_nucleus.sampler import AdaptiveNucleusSampler

def test_sample_returns_int():
    sampler = AdaptiveNucleusSampler()
    logits = np.random.randn(50)
    token = sampler.sample(logits)
    assert isinstance(token, int)
    assert 0 <= token < 50

Running and Testing It

  1. Install dependencies (optional, only if you want NumPy acceleration):

    pip install numpy
    
  2. Run the CLI:

    python -m adaptive_nucleus.cli --steps 20
    

    You should see a sequence of token IDs printed, each chosen via the adaptive nucleus procedure.

  3. Execute tests:

    pytest adaptive_nucleus/tests
    

    All tests should pass, confirming the sampler returns valid token indices.

  4. Observe entropy influence – By modifying entropy_threshold in the sampler constructor, you can tune how aggressively the parameters shift in response to token uncertainty.

Extending It: Your Roadmap to Senior-Level

  1. Persist generation state – Store the probability distributions and entropy traces in a SQLite or PostgreSQL table; this matters for debugging and A/B testing of sampling strategies.
  2. Horizontal scaling – Wrap the sampler in a FastAPI service and deploy multiple replicas behind a load balancer; this is essential for handling high‑throughput inference requests.
  3. Observability – Export metrics (latency, entropy, temperature) to Prometheus and visualize them in Grafana; observability is critical for maintaining SLAs in production.
  4. Fault tolerance – Introduce retry logic with exponential backoff for model calls and use a circuit‑breaker pattern; this prevents cascading failures when the underlying model service degrades.
  5. Benchmarking harness – Build a script that compares adaptive versus fixed‑parameter sampling on perplexity and diversity metrics; benchmarking data justifies design choices to stakeholders.
  6. Model integration – Plug in a real transformer (e.g., Hugging Face transformers) and expose a /generate endpoint; this turns the prototype into a production‑ready inference service.

Key Takeaways

  • Implementing entropy‑driven adaptation shows you can blend theory (Shannon entropy) with practical sampling.
  • A modular, pure‑Python design makes the code easy to test, extend, and discuss in interviews.
  • The included CLI and unit tests demonstrate reliability and a focus on developer experience.
  • The extension roadmap highlights awareness of scalability, observability, and fault tolerance—skills valued in senior engineering roles.
  • This project can be showcased on GitHub, in a technical blog, or as a talking point during recruiting conversations.

Further Reading