Skip to content

Commit eb29f82

Browse files
authored
docs: AST documentation (#963)
Signed-off-by: Louis Mandel <[email protected]>
1 parent bbb2f7c commit eb29f82

File tree

3 files changed

+90
-17
lines changed

3 files changed

+90
-17
lines changed

pdl-live-react/src/pdl_ast.d.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ export type Description20 = string | null
158158
*
159159
*/
160160
export type Def = string | null
161+
/**
162+
* Values allowed in the `contribute` field.
163+
*/
161164
export type ContributeTarget = "result" | "context"
162165
export type Value = LocalizedExpression | unknown[] | string
163166
export type Path = string[]
@@ -3075,9 +3078,9 @@ export interface Defs1 {
30753078
*
30763079
* Example:
30773080
* ```PDL
3078-
* - model: ollama/granite-code:8b
3079-
* parameters:
3080-
* stop: ['!']
3081+
* model: ollama/granite-code:8b
3082+
* parameters:
3083+
* stop: ['!']
30813084
* ```
30823085
*/
30833086
export interface LitellmModelBlock {
@@ -4688,6 +4691,9 @@ export interface Defs20 {
46884691
| EmptyBlock
46894692
| null
46904693
}
4694+
/**
4695+
* Contribution of a specific value instead of the default one.
4696+
*/
46914697
export interface ContributeValue {
46924698
value: Value
46934699
}
@@ -4710,6 +4716,9 @@ export interface PdlLocationType {
47104716
export interface Table {
47114717
[k: string]: number
47124718
}
4719+
/**
4720+
* Use a PDL program as a parser specification (experimental).
4721+
*/
47134722
export interface PdlParser {
47144723
description?: Description21
47154724
spec?:
@@ -4734,7 +4743,7 @@ export interface PdlParser {
47344743
pdl: Pdl
47354744
}
47364745
/**
4737-
* A regular expression parser
4746+
* A regular expression parser.
47384747
*/
47394748
export interface RegexParser {
47404749
description?: Description22
@@ -4769,16 +4778,28 @@ export interface PdlTiming {
47694778
first_use_nanos?: FirstUseNanos
47704779
timezone?: Timezone
47714780
}
4781+
/**
4782+
* Join loop iterations as a string.
4783+
*/
47724784
export interface JoinText {
47734785
as?: As
47744786
with?: With
47754787
}
4788+
/**
4789+
* Join loop iterations as an array.
4790+
*/
47764791
export interface JoinArray {
47774792
as: As1
47784793
}
4794+
/**
4795+
* Join loop iterations as an object.
4796+
*/
47794797
export interface JoinObject {
47804798
as: As2
47814799
}
4800+
/**
4801+
* Join loop iterations as the value of the last iteration.
4802+
*/
47824803
export interface JoinLastOf {
47834804
as: As3
47844805
}
@@ -4793,14 +4814,23 @@ export interface MatchCase {
47934814
pdl__if_result?: PdlIfResult
47944815
pdl__matched?: PdlMatched
47954816
}
4817+
/**
4818+
* Match any of the patterns.
4819+
*/
47964820
export interface OrPattern {
47974821
def?: Def12
47984822
anyOf: Anyof
47994823
}
4824+
/**
4825+
* Match an array.
4826+
*/
48004827
export interface ArrayPattern {
48014828
def?: Def13
48024829
array: Array1
48034830
}
4831+
/**
4832+
* Match an object.
4833+
*/
48044834
export interface ObjectPattern {
48054835
def?: Def14
48064836
object: Object2
@@ -4816,6 +4846,9 @@ export interface Object2 {
48164846
| AnyPattern
48174847
| null
48184848
}
4849+
/**
4850+
* Match any value.
4851+
*/
48194852
export interface AnyPattern {
48204853
def?: Def15
48214854
any: Any

src/pdl/pdl-schema.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"$defs": {
33
"AnyPattern": {
44
"additionalProperties": false,
5+
"description": "Match any value.",
56
"properties": {
67
"def": {
78
"anyOf": [
@@ -897,6 +898,7 @@
897898
},
898899
"ArrayPattern": {
899900
"additionalProperties": false,
901+
"description": "Match an array.",
900902
"properties": {
901903
"def": {
902904
"anyOf": [
@@ -1917,6 +1919,7 @@
19171919
"type": "object"
19181920
},
19191921
"ContributeTarget": {
1922+
"description": "Values allowed in the `contribute` field.",
19201923
"enum": [
19211924
"result",
19221925
"context"
@@ -1926,6 +1929,7 @@
19261929
},
19271930
"ContributeValue": {
19281931
"additionalProperties": false,
1932+
"description": "Contribution of a specific value instead of the default one.",
19291933
"properties": {
19301934
"value": {
19311935
"anyOf": [
@@ -6185,6 +6189,7 @@
61856189
},
61866190
"JoinArray": {
61876191
"additionalProperties": false,
6192+
"description": "Join loop iterations as an array.",
61886193
"properties": {
61896194
"as": {
61906195
"const": "array",
@@ -6201,6 +6206,7 @@
62016206
},
62026207
"JoinLastOf": {
62036208
"additionalProperties": false,
6209+
"description": "Join loop iterations as the value of the last iteration.",
62046210
"properties": {
62056211
"as": {
62066212
"const": "lastOf",
@@ -6217,6 +6223,7 @@
62176223
},
62186224
"JoinObject": {
62196225
"additionalProperties": false,
6226+
"description": "Join loop iterations as an object.",
62206227
"properties": {
62216228
"as": {
62226229
"const": "object",
@@ -6233,6 +6240,7 @@
62336240
},
62346241
"JoinText": {
62356242
"additionalProperties": false,
6243+
"description": "Join loop iterations as a string.",
62366244
"properties": {
62376245
"as": {
62386246
"const": "text",
@@ -6747,7 +6755,7 @@
67476755
},
67486756
"LitellmModelBlock": {
67496757
"additionalProperties": false,
6750-
"description": "Call an LLM through [the LiteLLM API](https://docs.litellm.ai/).\n\nExample:\n```PDL\n- model: ollama/granite-code:8b\n parameters:\n stop: ['!']\n```",
6758+
"description": "Call an LLM through [the LiteLLM API](https://docs.litellm.ai/).\n\nExample:\n```PDL\nmodel: ollama/granite-code:8b\nparameters:\n stop: ['!']\n```",
67516759
"properties": {
67526760
"description": {
67536761
"anyOf": [
@@ -9365,6 +9373,7 @@
93659373
},
93669374
"ObjectPattern": {
93679375
"additionalProperties": false,
9376+
"description": "Match an object.",
93689377
"properties": {
93699378
"def": {
93709379
"anyOf": [
@@ -9454,6 +9463,7 @@
94549463
},
94559464
"OrPattern": {
94569465
"additionalProperties": false,
9466+
"description": "Match any of the patterns.",
94579467
"properties": {
94589468
"def": {
94599469
"anyOf": [
@@ -9625,6 +9635,7 @@
96259635
},
96269636
"PdlParser": {
96279637
"additionalProperties": false,
9638+
"description": "Use a PDL program as a parser specification (experimental).",
96289639
"properties": {
96299640
"description": {
96309641
"anyOf": [
@@ -10360,7 +10371,7 @@
1036010371
},
1036110372
"RegexParser": {
1036210373
"additionalProperties": false,
10363-
"description": "A regular expression parser",
10374+
"description": "A regular expression parser.",
1036410375
"properties": {
1036510376
"description": {
1036610377
"anyOf": [

src/pdl/pdl_ast.py

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

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

104105

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

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

113114

114115
class OrPattern(Pattern):
115-
anyOf: list["PatternType"]
116116
"""Match any of the patterns."""
117117

118+
anyOf: list["PatternType"]
119+
"""List of possible patterns."""
120+
118121

119122
class ArrayPattern(Pattern):
120-
array: list["PatternType"]
121123
"""Match an array."""
122124

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

124129
class ObjectPattern(Pattern):
125-
object: dict[str, "PatternType"]
126130
"""Match an object."""
127131

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

129136
class AnyPattern(Pattern):
130-
any: Literal[None]
131137
"""Match any value."""
132138

139+
any: Literal[None]
140+
133141

134142
PatternType: TypeAlias = (
135143
None
@@ -142,11 +150,13 @@ class AnyPattern(Pattern):
142150
| ObjectPattern
143151
| AnyPattern
144152
)
153+
"""Patterns allowed to match values in a `case` clause."""
145154

146155

147156
BasePdlType: TypeAlias = Literal[
148157
"null", "boolean", "string", "number", "integer", "array", "object"
149158
]
159+
"""Base types."""
150160

151161

152162
class PdlType(BaseModel):
@@ -201,6 +211,7 @@ class ObjectPdlType(PdlType):
201211
Field(union_mode="left_to_right"),
202212
],
203213
)
214+
"""Types."""
204215

205216
pdl_type_adapter = TypeAdapter(PdlTypeType)
206217

@@ -218,12 +229,14 @@ class Parser(BaseModel):
218229

219230

220231
class PdlParser(Parser):
232+
"""Use a PDL program as a parser specification (experimental)."""
233+
221234
pdl: "BlockType"
222-
"""Use a PDL program as a parser specification."""
235+
"""PDL program describing the shape of the expected value."""
223236

224237

225238
class RegexParser(Parser):
226-
"""A regular expression parser"""
239+
"""A regular expression parser."""
227240

228241
regex: str
229242
"""Regular expression to parse the value."""
@@ -238,17 +251,24 @@ class RegexParser(Parser):
238251

239252

240253
RoleType: TypeAlias = Optional[str]
254+
"""Role name."""
241255

242256

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

247263

248264
class ContributeValue(BaseModel):
249-
value: ExpressionType[list[Any]]
265+
"""Contribution of a specific value instead of the default one."""
266+
250267
model_config = ConfigDict(extra="forbid")
251268

269+
value: ExpressionType[list[Any]]
270+
"""Value to contribute."""
271+
252272

253273
class PdlTiming(BaseModel):
254274
"""Internal data structure to record timing information in the trace."""
@@ -505,9 +525,9 @@ class LitellmModelBlock(ModelBlock):
505525
506526
Example:
507527
```PDL
508-
- model: ollama/granite-code:8b
509-
parameters:
510-
stop: ['!']
528+
model: ollama/granite-code:8b
529+
parameters:
530+
stop: ['!']
511531
```
512532
"""
513533

@@ -771,6 +791,8 @@ class JoinConfig(BaseModel):
771791

772792

773793
class JoinText(JoinConfig):
794+
"""Join loop iterations as a string."""
795+
774796
as_: Literal[IterationType.TEXT] = Field(alias="as", default=IterationType.TEXT)
775797
"""String concatenation of the result of each iteration.
776798
"""
@@ -781,24 +803,31 @@ class JoinText(JoinConfig):
781803

782804

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

788812

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

794820

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

800828

801829
JoinType: TypeAlias = JoinText | JoinArray | JoinObject | JoinLastOf
830+
"""Different ways to join loop iterations."""
802831

803832

804833
class RepeatBlock(StructuredBlock):

0 commit comments

Comments
 (0)