Constraints#

Central to Langworks’ DSL are so-called constraints. These constraints specify the rules a LLM must adhere to while generating output. In effect they help guide generation, allowing for structured output, or otherwise constrained output.

Within the DSL, constraints may be disclosed by the {% and %} tags, for example: {% gen %}. Within Langworks itself, as handled by Middleware, constraints are passed along as subclasses of the Constraint class:

class langworks.dsl.constraints.base.Constraint#

Specifies a generic constraint on the generation by an LLM, to be processed by a Middleware. Any constraint implemented needs to sub-class from this constraint.

Properties#

id: int = None#

Identifying hash of the constraint.

Added in version 0.3.1.

spec: Any = None#

The specification of the constraint.

var: str | None = None#

Name of the variable used to store the content.

params: SamplingParams = None#

Sampling parameters to apply when generating the content specified by this constraint.

finalize: Callable[[type[Constraint], str], Any] | None = None#

Invoked by Middleware upon generation of the text specified by the constraint, allowing for post-processing of the output, and inclusion of the output in the DSL’s context.