mixle.task.design module

LLM-designed models: let an LLM propose the mixle structure from data – and make mixle validate it.

The hardcoded auto-estimator (mixle.task.recommend.recommend_model()) is a fine fallback, but the differentiator is flexibility: an LLM can read a data profile and propose a structure no fixed heuristic encodes – a mixture here, a heavy-tailed leaf there, a composite of mixed families. The risk with “LLM picks the model” is hallucination; the answer is grounding. The LLM emits a small, allowlisted JSON spec (no code, no eval); spec_to_estimator() builds a real mixle estimator from it; and design_model() fits it before trusting it, falling back to the heuristic when the LLM is unavailable or its spec fails to build or fit.

So the LLM proposes and mixle disposes: you get flexible, data-aware model design with a hard correctness gate (it must parse, build, and fit), not a plausible-looking guess. design_model(data, llm) returns the chosen estimator, the spec, and whether it came from the LLM or the fallback.

spec_to_estimator(spec)[source]

Build a mixle estimator from an allowlisted spec dict (recursively); raise on anything off the allowlist.

Specs:
  • {"family": "<name>"} – a scalar leaf (see ALLOWED_FAMILIES);

  • {"type": "composite", "fields": [spec, ...]} – a tuple record of sub-models;

  • {"type": "mixture", "k": K, "component": spec} – a K-component mixture of the component model.

Parameters:

spec (dict[str, Any])

Return type:

Any

data_profile(data, *, max_rows=500)[source]

A compact, family-agnostic description of data for the LLM: per-field kind and a few sample values.

Parameters:
Return type:

dict[str, Any]

class DesignedModel(estimator, spec, source, note='')[source]

Bases: object

The model an LLM (or the fallback) designed: the estimator, the spec it built from, and the source.

Parameters:
estimator: Any
spec: dict[str, Any] | None
source: str
note: str = ''
fit(data, **kwargs)[source]
Parameters:
Return type:

Any

design_model(data, llm, *, fallback=True, validate_rows=200)[source]

Ask llm to design a model for data; build, fit-validate, and fall back to the heuristic on failure.

The LLM sees a compact data_profile() and returns a JSON spec; the spec is built into a real estimator and fit on a sample to prove it works. Any failure (no LLM, bad JSON, off-allowlist family, fit error) yields the heuristic mixle.task.recommend.recommend_model() estimator when fallback is set.

Parameters:
Return type:

DesignedModel