-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Implement DALL-E image generation for OpenAI Bridge #178
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fc88903
Implement Dall-E image generation
DZunke c687e5c
Adjust some testcases for styling
DZunke 1998614
Rename GeneratedImagesResponse to ImagesResponse
DZunke 0409956
Merge branch 'php-llm:main' into dall-e-image-generation
DZunke 3655afd
Some review adjustments
DZunke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$llm = new DallE(DallE::DALL_E_2); | ||
|
||
$response = $platform->request( | ||
model: $llm, | ||
input: 'A cartoon-style elephant with a long trunk and large ears.', | ||
options: ['response_format' => 'url', 'n' => 2], | ||
); | ||
|
||
chr-hertel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach ($response->getContent() as $index => $image) { | ||
echo 'Image '.$index.': '.$image->url.PHP_EOL; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE\ImagesResponse; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Model\Response\AsyncResponse; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$llm = new DallE(DallE::DALL_E_3); | ||
|
||
$response = $platform->request( | ||
model: $llm, | ||
input: 'A cartoon-style elephant with a long trunk and large ears.', | ||
options: ['response_format' => 'url', 'n' => 1], | ||
chr-hertel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
|
||
if ($response instanceof AsyncResponse) { | ||
$response = $response->unwrap(); | ||
} | ||
|
||
assert($response instanceof ImagesResponse); | ||
|
||
echo 'Revised Prompt: '.$response->revisedPrompt.PHP_EOL.PHP_EOL; | ||
|
||
foreach ($response->getContent() as $index => $image) { | ||
echo 'Image '.$index.': '.$image->url.PHP_EOL; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenAI; | ||
|
||
use PhpLlm\LlmChain\Model\Model; | ||
|
||
final readonly class DallE implements Model | ||
{ | ||
public const DALL_E_2 = 'dall-e-2'; | ||
public const DALL_E_3 = 'dall-e-3'; | ||
|
||
/** @param array<string, mixed> $options The default options for the model usage */ | ||
public function __construct( | ||
private string $version = self::DALL_E_2, | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private array $options = [ | ||
'response_format' => 'url', | ||
'n' => 1, | ||
], | ||
) { | ||
} | ||
|
||
public function getVersion(): string | ||
{ | ||
return $this->version; | ||
} | ||
|
||
/** @return array<string, mixed> */ | ||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
final readonly class Base64Image | ||
{ | ||
public function __construct( | ||
public string $encodedImage, | ||
) { | ||
Assert::stringNotEmpty($encodedImage, 'The base64 encoded image generated must be given.'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
|
||
use PhpLlm\LlmChain\Model\Response\ResponseInterface; | ||
|
||
class ImagesResponse implements ResponseInterface | ||
{ | ||
/** @var list<Base64Image|UrlImage> */ | ||
private readonly array $images; | ||
|
||
public function __construct( | ||
public ?string $revisedPrompt = null, // Only string on Dall-E 3 usage | ||
Base64Image|UrlImage ...$images, | ||
) { | ||
$this->images = \array_values($images); | ||
} | ||
|
||
/** | ||
* @return list<Base64Image|UrlImage> | ||
*/ | ||
public function getContent(): array | ||
{ | ||
return $this->images; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
use PhpLlm\LlmChain\Model\Model; | ||
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; | ||
use PhpLlm\LlmChain\Platform\ModelClient as PlatformResponseFactory; | ||
use PhpLlm\LlmChain\Platform\ResponseConverter as PlatformResponseConverter; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see https://platform.openai.com/docs/api-reference/images/create | ||
*/ | ||
final readonly class ModelClient implements PlatformResponseFactory, PlatformResponseConverter | ||
chr-hertel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
#[\SensitiveParameter] | ||
private string $apiKey, | ||
) { | ||
Assert::stringNotEmpty($apiKey, 'The API key must not be empty.'); | ||
Assert::startsWith($apiKey, 'sk-', 'The API key must start with "sk-".'); | ||
} | ||
|
||
public function supports(Model $model, array|string|object $input): bool | ||
{ | ||
return $model instanceof DallE; | ||
} | ||
|
||
public function request(Model $model, object|array|string $input, array $options = []): HttpResponse | ||
{ | ||
return $this->httpClient->request('POST', 'https://api.openai.com/v1/images/generations', [ | ||
'auth_bearer' => $this->apiKey, | ||
'json' => \array_merge($options, [ | ||
'model' => $model->getVersion(), | ||
'prompt' => $input, | ||
]), | ||
]); | ||
} | ||
|
||
public function convert(HttpResponse $response, array $options = []): LlmResponse | ||
{ | ||
$response = $response->toArray(); | ||
if (!isset($response['data'][0])) { | ||
throw new \RuntimeException('No image generated.'); | ||
} | ||
|
||
$images = []; | ||
foreach ($response['data'] as $image) { | ||
if ('url' === $options['response_format']) { | ||
$images[] = new UrlImage($image['url']); | ||
|
||
continue; | ||
} | ||
|
||
$images[] = new Base64Image($image['b64_json']); | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return new ImagesResponse($image['revised_prompt'] ?? null, ...$images); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Bridge\OpenAI\DallE; | ||
|
||
use Webmozart\Assert\Assert; | ||
|
||
final readonly class UrlImage | ||
{ | ||
public function __construct( | ||
public string $url, | ||
) { | ||
Assert::stringNotEmpty($url, 'The image url must be given.'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Tests\Bridge\OpenAI\DallE; | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE\Base64Image; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Base64Image::class)] | ||
#[Small] | ||
final class Base64ImageTest extends TestCase | ||
{ | ||
#[Test] | ||
public function itCreatesBase64Image(): void | ||
{ | ||
$emptyPixel = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='; | ||
$base64Image = new Base64Image($emptyPixel); | ||
|
||
self::assertSame($emptyPixel, $base64Image->encodedImage); | ||
} | ||
|
||
#[Test] | ||
public function itThrowsExceptionWhenBase64ImageIsEmpty(): void | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The base64 encoded image generated must be given.'); | ||
|
||
new Base64Image(''); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Tests\Bridge\OpenAI\DallE; | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE\Base64Image; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE\ImagesResponse; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\DallE\UrlImage; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(ImagesResponse::class)] | ||
#[UsesClass(Base64Image::class)] | ||
#[UsesClass(UrlImage::class)] | ||
#[Small] | ||
final class ImagesResponseTest extends TestCase | ||
{ | ||
#[Test] | ||
public function itCreatesImagesResponse(): void | ||
{ | ||
$base64Image = new Base64Image('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='); | ||
$generatedImagesResponse = new ImagesResponse(null, $base64Image); | ||
|
||
self::assertNull($generatedImagesResponse->revisedPrompt); | ||
self::assertCount(1, $generatedImagesResponse->getContent()); | ||
self::assertSame($base64Image, $generatedImagesResponse->getContent()[0]); | ||
} | ||
|
||
#[Test] | ||
public function itCreatesImagesResponseWithRevisedPrompt(): void | ||
{ | ||
$base64Image = new Base64Image('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='); | ||
$generatedImagesResponse = new ImagesResponse('revised prompt', $base64Image); | ||
|
||
self::assertSame('revised prompt', $generatedImagesResponse->revisedPrompt); | ||
self::assertCount(1, $generatedImagesResponse->getContent()); | ||
self::assertSame($base64Image, $generatedImagesResponse->getContent()[0]); | ||
} | ||
|
||
#[Test] | ||
public function itIsCreatableWithMultipleImages(): void | ||
{ | ||
$image1 = new UrlImage('https://example'); | ||
$image2 = new UrlImage('https://example2'); | ||
|
||
$generatedImagesResponse = new ImagesResponse(null, $image1, $image2); | ||
|
||
self::assertCount(2, $generatedImagesResponse->getContent()); | ||
self::assertSame($image1, $generatedImagesResponse->getContent()[0]); | ||
self::assertSame($image2, $generatedImagesResponse->getContent()[1]); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.