Skip to content

Commit 07c817a

Browse files
authored
Merge pull request #731 from parea-ai/PAI-993-expose-trace-log-info-via-api
feat: expose trace log via api
2 parents 83a67fe + 16c8d45 commit 07c817a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

parea/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from parea.cache.cache import Cache
1515
from parea.constants import PAREA_OS_ENV_EXPERIMENT_UUID
1616
from parea.experiment.datasets import create_test_cases, create_test_collection
17-
from parea.helpers import gen_trace_id, serialize_metadata_values
17+
from parea.helpers import gen_trace_id, serialize_metadata_values, structure_trace_log_from_api
1818
from parea.parea_logger import parea_logger
1919
from parea.schemas.models import (
2020
Completion,
@@ -29,6 +29,7 @@
2929
FinishExperimentRequestSchema,
3030
ProjectSchema,
3131
TestCaseCollection,
32+
TraceLog,
3233
UseDeployedPrompt,
3334
UseDeployedPromptResponse,
3435
)
@@ -48,6 +49,7 @@
4849
GET_COLLECTION_ENDPOINT = "/collection/{test_collection_identifier}"
4950
CREATE_COLLECTION_ENDPOINT = "/collection"
5051
ADD_TEST_CASES_ENDPOINT = "/testcases"
52+
GET_TRACE_LOG_ENDPOINT = "/trace_log/{trace_id}"
5153

5254

5355
@define
@@ -336,6 +338,14 @@ def _update_data_and_trace(self, data: Completion) -> Completion:
336338

337339
return data
338340

341+
def get_trace_log(self, trace_id: str) -> TraceLog:
342+
response = self._client.request("GET", GET_TRACE_LOG_ENDPOINT.format(trace_id=trace_id))
343+
return structure_trace_log_from_api(response.json())
344+
345+
async def aget_trace_log(self, trace_id: str) -> TraceLog:
346+
response = await self._client.request_async("GET", GET_TRACE_LOG_ENDPOINT.format(trace_id=trace_id))
347+
return structure_trace_log_from_api(response.json())
348+
339349

340350
_initialized_parea_wrapper = False
341351

parea/helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytz
1010
from attr import asdict, fields_dict
11+
from cattrs import GenConverter
1112

1213
from parea.constants import ADJECTIVES, NOUNS
1314
from parea.schemas.models import Completion, TraceLog, UpdateLog
@@ -78,3 +79,17 @@ def serialize_values(metadata: Dict[str, Any]) -> Dict[str, str]:
7879

7980
def timezone_aware_now() -> datetime:
8081
return datetime.now(pytz.utc)
82+
83+
84+
def structure_trace_log_from_api(d: dict) -> TraceLog:
85+
def structure_union_type(obj: Any, cl: type) -> Any:
86+
if isinstance(obj, str):
87+
return obj
88+
elif isinstance(obj, dict):
89+
return obj
90+
else:
91+
return None
92+
93+
converter = GenConverter()
94+
converter.register_structure_hook(Union[str, Dict[str, str], None], structure_union_type)
95+
return converter.structure(d, TraceLog)

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.128"
9+
version = "0.2.129"
1010
description = "Parea python sdk"
1111
readme = "README.md"
1212
authors = ["joel-parea-ai <[email protected]>"]

0 commit comments

Comments
 (0)