code cleanup, using click for cli asker

This commit is contained in:
Lorenze Jay
2024-08-01 11:55:47 -07:00
parent 9393373ff0
commit 98d139f66c
8 changed files with 92 additions and 79 deletions

View File

@@ -7,6 +7,9 @@ from .parser import YamlParser
from .printer import Printer
from .prompts import Prompts
from .rpm_controller import RPMController
from .exceptions.context_window_exceeding_exception import (
LLMContextLengthExceededException,
)
__all__ = [
"Converter",
@@ -19,4 +22,5 @@ __all__ = [
"Prompts",
"RPMController",
"YamlParser",
"LLMContextLengthExceededException",
]

View File

@@ -0,0 +1,30 @@
class LLMContextLengthExceededException(Exception):
CONTEXT_LIMIT_ERRORS = [
"maximum context length",
"context length exceeded",
"context window full",
"too many tokens",
"input is too long",
"exceeds token limit",
]
def __init__(self, error_message: str):
self.original_error_message = error_message
if self._is_context_limit_error(error_message):
super().__init__(self._get_error_message())
else:
raise ValueError(
"The provided error message is not related to context length limits."
)
def _is_context_limit_error(self, error_message: str) -> bool:
return any(
phrase.lower() in error_message.lower()
for phrase in self.CONTEXT_LIMIT_ERRORS
)
def _get_error_message(self):
return (
f"LLM context length exceeded. Original error: {self.original_error_message}\n"
"Consider using a smaller input or implementing a text splitting strategy."
)

View File

@@ -0,0 +1,8 @@
class ContextLengthExceeded(Exception):
def __init__(self, exceptions):
self.exceptions = exceptions
super().__init__(self.__str__())
def __str__(self):
error_messages = [str(e) for e in self.exceptions]
return f"Multiple BadRequestExceptions occurred: {', '.join(error_messages)}"