Skip to content

Commit ff8bc52

Browse files
committed
docs: AST documentation
Signed-off-by: Louis Mandel <[email protected]>
1 parent 9aa0df2 commit ff8bc52

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

src/pdl/pdl_ast.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ class LocalizedExpression(BaseModel, Generic[LocalizedExpressionT]):
9999

100100
ExpressionTypeT = TypeVar("ExpressionTypeT")
101101
ExpressionType: TypeAlias = LocalizedExpression[ExpressionTypeT] | ExpressionTypeT | str
102+
"""Expressions are represented Jinja as strings in between `${` and `}`."""
102103

103104

104105
class Pattern(BaseModel):
105-
"""Patterns allowed to match values in a `case` clause."""
106+
"""Common fields for structured patterns."""
106107

107108
model_config = ConfigDict(extra="forbid")
108109
def_: Optional[str] = Field(default=None, alias="def")
@@ -111,24 +112,31 @@ class Pattern(BaseModel):
111112

112113

113114
class OrPattern(Pattern):
114-
anyOf: list["PatternType"]
115115
"""Match any of the patterns."""
116116

117+
anyOf: list["PatternType"]
118+
"""List of possible patterns."""
119+
117120

118121
class ArrayPattern(Pattern):
119-
array: list["PatternType"]
120122
"""Match an array."""
121123

124+
array: list["PatternType"]
125+
"""Shape of the array to match."""
126+
122127

123128
class ObjectPattern(Pattern):
124-
object: dict[str, "PatternType"]
125129
"""Match an object."""
126130

131+
object: dict[str, "PatternType"]
132+
"""Shape of the object to match."""
133+
127134

128135
class AnyPattern(Pattern):
129-
any: Literal[None]
130136
"""Match any value."""
131137

138+
any: Literal[None]
139+
132140

133141
PatternType: TypeAlias = (
134142
None
@@ -141,11 +149,13 @@ class AnyPattern(Pattern):
141149
| ObjectPattern
142150
| AnyPattern
143151
)
152+
"""Patterns allowed to match values in a `case` clause."""
144153

145154

146155
BasePdlType: TypeAlias = Literal[
147156
"null", "boolean", "string", "number", "integer", "array", "object"
148157
]
158+
"""Base types."""
149159

150160

151161
class PdlType(BaseModel):
@@ -200,6 +210,7 @@ class ObjectPdlType(PdlType):
200210
Field(union_mode="left_to_right"),
201211
],
202212
)
213+
"""Types."""
203214

204215
pdl_type_adapter = TypeAdapter(PdlTypeType)
205216

@@ -217,12 +228,14 @@ class Parser(BaseModel):
217228

218229

219230
class PdlParser(Parser):
231+
"""Use a PDL program as a parser specification (experimental)."""
232+
220233
pdl: "BlockType"
221-
"""Use a PDL program as a parser specification."""
234+
"""PDL program describing the shape of the expected value."""
222235

223236

224237
class RegexParser(Parser):
225-
"""A regular expression parser"""
238+
"""A regular expression parser."""
226239

227240
regex: str
228241
"""Regular expression to parse the value."""
@@ -237,17 +250,24 @@ class RegexParser(Parser):
237250

238251

239252
RoleType: TypeAlias = Optional[str]
253+
"""Role name."""
240254

241255

242256
class ContributeTarget(StrEnum):
257+
"""Values allowed in the `contribute` field."""
258+
243259
RESULT = "result"
244260
CONTEXT = "context"
245261

246262

247263
class ContributeValue(BaseModel):
248-
value: ExpressionType[list[Any]]
264+
"""Contribution of a specific value instead of the default one."""
265+
249266
model_config = ConfigDict(extra="forbid")
250267

268+
value: ExpressionType[list[Any]]
269+
"""Value to contribute."""
270+
251271

252272
class PdlTiming(BaseModel):
253273
"""Internal data structure to record timing information in the trace."""
@@ -504,9 +524,9 @@ class LitellmModelBlock(ModelBlock):
504524
505525
Example:
506526
```PDL
507-
- model: ollama/granite-code:8b
508-
parameters:
509-
stop: ['!']
527+
model: ollama/granite-code:8b
528+
parameters:
529+
stop: ['!']
510530
```
511531
"""
512532

@@ -770,6 +790,8 @@ class JoinConfig(BaseModel):
770790

771791

772792
class JoinText(JoinConfig):
793+
"""Join loop iterations as a string."""
794+
773795
as_: Literal[IterationType.TEXT] = Field(alias="as", default=IterationType.TEXT)
774796
"""String concatenation of the result of each iteration.
775797
"""
@@ -780,24 +802,31 @@ class JoinText(JoinConfig):
780802

781803

782804
class JoinArray(JoinConfig):
805+
"""Join loop iterations as an array."""
806+
783807
as_: Literal[IterationType.ARRAY] = Field(alias="as")
784808
"""Return the result of each iteration as an array.
785809
"""
786810

787811

788812
class JoinObject(JoinConfig):
813+
"""Join loop iterations as an object."""
814+
789815
as_: Literal[IterationType.OBJECT] = Field(alias="as")
790816
"""Return the union of the objects created at each iteration.
791817
"""
792818

793819

794820
class JoinLastOf(JoinConfig):
821+
"""Join loop iterations as the value of the last iteration."""
822+
795823
as_: Literal[IterationType.LASTOF] = Field(alias="as")
796824
"""Return the result of the last iteration.
797825
"""
798826

799827

800828
JoinType: TypeAlias = JoinText | JoinArray | JoinObject | JoinLastOf
829+
"""Different ways to join loop iterations."""
801830

802831

803832
class RepeatBlock(StructuredBlock):

0 commit comments

Comments
 (0)