openai-php / client

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

Undefined array key "finish_reason" - gpt-4-vision-preview #258

Closed sevenpixels closed 8 months ago

sevenpixels commented 8 months ago

I'm trying out the new gpt-4-vision-preview model but I am getting an error when using the library, but not when I use CURL.

Request

$client = OpenAI::client(OPENAI_API_KEY);
$client->chat()->create([
  'model' => 'gpt-4-vision-preview',
  'messages' => [
    ['role' => 'user', 'content' => 
      [
        ['type' => 'text', 'text' => 'What is in this image?'],
        ['type' => 'image_url', 'image_url' => ['url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg']]
      ]
    ]
  ],
]);

Response Error: Undefined array key "finish_reason"

But here is the same call using CURL.

Request

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What is in this image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
            }
          }
        ]
      }
    ],
    "max_tokens": 3000
  }'

Response

{
  "id": "chatcmpl-8KpDxxM84pHKb1wCmz8JymEfRDxjn",
  "object": "chat.completion",
  "created": 1699974157,
  "model": "gpt-4-1106-vision-preview",
  "usage": {
    "prompt_tokens": 1118,
    "completion_tokens": 121,
    "total_tokens": 1239
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "This image features a wooden boardwalk stretching through a lush green wetland or marsh area. The grass is tall on either side of the path, which invites a person to walk through while keeping their feet dry and protecting the natural flora. The landscape includes a variety of green hues from the grass, shrubs, and trees in the distance. Above, the sky is partly cloudy with wispy clouds scattered across the blue backdrop, suggesting a pleasant day with good weather. The sunlight appears to be illuminating the scene from the left, casting a warm light over the landscape and creating a tranquil and picturesque environment."
      },
      "finish_details": {
        "type": "stop",
        "stop": "<|fim_suffix|>"
      },
      "index": 0
    }
  ]
}
ThibautPV commented 8 months ago

What version of 'openai-php/client' do you use?

gehrisandro commented 8 months ago

@sevenpixels This works fine with the latest version of the library (stable and beta)

sevenpixels commented 8 months ago

I am using openai-php/laravel ... it was on 0.6.3 and I ran composer update but it kept saying there were no updates. I looked and that is on 0.7.8 so I manually updated my composer.json to that version and updated. Now it works. Thanks guys.