Source code for langcheck.metrics.eval_clients.eval_response
from __future__ import annotations
from dataclasses import dataclass
from typing import Generic, TypeVar
[docs]
@dataclass
class MetricTokenUsage:
"""
A dataclass that contains the token usage information for a metric.
Args:
input_token_count: The number of input tokens used.
output_token_count: The number of output tokens used.
input_token_cost: The cost of the input tokens.
output_token_cost: The cost of the output tokens.
"""
input_token_count: int | None = None
output_token_count: int | None = None
input_token_cost: float | None = None
output_token_cost: float | None = None
def __add__(self, other: MetricTokenUsage) -> MetricTokenUsage:
"""
Add two MetricTokenUsage objects together.
Args:
other: The MetricTokenUsage object to add to the current object.
Returns:
A new MetricTokenUsage object with the sum of the input and output token counts and costs.
"""
def add_or_keep(a, b):
if a is not None and b is not None:
return a + b
return a if a is not None else b
if other is None:
return self
return MetricTokenUsage(
add_or_keep(self.input_token_count, other.input_token_count),
add_or_keep(self.output_token_count, other.output_token_count),
add_or_keep(self.input_token_cost, other.input_token_cost),
add_or_keep(self.output_token_cost, other.output_token_cost),
)
T = TypeVar("T")