Skip to content

Add one-time use context manager to track impacts without provider patching #243

Description

@omkar-foss

Description

Hi! So right now Ecologits.init() patches global provider modules, works great when using Ecologits application-wide, but for one-time use cases, a context manager would be very useful.

Also, it's a problem in shared worker processes (e.g. some data pipelines), where say there are 20 tasks out of which only one needs to be tracked with Ecologits, but the other 19 also end up getting tracked. So here too the context manager would be very helpful to provide tracking specificity.

Could we may be have an additional way like this below?

from ecologits import EcoLogits
from openai import OpenAI

client = OpenAI(api_key="<OPENAI_API_KEY>")

# No patching, Ecologits tracks the request within this context manager and exits cleanly
with Ecologits.track_impacts(client) as ecologits_tracker:
  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: {ecologits_tracker.impacts.energy.value} kWh")
print(f"GHG emissions: {ecologits_tracker.impacts.gwp.value} kgCO2eq")

Only way right now seems to be to use the Ecologits API directly, but it'd be great to have this feature in here.

Let me know if this makes sense, or if there's some better way to do this, thanks :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions