From 47918269557f65202e23a85dd8ace1cf318b6d01 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 23 Sep 2024 22:13:05 +0200 Subject: [PATCH] Support image url A simpler implementation of * #25 cc @DZunke --- src/Message/ImageUrl.php | 33 ++++++++++++++++++++++++++++++++ src/Message/Message.php | 13 ++++++++++--- tests/Message/MessageBagTest.php | 7 +++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/Message/ImageUrl.php diff --git a/src/Message/ImageUrl.php b/src/Message/ImageUrl.php new file mode 100644 index 00000000..f09ab45f --- /dev/null +++ b/src/Message/ImageUrl.php @@ -0,0 +1,33 @@ + 'image_url', + 'image_url' => [ + 'url' => $this->url, + ], + ]; + } +} diff --git a/src/Message/Message.php b/src/Message/Message.php index 57fd59de..0afaee9d 100644 --- a/src/Message/Message.php +++ b/src/Message/Message.php @@ -12,7 +12,7 @@ * @param ?ToolCall[] $toolCalls */ public function __construct( - public ?string $content, + public string|ImageUrl|null $content, public Role $role, public ?array $toolCalls = null, ) { @@ -31,7 +31,7 @@ public static function ofAssistant(?string $content = null, ?array $toolCalls = return new self($content, Role::Assistant, $toolCalls); } - public static function ofUser(string $content): self + public static function ofUser(string|ImageUrl $content): self { return new self($content, Role::User); } @@ -81,7 +81,14 @@ public function jsonSerialize(): array ]; if (null !== $this->content) { - $array['content'] = $this->content; + $content = $this->content; + + if ($this->content instanceof ImageUrl) { + $content = []; + $content[] = $this->content; + } + + $array['content'] = $content; } if ($this->hasToolCalls() && $this->isToolCall()) { diff --git a/tests/Message/MessageBagTest.php b/tests/Message/MessageBagTest.php index f179d046..6ddca6ad 100644 --- a/tests/Message/MessageBagTest.php +++ b/tests/Message/MessageBagTest.php @@ -4,6 +4,7 @@ namespace PhpLlm\LlmChain\Tests\Message; +use PhpLlm\LlmChain\Message\ImageUrl; use PhpLlm\LlmChain\Message\Message; use PhpLlm\LlmChain\Message\MessageBag; use PHPUnit\Framework\Attributes\CoversClass; @@ -115,6 +116,8 @@ public function jsonSerialize(): void Message::forSystem('My amazing system prompt.'), Message::ofAssistant('It is time to sleep.'), Message::ofUser('Hello, world!'), + Message::ofUser('Analyze the following image:'), + Message::ofUser(new ImageUrl('http://example.com/image.jpg')), ); $json = json_encode($messageBag); @@ -125,6 +128,10 @@ public function jsonSerialize(): void ['role' => 'system', 'content' => 'My amazing system prompt.'], ['role' => 'assistant', 'content' => 'It is time to sleep.'], ['role' => 'user', 'content' => 'Hello, world!'], + ['role' => 'user', 'content' => 'Analyze the following image:'], + ['role' => 'user', 'content' => [ + ['type' => 'image_url', 'image_url' => ['url' => 'http://example.com/image.jpg']], + ]], ]), $json );