|
223 | 223 | expect($response->meta->model)->toBe('dall-e-3');
|
224 | 224 | expect($response->meta->rateLimits)->not->toBeEmpty();
|
225 | 225 | });
|
| 226 | + |
| 227 | +it('can generate an image with gpt-image-1 returning base64', function (): void { |
| 228 | + Http::fake([ |
| 229 | + 'api.openai.com/v1/images/generations' => Http::response([ |
| 230 | + 'created' => 1713833628, |
| 231 | + 'data' => [ |
| 232 | + [ |
| 233 | + 'b64_json' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', |
| 234 | + ], |
| 235 | + ], |
| 236 | + 'usage' => [ |
| 237 | + 'total_tokens' => 100, |
| 238 | + 'input_tokens' => 50, |
| 239 | + 'output_tokens' => 50, |
| 240 | + 'input_tokens_details' => [ |
| 241 | + 'text_tokens' => 10, |
| 242 | + 'image_tokens' => 40, |
| 243 | + ], |
| 244 | + ], |
| 245 | + ], 200), |
| 246 | + ]); |
| 247 | + |
| 248 | + $response = Prism::image() |
| 249 | + ->using('openai', 'gpt-image-1') |
| 250 | + ->withPrompt('A cute baby sea otter') |
| 251 | + ->generate(); |
| 252 | + |
| 253 | + expect($response->firstImage())->not->toBeNull(); |
| 254 | + expect($response->firstImage()->base64)->toBe('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='); |
| 255 | + expect($response->firstImage()->hasBase64())->toBeTrue(); |
| 256 | + expect($response->firstImage()->hasUrl())->toBeFalse(); |
| 257 | + expect($response->firstImage()->url)->toBeNull(); |
| 258 | + expect($response->usage->promptTokens)->toBe(50); |
| 259 | + expect($response->imageCount())->toBe(1); |
| 260 | + |
| 261 | + Http::assertSent(function (Request $request): bool { |
| 262 | + $data = $request->data(); |
| 263 | + |
| 264 | + return $request->url() === 'https://api.openai.com/v1/images/generations' && |
| 265 | + $data['model'] === 'gpt-image-1' && |
| 266 | + $data['prompt'] === 'A cute baby sea otter'; |
| 267 | + }); |
| 268 | +}); |
| 269 | + |
| 270 | +it('can generate an image with dall-e-3 requesting base64 format', function (): void { |
| 271 | + Http::fake([ |
| 272 | + 'api.openai.com/v1/images/generations' => Http::response([ |
| 273 | + 'created' => 1713833628, |
| 274 | + 'data' => [ |
| 275 | + [ |
| 276 | + 'b64_json' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', |
| 277 | + 'revised_prompt' => 'A highly detailed mountain sunset scene', |
| 278 | + ], |
| 279 | + ], |
| 280 | + 'usage' => [ |
| 281 | + 'prompt_tokens' => 20, |
| 282 | + 'completion_tokens' => 0, |
| 283 | + ], |
| 284 | + ], 200), |
| 285 | + ]); |
| 286 | + |
| 287 | + $response = Prism::image() |
| 288 | + ->using('openai', 'dall-e-3') |
| 289 | + ->withPrompt('A mountain sunset') |
| 290 | + ->withProviderOptions([ |
| 291 | + 'response_format' => 'b64_json', |
| 292 | + ]) |
| 293 | + ->generate(); |
| 294 | + |
| 295 | + expect($response->firstImage())->not->toBeNull(); |
| 296 | + expect($response->firstImage()->base64)->toBe('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='); |
| 297 | + expect($response->firstImage()->hasBase64())->toBeTrue(); |
| 298 | + expect($response->firstImage()->hasUrl())->toBeFalse(); |
| 299 | + expect($response->firstImage()->hasRevisedPrompt())->toBeTrue(); |
| 300 | + expect($response->firstImage()->revisedPrompt)->toBe('A highly detailed mountain sunset scene'); |
| 301 | + |
| 302 | + Http::assertSent(function (Request $request): bool { |
| 303 | + $data = $request->data(); |
| 304 | + |
| 305 | + return $data['model'] === 'dall-e-3' && |
| 306 | + $data['prompt'] === 'A mountain sunset' && |
| 307 | + $data['response_format'] === 'b64_json'; |
| 308 | + }); |
| 309 | +}); |
| 310 | + |
| 311 | +it('can generate an image with dall-e-2 requesting base64 format', function (): void { |
| 312 | + Http::fake([ |
| 313 | + 'api.openai.com/v1/images/generations' => Http::response([ |
| 314 | + 'created' => 1713833628, |
| 315 | + 'data' => [ |
| 316 | + [ |
| 317 | + 'b64_json' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', |
| 318 | + ], |
| 319 | + ], |
| 320 | + 'usage' => [ |
| 321 | + 'prompt_tokens' => 12, |
| 322 | + 'completion_tokens' => 0, |
| 323 | + ], |
| 324 | + ], 200), |
| 325 | + ]); |
| 326 | + |
| 327 | + $response = Prism::image() |
| 328 | + ->using('openai', 'dall-e-2') |
| 329 | + ->withPrompt('Abstract geometric patterns') |
| 330 | + ->withProviderOptions([ |
| 331 | + 'response_format' => 'b64_json', |
| 332 | + 'size' => '256x256', |
| 333 | + ]) |
| 334 | + ->generate(); |
| 335 | + |
| 336 | + expect($response->firstImage())->not->toBeNull(); |
| 337 | + expect($response->firstImage()->base64)->toBe('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='); |
| 338 | + expect($response->firstImage()->hasBase64())->toBeTrue(); |
| 339 | + expect($response->firstImage()->hasUrl())->toBeFalse(); |
| 340 | + |
| 341 | + Http::assertSent(function (Request $request): bool { |
| 342 | + $data = $request->data(); |
| 343 | + |
| 344 | + return $data['model'] === 'dall-e-2' && |
| 345 | + $data['prompt'] === 'Abstract geometric patterns' && |
| 346 | + $data['response_format'] === 'b64_json' && |
| 347 | + $data['size'] === '256x256'; |
| 348 | + }); |
| 349 | +}); |
0 commit comments