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]: Cannot fake the response of "OpenAI\Responses\Images\CreateResponse" passed to OpenAI::fake to return data with "b64_json" #340

Open
RrNn opened this issue Feb 20, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@RrNn
Copy link

RrNn commented Feb 20, 2024

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

@RrNn RrNn added the bug Something isn't working label Feb 20, 2024
@JanSuthacheeva
Copy link
Contributor

JanSuthacheeva commented Mar 28, 2024

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.

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