Skip to content

_ecologits

EcoLogits

EcoLogits instrumentor to initialize function patching for each provider.

By default, the initialization will be done on all available and compatible providers that are supported by the library.

Examples:

EcoLogits initialization example with OpenAI.

from ecologits import EcoLogits
from openai import OpenAI

EcoLogits.init()

client = OpenAI(api_key="<OPENAI_API_KEY>")
response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "user", "content": "Tell me a funny joke!"}
    ]
)

# Get estimated environmental impacts of the inference
print(f"Energy consumption: {response.impacts.energy.value} kWh")
print(f"GHG emissions: {response.impacts.gwp.value} kgCO2eq")

init(providers=None, electricity_mix_zone='WOR') staticmethod

Initialization static method. Will attempt to initialize all providers by default.

Parameters:

Name Type Description Default
providers Optional[Union[str, list[str]]]

list of providers to initialize (all providers by default).

None
electricity_mix_zone Optional[str]

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

'WOR'
Source code in ecologits/_ecologits.py
@staticmethod
def init(
    providers: Optional[Union[str, list[str]]] = None,
    electricity_mix_zone: Optional[str] = "WOR",
) -> None:
    """
    Initialization static method. Will attempt to initialize all providers by default.

    Args:
        providers: list of providers to initialize (all providers by default).
        electricity_mix_zone: ISO 3166-1 alpha-3 code of the electricity mix zone (WOR by default).
    """
    if isinstance(providers, str):
        providers = [providers]
    if providers is None:
        providers = list(_INSTRUMENTS.keys())

    init_instruments(providers)

    EcoLogits.config.electricity_mix_zone = electricity_mix_zone
    EcoLogits.config.providers += providers
    EcoLogits.config.providers = list(set(EcoLogits.config.providers))