Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: failed to upload files with Azure OpenAI #465

Closed
chris-lee-lb opened this issue Aug 10, 2024 · 4 comments
Closed

[Bug]: failed to upload files with Azure OpenAI #465

chris-lee-lb opened this issue Aug 10, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@chris-lee-lb
Copy link

Description

Currently can not upload files with Azure OpenAI successfully. Will receive this error messages.

{
  "error": {
    "code": "invalidPayload",
    "message": "purpose contains an invalid purpose."
  }
}

Steps To Reproduce

<?php

$client = OpenAI::factory()
    ->withBaseUri('<azure_openai_endpoint>')
    ->withHttpHeader('api-key', '<azure_openai_api_key>')
    ->withQueryParam('api-version', '2024-05-01-preview')
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 60]))
    ->make();

$client->files()->upload([
    'file'    => fopen('<file_path>', 'r'),
    'purpose' => 'assistants',
]);

OpenAI PHP Client Version

v0.10.1

PHP Version

8.3.10

Notes

After further root cause analysis, it was discovered that the issue was due to the Azure OpenAI API Server being unable to correctly parse the content-length in the multipart/form-data request body sent by the OpenAI HTTP client (came from this class - Http\Message\MultipartStream\MultipartStreamBuilder). Once this content-length is removed, the upload works as expected. Currently, there is a related pull request (guzzle/psr7#581) for the GuzzleHttp Client that is awaiting merge.

@chris-lee-lb chris-lee-lb added the bug Something isn't working label Aug 10, 2024
@chris-lee-lb
Copy link
Author

chris-lee-lb commented Aug 10, 2024

I've send this PR to php-http/multipart-stream-builder (php-http/multipart-stream-builder#63), hope can be merged. Or we may have to look for other possible alternatives first.

@mohammedhanafy
Copy link

mohammedhanafy commented Aug 23, 2024

@chris-lee-lb hi chris, i'm having the same exact issue, but i tried to remove the content-length code from /vendor/guzzlehttp/psr7/src/MultipartStream.php, but i still have the same issue

$client = OpenAI::factory()
            ->withBaseUri('https://curans.openai.azure.com/openai/')
            ->withHttpHeader('api-key', $apiKey)
            ->withQueryParam('api-version', config('openai.azure.files_api_version'))
            ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 60]))
            ->make();
$response = $client->files()->upload([
    'purpose' => 'assistants',
    'file' => fopen(Storage::path('public/'.$pdfFilePath), 'r'),
]);

"message": "purpose contains an invalid purpose."

is there an alternative way to fix this issue ? thank you so much

@chris-lee-lb
Copy link
Author

chris-lee-lb commented Sep 2, 2024

@mohammedhanafy Because we currently create custom function with Laravel Http Client Facade and our own MultipartStream class (which based on GuzzleHttp\Psr7\MultipartStream) to workaround this issue.

use App\Support\MultipartStream;
use Illuminate\Support\Facades\Http;

$body     = new MultipartStream($elements);
$boundary = $body->getBoundary();

$response = retry(
    3,
    fn () => Http::baseUrl($endpoint)
        ->withBody($body, "multipart/form-data; boundary={$boundary}")
        ->timeout($timeout)
        ->retry(3, 100, fn ($ex) => $ex instanceof ConnectionException)
        ->withHeaders(['api-key' => $apiKey])
        ->withQueryParameters(['api-version' => $apiVersion])
        ->send('post', '/files')
        ->throwUnlessStatus(201),
    1000
);

@chris-lee-lb
Copy link
Author

php-http/multipart-stream-builder#63 has been approved & released as 1.4.0, so this issue can be closed. cc @mohammedhanafy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants