|
|
@ -78,7 +78,7 @@ class Models:
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def get_max_tokens(model: str) -> int:
|
|
|
|
def get_max_tokens(model: str) -> int:
|
|
|
|
return Models.TOKEN_MAPPING.get(model, 4024)
|
|
|
|
return Models.TOKEN_MAPPING.get(model, 2024)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageSize:
|
|
|
|
class ImageSize:
|
|
|
@ -576,11 +576,16 @@ class Model:
|
|
|
|
self._prompt_min_length = value
|
|
|
|
self._prompt_min_length = value
|
|
|
|
SETTINGS_DB["prompt_min_length"] = value
|
|
|
|
SETTINGS_DB["prompt_min_length"] = value
|
|
|
|
|
|
|
|
|
|
|
|
def backoff_handler(details):
|
|
|
|
def backoff_handler_http(details):
|
|
|
|
print(
|
|
|
|
print(
|
|
|
|
f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | "
|
|
|
|
f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | "
|
|
|
|
f"{details['exception'].status}: {details['exception'].message}"
|
|
|
|
f"{details['exception'].status}: {details['exception'].message}"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def backoff_handler_request(details):
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
|
|
|
f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | "
|
|
|
|
|
|
|
|
f"{details['exception'].args[0]}"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def valid_text_request(self, response):
|
|
|
|
async def valid_text_request(self, response):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -598,7 +603,7 @@ class Model:
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=4,
|
|
|
|
max_tries=4,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_http,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_embedding_request(self, text, custom_api_key=None):
|
|
|
|
async def send_embedding_request(self, text, custom_api_key=None):
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
|
|
@ -624,11 +629,11 @@ class Model:
|
|
|
|
|
|
|
|
|
|
|
|
@backoff.on_exception(
|
|
|
|
@backoff.on_exception(
|
|
|
|
backoff.expo,
|
|
|
|
backoff.expo,
|
|
|
|
aiohttp.ClientResponseError,
|
|
|
|
ValueError,
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=6,
|
|
|
|
max_tries=4,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_request,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_edit_request(
|
|
|
|
async def send_edit_request(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
@ -651,7 +656,7 @@ class Model:
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(f"Overrides -> temp:{temp_override}, top_p:{top_p_override}")
|
|
|
|
print(f"Overrides -> temp:{temp_override}, top_p:{top_p_override}")
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=False) as session:
|
|
|
|
payload = {
|
|
|
|
payload = {
|
|
|
|
"model": Models.EDIT if codex is False else Models.CODE_EDIT,
|
|
|
|
"model": Models.EDIT if codex is False else Models.CODE_EDIT,
|
|
|
|
"input": "" if text is None else text,
|
|
|
|
"input": "" if text is None else text,
|
|
|
@ -676,7 +681,7 @@ class Model:
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=6,
|
|
|
|
max_tries=6,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_http,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_moderations_request(self, text):
|
|
|
|
async def send_moderations_request(self, text):
|
|
|
|
# Use aiohttp to send the above request:
|
|
|
|
# Use aiohttp to send the above request:
|
|
|
@ -695,11 +700,11 @@ class Model:
|
|
|
|
|
|
|
|
|
|
|
|
@backoff.on_exception(
|
|
|
|
@backoff.on_exception(
|
|
|
|
backoff.expo,
|
|
|
|
backoff.expo,
|
|
|
|
aiohttp.ClientResponseError,
|
|
|
|
ValueError,
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=4,
|
|
|
|
max_tries=4,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_request,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_summary_request(self, prompt, custom_api_key=None):
|
|
|
|
async def send_summary_request(self, prompt, custom_api_key=None):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -718,7 +723,7 @@ class Model:
|
|
|
|
|
|
|
|
|
|
|
|
tokens = self.usage_service.count_tokens(summary_request_text)
|
|
|
|
tokens = self.usage_service.count_tokens(summary_request_text)
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=False) as session:
|
|
|
|
payload = {
|
|
|
|
payload = {
|
|
|
|
"model": Models.DAVINCI,
|
|
|
|
"model": Models.DAVINCI,
|
|
|
|
"prompt": summary_request_text,
|
|
|
|
"prompt": summary_request_text,
|
|
|
@ -746,11 +751,11 @@ class Model:
|
|
|
|
|
|
|
|
|
|
|
|
@backoff.on_exception(
|
|
|
|
@backoff.on_exception(
|
|
|
|
backoff.expo,
|
|
|
|
backoff.expo,
|
|
|
|
aiohttp.ClientResponseError,
|
|
|
|
ValueError,
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=4,
|
|
|
|
max_tries=4,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_request,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_request(
|
|
|
|
async def send_request(
|
|
|
|
self,
|
|
|
|
self,
|
|
|
@ -774,20 +779,24 @@ class Model:
|
|
|
|
f"Prompt must be greater than {self.prompt_min_length} characters, it is currently: {len(prompt)} characters"
|
|
|
|
f"Prompt must be greater than {self.prompt_min_length} characters, it is currently: {len(prompt)} characters"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not max_tokens_override:
|
|
|
|
|
|
|
|
if model:
|
|
|
|
|
|
|
|
max_tokens_override = Models.get_max_tokens(model) - tokens
|
|
|
|
|
|
|
|
|
|
|
|
print(f"The prompt about to be sent is {prompt}")
|
|
|
|
print(f"The prompt about to be sent is {prompt}")
|
|
|
|
print(
|
|
|
|
print(
|
|
|
|
f"Overrides -> temp:{temp_override}, top_p:{top_p_override} frequency:{frequency_penalty_override}, presence:{presence_penalty_override}"
|
|
|
|
f"Overrides -> temp:{temp_override}, top_p:{top_p_override} frequency:{frequency_penalty_override}, presence:{presence_penalty_override}"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
|
|
|
async with aiohttp.ClientSession(raise_for_status=False) as session:
|
|
|
|
payload = {
|
|
|
|
payload = {
|
|
|
|
"model": self.model if model is None else model,
|
|
|
|
"model": self.model if model is None else model,
|
|
|
|
"prompt": prompt,
|
|
|
|
"prompt": prompt,
|
|
|
|
"stop": "" if stop is None else stop,
|
|
|
|
"stop": "" if stop is None else stop,
|
|
|
|
"temperature": self.temp if temp_override is None else temp_override,
|
|
|
|
"temperature": self.temp if temp_override is None else temp_override,
|
|
|
|
"top_p": self.top_p if top_p_override is None else top_p_override,
|
|
|
|
"top_p": self.top_p if top_p_override is None else top_p_override,
|
|
|
|
"max_tokens": self.max_tokens - tokens
|
|
|
|
"max_tokens": self.max_tokens - tokens
|
|
|
|
if not max_tokens_override
|
|
|
|
if max_tokens_override is None
|
|
|
|
else max_tokens_override,
|
|
|
|
else max_tokens_override,
|
|
|
|
"presence_penalty": self.presence_penalty
|
|
|
|
"presence_penalty": self.presence_penalty
|
|
|
|
if presence_penalty_override is None
|
|
|
|
if presence_penalty_override is None
|
|
|
@ -839,7 +848,7 @@ class Model:
|
|
|
|
factor=3,
|
|
|
|
factor=3,
|
|
|
|
base=5,
|
|
|
|
base=5,
|
|
|
|
max_tries=4,
|
|
|
|
max_tries=4,
|
|
|
|
on_backoff=backoff_handler,
|
|
|
|
on_backoff=backoff_handler_http,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
async def send_image_request(
|
|
|
|
async def send_image_request(
|
|
|
|
self, ctx, prompt, vary=None, custom_api_key=None
|
|
|
|
self, ctx, prompt, vary=None, custom_api_key=None
|
|
|
|