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 16, 2024
1 parent a21e3bb commit a356211
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/instructlab/eval/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ 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
"""
Expand Down
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 a356211

Please sign in to comment.