Skip to content

Commit d93ab30

Browse files
authored
support for expressions in function call args (#221)
Signed-off-by: Mandana Vaziri <[email protected]>
1 parent ad048b8 commit d93ab30

File tree

10 files changed

+56
-17
lines changed

10 files changed

+56
-17
lines changed

examples/callback/repair_prompt.pdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ lastOf:
1111
- def: raw_output
1212
model: replicate/ibm-granite/granite-3.0-8b-instruct
1313
parameters:
14-
stop_sequences: "\n\n"
14+
#stop_sequences: "\n\n"
1515
temperature: 0
1616

1717
- lang: python

examples/demo/3-weather.pdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ text:
1919
- lang: python
2020
code: |
2121
import requests
22-
#response = requests.get('https://api.weatherapi.com/v1/current.json?key==XYZ=${ LOCATION }')
23-
#Mock response:
22+
#result = requests.get('https://api.weatherapi.com/v1/current.json?key==XYZ=${ LOCATION }')
23+
#Mock result:
2424
result = '{"location": {"name": "Madrid", "region": "Madrid", "country": "Spain", "lat": 40.4, "lon": -3.6833, "tz_id": "Europe/Madrid", "localtime_epoch": 1732543839, "localtime": "2024-11-25 15:10"}, "current": {"last_updated_epoch": 1732543200, "last_updated": "2024-11-25 15:00", "temp_c": 14.4, "temp_f": 57.9, "is_day": 1, "condition": {"text": "Partly cloudy", "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png", "code": 1003}, "wind_mph": 13.2, "wind_kph": 21.2, "wind_degree": 265, "wind_dir": "W", "pressure_mb": 1017.0, "pressure_in": 30.03, "precip_mm": 0.01, "precip_in": 0.0, "humidity": 77, "cloud": 75, "feelslike_c": 12.8, "feelslike_f": 55.1, "windchill_c": 13.0, "windchill_f": 55.4, "heatindex_c": 14.5, "heatindex_f": 58.2, "dewpoint_c": 7.3, "dewpoint_f": 45.2, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.4, "gust_mph": 15.2, "gust_kph": 24.4}}'
2525
def: WEATHER
2626
parser: json

examples/weather/weather.pdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ text:
1919
- lang: python
2020
code: |
2121
import requests
22-
#response = requests.get('https://api.weatherapi.com/v1/current.json?key==XYZ=${ LOCATION }')
23-
#Mock response:
22+
#result = requests.get('https://api.weatherapi.com/v1/current.json?key==XYZ=${ LOCATION }')
23+
#Mock result:
2424
result = '{"location": {"name": "Madrid", "region": "Madrid", "country": "Spain", "lat": 40.4, "lon": -3.6833, "tz_id": "Europe/Madrid", "localtime_epoch": 1732543839, "localtime": "2024-11-25 15:10"}, "current": {"last_updated_epoch": 1732543200, "last_updated": "2024-11-25 15:00", "temp_c": 14.4, "temp_f": 57.9, "is_day": 1, "condition": {"text": "Partly cloudy", "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png", "code": 1003}, "wind_mph": 13.2, "wind_kph": 21.2, "wind_degree": 265, "wind_dir": "W", "pressure_mb": 1017.0, "pressure_in": 30.03, "precip_mm": 0.01, "precip_in": 0.0, "humidity": 77, "cloud": 75, "feelslike_c": 12.8, "feelslike_f": 55.1, "windchill_c": 13.0, "windchill_f": 55.4, "heatindex_c": 14.5, "heatindex_f": 58.2, "dewpoint_c": 7.3, "dewpoint_f": 45.2, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.4, "gust_mph": 15.2, "gust_kph": 24.4}}'
2525
def: WEATHER
2626
parser: json

src/pdl/pdl-schema.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,10 +1442,18 @@
14421442
"title": "Call"
14431443
},
14441444
"args": {
1445+
"anyOf": [
1446+
{},
1447+
{
1448+
"$ref": "#/$defs/LocalizedExpression"
1449+
},
1450+
{
1451+
"type": "object"
1452+
}
1453+
],
14451454
"default": {},
14461455
"description": "Arguments of the function with their values.\n ",
1447-
"title": "Args",
1448-
"type": "object"
1456+
"title": "Args"
14491457
},
14501458
"trace": {
14511459
"anyOf": [

src/pdl/pdl_ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class CallBlock(Block):
165165
call: ExpressionType
166166
"""Function to call.
167167
"""
168-
args: dict[str, Any] = {}
168+
args: ExpressionType | dict[str, Any] = {}
169169
"""Arguments of the function with their values.
170170
"""
171171
# Field for internal use

src/pdl/pdl_ast_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ def map_block_children(f: MappedFunctions, block: BlockType) -> BlockType:
134134
block.returns = f.f_block(block.returns)
135135
case CallBlock():
136136
block.call = f.f_expr(block.call)
137-
block.args = {x: f.f_expr(e) for x, e in block.args.items()}
137+
if isinstance(block.args, dict):
138+
block.args = {x: f.f_expr(e) for x, e in block.args.items()}
139+
else:
140+
block.args = f.f_expr(block.args)
138141
if block.trace is not None:
139142
block.trace = f.f_block(block.trace)
140143
case BamModelBlock() | LitellmModelBlock():
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defs:
2+
get_current_stock:
3+
function:
4+
product_name: str
5+
return:
6+
"FN::get_current_stock:: '${product_name}'\n"
7+
text:
8+
9+
- def: object_example
10+
text: '{"product_name": "from_object"}'
11+
parser: json
12+
contribute: []
13+
14+
- def: simple_call
15+
call: get_current_stock
16+
args:
17+
product_name: "Simple call!"
18+
19+
- "${object_example}\n"
20+
21+
- def: with_object_args
22+
call: get_current_stock
23+
args: ${object_example}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FN::get_current_stock:: 'Simple call!'
2+
{'product_name': 'from_object'}
3+
FN::get_current_stock:: 'from_object'
4+

tests/test_examples_run.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ class InputsType:
9393
/ "hello"
9494
/ "hello-data.pdl": InputsType(scope={"something": "ABC"}),
9595
pathlib.Path("examples")
96-
/ "weather"
97-
/ "weather.pdl": InputsType(stdin="What is the weather in Yorktown Heights?\n"),
98-
pathlib.Path("examples")
99-
/ "demo"
100-
/ "3-weather.pdl": InputsType(
101-
stdin="What is the weather in Yorktown Heights?\n"
102-
),
103-
pathlib.Path("examples")
10496
/ "tutorial"
10597
/ "conditionals_loops.pdl": InputsType(
10698
stdin="What is APR?\nno\nSay it as a poem\nyes\n"

tests/test_function.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pdl.pdl import exec_file
12
from pdl.pdl_ast import Program # pylint: disable=no-name-in-module
23
from pdl.pdl_interpreter import ( # pylint: disable=no-name-in-module
34
InterpreterState,
@@ -107,3 +108,11 @@ def test_call_template():
107108
data = Program.model_validate(hello_call_template)
108109
text, _, _, _ = process_prog(state, empty_scope, data)
109110
assert text == "Hello World!"
111+
112+
113+
def test_call_expression_args():
114+
result = exec_file("tests/data/call_expression_args.pdl")
115+
assert (
116+
result
117+
== "FN::get_current_stock:: 'Simple call!'\n{'product_name': 'from_object'}\nFN::get_current_stock:: 'from_object'\n"
118+
)

0 commit comments

Comments
 (0)