Skip to content

Commit

Permalink
Support image url
Browse files Browse the repository at this point in the history
A simpler implementation of
* #25

cc @DZunke
  • Loading branch information
OskarStark committed Sep 23, 2024
1 parent dccc6dd commit 4791826
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/Message/ImageUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Message;

use Webmozart\Assert\Assert;

final readonly class ImageUrl implements \JsonSerializable
{
public function __construct(
public string $url,
) {
Assert::stringNotEmpty($url);
Assert::startsWith($url, 'http');
}

/**
* @return array{
* type: 'image_url',
* image_url: array{url: non-empty-string}
* }
*/
public function jsonSerialize(): array
{
return [
'type' => 'image_url',
'image_url' => [
'url' => $this->url,
],
];
}
}
13 changes: 10 additions & 3 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Message/MessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
);
Expand Down

0 comments on commit 4791826

Please sign in to comment.