Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in playground for Azure OpenAI #493

Merged
merged 2 commits into from
Oct 22, 2023
Merged

Conversation

rlnasuti
Copy link
Contributor

@rlnasuti rlnasuti commented Oct 19, 2023

Long time user, first time contributor. Apologies if I'm not following the right format for this PR.

When working with the Playground and AzureOpenAI, if you try to rerun your prompt for a new completion you receive the following error:

Error:

File "/.venv/lib/python3.11/site-packages/chainlit/playground/providers/openai.py", line 148, in create_event_stream
    token = stream_resp.choices[0]["delta"].get("content", "")
            ~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

This is because when AzureOpenAI has "moderation" turned on, the first chunk has no 'choices' and instead just has "prompt_filter_results". It looks like this:

{
  "id": "",
  "object": "",
  "created": 0,
  "model": "",
  "prompt_filter_results": [
    {
      "prompt_index": 0,
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      }
    }
  ],
  "choices": [],
  "usage": null
}

This PR introduces a very simple fix where we check for the existence of the choices field before referencing it. If it exists, we do business as usual. If it doesn't, we simply "continue" and wait for the next chunk.

I've tested this fix locally and it fixed the problem for me.

Copy link
Collaborator

@willydouhard willydouhard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome

Use hasattr instead of checking for choices in response
@rlnasuti rlnasuti requested a review from willydouhard October 20, 2023 13:56
@willydouhard willydouhard merged commit f8e649a into Chainlit:main Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants