CrucibleIR.Builder (CrucibleIR v0.3.0)
View SourceFluent builder API for constructing experiments.
The Builder module provides a fluent, chainable API for building complex CrucibleIR experiments with validation. It simplifies the creation of experiments by providing convenience methods and automatic validation.
Example
{:ok, exp} =
CrucibleIR.Builder.experiment(:my_exp)
|> CrucibleIR.Builder.with_description("Test experiment")
|> CrucibleIR.Builder.with_backend(:gpt4)
|> CrucibleIR.Builder.add_stage(:inference)
|> CrucibleIR.Builder.with_ensemble(:majority)
|> CrucibleIR.Builder.with_stats([:ttest], alpha: 0.01)
|> CrucibleIR.Builder.build()Fluent API
All builder functions return the modified experiment struct, allowing
for method chaining. Use build/1 at the end to validate and finalize.
Summary
Functions
Adds an output specification to the experiment.
Adds a stage to the experiment pipeline.
Validates and finalizes the experiment.
Creates a new experiment builder with the given ID.
Adds a backend to the experiment.
Sets a baseline model for comparison experiments.
Adds a dataset to the experiment.
Adds a description to the experiment.
Adds ensemble voting configuration to the experiment.
Sets the experiment type.
Adds fairness checking configuration to the experiment.
Adds feedback collection configuration to the experiment.
Adds guardrails configuration to the experiment.
Adds request hedging configuration to the experiment.
Sets a model version for the experiment.
Adds statistical testing configuration to the experiment.
Adds training configuration to the experiment.
Types
@type builder_experiment() :: %CrucibleIR.Experiment{ backend: CrucibleIR.BackendRef.t() | nil, baseline: term(), created_at: DateTime.t() | nil, dataset: CrucibleIR.DatasetRef.t() | nil, description: String.t() | nil, experiment_type: term(), id: atom(), metadata: map() | nil, model_version: term(), outputs: [CrucibleIR.OutputSpec.t()] | nil, owner: String.t() | nil, pipeline: [CrucibleIR.StageDef.t()], reliability: CrucibleIR.Reliability.Config.t() | nil, tags: [atom()] | nil, training_config: term(), updated_at: DateTime.t() | nil }
Functions
@spec add_output(builder_experiment(), atom(), keyword()) :: builder_experiment()
Adds an output specification to the experiment.
Parameters
exp- Experiment structname- Atom name for the outputopts- Optional keyword list with:formats,:sink,:options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.add_output(:results)
iex> hd(exp.outputs).name
:results
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.add_output(:results, formats: [:json, :html])
iex> hd(exp.outputs).formats
[:json, :html]
@spec add_stage(builder_experiment(), atom(), keyword()) :: builder_experiment()
Adds a stage to the experiment pipeline.
Parameters
exp- Experiment structstage_name- Atom name for the stageopts- Optional keyword list with:module,:options,:enabled
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.add_stage(:preprocessing)
iex> hd(exp.pipeline).name
:preprocessing
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.add_stage(:preprocessing, enabled: false)
iex> hd(exp.pipeline).enabled
false
@spec build(builder_experiment()) :: {:ok, CrucibleIR.Experiment.t()} | {:error, [String.t()]}
Validates and finalizes the experiment.
Returns {:ok, experiment} if valid, or {:error, errors} if invalid.
Examples
iex> {:ok, exp} = CrucibleIR.Builder.experiment(:test)
...> |> CrucibleIR.Builder.with_backend(:gpt4)
...> |> CrucibleIR.Builder.add_stage(:run)
...> |> CrucibleIR.Builder.build()
iex> exp.id
:test
iex> {:error, errors} = CrucibleIR.Builder.experiment(:test)
...> |> CrucibleIR.Builder.build()
iex> is_list(errors)
true
@spec experiment(atom()) :: builder_experiment()
Creates a new experiment builder with the given ID.
Parameters
id- Atom identifier for the experiment
Returns
An Experiment struct with only the id set (incomplete, not validated).
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> exp.id
:test
@spec with_backend(builder_experiment(), atom(), keyword()) :: builder_experiment()
Adds a backend to the experiment.
Parameters
exp- Experiment structbackend_id- Atom identifier for the backendopts- Optional keyword list with:profileand:options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_backend(:gpt4)
iex> exp.backend.id
:gpt4
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_backend(:gpt4, profile: :fast, options: %{temp: 0.7})
iex> exp.backend.profile
:fast
@spec with_baseline(builder_experiment(), atom(), keyword()) :: builder_experiment()
Sets a baseline model for comparison experiments.
Parameters
exp- Experiment structmodel_id- Baseline model identifieropts- Optional keyword list with model options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_baseline(:gpt3)
iex> exp.baseline.id
:gpt3
@spec with_dataset(builder_experiment(), atom() | String.t(), keyword()) :: builder_experiment()
Adds a dataset to the experiment.
Parameters
exp- Experiment structdataset_name- Atom or string name for the datasetopts- Optional keyword list with:provider,:split,:options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_dataset(:mmlu)
iex> exp.dataset.name
:mmlu
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_dataset(:mmlu, split: :test)
iex> exp.dataset.split
:test
@spec with_description(builder_experiment(), String.t()) :: builder_experiment()
Adds a description to the experiment.
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_description("My experiment")
iex> exp.description
"My experiment"
@spec with_ensemble(builder_experiment(), atom(), keyword()) :: builder_experiment()
Adds ensemble voting configuration to the experiment.
Parameters
exp- Experiment structstrategy- Voting strategy atomopts- Optional keyword list with ensemble options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_ensemble(:majority)
iex> exp.reliability.ensemble.strategy
:majority
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_ensemble(:weighted, models: [:gpt4, :claude])
iex> exp.reliability.ensemble.models
[:gpt4, :claude]
@spec with_experiment_type(builder_experiment(), atom()) :: builder_experiment()
Sets the experiment type.
Parameters
exp- Experiment structtype- Experiment type (:evaluation, :training, :comparison, :ablation)
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_experiment_type(:evaluation)
iex> exp.experiment_type
:evaluation
@spec with_fairness( builder_experiment(), keyword() ) :: builder_experiment()
Adds fairness checking configuration to the experiment.
Parameters
exp- Experiment structopts- Keyword list with fairness options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_fairness(metrics: [:demographic_parity])
iex> exp.reliability.fairness.metrics
[:demographic_parity]
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_fairness(threshold: 0.8, enabled: true)
iex> exp.reliability.fairness.threshold
0.8
@spec with_feedback( builder_experiment(), keyword() ) :: builder_experiment()
Adds feedback collection configuration to the experiment.
Parameters
exp- Experiment structopts- Keyword list with feedback options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_feedback(enabled: true, sampling_rate: 0.1)
iex> exp.reliability.feedback.sampling_rate
0.1
@spec with_guardrails( builder_experiment(), keyword() ) :: builder_experiment()
Adds guardrails configuration to the experiment.
Parameters
exp- Experiment structopts- Keyword list with guardrail options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_guardrails(profiles: [:strict])
iex> exp.reliability.guardrails.profiles
[:strict]
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_guardrails(pii_detection: true)
iex> exp.reliability.guardrails.pii_detection
true
@spec with_hedging(builder_experiment(), atom(), keyword()) :: builder_experiment()
Adds request hedging configuration to the experiment.
Parameters
exp- Experiment structstrategy- Hedging strategy atomopts- Optional keyword list with hedging options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_hedging(:fixed)
iex> exp.reliability.hedging.strategy
:fixed
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_hedging(:percentile, delay_ms: 100)
iex> exp.reliability.hedging.delay_ms
100
@spec with_model_version(builder_experiment(), atom(), String.t(), keyword()) :: builder_experiment()
Sets a model version for the experiment.
Parameters
exp- Experiment structmodel_id- Model identifierversion- Version stringopts- Optional keyword list with version options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_model_version(:gpt2, "1.0.0")
iex> exp.model_version.version
"1.0.0"
@spec with_stats(builder_experiment(), [atom()], keyword()) :: builder_experiment()
Adds statistical testing configuration to the experiment.
Parameters
exp- Experiment structtests- List of statistical test atomsopts- Optional keyword list with stats options
Examples
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_stats([:ttest])
iex> exp.reliability.stats.tests
[:ttest]
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_stats([:ttest, :bootstrap], alpha: 0.01)
iex> exp.reliability.stats.alpha
0.01
@spec with_training_config( builder_experiment(), CrucibleIR.ModelRef.t(), CrucibleIR.DatasetRef.t(), keyword() ) :: builder_experiment()
Adds training configuration to the experiment.
Parameters
exp- Experiment structmodel_ref- ModelRef struct or keyword to create onedataset_ref- DatasetRef struct or keyword to create oneopts- Optional keyword list with training options
Examples
iex> model = %CrucibleIR.ModelRef{id: :gpt2}
iex> dataset = %CrucibleIR.DatasetRef{name: :wikitext}
iex> exp = CrucibleIR.Builder.experiment(:test)
iex> |> CrucibleIR.Builder.with_training_config(model, dataset, epochs: 10)
iex> exp.training_config.epochs
10