Skip to content

Commit

Permalink
Merge pull request #90 from AgentOps-AI/upgrade_pyproject
Browse files Browse the repository at this point in the history
upgrade version and run black
  • Loading branch information
areibman authored Nov 16, 2024
2 parents abf5e6d + 0bf6df9 commit 47916b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
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 = "[email protected]" },
{ name = "Alex Reibman", email = "[email protected]" },
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

0 comments on commit 47916b1

Please sign in to comment.