dsl
Regular expression DSL and output types for structured generation.
This module contains elements related to three logical steps in the use of output types for structured generation:
- Definition of
Term
classes that contain output type definitions. That includes both terms intended to be used by themselves such asJsonSchema
orCFG
and terms that are part of the regular expression DSL such asAlternatives
orKleeneStar
(and the related functions). - Conversion of Python types into
Term
instances (python_types_to_terms
). - Conversion of a
Term
instance into a regular expression (to_regex
).
CFG
dataclass
Bases: Term
Class representing a context-free grammar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
definition
|
str
|
The definition of the context-free grammar as a string. |
required |
Source code in outlines/types/dsl.py
from_file(path)
classmethod
Create a CFG instance from a file containing a CFG definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the file containing the CFG definition. |
required |
Returns:
Type | Description |
---|---|
CFG
|
A CFG instance. |
Source code in outlines/types/dsl.py
FSM
dataclass
Bases: Term
Class representing a finite state machine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fsm
|
FSM
|
The finite state machine to store. This object must be an instance of
|
required |
Source code in outlines/types/dsl.py
JsonSchema
Bases: Term
Class representing a JSON schema.
The JSON schema object from which to instantiate the class can be a dictionary, a string, a Pydantic model, a typed dict, a dataclass, or a genSON schema builder.
Source code in outlines/types/dsl.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
__init__(schema, whitespace_pattern=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Union[dict, str, type[BaseModel], _TypedDictMeta, type, SchemaBuilder]
|
The object containing the JSON schema. |
required |
whitespace_pattern
|
Optional[str]
|
The pattern to use to match whitespace characters. |
None
|
Source code in outlines/types/dsl.py
from_file(path)
classmethod
Create a JsonSchema instance from a .json file containing a JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to the file containing the JSON schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchema
|
A JsonSchema instance. |
Source code in outlines/types/dsl.py
Regex
dataclass
Bases: Term
Class representing a regular expression.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
The regular expression as a string. |
required |
Source code in outlines/types/dsl.py
Term
Represents types defined with a regular expression.
Regex
instances can be used as a type in a Pydantic model definittion.
They will be translated to JSON Schema as a "string" field with the
"pattern" keyword set to the regular expression this class represents. The
class also handles validation.
Examples:
>>> from outlines.types import Regex
>>> from pydantic import BaseModel
>>>
>>> age_type = Regex("[0-9]+")
>>>
>>> class User(BaseModel):
>>> name: str
>>> age: age_type
Source code in outlines/types/dsl.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
display_ascii_tree(indent='', is_last=True)
Display the regex tree in ASCII format.
Source code in outlines/types/dsl.py
matches(value)
Check that a given value is in the language defined by the Term.
We make the assumption that the language defined by the term can be defined with a regular expression.
Source code in outlines/types/dsl.py
at_least(count, term)
at_most(count, term)
either(*terms)
Represents an alternative between different terms or strings.
This factory function automatically translates string arguments
into String
objects.
Source code in outlines/types/dsl.py
exactly(count, term)
python_types_to_terms(ptype, recursion_depth=0)
Convert Python types to Outlines DSL terms that constrain LLM output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ptype
|
Any
|
The Python type to convert |
required |
recursion_depth
|
int
|
Current recursion depth to prevent infinite recursion |
0
|
Returns:
Type | Description |
---|---|
Term
|
The corresponding DSL |
Source code in outlines/types/dsl.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|
to_regex(term)
Convert a term to a regular expression.
We only consider self-contained terms that do not refer to another rule.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
term
|
Term
|
The term to convert to a regular expression. |
required |
Returns:
Type | Description |
---|---|
str
|
The regular expression as a string. |