Skip to content

Commit

Permalink
Merge pull request #27 from oulianov/main
Browse files Browse the repository at this point in the history
Fix finetuned gpt3.5 price (Feb 2024)
  • Loading branch information
areibman authored Feb 6, 2024
2 parents aac8125 + f7f38f0 commit bfb4bcb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tokencost/costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
# https://github.com/anthropics/anthropic-tokenizer-typescript/blob/main/index.ts


def strip_ft_model_name(model: str) -> str:
"""
Finetuned models format: ft:gpt-3.5-turbo:my-org:custom_suffix:id
We only need the base model name to get cost info.
"""
if model.startswith("ft:gpt-3.5-turbo"):
model = "ft:gpt-3.5-turbo"
return model

def count_message_tokens(messages: List[Dict[str, str]], model: str) -> int:
"""
Return the total number of tokens in a prompt's messages.
Expand All @@ -25,6 +34,7 @@ def count_message_tokens(messages: List[Dict[str, str]], model: str) -> int:
Total number of tokens in message.
"""
model = model.lower()
model = strip_ft_model_name(model)
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
Expand Down Expand Up @@ -138,6 +148,12 @@ def calculate_prompt_cost(prompt: Union[List[dict], str], model: str) -> Decimal
Decimal('0.0000030')
"""
model = model.lower()
model = strip_ft_model_name(model)
if model not in TOKEN_COSTS:
raise KeyError(
f"""Model {model} is not implemented.
Double-check your spelling, or submit an issue/PR"""
)
if not isinstance(prompt, (list, str)):
raise TypeError(
f"""Prompt must be either a string or list of message objects.
Expand Down Expand Up @@ -169,6 +185,7 @@ def calculate_completion_cost(completion: str, model: str) -> Decimal:
>>> calculate_completion_cost(completion, "gpt-3.5-turbo")
Decimal('0.000014')
"""
model = strip_ft_model_name(model)
if model not in TOKEN_COSTS:
raise KeyError(
f"""Model {model} is not implemented.
Expand Down

0 comments on commit bfb4bcb

Please sign in to comment.