Skip to content

Commit ee6c556

Browse files
authored
use logging, log in sdk (#122)
Signed-off-by: Mandana Vaziri <[email protected]>
1 parent 947ff4a commit ee6c556

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

examples/sdk/hello.pdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
text:
22
- Hello,
3-
- model: watsonx/ibm/granite-20b-code-instruct
3+
- model: watsonx/ibm/granite-34b-code-instruct
44
parameters:
55
stop:
66
- '!'
77
include_stop_sequence: true
8-
- "\n"
8+

examples/sdk/hello_dict.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
"text": [
55
"Hello,",
66
{
7-
"model": "watsonx/ibm/granite-20b-code-instruct",
7+
"model": "watsonx/ibm/granite-34b-code-instruct",
88
"parameters": {"stop": ["!"], "include_stop_sequence": True},
99
},
10-
"\n",
1110
]
1211
}
1312

examples/sdk/hello_prog.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
text=[
77
"Hello,",
88
LitellmModelBlock(
9-
model="watsonx/ibm/granite-20b-code-instruct",
9+
model="watsonx/ibm/granite-34b-code-instruct",
1010
parameters=LitellmParameters(
1111
stop=["!"], include_stop_sequence=True # pyright: ignore
1212
),
1313
),
14-
"\n",
1514
]
1615
)
1716
)

examples/sdk/hello_str.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
HELLO = """
44
text:
55
- Hello,
6-
- model: watsonx/ibm/granite-20b-code-instruct
6+
- model: watsonx/ibm/granite-34b-code-instruct
77
parameters:
88
stop:
99
- '!'
1010
include_stop_sequence: true
11-
- "\n"
1211
"""
1312

1413

src/pdl/pdl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import json
3+
import logging
34
import os
45
import subprocess
56
import sys
@@ -23,6 +24,8 @@
2324
from .pdl_interpreter import InterpreterState, process_prog
2425
from .pdl_parser import parse_file, parse_str
2526

27+
logger = logging.getLogger(__name__)
28+
2629

2730
class InterpreterConfig(TypedDict, total=False):
2831
"""Configuration parameters of the PDL interpreter."""
@@ -62,6 +65,7 @@ def exec_program(
6265
Returns:
6366
Return the final result if `output` is set to `"result"`. If set of `all`, it returns a dictionary containing, `result`, `scope`, and `trace`.
6467
"""
68+
logging.basicConfig(filename="log.txt", encoding="utf-8", format="", filemode="w")
6569
config = config or {}
6670
state = InterpreterState(**config)
6771
scope = scope or {}

src/pdl/pdl_interpreter.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import re
34
import sys
45
import types
@@ -78,6 +79,8 @@
7879
from .pdl_schema_validator import type_check_args, type_check_spec
7980
from .pdl_utils import messages_concat, messages_to_str, stringify
8081

82+
logger = logging.getLogger(__name__)
83+
8184

8285
class PDLRuntimeError(PDLException):
8386
def __init__(
@@ -123,7 +126,6 @@ def __init__(
123126
class InterpreterState(BaseModel):
124127
yield_result: bool = False
125128
yield_background: bool = False
126-
log: list[str] = []
127129
batch: int = 1
128130
# batch=0: streaming
129131
# batch=1: call to generate with `input`
@@ -158,6 +160,7 @@ def generate(
158160
"""
159161
if log_file is None:
160162
log_file = "log.txt"
163+
logging.basicConfig(filename=log_file, encoding="utf-8", format="", filemode="w")
161164
try:
162165
prog, loc = parse_file(pdl_file)
163166
if state is None:
@@ -184,11 +187,6 @@ def generate(
184187
print(message, file=sys.stderr)
185188
if trace_file and exc.trace is not None:
186189
write_trace(trace_file, exc.trace)
187-
finally:
188-
if state is not None:
189-
with open(log_file, "w", encoding="utf-8") as log_fp:
190-
for line in state.log:
191-
log_fp.write(line)
192190

193191

194192
def write_trace(
@@ -288,7 +286,7 @@ def step_block(
288286
yield YieldBackgroundMessage(background)
289287
if state.yield_result:
290288
yield YieldResultMessage(result)
291-
append_log(state, "Document", background)
289+
append_log(state, "Context", background)
292290
else:
293291
result, background, scope, trace = yield from step_advanced_block(
294292
state, scope, block, loc
@@ -1446,5 +1444,5 @@ def get_var(var: str, scope: ScopeType, loc: LocationType) -> Any:
14461444

14471445

14481446
def append_log(state: InterpreterState, title, somestring):
1449-
state.log.append("********** " + title + " **********\n")
1450-
state.log.append(str(somestring) + "\n")
1447+
logger.warning("********** %s **********", title)
1448+
logger.warning(str(somestring))

0 commit comments

Comments
 (0)