Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade version and run black #90

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tokencost = ["model_prices.json"]

[project]
name = "tokencost"
version = "0.1.16"
version = "0.1.17"
authors = [
{ name = "Trisha Pan", email = "trishaepan@gmail.com" },
{ name = "Alex Reibman", email = "areibman@gmail.com" },
Expand Down
24 changes: 15 additions & 9 deletions tokencost/costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@

def get_anthropic_token_count(messages: List[Dict[str, str]], model: str) -> int:
if not any(
supported_model in model for supported_model in [
"claude-3-5-sonnet", "claude-3-5-haiku", "claude-3-haiku", "claude-3-opus"
supported_model in model
for supported_model in [
"claude-3-5-sonnet",
"claude-3-5-haiku",
"claude-3-haiku",
"claude-3-opus",
]
):
raise ValueError(
f"{model} is not supported in token counting (beta) API. Use the `usage` property in the response for exact counts."
)
try:
return anthropic.Anthropic().beta.messages.count_tokens(
model=model,
messages=messages,
).input_tokens
return (
anthropic.Anthropic()
.beta.messages.count_tokens(
model=model,
messages=messages,
)
.input_tokens
)
except TypeError as e:
raise e
except Exception as e:
Expand Down Expand Up @@ -286,9 +294,7 @@ def calculate_all_costs_and_tokens(
)

if "claude-" in model:
logger.warning(
"Warning: Token counting is estimated for "
)
logger.warning("Warning: Token counting is estimated for ")
completion_list = [{"role": "assistant", "content": completion}]
# Anthropic appends some 13 additional tokens to the actual completion tokens
completion_tokens = count_message_tokens(completion_list, model) - 13
Expand Down
Loading