From 9e28ab8b7e0086d30891f1d25c33617cbefcde96 Mon Sep 17 00:00:00 2001 From: Louis Mandel Date: Wed, 11 Sep 2024 00:19:17 -0400 Subject: [PATCH] Replace `show_result` by `contribute` --- README.md | 14 +- docs/README.md | 14 +- docs/tutorial.md | 28 +- examples/chatbot/chatbot.pdl | 2 +- examples/cldk/cldk-assistant.pdl | 11 +- examples/code/code-eval.pdl | 4 +- examples/code/code-json.pdl | 8 +- examples/code/code.pdl | 9 +- examples/demo/3-weather.pdl | 6 +- examples/demo/4-arith-detector.pdl | 8 +- examples/fibonacci/fib.pdl | 3 +- examples/granite/multi_round_chat.pdl | 2 +- examples/gsm8k/math-base.pdl | 1 - examples/gsm8k/math-jinja.pdl | 1 - examples/gsm8k/math-patterns.pdl | 3 +- examples/gsm8k/math-python.pdl | 1 - examples/gsm8k/math.pdl | 1 - examples/hello/hello-if.pdl | 2 +- examples/hello/hello-roles-array.pdl | 2 +- examples/hello/hello-type-code.pdl | 1 - examples/hello/weather.pdl | 6 +- examples/input/input_test.pdl | 2 +- examples/input/input_test3.json | 2 +- examples/input/input_test4.json | 2 +- examples/meta/arith-detector.pdl | 8 +- examples/meta/pdl-gen.pdl | 7 +- examples/prompt_library/PoT.pdl | 9 +- examples/prompt_library/RAG.pdl | 2 +- examples/prompt_library/ReWoo.pdl | 6 +- examples/prompt_library/demos/ReAct.pdl | 2 +- examples/prompt_library/demos/ReWOO.pdl | 2 +- examples/prompt_library/demos/Verifier.pdl | 6 +- .../prompt_library/demos/Verifier_json.pdl | 2 +- examples/prompt_library/tools.pdl | 2 +- examples/rag/rag.pdl | 4 +- examples/talk/12-multiagent.pdl | 2 +- examples/talk/6-chatbot.pdl | 2 +- examples/talk/7-chatbot-roles.pdl | 2 +- examples/talk/8-code-eval.pdl | 4 +- examples/tutorial/calling_apis.pdl | 6 +- examples/tutorial/data_block.pdl | 8 +- examples/tutorial/input_file_json.pdl | 10 +- examples/tutorial/muting_block_output.pdl | 2 +- pdl-live/src/pdl_ast.d.ts | 127 +++---- pdl-live/src/pdl_viewer.ts | 15 +- pdl-schema.json | 344 ++++++++++++------ pdl/pdl_ast.py | 12 +- pdl/pdl_dumper.py | 5 +- pdl/pdl_interpreter.py | 12 +- tests/data/line/hello10.pdl | 2 +- tests/data/line/hello11.pdl | 2 +- tests/data/line/hello12.pdl | 2 +- tests/data/line/hello13.pdl | 2 - tests/data/line/hello16.pdl | 2 +- tests/data/line/hello17.pdl | 2 +- tests/data/line/hello18.pdl | 2 - tests/data/line/hello26.pdl | 2 +- tests/data/line/hello28.pdl | 2 +- tests/data/line/hello29.pdl | 2 +- tests/test_array.py | 2 +- tests/test_code.py | 17 +- tests/test_cond.py | 18 +- tests/test_input.py | 3 +- tests/test_model.py | 1 - tests/test_var.py | 8 +- 65 files changed, 468 insertions(+), 335 deletions(-) diff --git a/README.md b/README.md index c7763188..34a2aac0 100644 --- a/README.md +++ b/README.md @@ -179,7 +179,7 @@ document: - read: examples/code/data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - "\n{{ CODE.source_code }}\n" - model: meta-llama/llama-3-70b-instruct input: @@ -233,7 +233,7 @@ document: - read: ./data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - "\n{{ CODE.source_code }}\n" - model: ibm/granite-34b-code-instruct def: EXPLANATION @@ -250,7 +250,7 @@ document: {{ CODE.source_code }}``` - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - | @@ -307,13 +307,13 @@ document: - read: ./data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - model: ibm/granite-34b-code-instruct def: EXPLANATION - show_result: False + contribute: [] input: | Here is some info about the location of the function in the repo. @@ -327,7 +327,7 @@ document: ``` {{ CODE.source_code }}``` - def: EVAL - show_result: False + contribute: [] lan: python code: | diff --git a/docs/README.md b/docs/README.md index 8e6a784e..b4673cf6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -161,7 +161,7 @@ document: - read: ./data.json parser: json def: CODE - show_result: False + contribute: [] - "\n{{ CODE.source_code }}\n" - model: ibm/granite-20b-code-instruct-v2 input: @@ -215,10 +215,10 @@ document: - read: ./data.json parser: json def: CODE - show_result: False + contribute: [] - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - "\n{{ CODE.source_code }}\n" - model: ibm/granite-20b-code-instruct-v2 def: EXPLANATION @@ -292,13 +292,13 @@ document: - read: ./data.json parser: json def: CODE - show_result: False + contribute: [] - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - model: ibm/granite-20b-code-instruct-v2 def: EXPLANATION - show_result: False + contribute: [] parameters: decoding_method: greedy max_new_tokens: 1024 @@ -315,7 +315,7 @@ document: ``` {{ CODE.source_code }}``` - def: EVAL - show_result: False + contribute: [] lan: python code: | diff --git a/docs/tutorial.md b/docs/tutorial.md index 999c252a..6c64d0a7 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -504,7 +504,7 @@ document: - "\nTranslate the sentence '{{ sentence }}' to {{ language }}\n" - model: ibm/granite-20b-multilingual - call: translate - show_result: false + contribute: [] def: FRENCH args: sentence: Hello,{{ GEN }} @@ -532,7 +532,7 @@ document: - def: NAME document: - model: "ibm/granite-20b-code-instruct-v2" - show_result: false # <-- MISTAKE: + contribute: [] # <-- MISTAKE: # `show_result` is specified on the document, # resulting in an _empty_ document and thus # an empty `NAME` @@ -547,7 +547,7 @@ document: - get: NAME - "`!\n" - def: TWO - show_result: false # <-- CORRECT: + contribute: [] # <-- CORRECT: # `show_result` is set at the root of the block, # which behaves as expected. document: @@ -624,11 +624,11 @@ The following PDL program reads this content and assigns it to variable `PERSON` ```yaml description: Input block example with json input +defs: + PERSON: + read: ./input.json + parser: json document: -- read: examples/tutorial/input.json - parser: json - def: PERSON - show_result: false - "{{ PERSON.name }} lives at the following address:\n" - "{{ PERSON.address.number }} {{ PERSON.address.street }} in the town of {{ PERSON.address.town }}, {{ PERSON.address.state }}" ``` @@ -671,7 +671,7 @@ document: - read: def: QUERY message: "Ask a query: " - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct-v2 input: |- Question: What is the weather in London? @@ -689,12 +689,12 @@ document: - '!' include_stop_sequence: false def: LOCATION - show_result: false + contribute: [] - api: https url: https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q= input: '{{ LOCATION }}' def: WEATHER - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct-v2 input: > Explain what the weather is from the following JSON: @@ -727,13 +727,13 @@ document: - read: examples/code/data.json parser: json def: CODE - show_result: False + contribute: [] - read: examples/code/ground_truth.txt def: TRUTH - show_result: False + contribute: [] - model: ibm/granite-20b-code-instruct-v2 def: EXPLANATION - show_result: False + contribute: [] parameters: decoding_method: greedy max_new_tokens: 1024 @@ -750,7 +750,7 @@ document: ``` {{ CODE.source_code }}``` - def: EVAL - show_result: False + contribute: [] lan: python code: | diff --git a/examples/chatbot/chatbot.pdl b/examples/chatbot/chatbot.pdl index af15886e..cf4f866f 100644 --- a/examples/chatbot/chatbot.pdl +++ b/examples/chatbot/chatbot.pdl @@ -10,7 +10,7 @@ document: - read: def: eval message: "\nIs this a good answer[yes/no]?\n" - show_result: false + contribute: [] - if: "{{ eval == 'no' }}" then: - read: diff --git a/examples/cldk/cldk-assistant.pdl b/examples/cldk/cldk-assistant.pdl index 3943f155..a5821787 100644 --- a/examples/cldk/cldk-assistant.pdl +++ b/examples/cldk/cldk-assistant.pdl @@ -3,11 +3,11 @@ document: - read: def: project message: "Please enter the path to your Java project:\n" - show_result: false -- "***Analyzing..." + contribute: [] +- "***Analyzing...\n" - lan: python def: analysis - show_result: false + contribute: [] code: | import os from rich import print # Optional, for pretty printing. @@ -30,13 +30,12 @@ document: - read: def: query message: "\n\nHow can I help you [Type 'quit' to quit]?\n" - show_result: false + contribute: [] - "\n***Generating PDL code for your query:\n" - if: "{{ query != 'quit'}}" then: - model: ibm/granite-20b-code-instruct def: PDL - show_result: true input: - | Question: What are all the classes? @@ -142,7 +141,7 @@ document: include_stop_sequence: false - "\n\n***Executing the above PDL code:\n\n" - lan: python - show_result: false + contribute: [] code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program diff --git a/examples/code/code-eval.pdl b/examples/code/code-eval.pdl index 8af063c5..427b855b 100644 --- a/examples/code/code-eval.pdl +++ b/examples/code/code-eval.pdl @@ -3,7 +3,7 @@ document: - read: ./data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - "\n{{ CODE.source_code }}\n" - model: ibm/granite-34b-code-instruct def: EXPLANATION @@ -20,7 +20,7 @@ document: {{ CODE.source_code }}``` - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - | diff --git a/examples/code/code-json.pdl b/examples/code/code-json.pdl index 26a238d1..5b9ed757 100644 --- a/examples/code/code-json.pdl +++ b/examples/code/code-json.pdl @@ -3,13 +3,13 @@ document: - read: ./data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - model: ibm/granite-34b-code-instruct def: EXPLANATION - show_result: False + contribute: [] input: | Here is some info about the location of the function in the repo. @@ -23,7 +23,7 @@ document: ``` {{ CODE.source_code }}``` - def: EVAL - show_result: False + contribute: [] lan: python code: | diff --git a/examples/code/code.pdl b/examples/code/code.pdl index e5a7170b..0a98895f 100644 --- a/examples/code/code.pdl +++ b/examples/code/code.pdl @@ -1,9 +1,9 @@ description: Code explanation example +defs: + CODE: + read: ./data.yaml + parser: yaml document: -- read: ./data.yaml - parser: yaml - def: CODE - show_result: False - "\n{{ CODE.source_code }}\n" - model: ibm/granite-34b-code-instruct input: @@ -17,4 +17,3 @@ document: Explain the following code: ``` {{ CODE.source_code }}``` -- "\n" \ No newline at end of file diff --git a/examples/demo/3-weather.pdl b/examples/demo/3-weather.pdl index ebdbbdb7..85fba69e 100644 --- a/examples/demo/3-weather.pdl +++ b/examples/demo/3-weather.pdl @@ -3,7 +3,7 @@ document: - read: def: QUERY message: "Ask a query: " - show_result: false + contribute: [] - model: ibm/granite-34b-code-instruct input: | Question: What is the weather in London? @@ -20,12 +20,12 @@ document: - '!' include_stop_sequence: false def: LOCATION - show_result: false + contribute: [] - api: https url: https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q= input: '{{ LOCATION }}' def: WEATHER - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct input: | Explain the weather from the following JSON. diff --git a/examples/demo/4-arith-detector.pdl b/examples/demo/4-arith-detector.pdl index 3e1f2f3f..998f82e1 100644 --- a/examples/demo/4-arith-detector.pdl +++ b/examples/demo/4-arith-detector.pdl @@ -3,7 +3,7 @@ document: - read: multiline: True def: QUERY - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct input: document: @@ -37,7 +37,7 @@ document: - get: QUERY - "\n\n" def: QUERY1 - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct @@ -118,7 +118,7 @@ document: - get: QUERY1 - "\n\n" def: PDL - show_result: false + contribute: [] - lan: python code: | @@ -136,5 +136,5 @@ document: data = Program.model_validate(obj) _, result, _, _ = process_prog(state, empty_scope, data) def: RESULT - show_result: false + contribute: [] - "\n" \ No newline at end of file diff --git a/examples/fibonacci/fib.pdl b/examples/fibonacci/fib.pdl index 9235d811..b458c8c4 100644 --- a/examples/fibonacci/fib.pdl +++ b/examples/fibonacci/fib.pdl @@ -11,7 +11,6 @@ document: code: | import random result = random.randint(1, 20) - show_result: true - "\nNow computing fib(" - get: N - ")\n" @@ -20,7 +19,7 @@ document: code: | {{ CODE }} result = fib({{ N }}) - show_result: false + contribute: [] - 'The result is: ' - get: RESULT - "\n\nExplain what the above code does and what the result means\n\n" diff --git a/examples/granite/multi_round_chat.pdl b/examples/granite/multi_round_chat.pdl index c8265fdb..f5de78b1 100644 --- a/examples/granite/multi_round_chat.pdl +++ b/examples/granite/multi_round_chat.pdl @@ -5,7 +5,7 @@ document: parser: json def: prompts spec: {prompts: [str]} - show_result: false + contribute: [] - "{{ SYSTEM_CONTENT_CHAT }}" - for: prompt: "{{ prompts.prompts }}" diff --git a/examples/gsm8k/math-base.pdl b/examples/gsm8k/math-base.pdl index 8f3bf392..108ac230 100644 --- a/examples/gsm8k/math-base.pdl +++ b/examples/gsm8k/math-base.pdl @@ -7,4 +7,3 @@ document: params: stop_sequences: ["Question"] include_stop_sequence: false - show_result: true diff --git a/examples/gsm8k/math-jinja.pdl b/examples/gsm8k/math-jinja.pdl index c1d9582d..c57f3f00 100644 --- a/examples/gsm8k/math-jinja.pdl +++ b/examples/gsm8k/math-jinja.pdl @@ -72,7 +72,6 @@ document: params: stop_sequences: ["Question"] include_stop_sequence: false - show_result: true - lan: python code: | from pdl import pdl_ast, pdl_interpreter diff --git a/examples/gsm8k/math-patterns.pdl b/examples/gsm8k/math-patterns.pdl index 7ee0e902..9f2e510b 100644 --- a/examples/gsm8k/math-patterns.pdl +++ b/examples/gsm8k/math-patterns.pdl @@ -13,7 +13,7 @@ document: - include: ../prompt_library/CoT.pdl - def: filtered_tools call: filter_tools_by_name - show_result: false + contribute: [] args: tools: "{{ default_tools }}" tool_names: "{{ available_tools }}" @@ -30,4 +30,3 @@ document: params: stop_sequences: ["<|endoftext|>"] include_stop_sequence: false - show_result: true diff --git a/examples/gsm8k/math-python.pdl b/examples/gsm8k/math-python.pdl index e18301b5..eb939acc 100644 --- a/examples/gsm8k/math-python.pdl +++ b/examples/gsm8k/math-python.pdl @@ -27,4 +27,3 @@ document: params: stop_sequences: ["Question"] include_stop_sequence: false - show_result: true diff --git a/examples/gsm8k/math.pdl b/examples/gsm8k/math.pdl index 56894fd5..69d48579 100644 --- a/examples/gsm8k/math.pdl +++ b/examples/gsm8k/math.pdl @@ -144,7 +144,6 @@ document: params: stop_sequences: ["Question"] include_stop_sequence: false - show_result: true - lan: python code: | from pdl import pdl_ast, pdl_interpreter diff --git a/examples/hello/hello-if.pdl b/examples/hello/hello-if.pdl index cbb52181..14ff22c9 100644 --- a/examples/hello/hello-if.pdl +++ b/examples/hello/hello-if.pdl @@ -6,7 +6,7 @@ document: import random result = random.choice([False, True]) def: QUESTION - show_result: false + contribute: [] - if: '{{ QUESTION }}' then: How are you? - "\n" \ No newline at end of file diff --git a/examples/hello/hello-roles-array.pdl b/examples/hello/hello-roles-array.pdl index 21662f3a..293d69ec 100644 --- a/examples/hello/hello-roles-array.pdl +++ b/examples/hello/hello-roles-array.pdl @@ -6,7 +6,7 @@ document: content: You are a helpful software engineer. You write clear, concise, well-commented code. - role: user content: Write a Python function that implement merge sort. - show_result: false + contribute: [] - model: ibm/granite-8b-code-instruct input: "{{ prompt }}" params: diff --git a/examples/hello/hello-type-code.pdl b/examples/hello/hello-type-code.pdl index ef5081c8..3ade3c45 100644 --- a/examples/hello/hello-type-code.pdl +++ b/examples/hello/hello-type-code.pdl @@ -2,7 +2,6 @@ description: Hello world showing call out to python code document: - lan: python - show_result: false spec: int code: | import string diff --git a/examples/hello/weather.pdl b/examples/hello/weather.pdl index 2bc98c23..6e5f27bf 100644 --- a/examples/hello/weather.pdl +++ b/examples/hello/weather.pdl @@ -3,7 +3,7 @@ document: - read: def: QUERY message: "Ask a query: " - show_result: false + contribute: [] - model: ibm/granite-34b-code-instruct input: | Question: What is the weather in London? @@ -20,12 +20,12 @@ document: - '!' include_stop_sequence: false def: LOCATION - show_result: false + contribute: [] - api: https url: https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q= input: '{{ LOCATION }}' def: WEATHER - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct input: | Explain the weather from the following JSON. diff --git a/examples/input/input_test.pdl b/examples/input/input_test.pdl index b3de5211..96a7c233 100644 --- a/examples/input/input_test.pdl +++ b/examples/input/input_test.pdl @@ -4,5 +4,5 @@ defs: document: - read: "{{ file }}" def: HELLO - show_result: False + contribute: [] - get: HELLO \ No newline at end of file diff --git a/examples/input/input_test3.json b/examples/input/input_test3.json index ed694860..11a6127a 100644 --- a/examples/input/input_test3.json +++ b/examples/input/input_test3.json @@ -5,7 +5,7 @@ "read": null, "parser": "json", "def": "PERSON", - "show_result": false + "contribute": [] }, { "get": "PERSON.name" diff --git a/examples/input/input_test4.json b/examples/input/input_test4.json index 82fe50f8..f343aa2b 100644 --- a/examples/input/input_test4.json +++ b/examples/input/input_test4.json @@ -5,7 +5,7 @@ "read": "tests/data/input.json", "parser": "json", "def": "PERSON", - "show_result": false + "contribute": [] }, {"get": "PERSON.name"}, " lives at the following address:\n", diff --git a/examples/meta/arith-detector.pdl b/examples/meta/arith-detector.pdl index 3e1f2f3f..998f82e1 100644 --- a/examples/meta/arith-detector.pdl +++ b/examples/meta/arith-detector.pdl @@ -3,7 +3,7 @@ document: - read: multiline: True def: QUERY - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct input: document: @@ -37,7 +37,7 @@ document: - get: QUERY - "\n\n" def: QUERY1 - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct @@ -118,7 +118,7 @@ document: - get: QUERY1 - "\n\n" def: PDL - show_result: false + contribute: [] - lan: python code: | @@ -136,5 +136,5 @@ document: data = Program.model_validate(obj) _, result, _, _ = process_prog(state, empty_scope, data) def: RESULT - show_result: false + contribute: [] - "\n" \ No newline at end of file diff --git a/examples/meta/pdl-gen.pdl b/examples/meta/pdl-gen.pdl index 3e1c19de..c2e48e71 100644 --- a/examples/meta/pdl-gen.pdl +++ b/examples/meta/pdl-gen.pdl @@ -13,7 +13,7 @@ document: contents.append(line + "\n") result = ''.join(contents) def: QUERY - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct params: decoding_method: greedy @@ -171,7 +171,7 @@ document: - model: "google/flan-t5-xl" params: decoding_method: greedy - show_results: false + contribute: [] ``` Question: Write a PDL program with a block that does not show its results @@ -191,7 +191,7 @@ document: break contents.append(line + "\n") result = ''.join(contents) - show_results: false + contribute: [] ``` Question: Write a PDL script with a block that has an assignment to variable QUERY @@ -307,7 +307,6 @@ document: - | result = input("How can I help you?: ") def: QUERY - show_result: true ``` Question: diff --git a/examples/prompt_library/PoT.pdl b/examples/prompt_library/PoT.pdl index b7a1058c..365bf7c0 100644 --- a/examples/prompt_library/PoT.pdl +++ b/examples/prompt_library/PoT.pdl @@ -100,10 +100,10 @@ defs: include_stop_sequence: false - def: ANSWER lan: python - show_result: false + contribute: [] code: "{{ PROGRAM }}" - get: ANSWER - show_result: false + contribute: [] program_of_thought_backtick: function: @@ -217,7 +217,6 @@ defs: # Python code, return ans - def: PROGRAM model: "{{ model }}" - show_result: true parser: regex: '```.*\n((?:.|\n|$)*?)$\n\s*```' # extracts code from backtick blocks mode: findall @@ -226,7 +225,7 @@ defs: include_stop_sequence: false - def: ANSWER lan: python - show_result: false + contribute: [] code: "{{ PROGRAM|join('\n') }}" - get: ANSWER - show_result: false \ No newline at end of file + contribute: [] \ No newline at end of file diff --git a/examples/prompt_library/RAG.pdl b/examples/prompt_library/RAG.pdl index e9f11060..19c2acf9 100644 --- a/examples/prompt_library/RAG.pdl +++ b/examples/prompt_library/RAG.pdl @@ -6,7 +6,7 @@ defs: corpus: {list: str} return: - lan: python - show_result: false + contribute: [] code: | from rank_bm25 import BM25Okapi PDL_SESSION.corpus = corpus diff --git a/examples/prompt_library/ReWoo.pdl b/examples/prompt_library/ReWoo.pdl index 9570ed38..114a2ced 100644 --- a/examples/prompt_library/ReWoo.pdl +++ b/examples/prompt_library/ReWoo.pdl @@ -8,7 +8,7 @@ defs: trajectory: { list: obj } return: - def: i - show_result: false + contribute: [] data: 1 - for: trajectory: "{{ trajectory }}" @@ -131,7 +131,7 @@ defs: else: "Invalid action. Valid actions are {{ TOOL_INFO.signatures|join(', ') }} and Finish[]." tool_output: "{{ raw_tool_output }}" - def: output - show_result: false + contribute: [] lan: python code: | output[ID] = str(tool_output) @@ -146,7 +146,7 @@ defs: Plan: {{ PLAN }} Evidence: {{ tool_output }} # - def: EVIDENCE - # show_result: false + # contribute: [] # document: # for: # plan: "{{ plans }}" diff --git a/examples/prompt_library/demos/ReAct.pdl b/examples/prompt_library/demos/ReAct.pdl index 52db2a9e..32fda1ec 100644 --- a/examples/prompt_library/demos/ReAct.pdl +++ b/examples/prompt_library/demos/ReAct.pdl @@ -7,7 +7,7 @@ document: - include: ../ReAct.pdl - def: filtered_tools call: filter_tools_by_name - show_result: false + contribute: [] args: tools: "{{ default_tools }}" tool_names: "{{ available_tools }}" diff --git a/examples/prompt_library/demos/ReWOO.pdl b/examples/prompt_library/demos/ReWOO.pdl index f0520d7d..b2b010fa 100644 --- a/examples/prompt_library/demos/ReWOO.pdl +++ b/examples/prompt_library/demos/ReWOO.pdl @@ -6,7 +6,7 @@ document: - include: ../ReWOO.pdl # - def: filtered_tools # call: filter_tools_by_name - # show_result: false + # contribute: [] # args: # tools: "{{ default_tools }}" # tool_names: "{{ available_tools }}" diff --git a/examples/prompt_library/demos/Verifier.pdl b/examples/prompt_library/demos/Verifier.pdl index c68b0a92..59f49d3f 100644 --- a/examples/prompt_library/demos/Verifier.pdl +++ b/examples/prompt_library/demos/Verifier.pdl @@ -7,17 +7,16 @@ document: - include: ../../granite/granite_defs.pdl - def: filtered_tools call: filter_tools_by_name - show_result: false + contribute: [] args: tools: "{{ default_tools }}" tool_names: "{{ available_tools }}" - def: QUESTION - show_result: false + contribute: [] read: message: "Please enter a question: " - def: GRANITE_RESULT call: react - show_result: true args: context: "{{ granite_models.granite_7b_lab.system_prompt }}" question: "{{ QUESTION }}" @@ -27,7 +26,6 @@ document: - "\n\n-------- Verifying answer --------\n\n" - def: LLAMA_RESULT call: react - show_result: true args: context: "" question: |- diff --git a/examples/prompt_library/demos/Verifier_json.pdl b/examples/prompt_library/demos/Verifier_json.pdl index 7e9e2e98..c5c23042 100644 --- a/examples/prompt_library/demos/Verifier_json.pdl +++ b/examples/prompt_library/demos/Verifier_json.pdl @@ -4,7 +4,7 @@ document: - include: ../../granite/granite_defs.pdl - def: filtered_tools call: filter_tools_by_name - show_result: false + contribute: [] args: tools: "{{ default_tools }}" tool_names: ["Search"] diff --git a/examples/prompt_library/tools.pdl b/examples/prompt_library/tools.pdl index ec1d7a05..99086d46 100644 --- a/examples/prompt_library/tools.pdl +++ b/examples/prompt_library/tools.pdl @@ -77,7 +77,7 @@ defs: - api: https url: https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q= input: "{{ subject }}" - show_result: false + contribute: [] default_tools: data: diff --git a/examples/rag/rag.pdl b/examples/rag/rag.pdl index 242743eb..34bfb2f0 100644 --- a/examples/rag/rag.pdl +++ b/examples/rag/rag.pdl @@ -25,7 +25,7 @@ document: document: >- Write a python function to remove first and last occurrence of a given character from the string. - show_result: false + contribute: [] - def: RETRIEVED lan: python spec: {prompt: [str], code: [str]} @@ -33,7 +33,7 @@ document: key = PDL_SESSION.embed("{{ TEST_PROMPT }}") nearest = PDL_SESSION.vec_db.get_nearest_examples("embeddings", key, 5) result = {col: nearest.examples[col] for col in ["prompt", "code"]} - show_result: false + contribute: [] - | Given the text after "Q:", generate a Python function after "A:". diff --git a/examples/talk/12-multiagent.pdl b/examples/talk/12-multiagent.pdl index 5a922260..a1019052 100644 --- a/examples/talk/12-multiagent.pdl +++ b/examples/talk/12-multiagent.pdl @@ -4,7 +4,7 @@ document: - include: ../granite/granite_defs.pdl - def: filtered_tools call: filter_tools_by_name - show_result: false + contribute: [] args: tools: "{{ default_tools }}" tool_names: ["Search"] diff --git a/examples/talk/6-chatbot.pdl b/examples/talk/6-chatbot.pdl index 40e626a9..1cfd3e28 100644 --- a/examples/talk/6-chatbot.pdl +++ b/examples/talk/6-chatbot.pdl @@ -12,7 +12,7 @@ document: - read: def: eval message: "\nIs this a good answer[yes/no]?\n" - show_result: false + contribute: [] - if: "{{ eval == 'no' }}" then: - read: diff --git a/examples/talk/7-chatbot-roles.pdl b/examples/talk/7-chatbot-roles.pdl index 8f5a6882..d21035b2 100644 --- a/examples/talk/7-chatbot-roles.pdl +++ b/examples/talk/7-chatbot-roles.pdl @@ -26,7 +26,7 @@ document: - read: def: eval message: "\nIs this a good answer[yes/no]?\n" - show_result: false + contribute: [] - if: "{{ eval == 'no' }}" then: - read: diff --git a/examples/talk/8-code-eval.pdl b/examples/talk/8-code-eval.pdl index 0cac2ad8..49bc7a22 100644 --- a/examples/talk/8-code-eval.pdl +++ b/examples/talk/8-code-eval.pdl @@ -3,7 +3,7 @@ document: - read: ./data.yaml parser: yaml def: CODE - show_result: False + contribute: [] - | Here is some info about the location of the function in the repo. repo: {{ CODE.repo_info.repo }} @@ -18,7 +18,7 @@ document: def: EXPLANATION - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - | EVALUATION: diff --git a/examples/tutorial/calling_apis.pdl b/examples/tutorial/calling_apis.pdl index 0ee6495b..28d2c205 100644 --- a/examples/tutorial/calling_apis.pdl +++ b/examples/tutorial/calling_apis.pdl @@ -3,7 +3,7 @@ document: - read: def: QUERY message: "Ask a query: " - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct-v2 input: |- Question: What is the weather in London? @@ -21,12 +21,12 @@ document: - '!' include_stop_sequence: false def: LOCATION - show_result: false + contribute: [] - api: https url: https://api.weatherapi.com/v1/current.json?key=cf601276764642cb96224947230712&q= input: '{{ LOCATION }}' def: WEATHER - show_result: false + contribute: [] - model: ibm/granite-20b-code-instruct-v2 input: > Explain what the weather is from the following JSON: diff --git a/examples/tutorial/data_block.pdl b/examples/tutorial/data_block.pdl index 971479a7..d34f5e6d 100644 --- a/examples/tutorial/data_block.pdl +++ b/examples/tutorial/data_block.pdl @@ -3,13 +3,13 @@ document: - read: ./data.json parser: json def: CODE - show_result: False + contribute: [] - read: ./ground_truth.txt def: TRUTH - show_result: False + contribute: [] - model: ibm/granite-20b-code-instruct-v2 def: EXPLANATION - show_result: False + contribute: [] parameters: decoding_method: greedy max_new_tokens: 1024 @@ -26,7 +26,7 @@ document: ``` {{ CODE.source_code }}``` - def: EVAL - show_result: False + contribute: [] lan: python code: | diff --git a/examples/tutorial/input_file_json.pdl b/examples/tutorial/input_file_json.pdl index ab0946ee..a1f78abd 100644 --- a/examples/tutorial/input_file_json.pdl +++ b/examples/tutorial/input_file_json.pdl @@ -1,8 +1,8 @@ description: Input block example with json input +defs: + PERSON: + read: ./input.json + parser: json document: -- read: ./input.json - parser: json - def: PERSON - show_result: false - "{{ PERSON.name }} lives at the following address:\n" -- "{{ PERSON.address.number }} {{ PERSON.address.street }} in the town of {{ PERSON.address.town }}, {{ PERSON.address.state }}" \ No newline at end of file +- "{{ PERSON.address.number }} {{ PERSON.address.street }} in the town of {{ PERSON.address.town }}, {{ PERSON.address.state }}" diff --git a/examples/tutorial/muting_block_output.pdl b/examples/tutorial/muting_block_output.pdl index c05f1ce5..3fd4a5dd 100644 --- a/examples/tutorial/muting_block_output.pdl +++ b/examples/tutorial/muting_block_output.pdl @@ -16,7 +16,7 @@ document: - model: ibm/granite-20b-multilingual platform: bam - call: translate - show_result: false + contribute: [] def: FRENCH args: sentence: Hello,{{ GEN }} diff --git a/pdl-live/src/pdl_ast.d.ts b/pdl-live/src/pdl_ast.d.ts index 157efd4c..ff5088ef 100644 --- a/pdl-live/src/pdl_ast.d.ts +++ b/pdl-live/src/pdl_ast.d.ts @@ -143,11 +143,12 @@ export type Description20 = string | null; * */ export type Def = string | null; +export type ContributeTarget = "result" | "context"; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult = boolean; +export type Contribute = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -282,10 +283,10 @@ export type Kind = "empty"; */ export type Def1 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult1 = boolean; +export type Contribute1 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -409,10 +410,10 @@ export type Program1 = */ export type Def2 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult2 = boolean; +export type Contribute2 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -515,10 +516,10 @@ export type Trace = */ export type Def3 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult3 = boolean; +export type Contribute3 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -606,10 +607,10 @@ export type Multiline = boolean; */ export type Def4 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult4 = boolean; +export type Contribute4 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -734,10 +735,10 @@ export type Content = */ export type Def5 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult5 = boolean; +export type Contribute5 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -860,10 +861,10 @@ export type Array = */ export type Def6 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult6 = boolean; +export type Contribute6 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -986,10 +987,10 @@ export type Sequence = */ export type Def7 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult7 = boolean; +export type Contribute7 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1116,10 +1117,10 @@ export type Document = */ export type Def8 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult8 = boolean; +export type Contribute8 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1305,10 +1306,10 @@ export type Trace1 = */ export type Def9 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult9 = boolean; +export type Contribute9 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1494,10 +1495,10 @@ export type Trace2 = */ export type Def10 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult10 = boolean; +export type Contribute10 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1688,10 +1689,10 @@ export type Trace3 = */ export type Def11 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult11 = boolean; +export type Contribute11 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1874,10 +1875,10 @@ export type IfResult = boolean | null; */ export type Def12 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult12 = boolean; +export type Contribute12 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -1954,10 +1955,10 @@ export type Raw = boolean; */ export type Def13 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult13 = boolean; +export type Contribute13 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2034,10 +2035,10 @@ export type Get = string; */ export type Def14 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult14 = boolean; +export type Contribute14 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2169,10 +2170,10 @@ export type Input = */ export type Def15 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult15 = boolean; +export type Contribute15 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2304,10 +2305,10 @@ export type Code = */ export type Def16 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult16 = boolean; +export type Contribute16 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2496,10 +2497,10 @@ export type MaxRetries = number | null; */ export type Def17 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult17 = boolean; +export type Contribute17 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2707,10 +2708,10 @@ export type ExampleFileIds = */ export type Def18 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult18 = boolean; +export type Contribute18 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -2868,10 +2869,10 @@ export type GuardrailsHapParams = { */ export type Def19 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult19 = boolean; +export type Contribute19 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -3000,10 +3001,10 @@ export type Trace7 = */ export type Def20 = string | null; /** - * Ignore the value computed by the block. + * Indicate if the block contributes to the result and background context. * */ -export type ShowResult20 = boolean; +export type Contribute20 = ContributeTarget[]; /** * Parser to use to construct a value out of a string result. */ @@ -3217,7 +3218,7 @@ export interface FunctionBlock { spec?: Spec; defs?: Defs; def?: Def20; - show_result?: ShowResult20; + contribute?: Contribute20; parser?: Parser20; fallback?: Fallback20; role?: Role20; @@ -3300,7 +3301,7 @@ export interface CallBlock { spec?: Spec1; defs?: Defs1; def?: Def19; - show_result?: ShowResult19; + contribute?: Contribute19; parser?: Parser19; fallback?: Fallback19; role?: Role19; @@ -3383,7 +3384,7 @@ export interface WatsonxModelBlock { spec?: Spec2; defs?: Defs2; def?: Def18; - show_result?: ShowResult18; + contribute?: Contribute18; parser?: Parser18; fallback?: Fallback18; role?: Role18; @@ -3467,7 +3468,7 @@ export interface BamModelBlock { spec?: Spec3; defs?: Defs3; def?: Def17; - show_result?: ShowResult17; + contribute?: Contribute17; parser?: Parser17; fallback?: Fallback17; role?: Role17; @@ -3556,7 +3557,7 @@ export interface LitellmModelBlock { spec?: Spec4; defs?: Defs4; def?: Def16; - show_result?: ShowResult16; + contribute?: Contribute16; parser?: Parser16; fallback?: Fallback16; role?: Role16; @@ -3641,7 +3642,7 @@ export interface CodeBlock { spec?: Spec5; defs?: Defs5; def?: Def15; - show_result?: ShowResult15; + contribute?: Contribute15; parser?: Parser15; fallback?: Fallback15; role?: Role15; @@ -3723,7 +3724,7 @@ export interface ApiBlock { spec?: Spec6; defs?: Defs6; def?: Def14; - show_result?: ShowResult14; + contribute?: Contribute14; parser?: Parser14; fallback?: Fallback14; role?: Role14; @@ -3806,7 +3807,7 @@ export interface GetBlock { spec?: Spec7; defs?: Defs7; def?: Def13; - show_result?: ShowResult13; + contribute?: Contribute13; parser?: Parser13; fallback?: Fallback13; role?: Role13; @@ -3887,7 +3888,7 @@ export interface DataBlock { spec?: Spec8; defs?: Defs8; def?: Def12; - show_result?: ShowResult12; + contribute?: Contribute12; parser?: Parser12; fallback?: Fallback12; role?: Role12; @@ -3969,7 +3970,7 @@ export interface IfBlock { spec?: Spec9; defs?: Defs9; def?: Def11; - show_result?: ShowResult11; + contribute?: Contribute11; parser?: Parser11; fallback?: Fallback11; role?: Role11; @@ -4053,7 +4054,7 @@ export interface RepeatBlock { spec?: Spec10; defs?: Defs10; def?: Def10; - show_result?: ShowResult10; + contribute?: Contribute10; parser?: Parser10; fallback?: Fallback10; role?: Role10; @@ -4137,7 +4138,7 @@ export interface RepeatUntilBlock { spec?: Spec11; defs?: Defs11; def?: Def9; - show_result?: ShowResult9; + contribute?: Contribute9; parser?: Parser9; fallback?: Fallback9; role?: Role9; @@ -4221,7 +4222,7 @@ export interface ForBlock { spec?: Spec12; defs?: Defs12; def?: Def8; - show_result?: ShowResult8; + contribute?: Contribute8; parser?: Parser8; fallback?: Fallback8; role?: Role8; @@ -4305,7 +4306,7 @@ export interface DocumentBlock { spec?: Spec13; defs?: Defs13; def?: Def7; - show_result?: ShowResult7; + contribute?: Contribute7; parser?: Parser7; fallback?: Fallback7; role?: Role7; @@ -4386,7 +4387,7 @@ export interface SequenceBlock { spec?: Spec14; defs?: Defs14; def?: Def6; - show_result?: ShowResult6; + contribute?: Contribute6; parser?: Parser6; fallback?: Fallback6; role?: Role6; @@ -4467,7 +4468,7 @@ export interface ArrayBlock { spec?: Spec15; defs?: Defs15; def?: Def5; - show_result?: ShowResult5; + contribute?: Contribute5; parser?: Parser5; fallback?: Fallback5; role?: Role5; @@ -4548,7 +4549,7 @@ export interface MessageBlock { spec?: Spec16; defs?: Defs16; def?: Def4; - show_result?: ShowResult4; + contribute?: Contribute4; parser?: Parser4; fallback?: Fallback4; role: Role4; @@ -4629,7 +4630,7 @@ export interface ReadBlock { spec?: Spec17; defs?: Defs17; def?: Def3; - show_result?: ShowResult3; + contribute?: Contribute3; parser?: Parser3; fallback?: Fallback3; role?: Role3; @@ -4712,7 +4713,7 @@ export interface IncludeBlock { spec?: Spec18; defs?: Defs18; def?: Def2; - show_result?: ShowResult2; + contribute?: Contribute2; parser?: Parser2; fallback?: Fallback2; role?: Role2; @@ -4791,7 +4792,7 @@ export interface ErrorBlock { spec?: Spec19; defs?: Defs19; def?: Def1; - show_result?: ShowResult1; + contribute?: Contribute1; parser?: Parser1; fallback?: Fallback1; role?: Role1; @@ -4873,7 +4874,7 @@ export interface EmptyBlock { spec?: Spec20; defs?: Defs20; def?: Def; - show_result?: ShowResult; + contribute?: Contribute; parser?: Parser; fallback?: Fallback; role?: Role; diff --git a/pdl-live/src/pdl_viewer.ts b/pdl-live/src/pdl_viewer.ts index fd3bdd08..60931f47 100644 --- a/pdl-live/src/pdl_viewer.ts +++ b/pdl-live/src/pdl_viewer.ts @@ -25,10 +25,10 @@ export function show_output(data: PdlBlocks) { const div = document.createElement('div'); div.classList.add('pdl_block'); match(data) - .with(P.string, output => { + .with(P.union(P.string, P.number), output => { div.innerHTML = htmlize(output); }) - .with({show_result: false}, () => { + .with({contribute: P.union([], ['context'])}, () => { div.classList.add('pdl_show_result_false'); div.innerHTML = '☐'; }) @@ -75,7 +75,7 @@ export function show_block(data: PdlBlock) { div.appendChild(body); add_def(body, data.def); body.classList.add('pdl_block'); - if (data?.show_result === false) { + if (!data?.contribute?.includes('context')) { body.classList.add('pdl_show_result_false'); } match(data) @@ -280,9 +280,12 @@ export function block_code_cleanup(data: string | PdlBlock): string | PdlBlock { match(new_data).with({trace: P._}, data => { data.trace = undefined; }); - // remove show_result: true - if (new_data?.show_result) { - new_data.show_result = undefined; + // remove contribute: ["result", context] + if ( + new_data?.contribute?.includes('result') && + new_data?.contribute?.includes('context') + ) { + new_data.contribute = undefined; } // remove empty defs list if (Object.keys(data?.defs ?? {}).length === 0) { diff --git a/pdl-schema.json b/pdl-schema.json index c1407984..bb4b910c 100644 --- a/pdl-schema.json +++ b/pdl-schema.json @@ -196,11 +196,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -805,11 +811,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -1401,11 +1413,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -2374,11 +2392,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -2986,11 +3010,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -3398,6 +3428,14 @@ "title": "CodeBlock", "type": "object" }, + "ContributeTarget": { + "enum": [ + "result", + "context" + ], + "title": "ContributeTarget", + "type": "string" + }, "DataBlock": { "additionalProperties": false, "description": "Arbitrary JSON value.", @@ -3594,11 +3632,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -4053,11 +4097,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -4651,11 +4701,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -5088,11 +5144,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -5690,11 +5752,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -6466,11 +6534,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -7089,11 +7163,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -7535,11 +7615,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -8311,11 +8397,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -8880,11 +8972,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -9963,11 +10061,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -11357,11 +11461,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -11879,11 +11989,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -12655,11 +12771,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -13430,11 +13552,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ @@ -14120,11 +14248,17 @@ "description": "Name of the variable used to store the result of the execution of the block.\n ", "title": "Def" }, - "show_result": { - "default": true, - "description": "Ignore the value computed by the block.\n ", - "title": "Show Result", - "type": "boolean" + "contribute": { + "default": [ + "result", + "context" + ], + "description": "Indicate if the block contributes to the result and background context.\n ", + "items": { + "$ref": "#/$defs/ContributeTarget" + }, + "title": "Contribute", + "type": "array" }, "parser": { "anyOf": [ diff --git a/pdl/pdl_ast.py b/pdl/pdl_ast.py index 154302e1..3c08c873 100644 --- a/pdl/pdl_ast.py +++ b/pdl/pdl_ast.py @@ -86,6 +86,11 @@ class RegexParser(Parser): RoleType: TypeAlias = Optional[str] +class ContributeTarget(StrEnum): + RESULT = "result" + CONTEXT = "context" + + class Block(BaseModel): """Common fields for all PDL blocks.""" @@ -103,8 +108,11 @@ class Block(BaseModel): assign: Optional[str] = Field(default=None, alias="def") """Name of the variable used to store the result of the execution of the block. """ - show_result: bool = True - """Ignore the value computed by the block. + contribute: list[ContributeTarget] = [ + ContributeTarget.RESULT, + ContributeTarget.CONTEXT, + ] + """Indicate if the block contributes to the result and background context. """ parser: Optional[ParserType] = None """Parser to use to construct a value out of a string result.""" diff --git a/pdl/pdl_dumper.py b/pdl/pdl_dumper.py index 4c6f5678..5400ce45 100644 --- a/pdl/pdl_dumper.py +++ b/pdl/pdl_dumper.py @@ -13,6 +13,7 @@ BlocksType, CallBlock, CodeBlock, + ContributeTarget, DataBlock, DocumentBlock, ErrorBlock, @@ -170,8 +171,8 @@ def block_to_dict(block: pdl_ast.BlockType) -> int | float | str | dict[str, Any d["program"] = blocks_to_dict(block.program) if block.assign is not None: d["def"] = block.assign - if block.show_result is False: - d["show_result"] = block.show_result + if set(block.contribute) != {ContributeTarget.RESULT, ContributeTarget.CONTEXT}: + d["contribute"] = block.contribute if block.result is not None: d["result"] = block.result if block.parser is not None: diff --git a/pdl/pdl_interpreter.py b/pdl/pdl_interpreter.py index 11828184..1dca7fab 100644 --- a/pdl/pdl_interpreter.py +++ b/pdl/pdl_interpreter.py @@ -22,6 +22,7 @@ BlockType, CallBlock, CodeBlock, + ContributeTarget, DataBlock, DocumentBlock, EmptyBlock, @@ -245,8 +246,12 @@ def step_advanced_block( scope, defs_trace = yield from step_defs(state, scope, block.defs, loc) else: defs_trace = block.defs - state = state.with_yield_result(state.yield_result and block.show_result) - state = state.with_yield_background(state.yield_background and block.show_result) + state = state.with_yield_result( + state.yield_result and ContributeTarget.RESULT in block.contribute + ) + state = state.with_yield_background( + state.yield_background and ContributeTarget.CONTEXT in block.contribute + ) result, background, scope, trace = yield from step_block_body( state, scope, block, loc ) @@ -265,8 +270,9 @@ def step_advanced_block( trace = handle_error( block, loc, "Type errors during spec checking", errors, trace ) - if block.show_result is False: + if ContributeTarget.RESULT not in block.contribute: result = "" + if ContributeTarget.CONTEXT not in block.contribute: background = [] return result, background, scope, trace diff --git a/tests/data/line/hello10.pdl b/tests/data/line/hello10.pdl index 9e4d9355..9722542b 100644 --- a/tests/data/line/hello10.pdl +++ b/tests/data/line/hello10.pdl @@ -5,7 +5,7 @@ document: code: | result = True defs: QUESTION - show_result: false + contribute: [] - if: '{{ QUESTION }}' then: How are you? spec: bool \ No newline at end of file diff --git a/tests/data/line/hello11.pdl b/tests/data/line/hello11.pdl index 66fbfa96..2516f9d4 100644 --- a/tests/data/line/hello11.pdl +++ b/tests/data/line/hello11.pdl @@ -5,7 +5,7 @@ document: code: | result = True defss: QUESTION - show_result: false + contribute: [] - if: '{{ QUESTION }}' then: How are you? spec: bool \ No newline at end of file diff --git a/tests/data/line/hello12.pdl b/tests/data/line/hello12.pdl index a5c6e53b..cecf6988 100644 --- a/tests/data/line/hello12.pdl +++ b/tests/data/line/hello12.pdl @@ -5,7 +5,7 @@ document: code: | result = True def: QUESTION - show_result: false + contribute: [] - if: '{{ QUESTION }}' then: How are you? spec: bool \ No newline at end of file diff --git a/tests/data/line/hello13.pdl b/tests/data/line/hello13.pdl index c8924c05..b33046fd 100644 --- a/tests/data/line/hello13.pdl +++ b/tests/data/line/hello13.pdl @@ -3,14 +3,12 @@ document: - def: I lan: python code: result = 0 - show_result: true - "\n" - repeat: document: - def: I lan: python code: result = {{ I }} + 1 - show_result: true spec: str - "\n" until: '{{ I == 5 }}' diff --git a/tests/data/line/hello16.pdl b/tests/data/line/hello16.pdl index 4f7fb50a..9a36e2f4 100644 --- a/tests/data/line/hello16.pdl +++ b/tests/data/line/hello16.pdl @@ -3,7 +3,7 @@ document: - read: ./hello16_data.json parser: json def: data - show_result: false + contribute: [] spec: { "questions": ["str"], "answers": ["obj"] } - model: ibm/granite-20b-code-instruct def: model_output diff --git a/tests/data/line/hello17.pdl b/tests/data/line/hello17.pdl index 52bfc7bf..62499cda 100644 --- a/tests/data/line/hello17.pdl +++ b/tests/data/line/hello17.pdl @@ -1,7 +1,7 @@ description: Hello world showing call out to python code document: - lan: python - show_result: false + contribute: [] spec: int code: | import string diff --git a/tests/data/line/hello18.pdl b/tests/data/line/hello18.pdl index 4f78f287..e4c4f7fb 100644 --- a/tests/data/line/hello18.pdl +++ b/tests/data/line/hello18.pdl @@ -3,14 +3,12 @@ document: - def: I lan: python code: result = 0 - show_result: true - "\n" - repeat: document: - def: I lan: python code: result = {{ I }} + 1 - show_result: true - "\n" until: '{{ J == 5 }}' as: document \ No newline at end of file diff --git a/tests/data/line/hello26.pdl b/tests/data/line/hello26.pdl index 7bb88061..36a1cda3 100644 --- a/tests/data/line/hello26.pdl +++ b/tests/data/line/hello26.pdl @@ -3,7 +3,7 @@ document: - read: hello16_data.json parser: json def: data - show_result: false + contribute: [] spec: { "questions": ["str"], "answers": ["obj"] } - model: ibm/granite-34b-code-instruct def: model_output diff --git a/tests/data/line/hello28.pdl b/tests/data/line/hello28.pdl index 6bcc0fe6..c286556e 100644 --- a/tests/data/line/hello28.pdl +++ b/tests/data/line/hello28.pdl @@ -5,5 +5,5 @@ document: code: | result = True def: QUESTION - show_result: false + contribute: [] - "{{ QUESTION1 }}" \ No newline at end of file diff --git a/tests/data/line/hello29.pdl b/tests/data/line/hello29.pdl index f023ec11..7e83f115 100644 --- a/tests/data/line/hello29.pdl +++ b/tests/data/line/hello29.pdl @@ -5,7 +5,7 @@ document: code: | result = True def: QUESTION - show_result: false + contribute: [] - data: x: "{{ QUESTION1 }}" y: "{{ QUESTION2 }}" diff --git a/tests/test_array.py b/tests/test_array.py index 67204552..d8866802 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -33,7 +33,7 @@ def test_for_data(): { "def": "I", "document": [{"lan": "python", "code": "result = 0"}], - "show_result": False, + "contribute": [], }, { "repeat": [ diff --git a/tests/test_code.py b/tests/test_code.py index a4ce6f3a..a0f96d90 100644 --- a/tests/test_code.py +++ b/tests/test_code.py @@ -32,21 +32,28 @@ def show_result_data(show): "document": [ {"lan": "python", "code": ["result = 'How can I help you?: '"]} ], - "show_result": show, + "contribute": show, } ], } -def test_show_result(): +def test_contribute_result(): state = InterpreterState() - data = Program.model_validate(show_result_data(True)) + data = Program.model_validate(show_result_data(["result"])) document, _, _, _ = process_prog(state, empty_scope, data) assert document == "How can I help you?: " -def test_show_result_false(): +def test_contribute_context(): state = InterpreterState() - data = Program.model_validate(show_result_data(False)) + data = Program.model_validate(show_result_data(["context"])) + _, background, _, _ = process_prog(state, empty_scope, data) + assert background[0] == [None, "How can I help you?: "] + + +def test_contribute_false(): + state = InterpreterState() + data = Program.model_validate(show_result_data([])) document, _, _, _ = process_prog(state, empty_scope, data) assert document == "" diff --git a/tests/test_cond.py b/tests/test_cond.py index 5c8cfca7..a8833dfb 100644 --- a/tests/test_cond.py +++ b/tests/test_cond.py @@ -1,4 +1,4 @@ -from pdl.pdl_ast import Program +from pdl.pdl_ast import ContributeTarget, Program from pdl.pdl_interpreter import InterpreterState, empty_scope, process_prog cond_data = { @@ -152,7 +152,7 @@ def cond_data1(show, name): }, } ], - "show_result": show, + "contribute": show, }, { "then": [", hello there!\n"], @@ -165,14 +165,16 @@ def cond_data1(show, name): def test_cond1(): state = InterpreterState() - data = Program.model_validate(cond_data1(False, "blah")) + data = Program.model_validate(cond_data1([], "blah")) document, _, _, _ = process_prog(state, empty_scope, data) assert document == "" def test_cond2(): state = InterpreterState() - data = Program.model_validate(cond_data1(True, "acy")) + data = Program.model_validate( + cond_data1([ContributeTarget.RESULT, ContributeTarget.CONTEXT], "acy") + ) document, _, _, _ = process_prog(state, empty_scope, data) assert document == "Tracy, hello there!\n" @@ -183,7 +185,6 @@ def test_cond2(): { "def": "I", "document": [{"lan": "python", "code": ["result = 0"]}], - "show_result": True, }, "\n", { @@ -194,7 +195,6 @@ def test_cond2(): "def": "I", "lan": "python", "code": "result = {{ I }} + 1", - "show_result": True, }, "\n", ] @@ -226,7 +226,6 @@ def test_repeat_until(): { "def": "I", "document": [{"lan": "python", "code": ["result = 0"]}], - "show_result": True, }, "\n", { @@ -237,7 +236,6 @@ def test_repeat_until(): "def": "I", "lan": "python", "code": "result = {{ I }} + 1", - "show_result": True, }, "\n", ] @@ -263,7 +261,6 @@ def test_repeat_until_array(): { "def": "I", "document": [{"lan": "python", "code": ["result = 0"]}], - "show_result": True, }, "\n", { @@ -274,7 +271,6 @@ def test_repeat_until_array(): "def": "I", "lan": "python", "code": ["result = {{ I }} + 1"], - "show_result": True, }, "\n", ] @@ -315,7 +311,6 @@ def test_repeat_until_document(): { "def": "I", "document": [{"lan": "python", "code": ["result = 0"]}], - "show_result": True, }, "\n", { @@ -329,7 +324,6 @@ def test_repeat_until_document(): "code": ["result = {{ I }} + 1"], } ], - "show_result": True, }, "\n", ], diff --git a/tests/test_input.py b/tests/test_input.py index f841db97..df71af42 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -16,7 +16,7 @@ "read": "./data/input.json", "parser": "json", "def": "PERSON", - "show_result": False, + "contribute": [], }, {"get": "PERSON.name"}, " lives at the following address:\n", @@ -60,7 +60,6 @@ def test_input_json_defs(): "defs": { "data": { "read": "tests/data/input_data.txt", - "show_result": True, } }, "document": ["{{ data }}"], diff --git a/tests/test_model.py b/tests/test_model.py index d2b6b272..6ba5046c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -100,7 +100,6 @@ def test_model_chain(): }, } ], - "show_result": True, } ], } diff --git a/tests/test_var.py b/tests/test_var.py index 4044cfa3..7e20efb1 100644 --- a/tests/test_var.py +++ b/tests/test_var.py @@ -47,7 +47,6 @@ def test_var(): "document": [ { "model": "ibm/granite-34b-code-instruct", - "show_result": True, "params": { "decoding_method": "greedy", "stop_sequences": ["!"], @@ -60,7 +59,7 @@ def test_var(): "def": "I", "lan": "python", "code": "result = NAME[::-1] + '!\\n'", - "show_result": False, + "contribute": [], }, {"get": "I"}, ], @@ -81,13 +80,13 @@ def test_code_shared_scope(): { "def": "NAME", "document": "foo", - "show_result": False, + "contribute": [], }, { "def": "I", "lan": "python", "code": {"document": ["NAME = NAME[::-1]\n", "result = NAME"]}, - "show_result": False, + "contribute": [], }, {"get": "NAME"}, {"get": "I"}, @@ -114,7 +113,6 @@ def test_code_shared_scope_no_mutate(): "def": "I", "lan": "python", "code": ["result = 0"], - "show_result": True, }, ], }