Examples

Task, latent, and enumeration examples

Compact modeling patterns for task learning, structured latent models, automatic fitting, enumeration, companion packages, and notebook material.

Task learning

LLM teachers and local models.

An LLM can be used as a teacher for labeling, extraction, or routing. mixle trains a local model and uses calibration/cascades to decide when to answer locally. The local URL in the snippets is just an OpenAI-compatible endpoint.

LLM active labeling

from mixle.task import *

# pool = ["free prize today", ...]
# calibration = ["cheap loan offer", ...]
# requests = ["winner click now", ...]
teacher = llm_labeler(
    OpenAICompatLLM(
        "http://localhost:11434/v1",
        "llama3.1",
    ),
    ["spam", "ham"],
)

cascade = Cascade(
    CalibratedTaskModel(
        active_distill(
            teacher,
            pool,
            budget=60,
        ).model,
    ).calibrate(calibration, teacher(calibration)),
    teacher,
    cost=CostModel(c_frontier=0.01),
)
cascade.serve(requests)
cascade.report()

LLM field extraction

from mixle.task import *

# train_lines = [
#     "INV-1001 Acme charged $42.10 on 2026-07-01",
#     "Payment to Globex $18.50 on 2026-07-02",
#     ...
# ]
# test_lines has the same shape.
fields = ["id", "amount", "date", "vendor"]
teacher = llm_extractor(
    OpenAICompatLLM(
        "http://localhost:11434/v1",
        "llama3.1",
    ),
    fields,
)

extraction_f1(
    distill_extractor(teacher, train_lines, fields),
    teacher(test_lines),
    test_lines,
)

Probability models

Structured estimators and enumeration.

Heterogeneous latent model

from mixle.inference import *
from mixle.stats import *

# data is a list of numeric sequences:
# data[:2] == [
#     [-1.2, -0.9, -1.0, 0.8, 1.1],
#     [1.0, 1.3, 0.9, -0.8, -1.1],
# ]

fit = optimize(data, HiddenMarkovEstimator([
    GaussianEstimator(),
    GaussianEstimator(),
]))

fit.log_density(data[0])

Automatic estimator inference

from numpy.random import *
from mixle.inference import *
from mixle.utils.automatic import *

# data is a list of mixed records:
# data[:2] == [
#     (1, None, "a", [("a", 1), ("b", 2)]),
#     (3, 2, "b", [("a", 1), ("b", 2), ("c", 3)]),
# ]

est = get_estimator(data, pseudo_count=1e-4)
model = estimate(
    data,
    est,
    prev_estimate=initialize(data, est, RandomState(1)),
)
model.log_density(data[0])