Skip to content

Commit

Permalink
Handle Azure OpenAI content filter (second try) (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanmai authored Feb 6, 2025
1 parent 5c2830d commit e34ec7b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/helm/clients/openai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ def do_it() -> Dict[str, Any]:

completions: List[GeneratedOutput] = []
for raw_completion in response["choices"]:
# Handle Azure OpenAI content filter
# See: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter
if raw_completion["finish_reason"] == "content_filter":
return RequestResult(
success=False,
cached=False,
error="Content blocked by OpenAI filter",
completions=[],
embedding=[],
error_flags=ErrorFlags(is_retriable=False, is_fatal=False),
)
# The OpenAI chat completion API doesn't support echo.
# If `echo_prompt` is true, combine the prompt and completion.
raw_completion_content = raw_completion["message"]["content"]
Expand All @@ -277,17 +288,6 @@ def do_it() -> Dict[str, Any]:
tokens: List[Token] = [
Token(text=cast(str, raw_token), logprob=0) for raw_token in tokenization_result.raw_tokens
]
# Handle Azure OpenAI content filter
# See: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/content-filter
if raw_completion["finish_reason"] == "content_filter":
return RequestResult(
success=False,
cached=False,
error="Content blocked by OpenAI filter",
completions=[],
embedding=[],
error_flags=ErrorFlags(is_retriable=False, is_fatal=False),
)
completion = GeneratedOutput(
text=text,
logprob=0, # OpenAI does not provide logprobs
Expand Down

0 comments on commit e34ec7b

Please sign in to comment.