openai-php / client

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

[Bug]: Can't download imageFile generated by code_interpreter #313

Closed udgithub closed 6 months ago

udgithub commented 6 months ago

Description

Hello guys,

first of all: thanks for your great development. So I as an PHP developer can use OpenAI API almost as convenient as with Python.

Now the problem: If I go through all messages I can find a $contentPart->type of "image_file" . If I try to get the image data like this:

$imageFileId = $contentPart->imageFile->fileId;
$imageFile = $client->files()->retrieve($imageFileId);
$imageData = $client->files()->download($imageFile);

I get an error: Fatal error: Uncaught TypeError: OpenAI\Responses\Files\RetrieveResponse::__construct(): Argument #3 ($bytes) must be of type int, null given, called in /my-project-path/vendor/openai-php/client/src/Responses/Files/RetrieveResponse.php on line 51 and defined in /my-project-path/vendor/openai-php/client/src/Responses/Files/RetrieveResponse.php on line 30

That is the loop to process the messages:

    foreach ($messages->data as $message) {
        if (!empty($message->content)) {
            foreach ($message->content as $contentPart) {
                if ($contentPart->type == "text") {
            # skipping this to keep code small
                } elseif ($contentPart->type == "image_file") {
                    $imageFileId = $contentPart->imageFile->fileId;
                    $imageFile = $client->files()->retrieve($imageFileId);
                    $imageData = $client->files()->download($imageFile);
                    print_r($imageData);
                    file_put_contents("image_$imageCounter.png", $imageData);
                    $imageCounter++;
                }
            }
        }
    }

It would be great if you could give me an hint what I'm doing wrong or fix the bug if it is one. Thanks a lot in advance for your help.

Steps To Reproduce

create an assistant of type code_interpreter and send a request like plot these numbers in a graph: 1,2,3,4,15,73,12 then try to read the messages and download the imageFile

OpenAI PHP Client Version

0.8.1

PHP Version

8.1.2

Notes

No response

udgithub commented 6 months ago

OK, I found the solution: Your documenation said: `$client->files()->download($file); So I retrieved the file info. But now it works, when I use the fileId:

$imageFileId = $contentPart->imageFile->fileId;
# don't do THIS:   $imageFile = $client->files()->retrieve($imageFileId);
$imageData = $client->files()->download($imageFileId);

Sorry for misunderstanding your excellent documentation.