Skip to content

Commit f8c44b1

Browse files
authored
Merge pull request #1765 from better629/main
update bedrock claude usage
2 parents 79390a2 + 1b8df6f commit f8c44b1

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

metagpt/provider/bedrock/base_provider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
from abc import ABC, abstractmethod
3-
from typing import Union
3+
from typing import Optional, Union
44

55

66
class BaseBedrockProvider(ABC):
77
# to handle different generation kwargs
88
max_tokens_field_name = "max_tokens"
9+
usage: Optional[dict] = None
910

1011
def __init__(self, reasoning: bool = False, reasoning_max_token: int = 4000):
1112
self.reasoning = reasoning

metagpt/provider/bedrock/bedrock_provider.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ def get_choice_text_from_stream(self, event) -> Union[bool, str]:
6868
elif delta_type == "signature_delta":
6969
completions = ""
7070
return reasoning, completions
71-
else:
72-
return False, ""
71+
elif rsp_dict["type"] == "message_stop":
72+
self.usage = {
73+
"prompt_tokens": rsp_dict.get("amazon-bedrock-invocationMetrics", {}).get("inputTokenCount", 0),
74+
"completion_tokens": rsp_dict.get("amazon-bedrock-invocationMetrics", {}).get("outputTokenCount", 0),
75+
}
76+
return False, ""
7377

7478

7579
class CohereProvider(BaseBedrockProvider):

metagpt/provider/bedrock/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
"anthropic.claude-3-opus-20240229-v1:0": 4096,
4949
# Claude 3.5 Sonnet
5050
"anthropic.claude-3-5-sonnet-20240620-v1:0": 8192,
51+
"anthropic.claude-3-5-sonnet-20241022-v2:0": 8192,
52+
"us.anthropic.claude-3-5-sonnet-20241022-v2:0": 8192,
5153
# Claude 3.7 Sonnet
5254
"us.anthropic.claude-3-7-sonnet-20250219-v1:0": 131072,
5355
"anthropic.claude-3-7-sonnet-20250219-v1:0": 131072,

metagpt/provider/bedrock_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ async def _achat_completion_stream(self, messages: list[dict], timeout=USE_CONFI
130130
collected_content = await self._get_stream_response_body(stream_response)
131131
log_llm_stream("\n")
132132
full_text = ("".join(collected_content)).lstrip()
133+
if self.__provider.usage:
134+
# if provider provide usage, update it
135+
self._update_costs(self.__provider.usage, self.config.model)
133136
return full_text
134137

135138
def _get_response_body(self, response) -> dict:

metagpt/utils/token_counter.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
"openai/o1-preview": {"prompt": 0.015, "completion": 0.06},
9494
"openai/o1-mini": {"prompt": 0.003, "completion": 0.012},
9595
"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},
9698
"anthropic/claude-3.7-sonnet": {"prompt": 0.003, "completion": 0.015},
9799
"anthropic/claude-3.7-sonnet:beta": {"prompt": 0.003, "completion": 0.015},
98100
"anthropic/claude-3.7-sonnet:thinking": {"prompt": 0.003, "completion": 0.015},
@@ -371,6 +373,10 @@
371373
"anthropic.claude-3-haiku-20240307-v1:0:200k": {"prompt": 0.00025, "completion": 0.00125},
372374
# currently (2024-4-29) only available at US West (Oregon) AWS Region.
373375
"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},
374380
"cohere.command-text-v14": {"prompt": 0.0015, "completion": 0.0015},
375381
"cohere.command-text-v14:7:4k": {"prompt": 0.0015, "completion": 0.0015},
376382
"cohere.command-light-text-v14": {"prompt": 0.0003, "completion": 0.0003},
@@ -403,15 +409,24 @@
403409
}
404410

405411

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+
406426
def count_message_tokens(messages, model="gpt-3.5-turbo-0125"):
407427
"""Return the number of tokens used by a list of messages."""
408428
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)
415430
return num_tokens
416431
try:
417432
encoding = tiktoken.encoding_for_model(model)
@@ -500,8 +515,8 @@ def count_output_tokens(string: str, model: str) -> int:
500515
int: The number of tokens in the text string.
501516
"""
502517
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)
505520
return num_tokens
506521
try:
507522
encoding = tiktoken.encoding_for_model(model)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def run(self):
9898

9999
setup(
100100
name="metagpt",
101-
version="0.8.1",
101+
version="1.0.0",
102102
description="The Multi-Agent Framework",
103103
long_description=long_description,
104104
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)