Delete api/tokens.py
Browse files- api/tokens.py +0 -86
api/tokens.py
DELETED
|
@@ -1,86 +0,0 @@
|
|
| 1 |
-
import math
|
| 2 |
-
|
| 3 |
-
import tiktoken
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
async def calculate_image_tokens(width, height, detail):
|
| 7 |
-
if detail == "low":
|
| 8 |
-
return 85
|
| 9 |
-
else:
|
| 10 |
-
max_dimension = max(width, height)
|
| 11 |
-
if max_dimension > 2048:
|
| 12 |
-
scale_factor = 2048 / max_dimension
|
| 13 |
-
new_width = int(width * scale_factor)
|
| 14 |
-
new_height = int(height * scale_factor)
|
| 15 |
-
else:
|
| 16 |
-
new_width = width
|
| 17 |
-
new_height = height
|
| 18 |
-
|
| 19 |
-
width, height = new_width, new_height
|
| 20 |
-
min_dimension = min(width, height)
|
| 21 |
-
if min_dimension > 768:
|
| 22 |
-
scale_factor = 768 / min_dimension
|
| 23 |
-
new_width = int(width * scale_factor)
|
| 24 |
-
new_height = int(height * scale_factor)
|
| 25 |
-
else:
|
| 26 |
-
new_width = width
|
| 27 |
-
new_height = height
|
| 28 |
-
|
| 29 |
-
width, height = new_width, new_height
|
| 30 |
-
num_masks_w = math.ceil(width / 512)
|
| 31 |
-
num_masks_h = math.ceil(height / 512)
|
| 32 |
-
total_masks = num_masks_w * num_masks_h
|
| 33 |
-
|
| 34 |
-
tokens_per_mask = 170
|
| 35 |
-
total_tokens = total_masks * tokens_per_mask + 85
|
| 36 |
-
|
| 37 |
-
return total_tokens
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
async def num_tokens_from_messages(messages, model=''):
|
| 41 |
-
try:
|
| 42 |
-
encoding = tiktoken.encoding_for_model(model)
|
| 43 |
-
except KeyError:
|
| 44 |
-
encoding = tiktoken.get_encoding("cl100k_base")
|
| 45 |
-
if model == "gpt-3.5-turbo-0301":
|
| 46 |
-
tokens_per_message = 4
|
| 47 |
-
else:
|
| 48 |
-
tokens_per_message = 3
|
| 49 |
-
num_tokens = 0
|
| 50 |
-
for message in messages:
|
| 51 |
-
num_tokens += tokens_per_message
|
| 52 |
-
for key, value in message.items():
|
| 53 |
-
if isinstance(value, list):
|
| 54 |
-
for item in value:
|
| 55 |
-
if item.get("type") == "text":
|
| 56 |
-
num_tokens += len(encoding.encode(item.get("text")))
|
| 57 |
-
if item.get("type") == "image_url":
|
| 58 |
-
pass
|
| 59 |
-
else:
|
| 60 |
-
num_tokens += len(encoding.encode(value))
|
| 61 |
-
num_tokens += 3
|
| 62 |
-
return num_tokens
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
async def num_tokens_from_content(content, model=None):
|
| 66 |
-
try:
|
| 67 |
-
encoding = tiktoken.encoding_for_model(model)
|
| 68 |
-
except KeyError:
|
| 69 |
-
encoding = tiktoken.get_encoding("cl100k_base")
|
| 70 |
-
encoded_content = encoding.encode(content)
|
| 71 |
-
len_encoded_content = len(encoded_content)
|
| 72 |
-
return len_encoded_content
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
async def split_tokens_from_content(content, max_tokens, model=None):
|
| 76 |
-
try:
|
| 77 |
-
encoding = tiktoken.encoding_for_model(model)
|
| 78 |
-
except KeyError:
|
| 79 |
-
encoding = tiktoken.get_encoding("cl100k_base")
|
| 80 |
-
encoded_content = encoding.encode(content)
|
| 81 |
-
len_encoded_content = len(encoded_content)
|
| 82 |
-
if len_encoded_content >= max_tokens:
|
| 83 |
-
content = encoding.decode(encoded_content[:max_tokens])
|
| 84 |
-
return content, max_tokens, "length"
|
| 85 |
-
else:
|
| 86 |
-
return content, len_encoded_content, "stop"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|