forked from block/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: track cost and token usage in log file (block#80)
- Loading branch information
1 parent
7b00c04
commit 9dbc0d9
Showing
8 changed files
with
122 additions
and
11 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
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,19 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
_LOGGER_NAME = "goose" | ||
_LOGGER_FILE_NAME = "goose.log" | ||
|
||
|
||
def setup_logging(log_file_directory: Path, log_level: str = "INFO") -> None: | ||
logger = logging.getLogger(_LOGGER_NAME) | ||
logger.setLevel(getattr(logging, log_level)) | ||
log_file_directory.mkdir(parents=True, exist_ok=True) | ||
file_handler = logging.FileHandler(log_file_directory / _LOGGER_FILE_NAME) | ||
logger.addHandler(file_handler) | ||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
file_handler.setFormatter(formatter) | ||
|
||
|
||
def get_logger() -> logging.Logger: | ||
return logging.getLogger(_LOGGER_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
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
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
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 typing import Optional | ||
from exchange.providers.base import Usage | ||
|
||
PRICES = { | ||
"gpt-4o": (5.00, 15.00), | ||
"gpt-4o-2024-08-06": (2.50, 10.00), | ||
"gpt-4o-mini": (0.150, 0.600), | ||
"gpt-4o-mini-2024-07-18": (0.150, 0.600), | ||
"o1-preview": (15.00, 60.00), | ||
"o1-mini": (3.00, 12.00), | ||
"claude-3-5-sonnet-20240620": (3.00, 15.00), | ||
"anthropic.claude-3-5-sonnet-20240620-v1:0": (3.00, 15.00), | ||
"claude-3-opus-20240229": (15.00, 75.00), | ||
"anthropic.claude-3-opus-20240229-v1:0": (15.00, 75.00), | ||
"claude-3-haiku-20240307": (0.25, 1.25), | ||
"anthropic.claude-3-haiku-20240307-v1:0": (0.25, 1.25), | ||
} | ||
|
||
|
||
def _calculate_cost(model: str, token_usage: Usage) -> Optional[float]: | ||
model_name = model.lower() | ||
if model_name in PRICES: | ||
input_token_price, output_token_price = PRICES[model_name] | ||
return (input_token_price * token_usage.input_tokens + output_token_price * token_usage.output_tokens) / 1000000 | ||
return None | ||
|
||
|
||
def get_total_cost_message(token_usages: dict[str, Usage]) -> str: | ||
total_cost = 0 | ||
message = "" | ||
for model, token_usage in token_usages.items(): | ||
cost = _calculate_cost(model, token_usage) | ||
if cost is not None: | ||
message += f"Cost for model {model} {str(token_usage)}: ${cost:.2f}\n" | ||
total_cost += cost | ||
else: | ||
message += f"Cost for model {model} {str(token_usage)}: Not available\n" | ||
message += f"Total cost: ${total_cost:.2f}" | ||
return message |
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
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,38 @@ | ||
from goose.utils._cost_calculator import _calculate_cost, get_total_cost_message | ||
from exchange.providers.base import Usage | ||
|
||
|
||
def test_calculate_cost(): | ||
cost = _calculate_cost("gpt-4o", Usage(input_tokens=10000, output_tokens=600, total_tokens=10600)) | ||
assert cost == 0.059 | ||
|
||
|
||
def test_get_total_cost_message(): | ||
message = get_total_cost_message( | ||
{ | ||
"gpt-4o": Usage(input_tokens=10000, output_tokens=600, total_tokens=10600), | ||
"gpt-4o-mini": Usage(input_tokens=3000000, output_tokens=4000000, total_tokens=7000000), | ||
} | ||
) | ||
expected_message = ( | ||
"Cost for model gpt-4o Usage(input_tokens=10000, output_tokens=600, total_tokens=10600): $0.06\n" | ||
+ "Cost for model gpt-4o-mini Usage(input_tokens=3000000, output_tokens=4000000, total_tokens=7000000)" | ||
+ ": $2.85\nTotal cost: $2.91" | ||
) | ||
assert message == expected_message | ||
|
||
|
||
def test_get_total_cost_message_with_non_available_pricing(): | ||
message = get_total_cost_message( | ||
{ | ||
"non_pricing_model": Usage(input_tokens=10000, output_tokens=600, total_tokens=10600), | ||
"gpt-4o-mini": Usage(input_tokens=3000000, output_tokens=4000000, total_tokens=7000000), | ||
} | ||
) | ||
expected_message = ( | ||
"Cost for model non_pricing_model Usage(input_tokens=10000, output_tokens=600, total_tokens=10600): " | ||
+ "Not available\n" | ||
+ "Cost for model gpt-4o-mini Usage(input_tokens=3000000, output_tokens=4000000, total_tokens=7000000)" | ||
+ ": $2.85\nTotal cost: $2.85" | ||
) | ||
assert message == expected_message |