πŸ”’ Constrainers#

class text_machina.src.constrainers.base.Constrainer[source]#

Bases: ABC

Base class for constrainers.

A constrainer is any kind of class that infers something from a dataset and constrains the generation parameters according to that. For instance, length constrainers, that automatically infer the length and return maximum or minimum number of tokens accordingly.

constrain(generation_config)[source]#

Constrains a generation_config.

Parameters:

generation_config (Dict[str, Any]) – a generation config.

Returns:

constrained generation config.

Return type:

Dict[str, Any]

abstract estimate()[source]#

Method to estimate values that will be used in constrain.

Returns:

any kind of value.

Return type:

Any

abstract get_constraints()[source]#

Method that return parameters with values to constrain later the generation parameters.

Example

output: {β€œmax_tokens”: 137, β€œmin_tokens”: 32}

Returns:

values to constrain generation parameters.

Return type:

Dict[str, Any]

class text_machina.src.constrainers.length.LengthConstrainer(lengths, provider, min_tokens=10, max_tokens=512)[source]#

Bases: Constrainer, ABC

Base class for length constrainers to compute automatically the number of tokens to generate.

abstract estimate()[source]#

Estimates min and max token lengths.

Returns:

tuple with the min and max token lengths.

Return type:

Tuple[int, int]

get_constraints()[source]#

Obtains min and max token lengths, and returns these params according to the provider.

Returns:

dict with the token length params.

Return type:

Dict[str, int]

class text_machina.src.constrainers.length.MeanLengthConstrainer(lengths, provider, min_tokens=10, max_tokens=512)[source]#

Bases: LengthConstrainer

Constrainer within one std radius of the mean of a list of numbers

estimate()[source]#

Estimates min and max token lengths.

Returns:

tuple with the min and max token lengths.

Return type:

Tuple[int, int]

class text_machina.src.constrainers.length.MedianLengthConstrainer(lengths, provider, min_tokens=10, max_tokens=512)[source]#

Bases: LengthConstrainer

Constrainer within one std radius of the median of a list of numbers

estimate()[source]#

Estimates min and max token lengths.

Returns:

tuple with the min and max token lengths.

Return type:

Tuple[int, int]