From 1057fe8a5221ea39f8112f8ad925df6d85882b1a Mon Sep 17 00:00:00 2001 From: Mandana Vaziri Date: Wed, 5 Feb 2025 17:31:49 -0500 Subject: [PATCH 1/3] Changed output to be only background context Signed-off-by: Mandana Vaziri --- src/pdl/pdl.py | 11 ++++------- src/pdl/pdl_interpreter.py | 12 ++---------- src/pdl/pdl_scheduler.py | 21 ++++++++++++++++++++- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/pdl/pdl.py b/src/pdl/pdl.py index 7745ca38..845741a5 100644 --- a/src/pdl/pdl.py +++ b/src/pdl/pdl.py @@ -174,9 +174,9 @@ def main(): ) parser.add_argument( "--stream", - choices=["result", "background", "none"], - default="result", - help="stream the result, the background messages, or nothing on the standard output", + choices=["context", "none"], + default="context", + help="stream the background context, or nothing on the standard output", ) parser.add_argument( "-t", @@ -244,10 +244,7 @@ def main(): validate_scope(initial_scope) match args.stream: - case "result": - stream_result = True - stream_background = False - case "background": + case "context": stream_result = False stream_background = True case "none": diff --git a/src/pdl/pdl_interpreter.py b/src/pdl/pdl_interpreter.py index 47a08e51..651c3fec 100644 --- a/src/pdl/pdl_interpreter.py +++ b/src/pdl/pdl_interpreter.py @@ -179,16 +179,8 @@ def generate( prog, loc = parse_file(pdl_file) if state is None: state = InterpreterState(cwd=Path(pdl_file).parent) - result, _, _, trace = process_prog(state, initial_scope, prog, loc) - if not state.yield_result: - if state.yield_background: - print("\n----------------") - if result is None: - print() - else: - print(stringify(result)) - else: - print() + _, _, _, trace = process_prog(state, initial_scope, prog, loc) + print() if trace_file: write_trace(trace_file, trace) except PDLParseError as exc: diff --git a/src/pdl/pdl_scheduler.py b/src/pdl/pdl_scheduler.py index af59a2f6..357a701d 100644 --- a/src/pdl/pdl_scheduler.py +++ b/src/pdl/pdl_scheduler.py @@ -53,6 +53,20 @@ def color_of(kind: BlockKind): return color +def color_of_role(role: str): + color: Optional[Color] = None + match role: + case "assistant": + color = "green" + case "user": + color = None + case "system": + color = "cyan" + case "available_tools": + color = "magenta" + return color + + def yield_result(result: Any, kind: BlockKind) -> None: if color_of(kind) is None: text = stringify(result) @@ -72,5 +86,10 @@ def yield_background(background) -> None: background = background[1:] else: s = "\n" - s += "\n".join([f"{msg['role']}: {msg['content']}" for msg in background]) + s += "\n".join( + [ + f"{colored(msg['role'], "blue")}: {colored(msg['content'], color_of_role(msg['role']))}" + for msg in background + ] + ) print(s, end="", flush=True) From ad28fcd9ae35c3ba382a2365763f80b815cfbb8c Mon Sep 17 00:00:00 2001 From: Mandana Vaziri Date: Wed, 5 Feb 2025 19:02:08 -0500 Subject: [PATCH 2/3] fix for python 3.11 Signed-off-by: Mandana Vaziri --- src/pdl/pdl_scheduler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pdl/pdl_scheduler.py b/src/pdl/pdl_scheduler.py index 357a701d..13220e73 100644 --- a/src/pdl/pdl_scheduler.py +++ b/src/pdl/pdl_scheduler.py @@ -76,7 +76,7 @@ def yield_result(result: Any, kind: BlockKind) -> None: _LAST_ROLE = None - +ROLE_COLOR = "blue" def yield_background(background) -> None: global _LAST_ROLE # pylint: disable= global-statement @@ -88,7 +88,7 @@ def yield_background(background) -> None: s = "\n" s += "\n".join( [ - f"{colored(msg['role'], "blue")}: {colored(msg['content'], color_of_role(msg['role']))}" + f"{colored(msg['role'], ROLE_COLOR)}: {colored(msg['content'], color_of_role(msg['role']))}" for msg in background ] ) From 11a7bd6b54ca1186821c40e4bcbbff7953d0c57a Mon Sep 17 00:00:00 2001 From: Mandana Vaziri Date: Wed, 5 Feb 2025 19:18:03 -0500 Subject: [PATCH 3/3] cleanup Signed-off-by: Mandana Vaziri --- src/pdl/pdl_scheduler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pdl/pdl_scheduler.py b/src/pdl/pdl_scheduler.py index 13220e73..9f47a48d 100644 --- a/src/pdl/pdl_scheduler.py +++ b/src/pdl/pdl_scheduler.py @@ -78,6 +78,7 @@ def yield_result(result: Any, kind: BlockKind) -> None: _LAST_ROLE = None ROLE_COLOR = "blue" + def yield_background(background) -> None: global _LAST_ROLE # pylint: disable= global-statement if len(background) > 0 and background[0]["role"] == _LAST_ROLE: