|
93 | 93 | "openai/o1-preview": {"prompt": 0.015, "completion": 0.06}, |
94 | 94 | "openai/o1-mini": {"prompt": 0.003, "completion": 0.012}, |
95 | 95 | "anthropic/claude-3-opus": {"prompt": 0.015, "completion": 0.075}, |
| 96 | + "anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015}, |
| 97 | + "us.anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015}, |
96 | 98 | "anthropic/claude-3.7-sonnet": {"prompt": 0.003, "completion": 0.015}, |
97 | 99 | "anthropic/claude-3.7-sonnet:beta": {"prompt": 0.003, "completion": 0.015}, |
98 | 100 | "anthropic/claude-3.7-sonnet:thinking": {"prompt": 0.003, "completion": 0.015}, |
|
371 | 373 | "anthropic.claude-3-haiku-20240307-v1:0:200k": {"prompt": 0.00025, "completion": 0.00125}, |
372 | 374 | # currently (2024-4-29) only available at US West (Oregon) AWS Region. |
373 | 375 | "anthropic.claude-3-opus-20240229-v1:0": {"prompt": 0.015, "completion": 0.075}, |
| 376 | + "anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015}, |
| 377 | + "us.anthropic.claude-3-5-sonnet-20241022-v2:0": {"prompt": 0.003, "completion": 0.015}, |
| 378 | + "anthropic.claude-3-7-sonnet-20250219-v1:0": {"prompt": 0.003, "completion": 0.015}, |
| 379 | + "us.anthropic.claude-3-7-sonnet-20250219-v1:0": {"prompt": 0.003, "completion": 0.015}, |
374 | 380 | "cohere.command-text-v14": {"prompt": 0.0015, "completion": 0.0015}, |
375 | 381 | "cohere.command-text-v14:7:4k": {"prompt": 0.0015, "completion": 0.0015}, |
376 | 382 | "cohere.command-light-text-v14": {"prompt": 0.0003, "completion": 0.0003}, |
|
403 | 409 | } |
404 | 410 |
|
405 | 411 |
|
| 412 | +def count_claude_message_tokens(messages: list[dict], model: str) -> int: |
| 413 | + # rough estimation for models newer than claude-2.1, needs api_key or auth_token |
| 414 | + ac = anthropic.Client() |
| 415 | + system_prompt = "" |
| 416 | + new_messages = [] |
| 417 | + for msg in messages: |
| 418 | + if msg.get("role") == "system": |
| 419 | + system_prompt = msg.get("content") |
| 420 | + else: |
| 421 | + new_messages.append(msg) |
| 422 | + num_tokens = ac.beta.messages.count_tokens(messages=new_messages, model=model, system=system_prompt) |
| 423 | + return num_tokens.input_tokens |
| 424 | + |
| 425 | + |
406 | 426 | def count_message_tokens(messages, model="gpt-3.5-turbo-0125"): |
407 | 427 | """Return the number of tokens used by a list of messages.""" |
408 | 428 | if "claude" in model: |
409 | | - # rough estimation for models newer than claude-2.1 |
410 | | - vo = anthropic.Client() |
411 | | - num_tokens = 0 |
412 | | - for message in messages: |
413 | | - for key, value in message.items(): |
414 | | - num_tokens += vo.count_tokens(str(value)) |
| 429 | + num_tokens = count_claude_message_tokens(messages, model) |
415 | 430 | return num_tokens |
416 | 431 | try: |
417 | 432 | encoding = tiktoken.encoding_for_model(model) |
@@ -500,8 +515,8 @@ def count_output_tokens(string: str, model: str) -> int: |
500 | 515 | int: The number of tokens in the text string. |
501 | 516 | """ |
502 | 517 | if "claude" in model: |
503 | | - vo = anthropic.Client() |
504 | | - num_tokens = vo.count_tokens(string) |
| 518 | + messages = [{"role": "assistant", "content": string}] |
| 519 | + num_tokens = count_claude_message_tokens(messages, model) |
505 | 520 | return num_tokens |
506 | 521 | try: |
507 | 522 | encoding = tiktoken.encoding_for_model(model) |
|
0 commit comments