Skip to content

Commit dec3eef

Browse files
authored
Rename the background context variable into pdl_context (#126)
1 parent 14cee1e commit dec3eef

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

src/pdl/pdl_interpreter.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def __init__(
122122
self.message = message
123123

124124

125-
empty_scope: ScopeType = {"context": []}
125+
empty_scope: ScopeType = {"pdl_context": []}
126126

127127

128128
class InterpreterState(BaseModel):
@@ -288,12 +288,12 @@ def step_block(
288288
yield YieldBackgroundMessage(background)
289289
if state.yield_result:
290290
yield YieldResultMessage(result)
291-
append_log(state, "Context", background)
291+
append_log(state, "pdl_context", background)
292292
else:
293293
result, background, scope, trace = yield from step_advanced_block(
294294
state, scope, block, loc
295295
)
296-
scope = scope | {"context": background}
296+
scope = scope | {"pdl_context": background}
297297
return result, background, scope, trace
298298

299299

@@ -516,7 +516,7 @@ def step_block_body(
516516
results = []
517517
background = []
518518
iterations_trace: list[BlocksType] = []
519-
context_init = scope_init["context"]
519+
pdl_context_init = scope_init["pdl_context"]
520520
iteration_state = state.with_yield_result(
521521
state.yield_result and block.join.iteration_type == IterationType.TEXT
522522
)
@@ -536,7 +536,7 @@ def step_block_body(
536536
[{"role": block.role, "content": join_string}]
537537
)
538538
scope = scope | {
539-
"context": messages_concat(context_init, background)
539+
"pdl_context": messages_concat(pdl_context_init, background)
540540
}
541541
(
542542
iteration_result,
@@ -569,7 +569,7 @@ def step_block_body(
569569
results = []
570570
background = []
571571
iter_trace: list[BlocksType] = []
572-
context_init = scope_init["context"]
572+
pdl_context_init = scope_init["pdl_context"]
573573
items, block = process_expr_of(block, "fors", scope, loc, "for")
574574
lengths = []
575575
for idx, lst in items.items():
@@ -613,7 +613,7 @@ def step_block_body(
613613
[{"role": block.role, "content": join_string}]
614614
)
615615
scope = scope | {
616-
"context": messages_concat(context_init, background)
616+
"pdl_context": messages_concat(pdl_context_init, background)
617617
}
618618
for k in items.keys():
619619
scope = scope | {k: items[k][i]}
@@ -649,7 +649,7 @@ def step_block_body(
649649
stop = False
650650
background = []
651651
iterations_trace = []
652-
context_init = scope_init["context"]
652+
pdl_context_init = scope_init["pdl_context"]
653653
iteration_state = state.with_yield_result(
654654
state.yield_result and block.join.iteration_type == IterationType.TEXT
655655
)
@@ -669,7 +669,7 @@ def step_block_body(
669669
[{"role": block.role, "content": join_string}]
670670
)
671671
scope = scope | {
672-
"context": messages_concat(context_init, background)
672+
"pdl_context": messages_concat(pdl_context_init, background)
673673
}
674674
(
675675
iteration_result,
@@ -806,10 +806,12 @@ def step_blocks(
806806
new_loc = None
807807
background = []
808808
trace = []
809-
context_init = scope["context"]
809+
pdl_context_init = scope["pdl_context"]
810810
try:
811811
for i, block in enumerate(blocks):
812-
scope = scope | {"context": messages_concat(context_init, background)}
812+
scope = scope | {
813+
"pdl_context": messages_concat(pdl_context_init, background)
814+
}
813815
new_loc = append(loc, "[" + str(i) + "]")
814816
if iteration_type == IterationType.LASTOF and state.yield_result:
815817
iteration_state = state.with_yield_result(i + 1 == len(blocks))
@@ -1029,7 +1031,7 @@ def step_call_model(
10291031
else:
10301032
model_input = model_input_result
10311033
else:
1032-
model_input = scope["context"]
1034+
model_input = scope["pdl_context"]
10331035
input_trace = None
10341036
concrete_block = concrete_block.model_copy(
10351037
update={
@@ -1294,7 +1296,7 @@ def step_call(
12941296
trace=block.model_copy(),
12951297
)
12961298
f_body = closure.returns
1297-
f_scope = closure.scope | {"context": scope["context"]} | args
1299+
f_scope = closure.scope | {"pdl_context": scope["pdl_context"]} | args
12981300
fun_loc = LocationType(
12991301
file=closure.location.file,
13001302
path=closure.location.path + ["return"],

src/pdl/pdl_notebook_ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def pdl(self, line, cell, local_ns):
3131
line = line.strip()
3232
args = parse_argstring(self.pdl, line)
3333
if args.reset_context:
34-
scope = local_ns | {"context": []}
34+
scope = local_ns | {"pdl_context": []}
3535
else:
3636
scope = local_ns
3737
try:

src/pdl/pdl_schema_validator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def type_check_args(args: dict[str, Any], params: dict[str, Any], loc) -> list[s
1616
args_copy = {}
1717
if params_copy is None:
1818
params_copy = {}
19-
# if "context" not in args_copy:
20-
# args_copy["context"] = "context"
21-
# if "context" not in params_copy:
22-
if "context" in args_copy:
23-
# params_copy["context"] = [{"role": "str?", "content": "str"}]
24-
params_copy["context"] = ["obj"]
19+
# if "pdl_context" not in args_copy:
20+
# args_copy["pdl_context"] = "pdl_context"
21+
# if "pdl_context" not in params_copy:
22+
if "pdl_context" in args_copy:
23+
# params_copy["pdl_context"] = [{"role": "str?", "content": "str"}]
24+
params_copy["pdl_context"] = ["obj"]
2525
schema = get_json_schema(params_copy)
2626
if schema is None:
2727
return ["Error obtaining a valid schema from function parameters definition"]

tests/test_function.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_function_params():
5353
hello_stutter = {
5454
"description": "Repeat the context",
5555
"text": [
56-
{"def": "stutter", "function": None, "return": "${ context[0].content }"},
56+
{"def": "stutter", "function": None, "return": "${ pdl_context[0].content }"},
5757
"Hello World!\n",
5858
{"call": "stutter"},
5959
],
@@ -70,9 +70,12 @@ def test_function_implicit_context():
7070
hello_bye = {
7171
"description": "Repeat the context",
7272
"text": [
73-
{"def": "stutter", "function": {}, "return": "${ context[0].content }"},
73+
{"def": "stutter", "function": {}, "return": "${ pdl_context[0].content }"},
7474
"Hello World!\n",
75-
{"call": "stutter", "args": {"context": [{"role": None, "content": "Bye!"}]}},
75+
{
76+
"call": "stutter",
77+
"args": {"pdl_context": [{"role": None, "content": "Bye!"}]},
78+
},
7679
],
7780
}
7881

tests/test_role.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def test_role1():
1515
description: Test role
1616
text:
1717
- defs:
18-
ctx1: ${context}
18+
ctx1: ${pdl_context}
1919
text: A
2020
role: A
2121
- Hi
2222
- defs:
23-
ctx2: ${context}
23+
ctx2: ${pdl_context}
2424
text: B
2525
role: B
2626
role: Top

tests/test_var.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_code_var():
126126
state = InterpreterState()
127127
data = Program.model_validate(code_var_data)
128128
text, _, scope, _ = process_prog(state, empty_scope, data)
129-
assert scope == {"context": [{"role": "user", "content": text}], "I": 0}
129+
assert scope == {"pdl_context": [{"role": "user", "content": text}], "I": 0}
130130
assert text == "0"
131131

132132

0 commit comments

Comments
 (0)