improved context window exceeding exception class

This commit is contained in:
Lorenze Jay
2024-08-01 13:12:43 -07:00
parent a7b0c7e122
commit c1cb7ee9c5

View File

@@ -11,12 +11,7 @@ class LLMContextLengthExceededException(Exception):
def __init__(self, error_message: str): def __init__(self, error_message: str):
self.original_error_message = error_message self.original_error_message = error_message
if self._is_context_limit_error(error_message): super().__init__(self._get_error_message(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: def _is_context_limit_error(self, error_message: str) -> bool:
return any( return any(
@@ -24,8 +19,8 @@ class LLMContextLengthExceededException(Exception):
for phrase in self.CONTEXT_LIMIT_ERRORS for phrase in self.CONTEXT_LIMIT_ERRORS
) )
def _get_error_message(self): def _get_error_message(self, error_message: str):
return ( return (
f"LLM context length exceeded. Original error: {self.original_error_message}\n" f"LLM context length exceeded. Original error: {error_message}\n"
"Consider using a smaller input or implementing a text splitting strategy." "Consider using a smaller input or implementing a text splitting strategy."
) )