generated from dataforgoodfr/d4g-project-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
180665e
commit 063f858
Showing
7 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from client_wrapper import OpenAI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters