added proper token count

This commit is contained in:
gilcu3
2023-11-19 20:50:29 +01:00
parent ac9e8e202f
commit 7fa2cdbcc4

View File

@@ -421,7 +421,7 @@ class OpenAIHelper:
self.__add_to_history(chat_id, role="assistant", content=content) self.__add_to_history(chat_id, role="assistant", content=content)
return content, self.__count_tokens_vision(fileobj) return content, response.usage.total_tokens
except openai.RateLimitError as e: except openai.RateLimitError as e:
raise e raise e
@@ -533,34 +533,36 @@ class OpenAIHelper:
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|> num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens return num_tokens
def __count_tokens_vision(self, fileobj) -> int: # no longer needed
"""
Counts the number of tokens for interpreting an image.
:param image: image to interpret
:return: the number of tokens required
"""
image = Image.open(fileobj)
model = 'gpt-4-vision-preview' # fixed for now
if model not in GPT_4_VISION_MODELS:
raise NotImplementedError(f"""count_tokens_vision() is not implemented for model {model}.""")
w, h = image.size # def __count_tokens_vision(self, fileobj) -> int:
if w > h: w, h = h, w # """
# this computation follows https://platform.openai.com/docs/guides/vision and https://openai.com/pricing#gpt-4-turbo # Counts the number of tokens for interpreting an image.
base_tokens = 85 # :param image: image to interpret
detail = self.config['vision_detail'] # :return: the number of tokens required
if detail == 'low': # """
return base_tokens # image = Image.open(fileobj)
elif detail == 'high': # model = 'gpt-4-vision-preview' # fixed for now
f = max(w / 768, h / 2048) # if model not in GPT_4_VISION_MODELS:
if f > 1: # raise NotImplementedError(f"""count_tokens_vision() is not implemented for model {model}.""")
w, h = int(w / f), int(h / f)
tw, th = (w + 511) // 512, (h + 511) // 512 # w, h = image.size
tiles = tw * th # if w > h: w, h = h, w
num_tokens = base_tokens + tiles * 170 # # this computation follows https://platform.openai.com/docs/guides/vision and https://openai.com/pricing#gpt-4-turbo
return num_tokens # base_tokens = 85
else: # detail = self.config['vision_detail']
raise NotImplementedError(f"""unknown parameter detail={detail} for model {model}.""") # if detail == 'low':
# return base_tokens
# elif detail == 'high':
# f = max(w / 768, h / 2048)
# if f > 1:
# w, h = int(w / f), int(h / f)
# tw, th = (w + 511) // 512, (h + 511) // 512
# tiles = tw * th
# num_tokens = base_tokens + tiles * 170
# return num_tokens
# else:
# raise NotImplementedError(f"""unknown parameter detail={detail} for model {model}.""")
# No longer works as of July 21st 2023, as OpenAI has removed the billing API # No longer works as of July 21st 2023, as OpenAI has removed the billing API
# def get_billing_current_month(self): # def get_billing_current_month(self):