Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/ai-providers/server-ai-langchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ provider = await LangChainProvider.create(config)
async def invoke():
return await provider.invoke_model(messages)

response = await config.tracker.track_metrics_of(
response = await config.tracker.track_metrics_of_async(
invoke,
lambda r: r.metrics
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,41 @@ def get_ai_metrics_from_response(response: Any) -> LDAIMetrics:
:return: LDAIMetrics with success status and token usage
"""
return LDAIMetrics(success=True, usage=get_ai_usage_from_response(response))


def get_tool_calls_from_response(response: Any) -> List[str]:
"""
Get tool call names from a LangChain provider response.

:param response: The response from the LangChain model
:return: List of tool names in order, or empty list if none
"""
names: List[str] = []
if hasattr(response, 'tool_calls') and response.tool_calls:
for tc in response.tool_calls:
n = tc.get('name')
if n:
names.append(str(n))
return names


def sum_token_usage_from_messages(messages: List[Any]) -> Optional[TokenUsage]:
"""
Sum token usage across LangChain messages using get_ai_usage_from_response per message.

:param messages: List of message objects (e.g. from a graph state)
:return: Aggregated TokenUsage, or None if no usage on any message
"""
in_sum = 0
out_sum = 0
total_sum = 0
for m in messages:
u = get_ai_usage_from_response(m)
if u is None:
continue
in_sum += u.input
out_sum += u.output
total_sum += u.total
if in_sum == 0 and out_sum == 0 and total_sum == 0:
return None
return TokenUsage(total=total_sum, input=in_sum, output=out_sum)
Comment thread
cursor[bot] marked this conversation as resolved.
6 changes: 3 additions & 3 deletions packages/sdk/server-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ async def main():
# Create LangChain model from configuration
llm = await LangChainProvider.create_langchain_model(ai_config)

# Use with tracking
response = await ai_config.tracker.track_metrics_of(
# Use with tracking (sync invoke)
response = ai_config.tracker.track_metrics_of(
lambda: llm.invoke(messages),
lambda result: LangChainProvider.get_ai_metrics_from_response(result)
)
Expand Down Expand Up @@ -190,7 +190,7 @@ async def main():
temperature=ai_config.model.get_parameter('temperature') if ai_config.model else 0.5,
)

result = await ai_config.tracker.track_metrics_of(
result = await ai_config.tracker.track_metrics_of_async(
call_custom_provider,
map_custom_provider_metrics
)
Expand Down
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 @@ -71,7 +71,7 @@ async def evaluate(
messages = self._construct_evaluation_messages(input_text, output_text)
assert self._evaluation_response_structure is not None

response = await self._ai_config_tracker.track_metrics_of(
response = await self._ai_config_tracker.track_metrics_of_async(
lambda: self._model_runner.invoke_structured_model(messages, self._evaluation_response_structure),
lambda result: result.metrics,
)
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 @@ -48,7 +48,7 @@ async def invoke(self, prompt: str) -> ModelResponse:
config_messages = self._ai_config.messages or []
all_messages = config_messages + self._messages

response = await self._tracker.track_metrics_of(
response = await self._tracker.track_metrics_of_async(
lambda: self._model_runner.invoke_model(all_messages),
lambda result: result.metrics,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def _with_fallback(
continue
result = fn(provider_factory)
if result is not None:
log.debug(f"Successfully created capability using provider '{provider_type}'")
log.debug(f"Successfully invoked create function with provider '{provider_type}'")
return result
except Exception as exc:
log.warning(f"Provider '{provider_type}' failed: {exc}")
Expand Down
Loading
Loading