Skip to content

Commit

Permalink
feat: implement energy estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelrince committed Feb 26, 2024
1 parent 180665e commit 063f858
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 49 deletions.
1 change: 1 addition & 0 deletions genai_impact/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from client_wrapper import OpenAI
12 changes: 6 additions & 6 deletions genai_impact/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from genai_impact.gen_ai_impacts import GenAIImpacts
from genai_impact.client_wrapper import OpenAI

client = GenAIImpacts()
client = OpenAI()

response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[...] # Your messages here
messages=[
{"role": "user", "content": "Hello World!"}
]
)

impacts = client.get_impacts(response)
print(impacts)
# Outputs: Impacts(energy=42, energy_unit='kWh', gwp=15, gwp_unit='kgCO2eq')
print(response.impacts)
43 changes: 43 additions & 0 deletions genai_impact/client_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from dataclasses import asdict
from typing import Optional

from openai import OpenAI as _OpenAI
from wrapt import wrap_function_wrapper

from genai_impact.compute_impacts import MODEL_SIZES, compute_llm_impact


def chat_wrapper(wrapped, instance, args, kwargs):
response = wrapped(*args, **kwargs)
model_size = MODEL_SIZES.get(response.model)
output_tokens = response.usage.completion_tokens
impacts = compute_llm_impact(model_parameter_count=model_size, output_token_count=output_tokens)
response.impacts = asdict(impacts)
return response


class OpenAI:
def __init__(
self,
api_key: Optional[str] = None,
base_url: Optional[str] = None,
**kwargs
):
self.__client = _OpenAI(
api_key=api_key,
base_url=base_url,
**kwargs
)

wrap_function_wrapper(
"openai.resources.chat.completions",
"Completions.create",
chat_wrapper
)

def __getattr__(self, name):
"""
Redirect attribute access to the underlying openai client if the attribute
is not defined in this class.
"""
return getattr(self.__client, name)
39 changes: 39 additions & 0 deletions genai_impact/compute_impacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dataclasses import dataclass


ENERGY_PROFILE = 1.17e-4

MODEL_SIZES = {
"gpt-4-0125-preview": None,
"gpt-4-turbo-preview": None,
"gpt-4-1106-preview": None,
"gpt-4-vision-preview": None,
"gpt-4": 200,
"gpt-4-0314": 200,
"gpt-4-0613": 200,
"gpt-4-32k": 200,
"gpt-4-32k-0314": 200,
"gpt-4-32k-0613": 200,
"gpt-3.5-turbo": 20,
"gpt-3.5-turbo-16k": 20,
"gpt-3.5-turbo-0301": 20,
"gpt-3.5-turbo-0613": 20,
"gpt-3.5-turbo-1106": 20,
"gpt-3.5-turbo-0125": 20,
"gpt-3.5-turbo-16k-0613": 20,
}


@dataclass
class Impacts:
energy: float
energy_unit: str = 'Wh'


def compute_llm_impact(
model_parameter_count: float,
output_token_count: int,
) -> Impacts:
return Impacts(
energy=ENERGY_PROFILE * model_parameter_count * output_token_count
)
42 changes: 0 additions & 42 deletions genai_impact/gen_ai_impacts.py

This file was deleted.

81 changes: 80 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.8,<4"
openai = "^1.12.0"
wrapt = "^1.16.0"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 063f858

Please sign in to comment.