Skip to content

Commit aebe635

Browse files
authored
feat: message blocks contribute the message instead of the content to the context (#862)
* feat: `message` blocks contribute the message instead of the content to the context Signed-off-by: Louis Mandel <[email protected]> * `role` is optional in `message` blocks Signed-off-by: Louis Mandel <[email protected]> * Add tests Signed-off-by: Louis Mandel <[email protected]> --------- Signed-off-by: Louis Mandel <[email protected]>
1 parent 8341344 commit aebe635

File tree

5 files changed

+93
-34
lines changed

5 files changed

+93
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ export type Fallback5 =
697697
| EmptyBlock
698698
| null
699699
/**
700-
* Role of associated to the message.
700+
* Role associated to the block and sub-blocks.
701701
* Typical roles are `system`, `user`, and `assistant`,
702702
* but there may be other roles such as `available_tools`.
703703
*/
@@ -716,7 +716,7 @@ export type Context5 =
716716
*
717717
*/
718718
export type PdlId5 = string | null
719-
export type PdlIsLeaf5 = false
719+
export type PdlIsLeaf5 = true
720720
export type Kind5 = "message"
721721
export type Content =
722722
| boolean
@@ -3491,7 +3491,7 @@ export interface MessageBlock {
34913491
contribute?: Contribute5
34923492
parser?: Parser5
34933493
fallback?: Fallback5
3494-
role: Role5
3494+
role?: Role5
34953495
context?: Context5
34963496
pdl__id?: PdlId5
34973497
pdl__result?: unknown

src/pdl/pdl-schema.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7981,7 +7981,8 @@
79817981
"type": "null"
79827982
}
79837983
],
7984-
"description": "Role of associated to the message.\nTypical roles are `system`, `user`, and `assistant`,\nbut there may be other roles such as `available_tools`.",
7984+
"default": null,
7985+
"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`.",
79857986
"title": "Role"
79867987
},
79877988
"context": {
@@ -8048,8 +8049,8 @@
80488049
"default": null
80498050
},
80508051
"pdl__is_leaf": {
8051-
"const": false,
8052-
"default": false,
8052+
"const": true,
8053+
"default": true,
80538054
"title": "Pdl Is Leaf",
80548055
"type": "boolean"
80558056
},
@@ -8176,7 +8177,6 @@
81768177
}
81778178
},
81788179
"required": [
8179-
"role",
81808180
"content"
81818181
],
81828182
"title": "MessageBlock",

src/pdl/pdl_ast.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,10 @@ class ObjectBlock(StructuredBlock):
580580
object: dict[str, "BlockType"] | list["BlockType"]
581581

582582

583-
class MessageBlock(StructuredBlock):
583+
class MessageBlock(LeafBlock):
584584
"""Create a message."""
585585

586586
kind: Literal[BlockKind.MESSAGE] = BlockKind.MESSAGE
587-
role: RoleType # pyright: ignore
588-
"""Role of associated to the message.
589-
Typical roles are `system`, `user`, and `assistant`,
590-
but there may be other roles such as `available_tools`.
591-
""" # pyright: ignore
592587
content: "BlockType"
593588
"""Content of the message."""
594589
name: Optional[ExpressionType[str]] = None

src/pdl/pdl_interpreter.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -570,28 +570,26 @@ def process_block_body(
570570
if state.yield_result and not iteration_state.yield_result:
571571
yield_result(result, block.kind)
572572
case MessageBlock():
573-
content, background, scope, trace = process_block_of(
573+
content, _, scope, trace = process_block_of(
574574
block,
575575
"content",
576576
state,
577577
scope,
578578
loc,
579579
)
580-
name, block = process_expr_of(
581-
block, "name", scope, loc # pyright: ignore
582-
) # pyright: ignore
583-
tool_call_id, block = process_expr_of(
584-
block, "tool_call_id", scope, loc # pyright: ignore
585-
) # pyright: ignore
586-
result = PdlDict(
587-
{
588-
"role": state.role,
589-
"content": content,
590-
"name": name,
591-
"tool_call_id": tool_call_id,
592-
"defsite": block.pdl__id,
593-
}
594-
)
580+
d = {
581+
"role": state.role,
582+
"content": content,
583+
"defsite": block.pdl__id,
584+
}
585+
if block.name is not None:
586+
name, block = process_expr_of(block, "name", scope, loc)
587+
d["name"] = name
588+
if block.tool_call_id is not None:
589+
tool_call_id, block = process_expr_of(block, "tool_call_id", scope, loc)
590+
d["tool_call_id"] = tool_call_id
591+
result = PdlDict(d)
592+
background = PdlList([result])
595593
case IfBlock():
596594
b, if_trace = process_condition_of(block, "condition", scope, loc, "if")
597595
if b:

tests/test_messages.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pdl.pdl import exec_str
22

33

4-
def test_messages1():
4+
def test_message1():
55
prog_str = """
66
description: Messages block
77
array:
@@ -17,15 +17,11 @@ def test_messages1():
1717
"role": "system",
1818
"content": "You are a helpful software engineer. You write clear, concise, well-commented code.",
1919
"defsite": "array.0.message",
20-
"name": None,
21-
"tool_call_id": None,
2220
},
2321
{
2422
"role": "user",
2523
"content": "Write a Python function that implement merge sort.",
2624
"defsite": "array.1.message",
27-
"name": None,
28-
"tool_call_id": None,
2925
},
3026
]
3127
assert context == [
@@ -40,3 +36,73 @@ def test_messages1():
4036
"defsite": "array.1.message",
4137
},
4238
]
39+
40+
41+
def test_message2():
42+
prog_str = """
43+
description: Messages block
44+
role: user
45+
content:
46+
array:
47+
- Hello
48+
- Bye
49+
"""
50+
result = exec_str(prog_str, output="all")
51+
context = result["scope"]["pdl_context"]
52+
assert result["result"] == {
53+
"role": "user",
54+
"content": ["Hello", "Bye"],
55+
"defsite": "message",
56+
}
57+
assert context == [
58+
{
59+
"role": "user",
60+
"content": ["Hello", "Bye"],
61+
"defsite": "message",
62+
},
63+
]
64+
65+
66+
def test_message3():
67+
prog_str = """
68+
description: Messages block
69+
content:
70+
data: {"a": 1}
71+
"""
72+
result = exec_str(prog_str, output="all")
73+
context = result["scope"]["pdl_context"]
74+
assert result["result"] == {
75+
"role": "user",
76+
"content": {"a": 1},
77+
"defsite": "message",
78+
}
79+
assert context == [
80+
{
81+
"role": "user",
82+
"content": {"a": 1},
83+
"defsite": "message",
84+
},
85+
]
86+
87+
88+
def test_message4():
89+
prog_str = """
90+
description: Messages block
91+
content:
92+
text:
93+
data: {"a": 1}
94+
"""
95+
result = exec_str(prog_str, output="all")
96+
context = result["scope"]["pdl_context"]
97+
assert result["result"] == {
98+
"role": "user",
99+
"content": '{"a": 1}',
100+
"defsite": "message",
101+
}
102+
assert context == [
103+
{
104+
"role": "user",
105+
"content": '{"a": 1}',
106+
"defsite": "message",
107+
},
108+
]

0 commit comments

Comments
 (0)