mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-18 13:55:17 +02:00
update cluade token count
This commit is contained in:
parent
79390a2824
commit
ed54cc6e28
2 changed files with 12 additions and 8 deletions
|
|
@ -406,13 +406,17 @@ SPARK_TOKENS = {
|
|||
def count_message_tokens(messages, model="gpt-3.5-turbo-0125"):
|
||||
"""Return the number of tokens used by a list of messages."""
|
||||
if "claude" in model:
|
||||
# rough estimation for models newer than claude-2.1
|
||||
vo = anthropic.Client()
|
||||
num_tokens = 0
|
||||
for message in messages:
|
||||
for key, value in message.items():
|
||||
num_tokens += vo.count_tokens(str(value))
|
||||
return num_tokens
|
||||
# rough estimation for models newer than claude-2.1, needs api_key or auth_token
|
||||
ac = anthropic.Client()
|
||||
system_prompt = ""
|
||||
new_messages = []
|
||||
for msg in messages:
|
||||
if msg.get("role") == "system":
|
||||
system_prompt = msg.get("content")
|
||||
else:
|
||||
new_messages.append(msg)
|
||||
num_tokens = ac.beta.messages.count_tokens(messages=new_messages, model=model, system=system_prompt)
|
||||
return num_tokens.get("input_tokens", 0)
|
||||
try:
|
||||
encoding = tiktoken.encoding_for_model(model)
|
||||
except KeyError:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue