-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #607 from InterwebAlchemy/feature/token-count
- Loading branch information
Showing
3 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import tiktoken | ||
from litellm import cost_per_token | ||
|
||
def count_tokens(text="", model="gpt-4"): | ||
""" | ||
Count the number of tokens in a string | ||
""" | ||
|
||
encoder = tiktoken.encoding_for_model(model) | ||
|
||
return len(encoder.encode(text)) | ||
|
||
def token_cost(tokens=0, model="gpt-4"): | ||
""" | ||
Calculate the cost of the current number of tokens | ||
""" | ||
|
||
(prompt_cost, _) = cost_per_token(model=model, prompt_tokens=tokens) | ||
|
||
return round(prompt_cost, 6) | ||
|
||
def count_messages_tokens(messages=[], model=None): | ||
""" | ||
Count the number of tokens in a list of messages | ||
""" | ||
|
||
tokens_used = 0 | ||
|
||
for message in messages: | ||
if isinstance(message, str): | ||
tokens_used += count_tokens(message, model=model) | ||
elif "message" in message: | ||
tokens_used += count_tokens(message["message"], model=model) | ||
|
||
if "code" in message: | ||
tokens_used += count_tokens(message["code"], model=model) | ||
|
||
if "output" in message: | ||
tokens_used += count_tokens(message["output"], model=model) | ||
|
||
prompt_cost = token_cost(tokens_used, model=model) | ||
|
||
return (tokens_used, prompt_cost) | ||
|