Skip to content

utils

ImpactsOutput

Bases: BaseModel

Impacts output data model.

Attributes:

Name Type Description
energy Optional[Energy]

Total energy consumption

gwp Optional[GWP]

Total Global Warming Potential (GWP) impact

adpe Optional[ADPe]

Total Abiotic Depletion Potential for Elements (ADPe) impact

pe Optional[PE]

Total Primary Energy (PE) impact

usage Optional[Usage]

Impacts for the usage phase

embodied Optional[Embodied]

Impacts for the embodied phase

warnings Optional[list[WarningMessage]]

List of warnings

errors Optional[list[ErrorMessage]]

List of errors

llm_impacts(provider, model_name, output_token_count, request_latency, electricity_mix_zone='WOR')

High-level function to compute the impacts of an LLM generation request.

Parameters:

Name Type Description Default
provider str

Name of the provider.

required
model_name str

Name of the LLM used.

required
output_token_count int

Number of generated tokens.

required
request_latency float

Measured request latency in seconds.

required
electricity_mix_zone str

ISO 3166-1 alpha-3 code of the electricity mix zone (WOR by default).

'WOR'

Returns:

Type Description
ImpactsOutput

The impacts of an LLM generation request.

Source code in ecologits/tracers/utils.py
def llm_impacts(
    provider: str,
    model_name: str,
    output_token_count: int,
    request_latency: float,
    electricity_mix_zone: str = "WOR",
) -> ImpactsOutput:
    """
    High-level function to compute the impacts of an LLM generation request.

    Args:
        provider: Name of the provider.
        model_name: Name of the LLM used.
        output_token_count: Number of generated tokens.
        request_latency: Measured request latency in seconds.
        electricity_mix_zone: ISO 3166-1 alpha-3 code of the electricity mix zone (WOR by default).

    Returns:
        The impacts of an LLM generation request.
    """

    model = models.find_model(provider=provider, model_name=model_name)
    if model is None:
        error = ModelNotRegisteredError(message=f"Could not find model `{model_name}` for {provider} provider.")
        logger.warning_once(str(error))
        return ImpactsOutput(errors=[error])

    if isinstance(model.architecture.parameters, ParametersMoE):
        model_total_params = model.architecture.parameters.total
        model_active_params = model.architecture.parameters.active
    else:
        model_total_params = model.architecture.parameters
        model_active_params = model.architecture.parameters

    electricity_mix = electricity_mixes.find_electricity_mix(zone=electricity_mix_zone)
    if electricity_mix is None:
        error = ZoneNotRegisteredError(message=f"Could not find electricity mix for `{electricity_mix_zone}` zone.")
        logger.warning_once(str(error))
        return ImpactsOutput(errors=[error])

    if_electricity_mix_adpe=electricity_mix.adpe
    if_electricity_mix_pe=electricity_mix.pe
    if_electricity_mix_gwp=electricity_mix.gwp
    impacts = compute_llm_impacts(
        model_active_parameter_count=model_active_params,
        model_total_parameter_count=model_total_params,
        output_token_count=output_token_count,
        request_latency=request_latency,
        if_electricity_mix_adpe=if_electricity_mix_adpe,
        if_electricity_mix_pe=if_electricity_mix_pe,
        if_electricity_mix_gwp=if_electricity_mix_gwp,
    )
    impacts = ImpactsOutput.model_validate(impacts.model_dump())

    if model.has_warnings:
        for w in model.warnings:
            logger.warning_once(str(w))
            impacts.add_warning(w)

    return impacts