openai-php / client

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

[Bug]: Cannot fake the response of "OpenAI\Responses\Images\CreateResponse" passed to OpenAI::fake to return data with "b64_json" #340

Open RrNn opened 4 months ago

RrNn commented 4 months ago

Description

Awesome package, thanks for it!! One issue am facing is that I could not mock the response from the images-resource to return me b64_json encoded image. It instead returns me a response with a url no matter what i pass in

Steps To Reproduce

My steps are; I have a class

class ImageClient
{
 public function generate(string $prompt)
  {
   OpenAI::images()->create(
     [
        'n' => 1,
        'prompt' => $prompt,
        'size' => '1024x1024',
        'response_format' => 'b64_json',
        'model' => 'dall-e-3',
     ]);
  }
}

In my controller, I just call something like (real time facade)

$response = ImageClient::generate('can you generate for me a cover image for a book about mars exploding');

which works fine - ie, it returns me an image in the format b64_json & I get that base 64 string as expected, do some magic & save the image to laravel local disk storage

However, in my tests, i have the following; (i've shown the relevant imports here)

use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Images\CreateResponse;
use Tests\TestCase;

class CoverImageClientTest extends TestCase
{
 public function test_calls_the_images_api(): void
 {

  OpenAI::fake([
    CreateResponse::fake(
     ['data' => [['b64_json' => 'b64_string']]],
    )
   ]);

   $prompt = 'can you generate for me a cover image for a book about mars exploding';

   $response = ImageClient::generate($prompt);

   dd($response);

  }

}

In the dd($response) I added there in the test, the response has the url & b64_json properties that have the following values url = "https://openai.com/fake-image.png" & b64_json = ""

The dd generally looks like this;

OpenAI\Responses\Images\CreateResponse^ {#7436
  +created: 1664136088
  +data: array:1 [
    0 => OpenAI\Responses\Images\CreateResponseData^ {#7059
      +url: "https://openai.com/fake-image.png"
      +b64_json: ""
    }
  ]
  -meta: OpenAI\Responses\Meta\MetaInformation^ {#7134
    +requestId: "3"
    +openai: OpenAI\Responses\Meta\MetaInformationOpenAI^ {#4913
      +model: "g"
      +organization: "o"
      +version: "2"
      +processingMs: 4
    }
    +requestLimit: OpenAI\Responses\Meta\MetaInformationRateLimit^ {#6740
      +limit: 3
      +remaining: 2
      +reset: "2"
    }
    +tokenLimit: OpenAI\Responses\Meta\MetaInformationRateLimit^ {#7087
      +limit: 2
      +remaining: 2
      +reset: "2"
    }
  }
}

I was able to have the response have the b64_json that i passed by looking at the logic here in this foreach & tweaking the CreateResponseFixture ATTRIBUTES to have the b64_json included as anything, just made it null for my source diving - to see how it goes

    public const ATTRIBUTES = [
        'created' => 1_664_136_088,
        'data' => [
            [
                'url' => 'https://openai.com/fake-image.png',
                'b64_json' => null,
            ],
        ],
    ];

& after doing this tweak, I could get my mock b64_json string returned.

Not sure whether thats the fix or whether I am missing something, but thats the issue I am facing rn

OpenAI PHP Client Version

v0.8.0-beta.1

PHP Version

8.2.16

Notes

No response

JanSuthacheeva commented 3 months ago

Second on this. I have the same issue. @RrNn FYI I fixed this issue by editing the following file: vendor/openai-php/client/src/Testing/Responses/Fixtures/Images/CreateResponseFixture.php and adding the 'b64_json' => 'test_string' by myself in the fixture.