openai-php / openai-php/client

Undefined array key "content" (MS OpenAI Content Filtering issue)

Open
#198 8 comments 0 reactions 1 assignee View on GitHub

@gehrisandro is already working on this.

Since Aug 28, 2023.

Non-OpenAI Model
Dominant language
PHP
Stars
5.8k
Forks
679
Avg merge
19m
Merged PRs (30d)
2

Description

When using using the Azure OpenAI API, if content filtering is kicked off an error is returned Undefined array key "content" as there is no content parameter in the array within CreateResponseMessage.php

    /**
     * @param  array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}}  $attributes
     */
    public static function from(array $attributes): self
    {
        return new self(
            $attributes['role'],
            $attributes['content'],
            isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null,
        );
    }

The choices array looks something like this,

    "choices": [
        {
            "index": 0,
            "finish_reason": "content_filter",
            "message": {
                "role": "assistant"
            },
            "content_filter_results": {
                "hate": {
                    "filtered": false,
                    "severity": "safe"
                },
                "self_harm": {
                    "filtered": false,
                    "severity": "safe"
                },
                "sexual": {
                    "filtered": true,
                    "severity": "medium"
                },
                "violence": {
                    "filtered": false,
                    "severity": "safe"
                }
            }
        }
    ],

That example above has the "sexual" filter triggered, hence why there is no content back from the assistant.

It would be great if this lib handled the content filtering issues (e.g. threw an exception with a message etc)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.