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. # def __count_tokens_vision(self, fileobj) -> int:
:param image: image to interpret # """
:return: the number of tokens required # Counts the number of tokens for interpreting an image.
""" # :param image: image to interpret
image = Image.open(fileobj) # :return: the number of tokens required
model = 'gpt-4-vision-preview' # fixed for now # """
if model not in GPT_4_VISION_MODELS: # image = Image.open(fileobj)
raise NotImplementedError(f"""count_tokens_vision() is not implemented for model {model}.""") # 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 # w, h = image.size
if w > h: w, h = h, w # 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 # # this computation follows https://platform.openai.com/docs/guides/vision and https://openai.com/pricing#gpt-4-turbo
base_tokens = 85 # base_tokens = 85
detail = self.config['vision_detail'] # detail = self.config['vision_detail']
if detail == 'low': # if detail == 'low':
return base_tokens # return base_tokens
elif detail == 'high': # elif detail == 'high':
f = max(w / 768, h / 2048) # f = max(w / 768, h / 2048)
if f > 1: # if f > 1:
w, h = int(w / f), int(h / f) # w, h = int(w / f), int(h / f)
tw, th = (w + 511) // 512, (h + 511) // 512 # tw, th = (w + 511) // 512, (h + 511) // 512
tiles = tw * th # tiles = tw * th
num_tokens = base_tokens + tiles * 170 # num_tokens = base_tokens + tiles * 170
return num_tokens # return num_tokens
else: # else:
raise NotImplementedError(f"""unknown parameter detail={detail} for model {model}.""") # 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):