openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.75k stars 491 forks source link

ErrorException: Undefined array key "choices" #132

Closed tomirons closed 1 year ago

tomirons commented 1 year ago

I'm currently using v0.5.2.

When using the chat completions, I keep getting the following exception when there is a server error on OpenAI's side.

CleanShot 2023-05-31 at 10 51 45@2x

While debugging I came across this line.

CleanShot 2023-05-31 at 10 56 51@2x

Why does it check the charset? Is that required?

If I remove the charset part, then the following exception is thrown which I would expect.

CleanShot 2023-05-31 at 11 02 52@2x

gehrisandro commented 1 year ago

Hi @tomirons

Thank you for raising the issue and your PR.

I made another PR #134 which solves the problem in a more generic way.

Am I right when I assume that the content type is application/json instead of application/json; charset=utf-8. Currently I am not able to reproduce the issue by myself.

tomirons commented 1 year ago

@gehrisandro yeah, the content type I've been seeing when debugging this is just application/json.

MikeCraig418 commented 1 year ago

I'm having the same issue today. Thanks for reporting @tomirons.

duyemura commented 1 year ago

Hi, I just encountered this when openAI was returning a content_filtered error. It would be really nice to know if there was an error on the completion call and then what type of error it was.

To help, here's the completion response on this error: CATEGORY:array(1) { ["error"]=> array(5) { ["message"]=> string(265) "The response was filtered due to the prompt triggering Azure OpenAI’s content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: https://go.microsoft.com/fwlink/?linkid=2198766" ["type"]=> NULL ["param"]=> string(6) "prompt" ["code"]=> string(14) "content_filter" ["status"]=> int(400) } }

xpethie commented 1 year ago

I have a similar issue... pls help: got the ErrorException Undefined array key "choices" in prompt

$client = \OpenAI::client(getenv('OPENAI_API_KEY'));
$result_text = $client->completions()->create([
    'model' => 'text-davinci-003',
    'prompt' => 'Analyse this text and give me sentiment of the context on a scale between -1.0 and 1.0 just as decimal without spaces and consider context about Ukraine and Russland or war or death as very negative: ' . $this->article->text,
gehrisandro commented 1 year ago

This has been fixed in the latest version: 0.5.3

ondraasek commented 1 year ago

Hi I am using the newest version 0.6.1 and still having this:

[2023-06-17 09:09:12] production.ERROR: Undefined array key "choices" {"exception":"[object] (ErrorException(code: 0): Undefined array key \"choices\" at /home/forge/app.deeply.cz/vendor/openai-php/client/src/Responses/Chat/CreateStreamedResponse.php:44) [stacktrace]

any idea? thanks a lot