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
Termclasses that contain output type definitions. That includes both terms intended to be used by themselves such asJsonSchemaorCFGand terms that are part of the regular expression DSL such asAlternativesorKleeneStar(and the related functions). - Conversion of Python types into
Terminstances (python_types_to_terms). - Conversion of a
Terminstance 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
Choice
dataclass
Bases: Term
Class representing a choice between different items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List[Any]
|
The items to choose from. |
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
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
__init__(schema, whitespace_pattern=None, ensure_ascii=True)
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
|
ensure_ascii
|
bool
|
Whether to ensure the schema is ASCII-only. |
True
|
Source code in outlines/types/dsl.py
convert_to(schema, target_types)
classmethod
Convert a JSON schema type to a different JSON schema type.
If the schema provided is already of a type in the target_types, return it unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Union[JsonSchema, type[BaseModel], _TypedDictMeta, type, SchemaBuilder]
|
The schema to convert |
required |
target_types
|
List[Literal['str', 'dict', 'pydantic', 'typeddict', 'dataclass', 'genson']]
|
The target types to convert to |
required |
Source code in outlines/types/dsl.py
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
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
is_json_schema(obj)
classmethod
Check if the object provided is a JSON schema type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the object is a JSON schema type, False otherwise |
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
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 | |
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
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | |
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. |