Skip to content

Commit

Permalink
ImagePart extend FilePart to keep compatibility and add content funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
AstritA authored and erdemkose committed Jan 13, 2025
1 parent f4cd5a1 commit a408659
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
38 changes: 37 additions & 1 deletion src/Resources/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GeminiAPI\Enums\MimeType;
use GeminiAPI\Enums\Role;
use GeminiAPI\Resources\Parts\FilePart;
use GeminiAPI\Traits\ArrayTypeValidator;
use GeminiAPI\Resources\Parts\ImagePart;
use GeminiAPI\Resources\Parts\PartInterface;
Expand Down Expand Up @@ -40,6 +41,13 @@ public function addImage(MimeType $mimeType, string $image): self
return $this;
}

public function addFile(MimeType $mimeType, string $file): self
{
$this->parts[] = new FilePart($mimeType, $file);

return $this;
}

public static function text(
string $text,
Role $role = Role::User,
Expand All @@ -65,6 +73,19 @@ public static function image(
);
}

public static function file(
MimeType $mimeType,
string $file,
Role $role = Role::User
): self {
return new self(
[
new FilePart($mimeType, $file),
],
$role,
);
}

public static function textAndImage(
string $text,
MimeType $mimeType,
Expand All @@ -80,6 +101,21 @@ public static function textAndImage(
);
}

public static function textAndFile(
string $text,
MimeType $mimeType,
string $file,
Role $role = Role::User,
): self {
return new self(
[
new TextPart($text),
new FilePart($mimeType, $file),
],
$role,
);
}

/**
* @param array{
* parts: array<int, array{text?: string, inlineData?: array{mimeType: string, data: string}}>,
Expand All @@ -97,7 +133,7 @@ public static function fromArray(array $content): self

if (!empty($part['inlineData'])) {
$mimeType = MimeType::from($part['inlineData']['mimeType']);
$parts[] = new ImagePart($mimeType, $part['inlineData']['data']);
$parts[] = new FilePart($mimeType, $part['inlineData']['data']);
}
}

Expand Down
35 changes: 1 addition & 34 deletions src/Resources/Parts/ImagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,6 @@

namespace GeminiAPI\Resources\Parts;

use GeminiAPI\Enums\MimeType;
use JsonSerializable;

use function json_encode;

class ImagePart implements PartInterface, JsonSerializable
class ImagePart extends FilePart
{
public function __construct(
public readonly MimeType $mimeType,
public readonly string $data,
) {
}

/**
* @return array{
* inlineData: array{
* mimeType: string,
* data: string,
* },
* }
*/
public function jsonSerialize(): array
{
return [
'inlineData' => [
'mimeType' => $this->mimeType->value,
'data' => $this->data,
],
];
}

public function __toString(): string
{
return json_encode($this) ?: '';
}
}

0 comments on commit a408659

Please sign in to comment.