From 47d928549975dad475546782db7593b6c5ce98f8 Mon Sep 17 00:00:00 2001 From: Louis Mandel Date: Tue, 27 Aug 2024 21:26:13 -0400 Subject: [PATCH] The code block and input field of the api block are sequences instead of documents (#303) --- examples/arith/Arith-new.pdl | 3 +-- examples/arith/Arith.pdl | 4 +--- examples/cldk/cldk-assistant.pdl | 11 +++-------- examples/cldk/inputs.txt | 4 ++++ examples/cldk/ns.pdl | 6 ++---- examples/demo/4-arith-detector.pdl | 8 ++------ examples/fibonacci/fib-new.pdl | 4 +++- examples/fibonacci/fib-small.pdl | 12 ++++++------ examples/fibonacci/fib-small1.pdl | 22 +++++++++++++--------- examples/fibonacci/fib.pdl | 16 ++++++++-------- examples/gsm8k/math-jinja.pdl | 8 ++------ examples/gsm8k/math.pdl | 8 ++------ examples/meta/arith.pdl | 8 ++------ pdl/pdl_interpreter.py | 15 ++------------- tests/test_array.py | 2 +- tests/test_code.py | 4 +++- tests/test_cond.py | 24 ++++++++++++++---------- tests/test_errors.py | 8 ++++++-- tests/test_parser.py | 10 ++++++++-- tests/test_var.py | 4 ++-- 20 files changed, 85 insertions(+), 96 deletions(-) create mode 100644 examples/cldk/inputs.txt diff --git a/examples/arith/Arith-new.pdl b/examples/arith/Arith-new.pdl index 61b7b699..54582fdd 100644 --- a/examples/arith/Arith-new.pdl +++ b/examples/arith/Arith-new.pdl @@ -30,8 +30,7 @@ document: - '= ' - def: RESULT lan: python - code: - - result = {{ EXPR }} + code: result = {{ EXPR }} - ' >>' until: '{{ "The answer is" in REASON_OR_CALC }}' - "\n\n" diff --git a/examples/arith/Arith.pdl b/examples/arith/Arith.pdl index 22b18876..0923e038 100644 --- a/examples/arith/Arith.pdl +++ b/examples/arith/Arith.pdl @@ -58,9 +58,7 @@ document: - '= ' - def: RESULT lan: python - code: - - 'result = ' - - get: EXPR + code: result = {{ EXPR }} - ' >>' else: "" until: '{{ "The answer is" in REASON_OR_CALC }}' diff --git a/examples/cldk/cldk-assistant.pdl b/examples/cldk/cldk-assistant.pdl index 78f462dc..a4b7b845 100644 --- a/examples/cldk/cldk-assistant.pdl +++ b/examples/cldk/cldk-assistant.pdl @@ -8,8 +8,7 @@ document: - lan: python def: analysis show_result: false - code: - - | + code: | import os from rich import print # Optional, for pretty printing. from cldk import CLDK @@ -144,8 +143,7 @@ document: - "\n\n***Executing the above PDL code:\n\n" - lan: python show_result: false - code: - - | + code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program from pdl.pdl_interpreter import process_prog @@ -153,10 +151,7 @@ document: from pdl.pdl_interpreter import empty_scope import re import yaml - s = """' - - get: PDL - - | - '""" + s = """'{{ PDL }}'""" pdl = s.split("```")[1] obj = yaml.safe_load(pdl) state = InterpreterState() diff --git a/examples/cldk/inputs.txt b/examples/cldk/inputs.txt new file mode 100644 index 00000000..fccf2746 --- /dev/null +++ b/examples/cldk/inputs.txt @@ -0,0 +1,4 @@ +/Users/lmandel/ml4code/codellm-devkit-examples/sample.daytrader8-1.2 +What are all the methods of class com.ibm.websphere.samples.daytrader.impl.direct.AsyncOrder +Generate a summary for method run() of the class com.ibm.websphere.samples.daytrader.impl.direct.AsyncOrder +quit diff --git a/examples/cldk/ns.pdl b/examples/cldk/ns.pdl index 1febacd7..72952de9 100644 --- a/examples/cldk/ns.pdl +++ b/examples/cldk/ns.pdl @@ -7,8 +7,7 @@ document: - lan: python def: analysis show_result: false - code: - - | + code: | from northstar import Northstar from pprint import pprint @@ -68,8 +67,7 @@ document: then: - model: meta-llama/llama-3-70b-instruct platform: bam - input: - - | + input: | In the following code, {{ query }} {{ analysis }} until: "{{ query == 'quit' }}" diff --git a/examples/demo/4-arith-detector.pdl b/examples/demo/4-arith-detector.pdl index 582a58a4..cd39bf3d 100644 --- a/examples/demo/4-arith-detector.pdl +++ b/examples/demo/4-arith-detector.pdl @@ -123,8 +123,7 @@ document: show_result: false - lan: python - code: - - | + code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program from pdl.pdl_interpreter import process_prog @@ -132,10 +131,7 @@ document: from pdl.pdl_interpreter import empty_scope import re import yaml - s = """' - - get: PDL - - | - '""" + s = """'{{ PDL }}'""" pdl = s.split("```")[1] obj = yaml.safe_load(pdl) state = InterpreterState() diff --git a/examples/fibonacci/fib-new.pdl b/examples/fibonacci/fib-new.pdl index ba9539ea..a828aae2 100644 --- a/examples/fibonacci/fib-new.pdl +++ b/examples/fibonacci/fib-new.pdl @@ -3,7 +3,9 @@ document: - def: CODE model: ibm/granite-20b-code-instruct platform: bam - input: "Write just the definition for an efficient Python function to compute the Fibonacci sequence\n\n" + input: "Write just the definition for an efficient Python function to compute the Fibonacci sequence called fib\n\n" + parameters: + include_stop_sequence: false - "\nFind a random number between 1 and 20\n" - def: N lan: python diff --git a/examples/fibonacci/fib-small.pdl b/examples/fibonacci/fib-small.pdl index c617e743..c7c07711 100644 --- a/examples/fibonacci/fib-small.pdl +++ b/examples/fibonacci/fib-small.pdl @@ -4,6 +4,8 @@ document: model: ibm/granite-20b-code-instruct platform: bam input: "Write just the definition for an efficient Python function to compute the Fibonacci sequence\n\n" + parameters: + include_stop_sequence: false - def: N lan: python code: @@ -15,12 +17,10 @@ document: - get: N - ') = ' - lan: python - code: - - get: CODE - - result = fibonacci( - - get: N - - ) - + code: | + {{ CODE }} + result = fibonacci({{ N }}) + diff --git a/examples/fibonacci/fib-small1.pdl b/examples/fibonacci/fib-small1.pdl index 63670a59..4d9b9ef5 100644 --- a/examples/fibonacci/fib-small1.pdl +++ b/examples/fibonacci/fib-small1.pdl @@ -4,25 +4,29 @@ document: - def: CODE model: ibm/granite-20b-code-instruct-v2 platform: bam + parameters: + include_stop_sequence: false - "What is the name of the function above?\n" - def: FUNCTION model: ibm/granite-20b-code-instruct parameters: stop_sequences: [" "] + include_stop_sequence: false input: document: - get: context - "Answer: " - def: N lan: python - code: - - "import random\n" - - result = random.randint(1, 20) + code: | + import random + result = random.randint(1, 20) show_result: false -- "\nfibonacci(" -- get: N -- ') = ' +- | + + + {{ FUNCTION }}({{ N }}) = - lan: python - code: - - get: CODE - - result = fibonacci(N) + code: | + {{ CODE }} + result = {{FUNCTION}}({{N}}) diff --git a/examples/fibonacci/fib.pdl b/examples/fibonacci/fib.pdl index 5b1c0433..fc62cb1e 100644 --- a/examples/fibonacci/fib.pdl +++ b/examples/fibonacci/fib.pdl @@ -4,23 +4,23 @@ document: model: ibm/granite-20b-code-instruct platform: bam input: "Write just the definition for an efficient Python function to compute the Fibonacci sequence\n\n" + parameters: + include_stop_sequence: false - "\nFind a random number between 1 and 20\n" - def: N lan: python - code: - - "import random\n" - - result = random.randint(1, 20) + code: | + import random + result = random.randint(1, 20) show_result: true - "\nNow computing fibonacci(" - get: N - ")\n" - def: RESULT lan: python - code: - - get: CODE - - result = fibonacci( - - get: N - - ) + code: | + {{ CODE }} + result = fibonacci({{ N }}) show_result: false - 'The result is: ' - get: RESULT diff --git a/examples/gsm8k/math-jinja.pdl b/examples/gsm8k/math-jinja.pdl index c039c9ab..c1d9582d 100644 --- a/examples/gsm8k/math-jinja.pdl +++ b/examples/gsm8k/math-jinja.pdl @@ -74,8 +74,7 @@ document: include_stop_sequence: false show_result: true - lan: python - code: - - | + code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program from pdl.pdl_interpreter import process_prog @@ -83,10 +82,7 @@ document: from pdl.pdl_interpreter import empty_scope import re import yaml - s = """' - - get: PDL - - | - '""" + s = """'{{ PDL }}'""" pdl = s.split("```")[1] obj = yaml.safe_load(pdl) state = InterpreterState() diff --git a/examples/gsm8k/math.pdl b/examples/gsm8k/math.pdl index d262d284..e356063e 100644 --- a/examples/gsm8k/math.pdl +++ b/examples/gsm8k/math.pdl @@ -146,8 +146,7 @@ document: include_stop_sequence: false show_result: true - lan: python - code: - - | + code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program from pdl.pdl_interpreter import process_prog @@ -155,10 +154,7 @@ document: from pdl.pdl_interpreter import empty_scope import re import yaml - s = """' - - get: PDL - - | - '""" + s = """'{{ PDL }}'""" pdl = s.split("```")[1] obj = yaml.safe_load(pdl) state = InterpreterState() diff --git a/examples/meta/arith.pdl b/examples/meta/arith.pdl index 582a58a4..cd39bf3d 100644 --- a/examples/meta/arith.pdl +++ b/examples/meta/arith.pdl @@ -123,8 +123,7 @@ document: show_result: false - lan: python - code: - - | + code: | from pdl import pdl_ast, pdl_interpreter from pdl.pdl_ast import Program from pdl.pdl_interpreter import process_prog @@ -132,10 +131,7 @@ document: from pdl.pdl_interpreter import empty_scope import re import yaml - s = """' - - get: PDL - - | - '""" + s = """'{{ PDL }}'""" pdl = s.split("```")[1] obj = yaml.safe_load(pdl) state = InterpreterState() diff --git a/pdl/pdl_interpreter.py b/pdl/pdl_interpreter.py index 2ff4d091..1e4cb189 100644 --- a/pdl/pdl_interpreter.py +++ b/pdl/pdl_interpreter.py @@ -642,11 +642,6 @@ def step_blocks( trace: BlocksType results = [] iteration_state = state - # match iteration_type: - # case IterationType.DOCUMENT: - # iteration_state = state - # case _: - # iteration_state = state.with_yield_output(False) if not isinstance(blocks, str) and isinstance(blocks, Sequence): background = [] trace = [] @@ -666,12 +661,6 @@ def step_blocks( ) results.append(block_result) result = combine_results(iteration_type, results) - # match iteration_type: - # case IterationType.DOCUMENT: - # pass - # case _: - # if state.yield_output: - # yield OutputMessage(stringify(result)) return result, background, scope, trace @@ -1000,7 +989,7 @@ def step_call_api( ]: background: Messages input_value, _, _, input_trace = yield from step_blocks( - IterationType.DOCUMENT, + IterationType.SEQUENCE, state.with_yield_output(False), scope, block.input, @@ -1034,7 +1023,7 @@ def step_call_code( ]: background: Messages code_s, _, _, code_trace = yield from step_blocks( - IterationType.DOCUMENT, + IterationType.SEQUENCE, state.with_yield_output(False), scope, block.code, diff --git a/tests/test_array.py b/tests/test_array.py index f70e2f9c..920337b9 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -33,7 +33,7 @@ def test_for_data(): "sequence": [ { "def": "I", - "document": [{"lan": "python", "code": ["result = 0"]}], + "document": [{"lan": "python", "code": "result = 0"}], "show_result": False, }, { diff --git a/tests/test_code.py b/tests/test_code.py index 80995fac..3aab98ef 100644 --- a/tests/test_code.py +++ b/tests/test_code.py @@ -8,7 +8,9 @@ "Hello, ", { "lan": "python", - "code": ["import random\n", "import string\n", "result = 'Tracy'"], + "code": { + "document": ["import random\n", "import string\n", "result = 'Tracy'"] + }, }, "!\n", ], diff --git a/tests/test_cond.py b/tests/test_cond.py index f3647f60..0475478d 100644 --- a/tests/test_cond.py +++ b/tests/test_cond.py @@ -77,7 +77,9 @@ { "def": "RESULT", "lan": "python", - "code": ["result = ", {"get": "EXPR"}], + "code": { + "document": ["result = ", {"get": "EXPR"}] + }, }, " >>", ], @@ -153,11 +155,13 @@ def cond_data1(show, name): "document": [ { "lan": "python", - "code": [ - "import random\n", - "import string\n", - "result = 'Tracy'", - ], + "code": { + "document": [ + "import random\n", + "import string\n", + "result = 'Tracy'", + ] + }, } ], "show_result": show, @@ -201,7 +205,7 @@ def test_cond2(): { "def": "I", "lan": "python", - "code": ["result = ", {"get": "I"}, " + 1"], + "code": "result = {{ I }} + 1", "show_result": True, }, "\n", @@ -244,7 +248,7 @@ def test_repeat_until(): { "def": "I", "lan": "python", - "code": ["result = ", {"get": "I"}, " + 1"], + "code": "result = {{ I }} + 1", "show_result": True, }, "\n", @@ -281,7 +285,7 @@ def test_repeat_until_array(): { "def": "I", "lan": "python", - "code": ["result = ", {"get": "I"}, " + 1"], + "code": ["result = {{ I }} + 1"], "show_result": True, }, "\n", @@ -334,7 +338,7 @@ def test_repeat_until_document(): "document": [ { "lan": "python", - "code": ["result = ", {"get": "I"}, " + 1"], + "code": ["result = {{ I }} + 1"], } ], "show_result": True, diff --git a/tests/test_errors.py b/tests/test_errors.py index 1f183209..f7d8845f 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -128,7 +128,9 @@ def test_error4(): "Hello, ", { "lans": "python", - "code": ["import random\n", "import string\n", "result = 'Tracy'"], + "code": { + "document": ["import random\n", "import string\n", "result = 'Tracy'"] + }, }, "!\n", ], @@ -151,7 +153,9 @@ def test_error5(): "Hello, ", { "lans": "python", - "codes": ["import random\n", "import string\n", "result = 'Tracy'"], + "codes": { + "document": ["import random\n", "import string\n", "result = 'Tracy'"] + }, }, "!\n", ], diff --git a/tests/test_parser.py b/tests/test_parser.py index c130f7a7..e1f0e64b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -76,7 +76,13 @@ def test_get_parser(): code_parser = { "lan": "python", "parser": "json", - "code": ["import json\n", "r = {'a':'b', 'c':'d'}\n", "result=json.dumps(r)"], + "code": { + "document": [ + "import json\n", + "r = {'a':'b', 'c':'d'}\n", + "result=json.dumps(r)", + ] + }, } @@ -90,7 +96,7 @@ def test_code_parser(): code_parser1 = { "lan": "python", - "code": ["r = {'a':'b', 'c':'d'}\n", "result=str(r)"], + "code": "r = {'a':'b', 'c':'d'}\nresult=str(r)", } diff --git a/tests/test_var.py b/tests/test_var.py index f4384149..398032d9 100644 --- a/tests/test_var.py +++ b/tests/test_var.py @@ -59,7 +59,7 @@ def test_var(): { "def": "I", "lan": "python", - "code": ["result = NAME[::-1] + '!\\n'"], + "code": "result = NAME[::-1] + '!\\n'", "show_result": False, }, {"get": "I"}, @@ -86,7 +86,7 @@ def test_code_shared_scope(): { "def": "I", "lan": "python", - "code": ["NAME = NAME[::-1]\n", "result = NAME"], + "code": {"document": ["NAME = NAME[::-1]\n", "result = NAME"]}, "show_result": False, }, {"get": "NAME"},