Skip to content

Commit dfe969c

Browse files
authored
Change loop combination default and syntax (#107)
1 parent 7d20152 commit dfe969c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+380
-153
lines changed

docs/tutorial.md

+39-37
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ text:
480480
parameters:
481481
decoding_method: sample
482482
max_new_tokens: 512
483-
as: text
484483
role: user
485484
```
486485
@@ -557,13 +556,11 @@ text:
557556
code: result = ${ EXPR }
558557
- ' >>'
559558
until: ${ "The answer is" in REASON_OR_CALC }
560-
as: text
561559
- "\n\n"
562-
as: text
563560
num_iterations: 3
564561
```
565562
566-
The first two blocks read math problem examples and include them in the document. These will be our few-shot examples. The next block is a repetion as indicated by the fields: `repeat` and the accompanying `num_iterations`. The field `repeat` can contain any document (string or block or list of strings and blocks), the `num_iterations` indicates how many times to repeat.
563+
The first two blocks read math problem examples and include them in the document. These will be our few-shot examples. The next block is a repetition as indicated by the fields: `repeat` and the accompanying `num_iterations`. The field `repeat` can contain any document (string or block or list of strings and blocks), the `num_iterations` indicates how many times to repeat.
567564

568565
In the body of the `repeat` block, the program first asks granite to generate a question and add it to the document. Next we print `Answer: Let's think step by step.\n`. The following block is a repeat-until: the text in `repeat` is repeated until the condition in the `until` field becomes true. Here the condition states that we stop the iteration when variable `REASON_OR_CALC` contains `<<`. That variable is defined in the first block of the repeat-until -- we prompt a granite model and stop at the character `<<`.
569566

@@ -578,66 +575,72 @@ Similarly, the `read` block has an annotation `as: text`. This means that the re
578575
## For Loops
579576

580577
PDL also offers `for` loops over lists.
581-
The following [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/for.pdl) creates a list as a result of iteration.
578+
The following [example](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/for.pdl) stringifies and outputs each number.
582579

583580
```
584581
description: for loop
585-
text:
586-
- for:
587-
i: [1, 2, 3, 4]
588-
repeat:
589-
${ i }
582+
for:
583+
i: [1, 2, 3, 4]
584+
repeat:
585+
${ i }
590586
```
591587
592-
This program outputs the following list:
588+
This program outputs:
593589
```
594-
[1, 2, 3, 4]
590+
1234
595591
```
596592
597-
To stringify and output each number on a line, we would write:
593+
To output a number of each line, we can specify which string to use to join the results.
598594
```
599595
description: for loop
600-
text:
601-
- for:
602-
i: [1, 2, 3, 4]
603-
repeat:
604-
text:
605-
- ${ i }
606-
- "\n"
607-
as: text
596+
for:
597+
i: [1, 2, 3, 4]
598+
repeat:
599+
${ i }
600+
join:
601+
with: "\n"
608602
```
609603
610-
which outputs:
611-
612604
```
613605
1
614606
2
615607
3
616608
4
609+
```
610+
617611
612+
To creates an array as a result of iteration, we would write:
613+
```
614+
description: for loop
615+
for:
616+
i: [1, 2, 3, 4]
617+
repeat:
618+
- ${ i }
619+
join:
620+
as: array
618621
```
619622
623+
which outputs the following list:
624+
```
625+
[1, 2, 3, 4]
626+
```
627+
628+
620629
621630
The `for` loop constructs also allows iterating over 2 or more lists of the same length simultaneously:
622631
623632
```
624633
description: for loop
625634
defs:
626-
numbers: {
635+
numbers:
627636
data: [1, 2, 3, 4]
628-
}
629-
names: {
637+
names:
630638
data: ["Bob", "Carol", "David", "Ernest"]
631-
}
632-
633-
text:
634-
- for:
635-
number: ${ numbers }
636-
name: ${ names }
637-
repeat:
638-
text:
639-
"${ name }'s number is ${ number }\n"
640-
as: text
639+
for:
640+
number: ${ numbers }
641+
name: ${ names }
642+
repeat:
643+
"${ name }'s number is ${ number }\n"
641644
```
642645
643646
This results in the following output:
@@ -675,7 +678,6 @@ text:
675678
- |
676679
${ question }
677680
${ answer }
678-
as: text
679681
- >
680682
Question: Create a JSON object with fields 'name' and 'age'
681683
and set them appropriately. Write the age in letters.

examples/arith/Arith-simple.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ text:
3939
lan: python
4040
code: result = ${ EXPR }
4141
- ' >>'
42-
as: text
4342
until: ${ "The answer is" in REASON_OR_CALC }
4443
- "\n"

examples/arith/arith.pdl

-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,5 @@ text:
3939
code: result = ${ EXPR }
4040
- ' >>'
4141
until: ${ "The answer is" in REASON_OR_CALC }
42-
as: text
4342
- "\n\n"
44-
as: text
4543
num_iterations: 3

examples/chatbot/chatbot.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ text:
1515
- read:
1616
message: "Why not?\n"
1717
until: ${ eval == 'yes'}
18-
as: text
1918
role: user
2019

2120

examples/demo/1-gen-data.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ text:
1717
- |
1818
${ question }
1919
${ answer }
20-
as: text
2120
- >
2221
Question: Create a JSON object with fields 'name' and 'age'
2322
and set them appropriately. Write the age in letters.

examples/demo/2-teacher.pdl

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ defs:
8080
icl_question: ${icl_question}
8181
icl_answer: ${icl_answer}
8282
question: ${question}
83+
join:
84+
as: array
8385

8486
gen_questions_freeform:
8587
function:
@@ -98,6 +100,8 @@ defs:
98100
task_description: ${task_description}
99101
icl_question: ${example.question}
100102
icl_answer: ${example.answer}
103+
join:
104+
as: array
101105
- lan: python
102106
code: | # flatten list_of_lists into simple list
103107
result = [q for qs in ${list_of_lists} for q in qs]
@@ -181,6 +185,8 @@ defs:
181185
- data:
182186
question: ${question}
183187
keep: ${filter_output}
188+
join:
189+
as: array
184190
filtered:
185191
lan: python
186192
code: | # keep only if "keep" column is non-zero
@@ -274,6 +280,8 @@ defs:
274280
call: gen_answers_inner
275281
args:
276282
question: ${question}
283+
join:
284+
as: array
277285
- lan: python
278286
spec: [{question: str, answer: str}]
279287
code: | # keep only if answer non-empty
@@ -358,6 +366,8 @@ defs:
358366
- data:
359367
qa_pair: ${qa_pair}
360368
rating: ${filter_output}
369+
join:
370+
as: array
361371
filtered:
362372
lan: python
363373
spec: [{question: str, answer: str}]

examples/demo/4-translator.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ text:
1717
- "\n\nTranslate the above to ${ language }"
1818
- model: watsonx/ibm/granite-13b-chat-v2
1919
until: ${ language == 'stop' }
20-
as: text

examples/granite/multi_round_chat.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ text:
1717
parameters:
1818
decoding_method: sample
1919
max_new_tokens: 512
20-
as: text
2120
role: user

examples/hello/hello-for-loop.pdl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ for:
99
name: ${ names }
1010
repeat:
1111
"${ name }'s number is ${ number }\n"
12-
as: text
12+

examples/hello/hello-iteration.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ text:
44
- "\n"
55
- repeat:
66
"This is your first PDL program\n"
7-
as: text
87
num_iterations: 3

examples/hello/hello-parser-json.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ text:
1717
- |
1818
${ question }
1919
${ answer }
20-
as: text
2120
- >
2221
Question: Create a JSON object with fields 'name' and 'age'
2322
and set them appropriately.

examples/joke/Joke.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ text:
2727
include_stop_sequence: true
2828
- "\n"
2929
num_iterations: 2
30-
as: text

examples/multi-agent/react-fun.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ defs:
5757
except wikipedia.WikipediaException as e:
5858
result = str(e)
5959
until: ${ action.name != "Search" }
60-
as: text
6160

examples/oddOneOut/OddOneOut.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,3 @@ text:
3535
include_stop_sequence: false
3636
- "\n\n"
3737
num_iterations: 2
38-
as: text

examples/react/react_fun.pdl

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ defs:
1111
ex: ${ examples }
1212
repeat:
1313
"${ ex }\n"
14-
as: text
1514
- "\n"
1615
- ${ question }
1716
- "\n"
@@ -66,7 +65,6 @@ defs:
6665
contribute: []
6766
data: ${ action }
6867
until: ${ action.name == "Finish" or exit }
69-
as: text
7068

7169
react:
7270
function:

examples/react/wikipedia.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ text:
4848
except wikipedia.WikipediaException as e:
4949
result = str(e)
5050
until: ${ action.name != "Search" }
51-
as: text
5251
- "\n"

examples/talk/5-chatbot-roles.pdl

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ text:
66
text: ${ SYSTEM_CONTENT_CHAT }
77
- "Type `quit` to exit this chatbot.\n"
88
- repeat:
9-
text:
10-
- read:
11-
message: ">>> "
12-
def: query
13-
contribute: [context]
14-
- model: watsonx/ibm/granite-13b-chat-v2
15-
- "\n\n"
9+
- read:
10+
message: ">>> "
11+
def: query
12+
- model: watsonx/ibm/granite-13b-chat-v2
1613
until: ${ query == 'quit'}
17-
as: text
14+
join:
15+
with: "\n\n"
1816
role: user
19-

examples/talk/7-react.pdl

-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ text:
4949
except wikipedia.WikipediaException as e:
5050
result = str(e)
5151
until: '${ action.name != "Search" }'
52-
as: text
5352

examples/talk/8-sdg.pdl

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ defs:
8181
icl_question: ${icl_question}
8282
icl_answer: ${icl_answer}
8383
question: ${question}
84+
join:
85+
as: array
8486

8587
gen_questions_freeform:
8688
function:
@@ -99,6 +101,8 @@ defs:
99101
task_description: ${task_description}
100102
icl_question: ${example.question}
101103
icl_answer: ${example.answer}
104+
join:
105+
as: array
102106
- lan: python
103107
code: | # flatten list_of_lists into simple list
104108
result = [q for qs in ${list_of_lists} for q in qs]
@@ -182,6 +186,8 @@ defs:
182186
- data:
183187
question: ${question}
184188
keep: ${filter_output}
189+
join:
190+
as: array
185191
filtered:
186192
lan: python
187193
code: | # keep only if "keep" column is non-zero
@@ -275,6 +281,8 @@ defs:
275281
call: gen_answers_inner
276282
args:
277283
question: ${question}
284+
join:
285+
as: array
278286
- lan: python
279287
spec: [{question: str, answer: str}]
280288
code: | # keep only if answer non-empty
@@ -359,6 +367,8 @@ defs:
359367
- data:
360368
qa_pair: ${qa_pair}
361369
rating: ${filter_output}
370+
join:
371+
as: array
362372
filtered:
363373
lan: python
364374
spec: [{question: str, answer: str}]

0 commit comments

Comments
 (0)