Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Changed output to be only background context #375

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/pdl/pdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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":
Expand Down
12 changes: 2 additions & 10 deletions src/pdl/pdl_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 21 additions & 1 deletion src/pdl/pdl_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -62,6 +76,7 @@ def yield_result(result: Any, kind: BlockKind) -> None:


_LAST_ROLE = None
ROLE_COLOR = "blue"


def yield_background(background) -> None:
Expand All @@ -72,5 +87,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'], ROLE_COLOR)}: {colored(msg['content'], color_of_role(msg['role']))}"
for msg in background
]
)
print(s, end="", flush=True)