openai-php / client

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

Added filename support for the file payload #416

Open apc-harold opened 5 months ago

apc-harold commented 5 months ago

What:

Description:

When uploading a file using:

$client->files()->upload([
    'purpose' => $purpose,
    'file' => $file,
]);

The uploaded file is automatically named Nyholm-Psr7-Zval:, which causes issues because it doesn't have a file extension. This prevents the file from being attached to an assistant.

The proposed changes allow for a file name to be provided by instead passing an array value for the file key:

$client->files()->upload([
    'purpose' => $purpose,
    'file' => [
        'data' => $file,
        'fileName' => $fileName,
    ],
]);
gehrisandro commented 5 months ago

Hi @apc-harold

I am not able to reproduce the issue. When I upload a file, the filename is set properly.

Could you please provided a full example or a demo repository which reproduces the issue?

apc-harold commented 5 months ago

Hi @gehrisandro,

Thanks for checking it out!

I'm quite busy at the moment so won't be able to provide an example, but:

I'll maybe have some time on w/s 17th, so will make a reminder to come back and make an example if it's still here.

DennisBirkholz commented 5 months ago

I stumbled on the same problem: it is not easily possible to upload string data as a file, because no filename (or an invalid filename) gets sent that is missing the ".jsonl" extension.

This may also be remedied by adding the following code to the file upload example:

$jsonlData = "{...}\n{...}";
$response = $client->files()->upload([
        'purpose' => 'fine-tune',
        'file' => \GuzzleHttp\Psr7\Utils::streamFor($jsonlData, ["metadata" => ["uri" => "upload.jsonl"]]),
    ]);
olimarferraz commented 3 weeks ago

@DennisBirkholz thanks! Works for me. I'm using the Laravel driver and was stuck in how properly handle this uploads.