_ecologits
EcoLogits
EcoLogits instrumentor to initialize function patching for each provider.
Examples:
EcoLogits initialization example with OpenAI.
from ecologits import EcoLogits
from openai import OpenAI
EcoLogits.init(providers=["openai"])
client = OpenAI(api_key="<OPENAI_API_KEY>")
response = client.chat.completions.create(
model="gpt-4o-mini",
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', opentelemetry_endpoint=None)
staticmethod
Initialization static method. Will attempt to initialize all providers by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
providers |
str | list[str] | None
|
list of providers to initialize (must select at least one provider). |
None
|
electricity_mix_zone |
str
|
ISO 3166-1 alpha-3 code of the electricity mix zone (WOR by default). |
'WOR'
|
opentelemetry_endpoint |
str | None
|
enable OpenTelemetry with the URL endpoint. |
None
|
Source code in ecologits/_ecologits.py
label(**labels)
staticmethod
Create OpenTelemetry labels. Can be used as a context manager or as a function decorator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**labels |
str
|
Key-value pairs of OpenTelemetry labels. |
{}
|
Returns:
Type | Description |
---|---|
OpenTelemetryLabels
|
OpenTelemetryLabels instance. |
Examples:
Context manager usage:
with EcoLogits.label(task="summarization"):
response = client.chat.completions.create(...)
# or in async mode
async with EcoLogits.label(task="summarization"):
response = await async_client.chat.completions.create(...)
Decorator usage:
@EcoLogits.label(task="summarization")
def text_summarization(text: str) -> str:
response = client.chat.completions.create(...)
...
# or in async mode
@EcoLogits.label(task="summarization")
async def text_summarization(text: str) -> str:
response = await async_client.chat.completions.create(...)
...