Skip to content

Commit f000e02

Browse files
authored
Merge pull request #761 from parea-ai/PAI-1056-capture-templated-messages-from-openai-wrapper
feat: capture inputs to auto-traced LLM calls
2 parents 025f85c + 1f5fcfd commit f000e02

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

parea/wrapper/wrapper.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ def _get_decorator(self, unwrapped_func: Callable, original_func: Callable):
7272
else:
7373
return self.sync_decorator(original_func)
7474

75-
def _init_trace(self) -> Tuple[str, datetime, contextvars.Token]:
75+
def _init_trace(self, kwargs) -> Tuple[str, datetime, contextvars.Token]:
7676
start_time = timezone_aware_now()
7777
trace_id = str(uuid4())
7878

7979
new_trace_context = trace_context.get() + [trace_id]
8080
token = trace_context.set(new_trace_context)
8181

82+
if template_inputs := kwargs.pop("template_inputs", None):
83+
for m in kwargs["messages"] or []:
84+
if isinstance(m, dict) and "content" in m:
85+
m["content"] = m["content"].format(**template_inputs)
86+
8287
if TURN_OFF_PAREA_LOGGING:
8388
return trace_id, start_time, token
8489
try:
@@ -93,7 +98,7 @@ def _init_trace(self) -> Tuple[str, datetime, contextvars.Token]:
9398
metadata=None,
9499
target=None,
95100
tags=None,
96-
inputs={},
101+
inputs=template_inputs,
97102
experiment_uuid=os.getenv(PAREA_OS_ENV_EXPERIMENT_UUID, None),
98103
)
99104

@@ -109,7 +114,7 @@ def _init_trace(self) -> Tuple[str, datetime, contextvars.Token]:
109114
def async_decorator(self, orig_func: Callable) -> Callable:
110115
@functools.wraps(orig_func)
111116
async def wrapper(*args, **kwargs):
112-
trace_id, start_time, context_token = self._init_trace()
117+
trace_id, start_time, context_token = self._init_trace(kwargs)
113118
response = None
114119
exception = None
115120
error = None
@@ -141,7 +146,7 @@ async def wrapper(*args, **kwargs):
141146
def sync_decorator(self, orig_func: Callable) -> Callable:
142147
@functools.wraps(orig_func)
143148
def wrapper(*args, **kwargs):
144-
trace_id, start_time, context_token = self._init_trace()
149+
trace_id, start_time, context_token = self._init_trace(kwargs)
145150
response = None
146151
error = None
147152
cache_hit = False

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api"
66
[tool.poetry]
77
name = "parea-ai"
88
packages = [{ include = "parea" }]
9-
version = "0.2.130"
9+
version = "0.2.131"
1010
description = "Parea python sdk"
1111
readme = "README.md"
1212
authors = ["joel-parea-ai <[email protected]>"]

0 commit comments

Comments
 (0)