Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sdk/server-ai/src/ldai/judge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ async def evaluate(
assert self._evaluation_response_structure is not None

response = await tracker.track_metrics_of_async(
lambda: self._model_runner.invoke_structured_model(messages, self._evaluation_response_structure),
lambda result: result.metrics,
lambda: self._model_runner.invoke_structured_model(messages, self._evaluation_response_structure),
)

parsed = self._parse_evaluation_response(response.data)
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/server-ai/src/ldai/managed_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ async def run(self, input: str) -> AgentResult:
"""
tracker = self._ai_config.create_tracker()
return await tracker.track_metrics_of_async(
lambda: self._agent_runner.run(input),
lambda result: result.metrics,
lambda: self._agent_runner.run(input),
)

def get_agent_runner(self) -> AgentRunner:
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/server-ai/src/ldai/managed_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ async def invoke(self, prompt: str) -> ModelResponse:
all_messages = config_messages + self._messages

response = await tracker.track_metrics_of_async(
lambda: self._model_runner.invoke_model(all_messages),
lambda result: result.metrics,
lambda: self._model_runner.invoke_model(all_messages),
)

if (
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/server-ai/src/ldai/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def _track_from_metrics_extractor(

def track_metrics_of(
self,
func: Callable[[], Any],
metrics_extractor: Callable[[Any], Any],
func: Callable[[], Any],
) -> Any:
"""
Track metrics for a synchronous AI operation.
Expand All @@ -277,8 +277,8 @@ def track_metrics_of(

For async operations, use :meth:`track_metrics_of_async`.

:param func: Synchronous callable that runs the operation
:param metrics_extractor: Function that extracts LDAIMetrics from the operation result
:param func: Synchronous callable that runs the operation
:return: The result of the operation
"""
start_ns = time.perf_counter_ns()
Expand All @@ -294,14 +294,14 @@ def track_metrics_of(
self.track_duration(duration)
return self._track_from_metrics_extractor(result, metrics_extractor)

async def track_metrics_of_async(self, func, metrics_extractor):
async def track_metrics_of_async(self, metrics_extractor, func):
"""
Track metrics for an async AI operation (``func`` is awaited).

Same event semantics as :meth:`track_metrics_of`.

:param func: Async callable or zero-arg callable that returns an awaitable when called
:param metrics_extractor: Function that extracts LDAIMetrics from the operation result
:param func: Async callable or zero-arg callable that returns an awaitable when called
:return: The result of the operation
"""
start_ns = time.perf_counter_ns()
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/server-ai/tests/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def fn():
def extract(r):
return LDAIMetrics(success=True, usage=TokenUsage(5, 2, 3))

out = tracker.track_metrics_of(fn, extract)
out = tracker.track_metrics_of(extract, fn)
assert out == "done"
calls = client.track.mock_calls # type: ignore
assert any(c.args[0] == "$ld:ai:generation:success" for c in calls)
Expand All @@ -551,7 +551,7 @@ async def fn():
def extract(r):
return LDAIMetrics(success=True, usage=TokenUsage(5, 2, 3))

await tracker.track_metrics_of_async(fn, extract)
await tracker.track_metrics_of_async(extract, fn)
gk_td = {**_base_td(), "graphKey": "gg"}
calls = client.track.mock_calls # type: ignore
assert any(
Expand Down
Loading