diff --git a/contrib/prompt_library/CoT.pdl b/contrib/prompt_library/CoT.pdl index 9c03d192d..6e323796c 100644 --- a/contrib/prompt_library/CoT.pdl +++ b/contrib/prompt_library/CoT.pdl @@ -24,7 +24,7 @@ defs: question: ${ example.question } reasoning: ${ example.reasoning } answer: ${ example.answer } - join: + reduce: with: "\n\n" chain_of_thought: diff --git a/docs/tutorial.md b/docs/tutorial.md index 2e6f84fc3..8574ce86e 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -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). @@ -827,10 +827,10 @@ 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: "" ``` @@ -838,7 +838,7 @@ join: 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: diff --git a/examples/demo/10-sdg.pdl b/examples/demo/10-sdg.pdl index 886fe02c9..0253ed009 100644 --- a/examples/demo/10-sdg.pdl +++ b/examples/demo/10-sdg.pdl @@ -80,7 +80,7 @@ defs: icl_question: ${icl_question} icl_answer: ${icl_answer} question: ${question} - join: + reduce: as: array gen_questions_freeform: @@ -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 @@ -186,7 +186,7 @@ defs: data: question: ${question} keep: ${filter_output} - join: + reduce: as: array filtered: lang: python @@ -281,7 +281,7 @@ defs: call: ${gen_answers_inner} args: question: ${question} - join: + reduce: as: array lang: python spec: [{question: string, answer: string}] @@ -368,7 +368,7 @@ defs: data: qa_pair: ${qa_pair} rating: ${filter_output} - join: + reduce: as: array filtered: lang: python diff --git a/examples/demo/11-repeat.pdl b/examples/demo/11-repeat.pdl index 07a7b56ea..adf30dea8 100644 --- a/examples/demo/11-repeat.pdl +++ b/examples/demo/11-repeat.pdl @@ -13,5 +13,5 @@ repeat: - data: name: ${ name } number: ${ number } -join: +reduce: with: "\n" \ No newline at end of file diff --git a/examples/demo/7-chatbot-roles.pdl b/examples/demo/7-chatbot-roles.pdl index cd35c4948..9b2e06267 100644 --- a/examples/demo/7-chatbot-roles.pdl +++ b/examples/demo/7-chatbot-roles.pdl @@ -13,6 +13,6 @@ text: - model: ollama_chat/granite3.2:2b - "\n\n" until: ${ query == 'quit'} - join: + reduce: with: "\n\n" role: user diff --git a/examples/demos/granite_io_hallucinations.pdl b/examples/demos/granite_io_hallucinations.pdl index 712b59bde..b9dc311f9 100644 --- a/examples/demos/granite_io_hallucinations.pdl +++ b/examples/demos/granite_io_hallucinations.pdl @@ -58,5 +58,5 @@ text: text: - "Hallucination Risk: ${ hallucination.risk }" - "\nSentence: ${ hallucination.response_text }" - join: + reduce: with: "\n" diff --git a/examples/granite-io/granite_io_hallucinations.pdl b/examples/granite-io/granite_io_hallucinations.pdl index 712b59bde..b9dc311f9 100644 --- a/examples/granite-io/granite_io_hallucinations.pdl +++ b/examples/granite-io/granite_io_hallucinations.pdl @@ -58,5 +58,5 @@ text: text: - "Hallucination Risk: ${ hallucination.risk }" - "\nSentence: ${ hallucination.response_text }" - join: + reduce: with: "\n" diff --git a/examples/gsm8k/gsm8.pdl b/examples/gsm8k/gsm8.pdl index 5f43320f3..f845fddaa 100644 --- a/examples/gsm8k/gsm8.pdl +++ b/examples/gsm8k/gsm8.pdl @@ -35,7 +35,7 @@ text: input: | Question: ${ TEST.question } Answer: - join: + reduce: as: array contribute: [] def: ALL_LLM_FULL_A @@ -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 diff --git a/examples/gsm8k/gsm8k-plan-few-shots.pdl b/examples/gsm8k/gsm8k-plan-few-shots.pdl index 582ebba06..342692f59 100644 --- a/examples/gsm8k/gsm8k-plan-few-shots.pdl +++ b/examples/gsm8k/gsm8k-plan-few-shots.pdl @@ -22,7 +22,7 @@ defs: demo: ${ demos } repeat: ${ demo } - join: + reduce: with: "\n" - text: - "\nProblem:\n" @@ -91,7 +91,7 @@ text: demos: ${ demos } maxIterations: ${ MAX_ITERATIONS } def: plans - join: + reduce: as: array - for: @@ -103,7 +103,7 @@ text: plan: ${ plan } maxIterations: ${ MAX_ITERATIONS } def: solutions - join: + reduce: as: array - for: @@ -115,7 +115,7 @@ text: solution: ${ solution } maxIterations: ${ MAX_ITERATIONS } def: results - join: + reduce: as: array - for: @@ -129,7 +129,7 @@ text: truth: ${ problem.answer } maxIterations: ${ MAX_ITERATIONS } def: stats - join: + reduce: as: array - "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% " \ No newline at end of file diff --git a/examples/gsm8k/gsm8k-plan.pdl b/examples/gsm8k/gsm8k-plan.pdl index 89b02a364..424bb1dc7 100644 --- a/examples/gsm8k/gsm8k-plan.pdl +++ b/examples/gsm8k/gsm8k-plan.pdl @@ -76,7 +76,7 @@ text: problem: ${ problem.question } maxIterations: ${ MAX_ITERATIONS } def: plans - join: + reduce: as: array - for: @@ -88,7 +88,7 @@ text: plan: ${ plan } maxIterations: ${ MAX_ITERATIONS } def: solutions - join: + reduce: as: array - for: @@ -100,7 +100,7 @@ text: solution: ${ solution } maxIterations: ${ MAX_ITERATIONS } def: results - join: + reduce: as: array - for: @@ -114,7 +114,7 @@ text: truth: ${ problem.answer } maxIterations: ${ MAX_ITERATIONS } def: stats - join: + reduce: as: array - "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% " \ No newline at end of file diff --git a/examples/gsm8k/gsm8k-tot-few-shot.pdl b/examples/gsm8k/gsm8k-tot-few-shot.pdl index 7fe7a58e2..056c9b248 100644 --- a/examples/gsm8k/gsm8k-tot-few-shot.pdl +++ b/examples/gsm8k/gsm8k-tot-few-shot.pdl @@ -48,7 +48,7 @@ defs: demo: ${ demos } repeat: ${ demo } - join: + reduce: with: "\n" - text: - "\nProblem:\n" @@ -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: @@ -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: @@ -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: @@ -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: @@ -200,7 +200,7 @@ text: pdl_context: [] numbers: ${ votes } maxIterations: ${ MAX_ITERATIONS } - join: + reduce: as: array def: results @@ -215,7 +215,7 @@ text: truth: ${ problem.answer } maxIterations: ${ MAX_ITERATIONS } def: stats - join: + reduce: as: array - "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% " \ No newline at end of file diff --git a/examples/gsm8k/gsm8k-tot-multiplan.pdl b/examples/gsm8k/gsm8k-tot-multiplan.pdl index 136f897ef..2e4fffbdf 100644 --- a/examples/gsm8k/gsm8k-tot-multiplan.pdl +++ b/examples/gsm8k/gsm8k-tot-multiplan.pdl @@ -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: @@ -114,11 +114,11 @@ text: args: pdl_context: [] plan: ${ plan } - join: + reduce: as: array maxIterations: ${ MAX_ITERATIONS } def: solutions - join: + reduce: as: array - for: @@ -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: @@ -147,7 +147,7 @@ text: results: ${ results_for_problem } maxIterations: ${ MAX_ITERATIONS } def: votes - join: + reduce: as: array - for: @@ -161,7 +161,7 @@ text: truth: ${ problem.answer } maxIterations: ${ MAX_ITERATIONS } def: stats - join: + reduce: as: array - "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% " \ No newline at end of file diff --git a/examples/gsm8k/gsm8k-tot.pdl b/examples/gsm8k/gsm8k-tot.pdl index d5f38f214..076832c73 100644 --- a/examples/gsm8k/gsm8k-tot.pdl +++ b/examples/gsm8k/gsm8k-tot.pdl @@ -110,11 +110,11 @@ text: pdl_context: [] problem: ${ problem.question } maxIterations: ${ N } - join: + reduce: as: array maxIterations: ${ MAX_ITERATIONS } def: plans - join: + reduce: as: array - for: @@ -129,13 +129,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: @@ -152,13 +152,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: @@ -172,11 +172,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: @@ -187,7 +187,7 @@ text: pdl_context: [] numbers: ${ votes } maxIterations: ${ MAX_ITERATIONS } - join: + reduce: as: array def: results @@ -202,7 +202,7 @@ text: truth: ${ problem.answer } maxIterations: ${ MAX_ITERATIONS } def: stats - join: + reduce: as: array - "\nAccuracy: ${ stats|sum / MAX_ITERATIONS * 100}% " \ No newline at end of file diff --git a/examples/map-reduce/python-reduce.pdl b/examples/map-reduce/python-reduce.pdl index 3efbdc531..508e4a076 100644 --- a/examples/map-reduce/python-reduce.pdl +++ b/examples/map-reduce/python-reduce.pdl @@ -8,6 +8,5 @@ for: i: [1,2,3,4] map: ${i} -join: - as: reduce - reduce: ${ plus } +reduce: + function: ${ plus } diff --git a/examples/map-reduce/reduce.pdl b/examples/map-reduce/reduce.pdl index 2bbaaab6d..84d34751e 100644 --- a/examples/map-reduce/reduce.pdl +++ b/examples/map-reduce/reduce.pdl @@ -8,6 +8,6 @@ defs: for: i: [1,2,3,4] map: - ${i} -join: - reduce: ${ plus } + ${ i } +reduce: + function: ${ plus } diff --git a/examples/notebooks/granite_io_demo.ipynb b/examples/notebooks/granite_io_demo.ipynb index 8fb245db4..bd541195a 100644 --- a/examples/notebooks/granite_io_demo.ipynb +++ b/examples/notebooks/granite_io_demo.ipynb @@ -170,7 +170,7 @@ " text: \n", " - \"Hallucination Risk: ${ hallucination.risk }\"\n", " - \"\\nSentence: ${ hallucination.response_text }\"\n", - " join: \n", + " reduce: \n", " with: \"\\n\"\n", "- \"\\n\\nCitations:\\n\"\n", "- for:\n", @@ -179,7 +179,7 @@ " text: \n", " - \"Citation: ${ citation.context_text }\"\n", " - \"\\nSentence: ${ citation.response_text }\"\n", - " join:\n", + " reduce:\n", " with: \"\\n\"" ] }, @@ -206,7 +206,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "pdl-3.13", "language": "python", "name": "python3" }, @@ -220,7 +220,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.5" + "version": "3.13.0" } }, "nbformat": 4, diff --git a/examples/notebooks/pdl.ipynb b/examples/notebooks/pdl.ipynb index e644721e5..3f6019fb3 100644 --- a/examples/notebooks/pdl.ipynb +++ b/examples/notebooks/pdl.ipynb @@ -141,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "455b2dbc-69fb-4164-9b8b-5817b3f33e9b", "metadata": {}, "outputs": [ @@ -229,7 +229,7 @@ " contribute: [context]\n", " - model: replicate/ibm-granite/granite-3.2-8b-instruct\n", " until: ${ query == 'quit'}\n", - " join:\n", + " reduce:\n", " with: \"\\n\\n\"\n", "role: user\n" ] diff --git a/examples/rag/pdf_query.pdl b/examples/rag/pdf_query.pdl index 9769cf6a7..d0e09a228 100644 --- a/examples/rag/pdf_query.pdl +++ b/examples/rag/pdf_query.pdl @@ -42,6 +42,6 @@ lastOf: temperature: 0 data: ${question}: ${CONCLUSION} - join: + reduce: as: array text: "${ CONCLUSIONS | tojson }\n" diff --git a/examples/skeleton-of-thought/tips.pdl b/examples/skeleton-of-thought/tips.pdl index d41cba2da..49537b37d 100644 --- a/examples/skeleton-of-thought/tips.pdl +++ b/examples/skeleton-of-thought/tips.pdl @@ -56,7 +56,7 @@ text: topic: ${ topic.topic } number: ${ topic.number } pdl_context: [] - join: + reduce: as: array maxIterations: ${ MAX_ITERATIONS } def: skeletons @@ -68,7 +68,7 @@ text: code: | skel = """ ${skeleton} """ result = skel.split("\n") - join: + reduce: as: array def: skeletons_array - for: @@ -83,7 +83,7 @@ text: topic: ${ topic.topic } tip: ${ tip } pdl_context: [] - join: + reduce: as: array \ No newline at end of file diff --git a/examples/teacher/teacher.pdl b/examples/teacher/teacher.pdl index 886fe02c9..0253ed009 100644 --- a/examples/teacher/teacher.pdl +++ b/examples/teacher/teacher.pdl @@ -80,7 +80,7 @@ defs: icl_question: ${icl_question} icl_answer: ${icl_answer} question: ${question} - join: + reduce: as: array gen_questions_freeform: @@ -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 @@ -186,7 +186,7 @@ defs: data: question: ${question} keep: ${filter_output} - join: + reduce: as: array filtered: lang: python @@ -281,7 +281,7 @@ defs: call: ${gen_answers_inner} args: question: ${question} - join: + reduce: as: array lang: python spec: [{question: string, answer: string}] @@ -368,7 +368,7 @@ defs: data: qa_pair: ${qa_pair} rating: ${filter_output} - join: + reduce: as: array filtered: lang: python diff --git a/examples/tutorial/for_array.pdl b/examples/tutorial/for_array.pdl index 6d14b09bd..e6bcb3d39 100644 --- a/examples/tutorial/for_array.pdl +++ b/examples/tutorial/for_array.pdl @@ -3,5 +3,5 @@ for: i: [1, 2, 3, 4] repeat: ${ i } -join: +reduce: as: array diff --git a/examples/tutorial/for_lastOf.pdl b/examples/tutorial/for_lastOf.pdl index 5a5cf7814..0b417b91d 100644 --- a/examples/tutorial/for_lastOf.pdl +++ b/examples/tutorial/for_lastOf.pdl @@ -3,5 +3,5 @@ for: i: [1, 2, 3, 4] repeat: ${ i } -join: +reduce: as: lastOf diff --git a/examples/tutorial/for_object.pdl b/examples/tutorial/for_object.pdl index 4d4e287bd..383d7fd97 100644 --- a/examples/tutorial/for_object.pdl +++ b/examples/tutorial/for_object.pdl @@ -10,5 +10,5 @@ for: repeat: data: ${ name }: ${ number } -join: +reduce: as: object \ No newline at end of file diff --git a/examples/tutorial/for_with.pdl b/examples/tutorial/for_with.pdl index daefd851c..e392cf6f6 100644 --- a/examples/tutorial/for_with.pdl +++ b/examples/tutorial/for_with.pdl @@ -3,5 +3,5 @@ for: i: [1, 2, 3, 4] repeat: ${ i } -join: +reduce: with: "\n" diff --git a/pdl-live-react/src-tauri/src/compile/beeai.rs b/pdl-live-react/src-tauri/src/compile/beeai.rs index c8edf3e16..65d29e835 100644 --- a/pdl-live-react/src-tauri/src/compile/beeai.rs +++ b/pdl-live-react/src-tauri/src/compile/beeai.rs @@ -270,7 +270,7 @@ fn call_tools(model: &String, parameters: &HashMap) -> PdlBlock { body: Repeat(RepeatBlock { for_: for_, repeat: Box::new(repeat), - join: None, + reduce: None, }), }) } diff --git a/pdl-live-react/src/pdl_ast.d.ts b/pdl-live-react/src/pdl_ast.d.ts index 8c203db9b..73a5f36d4 100644 --- a/pdl-live-react/src/pdl_ast.d.ts +++ b/pdl-live-react/src/pdl_ast.d.ts @@ -492,7 +492,7 @@ export type As4 = "reduce" /** * Function used to combine the results. */ -export type Reduce = LocalizedExpression | string +export type Function = LocalizedExpression | string export type PdlTrace = BlockType[] | null export type PdlTrace1 = BlockType[] | null /** @@ -591,7 +591,7 @@ export type Kind21 = "function" * Functions parameters with their types. * */ -export type Function = { +export type Function1 = { [k: string]: PdlTypeType } | null /** @@ -713,7 +713,7 @@ export interface FunctionBlock { pdl__timing?: PdlTiming | null pdl__is_leaf?: PdlIsLeaf21 kind?: Kind21 - function: Function + function: Function1 /** * Body of the function. * @@ -2046,7 +2046,7 @@ export interface AnyPattern { * defs: * i: ${ i + 1} * data: ${ i } - * join: + * reduce: * as: array * ``` */ @@ -2190,7 +2190,7 @@ export interface RepeatBlock { * Define how to combine the result of each iteration. * */ - join?: JoinText | JoinArray | JoinObject | JoinLastOf | JoinReduce + reduce?: JoinText | JoinArray | JoinObject | JoinLastOf | JoinReduce pdl__trace?: PdlTrace1 } /** @@ -2220,7 +2220,7 @@ export interface Defs10 { * maxIterations: 5 * map: * ${ i } - * join: + * reduce: * as: array * ``` */ @@ -2354,7 +2354,7 @@ export interface MapBlock { * Define how to combine the result of each iteration. * */ - join?: JoinText | JoinArray | JoinObject | JoinLastOf | JoinReduce + reduce?: JoinText | JoinArray | JoinObject | JoinLastOf | JoinReduce pdl__trace?: PdlTrace } /** @@ -3440,7 +3440,7 @@ export interface JoinLastOf { */ export interface JoinReduce { as?: As4 - reduce: Reduce + function: Function } /** * Internal data structure to record token consumption usage information. diff --git a/src/pdl/pdl-schema.json b/src/pdl/pdl-schema.json index 230ad79bd..bab17d2f4 100644 --- a/src/pdl/pdl-schema.json +++ b/src/pdl/pdl-schema.json @@ -1928,7 +1928,7 @@ "title": "As", "type": "string" }, - "reduce": { + "function": { "anyOf": [ { "$ref": "#/$defs/LocalizedExpression_TypeVar_" @@ -1938,11 +1938,11 @@ } ], "description": "Function used to combine the results.", - "title": "Reduce" + "title": "Function" } }, "required": [ - "reduce" + "function" ], "title": "JoinReduce", "type": "object" @@ -2716,7 +2716,7 @@ }, "MapBlock": { "additionalProperties": false, - "description": "Independent executions of a block.\nRepeat the execution of a block starting from the initial scope\nand `pdl_context`.\n\nFor loop example:\n```PDL\nfor:\n number: [1, 2, 3, 4]\n name: [\"Bob\", \"Carol\", \"David\", \"Ernest\"]\nmap:\n \"${ name }'s number is ${ number }\\n\"\n```\n\nBounded loop:\n```PDL\nindex: i\nmaxIterations: 5\nmap:\n ${ i }\njoin:\n as: array\n```", + "description": "Independent executions of a block.\nRepeat the execution of a block starting from the initial scope\nand `pdl_context`.\n\nFor loop example:\n```PDL\nfor:\n number: [1, 2, 3, 4]\n name: [\"Bob\", \"Carol\", \"David\", \"Ernest\"]\nmap:\n \"${ name }'s number is ${ number }\\n\"\n```\n\nBounded loop:\n```PDL\nindex: i\nmaxIterations: 5\nmap:\n ${ i }\nreduce:\n as: array\n```", "properties": { "description": { "$ref": "#/$defs/OptionalStr", @@ -2856,7 +2856,7 @@ "default": null, "description": "Maximal number of iterations to perform.\n " }, - "join": { + "reduce": { "$ref": "#/$defs/JoinType", "default": { "as": "text", @@ -3893,7 +3893,7 @@ }, "RepeatBlock": { "additionalProperties": false, - "description": "Repeat the execution of a block sequentially.\nThe scope and `pdl_context` are accumulated in between iterations.\n\nFor loop example:\n```PDL\nfor:\n number: [1, 2, 3, 4]\n name: [\"Bob\", \"Carol\", \"David\", \"Ernest\"]\nrepeat:\n \"${ name }'s number is ${ number }\\n\"\n```\n\nWhile loop:\n```PDL\ndefs:\n i: 0\nwhile: ${i < 5}\nrepeat:\n defs:\n i: ${ i + 1}\n data: ${ i }\njoin:\n as: array\n```", + "description": "Repeat the execution of a block sequentially.\nThe scope and `pdl_context` are accumulated in between iterations.\n\nFor loop example:\n```PDL\nfor:\n number: [1, 2, 3, 4]\n name: [\"Bob\", \"Carol\", \"David\", \"Ernest\"]\nrepeat:\n \"${ name }'s number is ${ number }\\n\"\n```\n\nWhile loop:\n```PDL\ndefs:\n i: 0\nwhile: ${i < 5}\nrepeat:\n defs:\n i: ${ i + 1}\n data: ${ i }\nreduce:\n as: array\n```", "properties": { "description": { "$ref": "#/$defs/OptionalStr", @@ -4043,7 +4043,7 @@ "default": null, "description": "Maximal number of iterations to perform.\n " }, - "join": { + "reduce": { "$ref": "#/$defs/JoinType", "default": { "as": "text", diff --git a/src/pdl/pdl_ast.py b/src/pdl/pdl_ast.py index 65e066784..48d2c122c 100644 --- a/src/pdl/pdl_ast.py +++ b/src/pdl/pdl_ast.py @@ -892,7 +892,7 @@ class JoinReduce(JoinConfig): as_: Literal["reduce"] = Field(alias="as", default="reduce") - reduce: ExpressionType[Callable] + function: ExpressionType[Callable] """Function used to combine the results.""" @@ -925,7 +925,7 @@ class RepeatBlock(StructuredBlock): defs: i: ${ i + 1} data: ${ i } - join: + reduce: as: array ``` """ @@ -949,7 +949,7 @@ class RepeatBlock(StructuredBlock): maxIterations: OptionalExpressionInt = None """Maximal number of iterations to perform. """ - join: JoinType = JoinText() + reduce: JoinType = JoinText() """Define how to combine the result of each iteration. """ # Field for internal use @@ -977,7 +977,7 @@ class MapBlock(StructuredBlock): maxIterations: 5 map: ${ i } - join: + reduce: as: array ``` """ @@ -995,7 +995,7 @@ class MapBlock(StructuredBlock): maxIterations: OptionalExpressionInt = None """Maximal number of iterations to perform. """ - join: JoinType = JoinText() + reduce: JoinType = JoinText() """Define how to combine the result of each iteration. """ # Field for internal use diff --git a/src/pdl/pdl_dumper.py b/src/pdl/pdl_dumper.py index 3e6f2ace5..36d396f9f 100644 --- a/src/pdl/pdl_dumper.py +++ b/src/pdl/pdl_dumper.py @@ -248,7 +248,7 @@ def block_to_dict( # noqa: C901 d["until"] = expr_to_dict(block.until, json_compatible) if block.maxIterations is not None: d["maxIterations"] = expr_to_dict(block.maxIterations, json_compatible) - d["join"] = join_to_dict(block.join, json_compatible) + d["join"] = join_to_dict(block.reduce, json_compatible) if block.pdl__trace is not None: d["pdl__trace"] = [ block_to_dict(b, json_compatible) for b in block.pdl__trace @@ -261,7 +261,7 @@ def block_to_dict( # noqa: C901 d["map"] = block_to_dict(block.map, json_compatible) if block.maxIterations is not None: d["maxIterations"] = expr_to_dict(block.maxIterations, json_compatible) - d["join"] = join_to_dict(block.join, json_compatible) + d["join"] = join_to_dict(block.reduce, json_compatible) if block.pdl__trace is not None: d["pdl__trace"] = [ block_to_dict(b, json_compatible) for b in block.pdl__trace @@ -428,7 +428,7 @@ def join_to_dict(join: JoinType, json_compatible: bool) -> dict[str, Any]: case JoinArray() | JoinObject() | JoinLastOf(): d["as"] = str(join.as_) case JoinReduce(): - d["reduce"] = expr_to_dict(join.reduce, json_compatible) + d["reduce"] = expr_to_dict(join.function, json_compatible) case _: assert False return d diff --git a/src/pdl/pdl_interpreter.py b/src/pdl/pdl_interpreter.py index 23552d8a2..d56bd53d9 100644 --- a/src/pdl/pdl_interpreter.py +++ b/src/pdl/pdl_interpreter.py @@ -845,7 +845,7 @@ def process_block_body( iter_trace: list[BlockType] = [] pdl_context_init = scope_init.data["pdl_context"] iteration_state = state.with_yield_result( - state.yield_result and isinstance(block.join, JoinText) + state.yield_result and isinstance(block.reduce, JoinText) ) block, items, length = _evaluate_for_field(scope, block, loc) block, max_iterations = _evaluate_max_iterations_field(scope, block, loc) @@ -868,8 +868,8 @@ def process_block_body( iteration_state = iteration_state.with_iter(iidx) if first: first = False - elif isinstance(block.join, JoinText): - join_string = block.join.with_ + elif isinstance(block.reduce, JoinText): + join_string = block.reduce.with_ if iteration_state.yield_result: yield_result(join_string, block.kind) if iteration_state.yield_background: @@ -923,7 +923,7 @@ def process_block_body( loc=exc.loc or repeat_loc, trace=trace, ) from exc - result = combine_results(block.join, results) + result = combine_results(block.reduce, results) if block.context is IndependentEnum.INDEPENDENT: background = saved_background if state.yield_result and not iteration_state.yield_result: @@ -979,7 +979,7 @@ def process_block_body( loc=exc.loc or map_loc, trace=trace, ) from exc - result = combine_results(block.join, results) + result = combine_results(block.reduce, results) # background = saved_background # commented because the block do not contribute to the background if state.yield_result and not iteration_state.yield_result: yield_result(result.result(), block.kind) @@ -1101,14 +1101,14 @@ def _evaluate_max_iterations_field( def _evaluate_join_field( scope: ScopeType, block: BlockTVarEvalJoin, loc: PdlLocationType ) -> BlockTVarEvalJoin: - match block.join: + match block.reduce: case JoinText() | JoinArray() | JoinObject() | JoinLastOf(): pass case JoinReduce(): loc = append(loc, "reduce") - _, expr = process_expr(scope, block.join.reduce, loc) - join = block.join.model_copy(update={"reduce": expr}) - block = block.model_copy(update={"join": join}) + _, expr = process_expr(scope, block.reduce.function, loc) + join = block.reduce.model_copy(update={"function": expr}) + block = block.model_copy(update={"reduce": join}) return block @@ -1355,7 +1355,7 @@ def combine_results(join_type: JoinType, results: list[PdlLazy[Any]]): result = lazy_apply( ( lambda _: reduce( - value_of_expr(join_type.reduce), + value_of_expr(join_type.function), [r.result() for r in results], ) ), diff --git a/tests/test_array.py b/tests/test_array.py index 95244133f..1e1229b18 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -14,7 +14,7 @@ def test_array_data(): "i": [1, 2, 3, 4], }, "repeat": "${ i }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, } @@ -37,7 +37,7 @@ def test_for_data(): "code": "result = ${ I } + 1", }, "until": "${ I == 5 }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, ], } diff --git a/tests/test_cond.py b/tests/test_cond.py index 32eb37d77..f8572fbc8 100644 --- a/tests/test_cond.py +++ b/tests/test_cond.py @@ -210,7 +210,7 @@ def test_cond2(): ] }, "until": "${ I == 5 }", - "join": {"as": "lastOf"}, + "reduce": {"as": "lastOf"}, }, ], } @@ -248,7 +248,7 @@ def test_repeat_until(): ] }, "until": "${ I == 5 }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, ], } @@ -321,7 +321,7 @@ def test_repeat_until_text(): }, }, "until": "${ I == 5 }", - "join": {"with": "\n"}, + "reduce": {"with": "\n"}, }, ], } diff --git a/tests/test_for.py b/tests/test_for.py index 88cdaf20f..96008a3f5 100644 --- a/tests/test_for.py +++ b/tests/test_for.py @@ -77,7 +77,7 @@ def for_data3(loop_kind): "id": "${ ids }", }, loop_kind: "${ i }: ${ name }: ${ id }\n", - "join": {"as": "array"}, + "reduce": {"as": "array"}, } ], } @@ -97,7 +97,7 @@ def for_data4(loop_kind): "def": "x", "for": {"i": [1, 2, 3, 4]}, loop_kind: "${ i + 1 }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, } ], } @@ -139,7 +139,7 @@ def for_data5(loop_kind): "text": { "for": {"i": [1, 2, 3, 4]}, loop_kind: "${ i }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, } ], @@ -162,9 +162,9 @@ def test_for_nested_array(): for: j: [1,2] {loop_kind}: "${{i}}${{j}}" - join: + reduce: as: array -join: +reduce: as: array """ result = exec_str(prog_str) @@ -181,7 +181,7 @@ def test_for_nested_text(): for: j: [1,2] {loop_kind2}: "${{i}}${{j}}" -join: +reduce: with: "\\n" """ result = exec_str(prog_str) @@ -194,7 +194,7 @@ def for_scope(loop_kind): "text": { "for": {"i": [1, 2, 3, 4]}, loop_kind: {"defs": {"x": "${ x + i }"}, "data": "${ i + x }"}, - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, } @@ -217,7 +217,7 @@ def for_index(loop_kind): "index": "idx", "for": {"i": [1, 2, 3, 4]}, loop_kind: "${ i + idx }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, } @@ -241,7 +241,7 @@ def for_context(loop_kind): ] }, loop_kind: "${ pdl_context.serialize('litellm') }", - "join": {"as": "array"}, + "reduce": {"as": "array"}, }, ] } @@ -333,8 +333,8 @@ def test_for_reduce(): for: i: [1,2,3,4] {loop_kind1}: ${{ i }} -join: - reduce: ${{ plus }} +reduce: + function: ${{ plus }} """ result = exec_str(prog_str) assert result == 10 diff --git a/tests/test_repeat.py b/tests/test_repeat.py index 4f6f5b7b3..c45be7122 100644 --- a/tests/test_repeat.py +++ b/tests/test_repeat.py @@ -110,7 +110,7 @@ def test_repeat_nested3(): ] }, "maxIterations": 3, - "join": {"as": "lastOf"}, + "reduce": {"as": "lastOf"}, }