Merge branch 'main' into add-balance-function

This commit is contained in:
AlexHTW
2023-03-24 11:32:16 +01:00
3 changed files with 47 additions and 127 deletions

View File

@@ -6,7 +6,6 @@ import tiktoken
import openai
import requests
import json
from datetime import date
@@ -215,18 +214,24 @@ class OpenAIHelper:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("gpt-3.5-turbo")
if model in GPT_ALL_MODELS:
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
if model in GPT_3_MODELS:
tokens_per_message = 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model in GPT_4_MODELS + GPT_4_32K_MODELS:
tokens_per_message = 3
tokens_per_name = 1
else:
raise NotImplementedError(f"__count_tokens() is not presently implemented for model {model}")
raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}.""")
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
def get_grant_balance(self):
"""Gets remaining grant balance for new users from OpenAI API.