Skip to content

feat: rename join into reduce #1092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/prompt_library/CoT.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defs:
question: ${ example.question }
reasoning: ${ example.reasoning }
answer: ${ example.answer }
join:
reduce:
with: "\n\n"

chain_of_thought:
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ and asking `why not?` if the answer (stored in variable `eval`) is `no`. The loo
Notice that the `repeat` and `then` blocks are followed by `text`. This is because of the semantics of lists in PDL. If we want to aggregate the result by stringifying every element in the list and collating them together, then we need the keyword `text` to precede a list. The number of iterations of a loop can be bounded by adding a `maxIterations` field.

The way that the result of each iteration is collated with other iterations can be customized in PDL using
the `join` feature (see the following section).
the `reduce` feature (see the following section).

Another simple example of using an `if` statement is [this](https://github.com/IBM/prompt-declaration-language//blob/main/examples/tutorial/if.pdl).

Expand Down Expand Up @@ -827,18 +827,18 @@ which outputs:
4
```

When `join` is not specified, the collation defaults to
When `reduce` is not specified, the collation defaults to

```
join:
reduce:
as: text
with: ""
```

meaning that result of each iteration is stringified and concatenated with that of other iterations. When using `with`,
`as: text` can be elided.

Note that `join` can be added to any looping construct (`repeat`) not just `for` loops.
Note that `reduce` can be added to any looping construct (`repeat`) not just `for` loops.

The `for` loop construct also allows iterating over 2 or more lists of the same length simultaneously:

Expand Down
10 changes: 5 additions & 5 deletions examples/demo/10-sdg.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defs:
icl_question: ${icl_question}
icl_answer: ${icl_answer}
question: ${question}
join:
reduce:
as: array

gen_questions_freeform:
Expand All @@ -100,7 +100,7 @@ defs:
task_description: ${task_description}
icl_question: ${example.question}
icl_answer: ${example.answer}
join:
reduce:
as: array
lang: python
code: | # flatten list_of_lists into simple list
Expand Down Expand Up @@ -186,7 +186,7 @@ defs:
data:
question: ${question}
keep: ${filter_output}
join:
reduce:
as: array
filtered:
lang: python
Expand Down Expand Up @@ -281,7 +281,7 @@ defs:
call: ${gen_answers_inner}
args:
question: ${question}
join:
reduce:
as: array
lang: python
spec: [{question: string, answer: string}]
Expand Down Expand Up @@ -368,7 +368,7 @@ defs:
data:
qa_pair: ${qa_pair}
rating: ${filter_output}
join:
reduce:
as: array
filtered:
lang: python
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/11-repeat.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ repeat:
- data:
name: ${ name }
number: ${ number }
join:
reduce:
with: "\n"
2 changes: 1 addition & 1 deletion examples/demo/7-chatbot-roles.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ text:
- model: ollama_chat/granite3.2:2b
- "\n\n"
until: ${ query == 'quit'}
join:
reduce:
with: "\n\n"
role: user
2 changes: 1 addition & 1 deletion examples/demos/granite_io_hallucinations.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ text:
text:
- "Hallucination Risk: ${ hallucination.risk }"
- "\nSentence: ${ hallucination.response_text }"
join:
reduce:
with: "\n"
2 changes: 1 addition & 1 deletion examples/granite-io/granite_io_hallucinations.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ text:
text:
- "Hallucination Risk: ${ hallucination.risk }"
- "\nSentence: ${ hallucination.response_text }"
join:
reduce:
with: "\n"
4 changes: 2 additions & 2 deletions examples/gsm8k/gsm8.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ text:
input: |
Question: ${ TEST.question }
Answer:
join:
reduce:
as: array
contribute: []
def: ALL_LLM_FULL_A
Expand All @@ -56,7 +56,7 @@ text:
input: | # 'input' is the prompt
Generate the final answer from the conclusion of this text as JSON with a single key named answer.
${ LLM_FULL_ANSWER }
join:
reduce:
as: array
contribute: []
def: SIMPLIFIED_LLM_ANSWERS
Expand Down
10 changes: 5 additions & 5 deletions examples/gsm8k/gsm8k-plan-few-shots.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defs:
demo: ${ demos }
repeat:
${ demo }
join:
reduce:
with: "\n"
- text:
- "\nProblem:\n"
Expand Down Expand Up @@ -91,7 +91,7 @@ text:
demos: ${ demos }
maxIterations: ${ MAX_ITERATIONS }
def: plans
join:
reduce:
as: array

- for:
Expand All @@ -103,7 +103,7 @@ text:
plan: ${ plan }
maxIterations: ${ MAX_ITERATIONS }
def: solutions
join:
reduce:
as: array

- for:
Expand All @@ -115,7 +115,7 @@ text:
solution: ${ solution }
maxIterations: ${ MAX_ITERATIONS }
def: results
join:
reduce:
as: array

- for:
Expand All @@ -129,7 +129,7 @@ text:
truth: ${ problem.answer }
maxIterations: ${ MAX_ITERATIONS }
def: stats
join:
reduce:
as: array

- "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% "
8 changes: 4 additions & 4 deletions examples/gsm8k/gsm8k-plan.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ text:
problem: ${ problem.question }
maxIterations: ${ MAX_ITERATIONS }
def: plans
join:
reduce:
as: array

- for:
Expand All @@ -88,7 +88,7 @@ text:
plan: ${ plan }
maxIterations: ${ MAX_ITERATIONS }
def: solutions
join:
reduce:
as: array

- for:
Expand All @@ -100,7 +100,7 @@ text:
solution: ${ solution }
maxIterations: ${ MAX_ITERATIONS }
def: results
join:
reduce:
as: array

- for:
Expand All @@ -114,7 +114,7 @@ text:
truth: ${ problem.answer }
maxIterations: ${ MAX_ITERATIONS }
def: stats
join:
reduce:
as: array

- "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% "
26 changes: 13 additions & 13 deletions examples/gsm8k/gsm8k-tot-few-shot.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defs:
demo: ${ demos }
repeat:
${ demo }
join:
reduce:
with: "\n"
- text:
- "\nProblem:\n"
Expand Down Expand Up @@ -123,11 +123,11 @@ text:
problem: ${ problem.question }
demos: ${ demos }
maxIterations: ${ N }
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: plans
join:
reduce:
as: array

- for:
Expand All @@ -142,13 +142,13 @@ text:
pdl_context: []
plan: ${ plan }
maxIterations: ${ N }
join:
reduce:
as: array
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: solutions
join:
reduce:
as: array

- for:
Expand All @@ -165,13 +165,13 @@ text:
pdl_context: []
solution: ${ solution_for_problem }
maxIterations: ${ N }
join:
reduce:
as: array
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: results
join:
reduce:
as: array

- for:
Expand All @@ -185,11 +185,11 @@ text:
pdl_context: []
results: ${ results_for_problem }
maxIterations: ${ N }
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: per_plan_votes
join:
reduce:
as: array

- for:
Expand All @@ -200,7 +200,7 @@ text:
pdl_context: []
numbers: ${ votes }
maxIterations: ${ MAX_ITERATIONS }
join:
reduce:
as: array
def: results

Expand All @@ -215,7 +215,7 @@ text:
truth: ${ problem.answer }
maxIterations: ${ MAX_ITERATIONS }
def: stats
join:
reduce:
as: array

- "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% "
16 changes: 8 additions & 8 deletions examples/gsm8k/gsm8k-tot-multiplan.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ text:
pdl_context: []
problem: ${ problem.question }
maxIterations: ${ N }
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: plans
join:
reduce:
as: array

- for:
Expand All @@ -114,11 +114,11 @@ text:
args:
pdl_context: []
plan: ${ plan }
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: solutions
join:
reduce:
as: array

- for:
Expand All @@ -131,11 +131,11 @@ text:
args:
pdl_context: []
solution: ${ solution_for_problem }
join:
reduce:
as: array
maxIterations: ${ MAX_ITERATIONS }
def: results
join:
reduce:
as: array

- for:
Expand All @@ -147,7 +147,7 @@ text:
results: ${ results_for_problem }
maxIterations: ${ MAX_ITERATIONS }
def: votes
join:
reduce:
as: array

- for:
Expand All @@ -161,7 +161,7 @@ text:
truth: ${ problem.answer }
maxIterations: ${ MAX_ITERATIONS }
def: stats
join:
reduce:
as: array

- "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% "
Loading