Skip to content

Commit a6afb00

Browse files
authored
feat: new sequence block (#1383)
Signed-off-by: Louis Mandel <[email protected]>
1 parent 8b32517 commit a6afb00

File tree

11 files changed

+600
-226
lines changed

11 files changed

+600
-226
lines changed

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

Lines changed: 296 additions & 162 deletions
Large diffs are not rendered by default.

pdl-live-react/src/pdl_ast_utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ export function map_block_children(
129129
const data = f_expr(block.data)
130130
return { ...block, data }
131131
})
132+
.with({ kind: "sequence" }, (block) => {
133+
const sequence = block.sequence.map(f_block)
134+
return { ...block, sequence: sequence }
135+
})
132136
.with({ kind: "text" }, (block) => {
133137
let text
134138
if (block.text instanceof Array) {
@@ -267,6 +271,9 @@ export function iter_block_children(
267271
.with({ kind: "data" }, (block) => {
268272
if (block.data) f(block.data)
269273
})
274+
.with({ kind: "sequence" }, (block) => {
275+
block.sequence.forEach(f)
276+
})
270277
.with({ kind: "text" }, (block) => {
271278
if (block.text instanceof Array) {
272279
block.text.forEach(f)

pdl-live-react/src/view/breadcrumbs/BreadcrumbBar.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
}
4242
&[data-detail="lastOf"],
4343
&[data-detail="text"],
44+
&[data-detail="sequence"],
4445
&[data-detail="array"] {
4546
--pdl-bb-color: var(--pf-t--global--icon--color--disabled);
4647
}

pdl-live-react/src/view/timeline/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export function childrenOf(block: NonScalarPdlBlock) {
142142
.with({ kind: "aggregator" }, () => [])
143143
.with({ kind: "function" }, () => [])
144144
.with({ kind: "call" }, (data) => [data.pdl__trace ?? data.pdl__result])
145+
.with({ kind: "sequence" }, (data) => [data.sequence])
145146
.with({ kind: "text" }, (data) => [data.text])
146147
.with({ kind: "lastOf" }, (data) => [data.lastOf])
147148
.with({ kind: "array" }, (data) => [data.array])

src/pdl/pdl-schema.json

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@
460460
{
461461
"$ref": "#/$defs/MapBlock"
462462
},
463+
{
464+
"$ref": "#/$defs/SequenceBlock"
465+
},
463466
{
464467
"$ref": "#/$defs/TextBlock"
465468
},
@@ -2273,7 +2276,7 @@
22732276
},
22742277
"JoinArray": {
22752278
"additionalProperties": false,
2276-
"description": "Join loop iterations as an array.",
2279+
"description": "Join loop iterations or sequence of blocks as an array.",
22772280
"properties": {
22782281
"as": {
22792282
"const": "array",
@@ -2290,7 +2293,7 @@
22902293
},
22912294
"JoinLastOf": {
22922295
"additionalProperties": false,
2293-
"description": "Join loop iterations as the value of the last iteration.",
2296+
"description": "Join loop iterations or sequence of blocks as the value of the last iteration.",
22942297
"properties": {
22952298
"as": {
22962299
"const": "lastOf",
@@ -2307,7 +2310,7 @@
23072310
},
23082311
"JoinObject": {
23092312
"additionalProperties": false,
2310-
"description": "Join loop iterations as an object.",
2313+
"description": "Join loop iterations or sequence of blocks as an object.",
23112314
"properties": {
23122315
"as": {
23132316
"const": "object",
@@ -2324,7 +2327,7 @@
23242327
},
23252328
"JoinReduce": {
23262329
"additionalProperties": false,
2327-
"description": "Join loop iterations as the value of the last iteration.",
2330+
"description": "Join loop iterations or sequence of blocks as the value of the last iteration.",
23282331
"properties": {
23292332
"as": {
23302333
"const": "reduce",
@@ -2353,7 +2356,7 @@
23532356
},
23542357
"JoinText": {
23552358
"additionalProperties": false,
2356-
"description": "Join loop iterations as a string.",
2359+
"description": "Join loop iterations or sequence of blocks as a string.",
23572360
"properties": {
23582361
"as": {
23592362
"const": "text",
@@ -4553,6 +4556,136 @@
45534556
"title": "RepeatBlock",
45544557
"type": "object"
45554558
},
4559+
"SequenceBlock": {
4560+
"additionalProperties": false,
4561+
"description": "Generalization of the `text`, `lastOf`, `array`, and `object` blocks combining a list of blocks with a `join` operator.",
4562+
"properties": {
4563+
"description": {
4564+
"$ref": "#/$defs/OptionalStr",
4565+
"default": null,
4566+
"description": "Documentation associated to the block.\n "
4567+
},
4568+
"spec": {
4569+
"$ref": "#/$defs/PdlTypeType",
4570+
"default": null,
4571+
"description": "Type specification of the result of the block.\n "
4572+
},
4573+
"defs": {
4574+
"additionalProperties": {
4575+
"$ref": "#/$defs/BlockType"
4576+
},
4577+
"default": {},
4578+
"description": "Set of definitions executed before the execution of the block.\n ",
4579+
"title": "Defs",
4580+
"type": "object"
4581+
},
4582+
"def": {
4583+
"$ref": "#/$defs/OptionalStr",
4584+
"default": null,
4585+
"description": "Name of the variable used to store the result of the execution of the block.\n "
4586+
},
4587+
"contribute": {
4588+
"default": [
4589+
"result",
4590+
"context"
4591+
],
4592+
"description": "Indicate if the block contributes to the result and background context.\n ",
4593+
"items": {
4594+
"$ref": "#/$defs/ContributeElement"
4595+
},
4596+
"title": "Contribute",
4597+
"type": "array"
4598+
},
4599+
"parser": {
4600+
"$ref": "#/$defs/OptionalParserType",
4601+
"default": null,
4602+
"description": "Parser to use to construct a value out of a string result."
4603+
},
4604+
"fallback": {
4605+
"$ref": "#/$defs/OptionalBlockType",
4606+
"default": null,
4607+
"description": "Block to execute in case of error.\n "
4608+
},
4609+
"retry": {
4610+
"$ref": "#/$defs/OptionalInt",
4611+
"default": null,
4612+
"description": "The maximum number of times to retry when an error occurs within a block.\n "
4613+
},
4614+
"trace_error_on_retry": {
4615+
"$ref": "#/$defs/OptionalBoolOrStr",
4616+
"default": null,
4617+
"description": "Whether to add the errors while retrying to the trace. Set this to true to use retry feature for multiple LLM trials.\n "
4618+
},
4619+
"expectations": {
4620+
"$ref": "#/$defs/ExpectationsType",
4621+
"default": [],
4622+
"description": "Specify any expectations that the result of the block must satisfy.\n "
4623+
},
4624+
"role": {
4625+
"$ref": "#/$defs/OptionalStr",
4626+
"default": null,
4627+
"description": "Role associated to the block and sub-blocks.\nTypical roles are `system`, `user`, and `assistant`,\nbut there may be other roles such as `available_tools`."
4628+
},
4629+
"pdl__context": {
4630+
"$ref": "#/$defs/OptionalModelInput",
4631+
"default": [],
4632+
"description": "Current context."
4633+
},
4634+
"pdl__id": {
4635+
"$ref": "#/$defs/OptionalStr",
4636+
"default": "",
4637+
"description": "Unique identifier for this block."
4638+
},
4639+
"pdl__result": {
4640+
"$ref": "#/$defs/OptionalAny",
4641+
"default": null,
4642+
"description": "Result of the execution of the block."
4643+
},
4644+
"pdl__location": {
4645+
"$ref": "#/$defs/OptionalPdlLocationType",
4646+
"default": null
4647+
},
4648+
"pdl__timing": {
4649+
"$ref": "#/$defs/OptionalPdlTiming",
4650+
"default": null,
4651+
"description": "Execution timing information."
4652+
},
4653+
"pdl__is_leaf": {
4654+
"const": false,
4655+
"default": false,
4656+
"title": "Pdl Is Leaf",
4657+
"type": "boolean"
4658+
},
4659+
"context": {
4660+
"$ref": "#/$defs/IndependentEnum",
4661+
"default": "dependent"
4662+
},
4663+
"kind": {
4664+
"const": "sequence",
4665+
"default": "sequence",
4666+
"title": "Kind",
4667+
"type": "string"
4668+
},
4669+
"sequence": {
4670+
"description": "Sequence of blocks to join.",
4671+
"items": {
4672+
"$ref": "#/$defs/BlockType"
4673+
},
4674+
"title": "Sequence",
4675+
"type": "array"
4676+
},
4677+
"join": {
4678+
"$ref": "#/$defs/JoinType",
4679+
"description": "Define how to combine the result of each block.\n "
4680+
}
4681+
},
4682+
"required": [
4683+
"sequence",
4684+
"join"
4685+
],
4686+
"title": "SequenceBlock",
4687+
"type": "object"
4688+
},
45564689
"TextBlock": {
45574690
"additionalProperties": false,
45584691
"description": "Create the concatenation of the stringify version of the result of each block of the list of blocks.",

src/pdl/pdl_ast.py

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class BlockKind(StrEnum):
7171
CODE = "code"
7272
GET = "get"
7373
DATA = "data"
74+
SEQUENCE = "sequence"
7475
TEXT = "text"
7576
LASTOF = "lastOf"
7677
ARRAY = "array"
@@ -754,6 +755,76 @@ class DataBlock(LeafBlock):
754755
"""Do not evaluate expressions inside strings."""
755756

756757

758+
class JoinConfig(BaseModel):
759+
"""Configure how loop iterations or sequence of blocks should be combined."""
760+
761+
model_config = ConfigDict(
762+
extra="forbid", use_attribute_docstrings=True, validate_by_name=True
763+
)
764+
765+
766+
class JoinText(JoinConfig):
767+
"""Join loop iterations or sequence of blocks as a string."""
768+
769+
as_: Literal["text"] = Field(alias="as", default="text")
770+
"""String concatenation of the result of each iteration.
771+
"""
772+
773+
with_: str = Field(alias="with", default="")
774+
"""String used to concatenate each iteration of the loop.
775+
"""
776+
777+
778+
class JoinArray(JoinConfig):
779+
"""Join loop iterations or sequence of blocks as an array."""
780+
781+
as_: Literal["array"] = Field(alias="as")
782+
"""Return the result of each iteration as an array.
783+
"""
784+
785+
786+
class JoinObject(JoinConfig):
787+
"""Join loop iterations or sequence of blocks as an object."""
788+
789+
as_: Literal["object"] = Field(alias="as")
790+
"""Return the union of the objects created at each iteration.
791+
"""
792+
793+
794+
class JoinLastOf(JoinConfig):
795+
"""Join loop iterations or sequence of blocks as the value of the last iteration."""
796+
797+
as_: Literal["lastOf"] = Field(alias="as")
798+
"""Return the result of the last iteration.
799+
"""
800+
801+
802+
class JoinReduce(JoinConfig):
803+
"""Join loop iterations or sequence of blocks as the value of the last iteration."""
804+
805+
as_: Literal["reduce"] = Field(alias="as", default="reduce")
806+
807+
reduce: ExpressionType[Callable]
808+
"""Function used to combine the results."""
809+
810+
811+
JoinType = TypeAliasType(
812+
"JoinType", Union[JoinText, JoinArray, JoinObject, JoinLastOf, JoinReduce]
813+
)
814+
"""Different ways to join loop iterations or sequence of blocks."""
815+
816+
817+
class SequenceBlock(StructuredBlock):
818+
"""Generalization of the `text`, `lastOf`, `array`, and `object` blocks combining a list of blocks with a `join` operator."""
819+
820+
kind: Literal[BlockKind.SEQUENCE] = BlockKind.SEQUENCE
821+
sequence: list["BlockType"]
822+
"""Sequence of blocks to join."""
823+
join: JoinType
824+
"""Define how to combine the result of each block.
825+
"""
826+
827+
757828
class TextBlock(StructuredBlock):
758829
"""Create the concatenation of the stringify version of the result of each block of the list of blocks."""
759830

@@ -874,65 +945,6 @@ class MatchBlock(StructuredBlock):
874945
"""
875946

876947

877-
class JoinConfig(BaseModel):
878-
"""Configure how loop iterations should be combined."""
879-
880-
model_config = ConfigDict(
881-
extra="forbid", use_attribute_docstrings=True, validate_by_name=True
882-
)
883-
884-
885-
class JoinText(JoinConfig):
886-
"""Join loop iterations as a string."""
887-
888-
as_: Literal["text"] = Field(alias="as", default="text")
889-
"""String concatenation of the result of each iteration.
890-
"""
891-
892-
with_: str = Field(alias="with", default="")
893-
"""String used to concatenate each iteration of the loop.
894-
"""
895-
896-
897-
class JoinArray(JoinConfig):
898-
"""Join loop iterations as an array."""
899-
900-
as_: Literal["array"] = Field(alias="as")
901-
"""Return the result of each iteration as an array.
902-
"""
903-
904-
905-
class JoinObject(JoinConfig):
906-
"""Join loop iterations as an object."""
907-
908-
as_: Literal["object"] = Field(alias="as")
909-
"""Return the union of the objects created at each iteration.
910-
"""
911-
912-
913-
class JoinLastOf(JoinConfig):
914-
"""Join loop iterations as the value of the last iteration."""
915-
916-
as_: Literal["lastOf"] = Field(alias="as")
917-
"""Return the result of the last iteration.
918-
"""
919-
920-
921-
class JoinReduce(JoinConfig):
922-
"""Join loop iterations as the value of the last iteration."""
923-
924-
as_: Literal["reduce"] = Field(alias="as", default="reduce")
925-
926-
reduce: ExpressionType[Callable]
927-
"""Function used to combine the results."""
928-
929-
930-
JoinType = TypeAliasType(
931-
"JoinType", Union[JoinText, JoinArray, JoinObject, JoinLastOf, JoinReduce]
932-
)
933-
"""Different ways to join loop iterations."""
934-
935-
936948
class RepeatBlock(StructuredBlock):
937949
"""
938950
Repeat the execution of a block sequentially.
@@ -1158,6 +1170,7 @@ class EmptyBlock(LeafBlock):
11581170
| MatchBlock
11591171
| RepeatBlock
11601172
| MapBlock
1173+
| SequenceBlock
11611174
| TextBlock
11621175
| LastOfBlock
11631176
| ArrayBlock

0 commit comments

Comments
 (0)