Skip to content

Commit

Permalink
Merge branch 'python'
Browse files Browse the repository at this point in the history
  • Loading branch information
sethjuarez committed Nov 13, 2024
2 parents 5bbdbee + 1f87dd1 commit ec91629
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion runtime/prompty/prompty/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def sanitize(key: str, value: Any) -> Any:
class Tracer:
_tracers: Dict[str, Callable[[str], Iterator[Callable[[str, Any], None]]]] = {}

SIGNATURE = "signature"
INPUTS = "inputs"
RESULT = "result"

@classmethod
def add(
cls, name: str, tracer: Callable[[str], Iterator[Callable[[str, Any], None]]]
Expand All @@ -40,11 +44,17 @@ def clear(cls) -> None:

@classmethod
@contextlib.contextmanager
def start(cls, name: str) -> Iterator[Callable[[str, Any], None]]:
def start(cls, name: str, attributes: Dict[str, Any] = None) -> Iterator[Callable[[str, Any], None]]:
with contextlib.ExitStack() as stack:
traces = [
stack.enter_context(tracer(name)) for tracer in cls._tracers.values()
]

if attributes:
for trace in traces:
for key, value in attributes.items():
trace(key, value)

yield lambda key, value: [
# normalize and sanitize any trace values
trace(key, sanitize(key, to_dict(value)))
Expand Down
12 changes: 12 additions & 0 deletions runtime/prompty/tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,15 @@ async def test_streaming_async():
if isinstance(result, AsyncIterator):
async for item in result:
print(item)

@trace
def test_tracing_attributes():
with Tracer.start("Test1", {Tracer.SIGNATURE: "test1", "two": 2}) as trace:
trace(Tracer.INPUTS, 3)
trace(Tracer.INPUTS, 4)
with Tracer.start("Test2", {"signature": "5", "six": 6}) as trace:
trace("inputs", 7)
trace(Tracer.RESULT, 8)
with Tracer.start("Test3", {"signature": "9", "ten": 10}) as trace:
trace("inputs", 11)
trace(Tracer.RESULT, 12)

0 comments on commit ec91629

Please sign in to comment.