Skip to content

Commit

Permalink
Add to fatal exceptions and handle OpenAIError
Browse files Browse the repository at this point in the history
Signed-off-by: Dan McPherson <[email protected]>
  • Loading branch information
danmcp committed Aug 19, 2024
1 parent a21e3bb commit 1561eee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/instructlab/eval/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ def __init__(self, tasks_dir) -> None:
self.message = f"Invalid Tasks Dir: {tasks_dir}"


class OpenAIError(EvalError):
class ModelServingAPIError(EvalError):
"""
Error raised when reply retrieval from OpenAI API fails.
Error raised when reply retrieval from model serving fails.
Attributes
message error message to be printed on raise
"""

def __init__(self) -> None:
super().__init__()
self.message = "Failed to receive a reply from API."
self.message = "Failed to receive a reply from model serving API."
28 changes: 19 additions & 9 deletions src/instructlab/eval/mt_bench_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,15 @@ def play_a_match_single(


def _is_fatal_openai_error(e: openai.OpenAIError) -> bool:
return isinstance(e, openai.APIConnectionError)
return isinstance(
e,
(
openai.APIConnectionError,
openai.AuthenticationError,
openai.PermissionDeniedError,
openai.NotFoundError,
),
)


# TODO: copied from instructlab (cli) utils module; consolidate somewhere?
Expand Down Expand Up @@ -302,6 +310,13 @@ def chat_completion_openai(
)
output = response.choices[0].message.content
break
except (
# retry won't fix these errors
openai.BadRequestError, # 400
openai.UnprocessableEntityError, # 422
) as e:
logger.debug(e)
return API_ERROR_OUTPUT # immediately soft fail
except (
# retry may help with these errors
openai.APIConnectionError,
Expand All @@ -312,6 +327,8 @@ def chat_completion_openai(
openai.AuthenticationError, # 401
openai.PermissionDeniedError, # 403
openai.NotFoundError, # 404
# General catch-all
openai.OpenAIError,
) as e:
if not _is_fatal_openai_error(e):
output = API_ERROR_OUTPUT # disable hard fail (never raise!)
Expand All @@ -321,18 +338,11 @@ def chat_completion_openai(
break
logger.debug(e)
time.sleep(API_RETRY_SLEEP)
except (
# retry won't fix these errors
openai.BadRequestError, # 400
openai.UnprocessableEntityError, # 422
) as e:
logger.debug(e)
return API_ERROR_OUTPUT # immediately soft fail

if output is None:
# not a single attempt was non-fatal; this is indicative of
# basic connectivity or server issue -> hard fail
raise exceptions.OpenAIError
raise exceptions.ModelServingAPIError
return output


Expand Down

0 comments on commit 1561eee

Please sign in to comment.