Skip to content

Commit 7ce30a3

Browse files
authored
Update command line arguments (#22)
* Add options --(no-)stream-result and --(no-)-stream-background * Use the option --trace to directly provide the name of the trace file and use *_trace.json as default trace file name * Change `--data_file` into `--data-file`
1 parent 125406e commit 7ce30a3

File tree

7 files changed

+59
-44
lines changed

7 files changed

+59
-44
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ pdl-live/package-lock.json
147147

148148

149149
# PDL trace
150-
*_result.json
151-
*_result.yaml
150+
*_trace.json
152151

153152
# Built docs
154153
_site

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ pdl --data <JSON-or-YAML-data> <my-example>
7777
This can also be done by passing a JSON or YAML file:
7878

7979
```
80-
pdl --data_file <JSON-or-YAML-file> <my-example>
80+
pdl --data-file <JSON-or-YAML-file> <my-example>
8181
```
8282

8383
The interpreter can also output a trace file that is used by the Live Document visualization tool (see [Live Document](#live_document)):
8484

8585
```
86-
pdl --trace <json | yaml> <my-example>
86+
pdl <my-example> --trace
8787
```
8888

8989
For more information:
@@ -356,13 +356,13 @@ See [PDL Language Tutorial](https://ibm.github.io/prompt-declaration-language/tu
356356
## Live Document Visualizer
357357

358358
PDL has a Live Document visualizer to help in program understanding given an execution trace.
359-
To produce an execution trace consumable by the Live Document, you can run the interpreter with the `--trace` argument and set the value to either `json` or `yaml`:
359+
To produce an execution trace consumable by the Live Document, you can run the interpreter with the `--trace` argument:
360360

361361
```
362-
pdl --trace <json | yaml> <my-example>
362+
pdl <my-example> --trace
363363
```
364364

365-
This produces an additional file named `my-example_result.json` or `my-example_result.yaml` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
365+
This produces an additional file named `my-example_trace.json` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
366366
in the right pane.
367367

368368
This is similar to a spreadsheet for tabular data, where data is in the forefront and the user can inspect the formula that generates the data in each cell. In the Live Document, cells are not uniform but can take arbitrary extents. Clicking on them similarly reveals the part of the code that produced them.

docs/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ python3 -m pdl.pdl --data <JSON-or-YAML-data> <my-example>
7979
This can also be done by passing a JSON or YAML file:
8080

8181
```
82-
python3 -m pdl.pdl --data_file <JSON-or-YAML-file> <my-example>
82+
python3 -m pdl.pdl --data-file <JSON-or-YAML-file> <my-example>
8383
```
8484

8585
## Overview
@@ -345,10 +345,10 @@ PDL has a Live Document visualizer to help in program understanding given an exe
345345
To produce an execution trace consumable by the Live Document, you can run the interpreter with the `--trace` argument and set the value to either `json` or `yaml`:
346346

347347
```
348-
python -m pdl.pdl --trace <json | yaml> <my-example>
348+
python -m pdl.pdl <my-example> --trace
349349
```
350350

351-
This produces an additional file named `my-example_result.json` or `my-example_result.yaml` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
351+
This produces an additional file named `my-example_trace.json` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
352352
in the right pane.
353353

354354
This is similar to a spreadsheet for tabular data, where data is in the forefront and the user can inspect the formula that generates the data in each cell. In the Live Document, cells are not uniform but can take arbitrary extents. Clicking on them similarly reveals the part of the code that produced them.

docs/tutorial.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,10 @@ PDL has a Live Document visualizer to help in program understanding given an exe
12831283
To produce an execution trace consumable by the Live Document, you can run the interpreter with the `--trace` argument and set the value to either `json` or `yaml`:
12841284
12851285
```
1286-
python -m pdl.pdl --trace <json | yaml> <my-example>
1286+
python -m pdl.pdl <my-example> --trace
12871287
```
12881288
1289-
This produces an additional file named `my-example_result.json` or `my-example_result.yaml` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
1289+
This produces an additional file named `my-example_trace.json` that can be uploaded to the [Live Document](https://ibm.github.io/prompt-declaration-language/viewer/) visualizer tool. Clicking on different parts of the Live Document will show the PDL code that produced that part
12901290
in the right pane.
12911291
12921292
This is similar to a spreadsheet for tabular data, where data is in the forefront and the user can inspect the formula that generates the data in each cell. In the Live Document, cells are not uniform but can take arbitrary extents. Clicking on them similarly reveals the part of the code that produced them.

pdl/pdl.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import json
3+
from pathlib import Path
34
from typing import Any, Optional, TypedDict
45

56
import yaml
@@ -140,7 +141,8 @@ def main():
140141

141142
parser.add_argument(
142143
"-f",
143-
"--data_file",
144+
"--data-file",
145+
dest="data_file",
144146
help="initial scope data file",
145147
)
146148

@@ -150,12 +152,29 @@ def main():
150152
help="scope data",
151153
)
152154

153-
parser.add_argument("-o", "--output", help="output file")
154155
parser.add_argument(
155-
"-t", "--trace", help="output trace for live document", choices=["json", "yaml"]
156+
"--stream-result",
157+
dest="stream_result",
158+
action=argparse.BooleanOptionalAction,
159+
default=True,
160+
help="stream the result on the standard output instead of printing it at the end of the execution",
161+
)
162+
163+
parser.add_argument(
164+
"--stream-background",
165+
dest="stream_background",
166+
action=argparse.BooleanOptionalAction,
167+
default=False,
168+
help="stream the background messages on the standard output",
169+
)
170+
171+
parser.add_argument(
172+
"-t",
173+
"--trace",
174+
nargs="?",
175+
const="*_trace.json",
176+
help="output trace for live document",
156177
)
157-
parser.add_argument("--json", help="json file")
158-
parser.add_argument("--yaml", help="yaml file")
159178
parser.add_argument("pdl", nargs="?", help="pdl file", type=str)
160179

161180
args = parser.parse_args()
@@ -180,12 +199,19 @@ def main():
180199
if args.data is not None:
181200
initial_scope = initial_scope | yaml.safe_load(args.data)
182201

202+
config = InterpreterConfig(
203+
yield_result=args.stream_result, yield_background=args.stream_background
204+
)
205+
if args.trace == "*_trace.json":
206+
trace_file = str(Path(args.pdl).with_suffix("")) + "_trace.json"
207+
else:
208+
trace_file = args.trace
183209
pdl_interpreter.generate(
184210
args.pdl,
185211
args.log,
212+
InterpreterState(**config),
186213
initial_scope,
187-
args.trace,
188-
args.output,
214+
trace_file,
189215
)
190216

191217

pdl/pdl_interpreter.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import re
33
import types
44
from itertools import batched
5-
from pathlib import Path
6-
from typing import Any, Generator, Iterable, Literal, Optional, Sequence
5+
from typing import Any, Generator, Iterable, Optional, Sequence
76

87
import requests
98
import yaml
@@ -55,7 +54,7 @@
5554
empty_block_location,
5655
)
5756
from .pdl_ast_utils import iter_block_children, iter_blocks
58-
from .pdl_dumper import block_to_dict, dump_yaml
57+
from .pdl_dumper import block_to_dict
5958
from .pdl_llms import BamModel, LitellmModel, WatsonxModel
6059
from .pdl_location_utils import append, get_loc_string
6160
from .pdl_parser import PDLParseError, parse_file
@@ -99,24 +98,25 @@ def with_role(self: "InterpreterState", role: RoleType) -> "InterpreterState":
9998
def generate(
10099
pdl_file: str,
101100
log_file: Optional[str],
101+
state: Optional[InterpreterState],
102102
initial_scope: ScopeType,
103-
output_trace: Optional[Literal["json", "yaml"]],
104-
output_file: Optional[str],
103+
trace_file: Optional[str],
105104
):
106105
"""Execute the PDL program defined in `pdl_file`.
107106
108107
Args:
109108
pdl_file: Program to execute.
110109
log_file: File where the log is written. If `None`, use `log.txt`.
111110
initial_scope: Environment defining the variables in scope to execute the program.
112-
output_trace: Format in which the execution trace must be produced.
113-
output_file: File to save the execution trace.
111+
state: Initial state of the interpreter.
112+
trace_file: Indicate if the execution trace must be produced and the file to save it.
114113
"""
115114
if log_file is None:
116115
log_file = "log.txt"
117116
try:
118117
prog, loc = parse_file(pdl_file)
119-
state = InterpreterState()
118+
if state is None:
119+
state = InterpreterState()
120120
result, _, _, trace = process_prog(state, initial_scope, prog, loc)
121121
if not state.yield_result:
122122
if state.yield_background:
@@ -125,34 +125,24 @@ def generate(
125125
with open(log_file, "w", encoding="utf-8") as log_fp:
126126
for line in state.log:
127127
log_fp.write(line)
128-
if output_trace is not None and trace is not None:
129-
write_trace(pdl_file, output_trace, output_file, trace)
128+
if trace_file:
129+
write_trace(trace_file, trace)
130130
except PDLParseError as e:
131131
print("\n".join(e.msg))
132132

133133

134134
def write_trace(
135-
pdl_file: str,
136-
mode: Literal["json", "yaml"],
137-
output_file: Optional[str],
135+
trace_file: str,
138136
trace: BlockType,
139137
):
140138
"""Write the execution trace into a file.
141139
142140
Args:
143-
pdl_file: Name of the PDL program executed.
144-
mode: Format in which the execution trace must be produced.
145-
output_file: File to save the execution trace.
141+
trace_file: File to save the execution trace.
146142
trace: Execution trace.
147143
"""
148-
if output_file is None:
149-
output_file = str(Path(pdl_file).with_suffix("")) + f"_result.{mode}"
150-
with open(output_file, "w", encoding="utf-8") as fp:
151-
match mode:
152-
case "json":
153-
json.dump(block_to_dict(trace), fp)
154-
case "yaml":
155-
dump_yaml(block_to_dict(trace), stream=fp)
144+
with open(trace_file, "w", encoding="utf-8") as fp:
145+
json.dump(block_to_dict(trace), fp)
156146

157147

158148
def process_prog(

tests/test_line_table.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def do_test(t, capsys):
5-
generate(t["file"], None, {}, None, None)
5+
generate(t["file"], None, None, {}, None)
66
captured = capsys.readouterr()
77
output = captured.out.split("\n")
88
print(output)

0 commit comments

Comments
 (0)