-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from aydinfatih/main
feat: Add batchEmbedContents method to embeddingModel
- Loading branch information
Showing
9 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gemini\Requests\GenerativeModel; | ||
|
||
use Gemini\Concerns\HasContents; | ||
use Gemini\Data\Blob; | ||
use Gemini\Data\Content; | ||
use Gemini\Enums\Method; | ||
use Gemini\Foundation\Request; | ||
use Gemini\Requests\Concerns\HasJsonBody; | ||
|
||
class BatchEmbedContentRequest extends Request | ||
{ | ||
use HasContents; | ||
use HasJsonBody; | ||
|
||
protected Method $method = Method::POST; | ||
|
||
/** | ||
* @param string $model The model's resource name. This serves as an ID for the Model to use. | ||
* @param array<string|Blob|array<string|Blob>|Content|EmbedContentRequest> $parts | ||
*/ | ||
public function __construct( | ||
protected readonly string $model, | ||
protected readonly array $parts, | ||
) {} | ||
|
||
public function resolveEndpoint(): string | ||
{ | ||
return "{$this->model}:batchEmbedContents"; | ||
} | ||
|
||
/** | ||
* Default body | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
protected function defaultBody(): array | ||
{ | ||
return [ | ||
'requests' => array_map( | ||
fn (EmbedContentRequest|string|Blob|array|Content $part) => [ | ||
'model' => $this->model, | ||
...match (true) { | ||
$part instanceof EmbedContentRequest => $part->body(), | ||
default => (new EmbedContentRequest( | ||
model: $this->model, | ||
part: $part | ||
))->body(), | ||
}, | ||
], | ||
$this->parts | ||
), | ||
]; | ||
} | ||
} |
This file contains 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
49 changes: 49 additions & 0 deletions
49
src/Responses/GenerativeModel/BatchEmbedContentsResponse.php
This file contains 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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gemini\Responses\GenerativeModel; | ||
|
||
use Gemini\Contracts\ResponseContract; | ||
use Gemini\Data\ContentEmbedding; | ||
use Gemini\Testing\Responses\Concerns\Fakeable; | ||
|
||
/** | ||
* https://ai.google.dev/api/rest/v1/models/embedContent#response-body | ||
*/ | ||
final class BatchEmbedContentsResponse implements ResponseContract | ||
{ | ||
use Fakeable; | ||
|
||
/** | ||
* @param array<ContentEmbedding> $embeddings | ||
*/ | ||
private function __construct( | ||
public readonly array $embeddings, | ||
) {} | ||
|
||
/** | ||
* @param array{ embeddings: array{ array{ values: array<float> } } } $attributes | ||
*/ | ||
public static function from(array $attributes): self | ||
{ | ||
$embeddings = array_map( | ||
static fn (array $embedding): ContentEmbedding => ContentEmbedding::from($embedding), | ||
$attributes['embeddings'], | ||
); | ||
|
||
return new self( | ||
embeddings: $embeddings | ||
); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'embeddings' => array_map( | ||
static fn (ContentEmbedding $embedding): array => $embedding->toArray(), | ||
$this->embeddings | ||
), | ||
]; | ||
} | ||
} |
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/Testing/Responses/Fixtures/GenerativeModel/BatchEmbedContentsResponseFixture.php
This file contains 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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gemini\Testing\Responses\Fixtures\GenerativeModel; | ||
|
||
final class BatchEmbedContentsResponseFixture | ||
{ | ||
public const ATTRIBUTES = [ | ||
'embeddings' => [ | ||
[ | ||
'values' => [ | ||
0.008624583, | ||
-0.030451821, | ||
-0.042496547, | ||
-0.029230341, | ||
0.05486475, | ||
0.006694871, | ||
0.004025645, | ||
], | ||
], | ||
[ | ||
'values' => [ | ||
0.008624583, | ||
-0.030451821, | ||
-0.042496547, | ||
-0.029230341, | ||
0.05486475, | ||
0.006694871, | ||
0.004025645, | ||
], | ||
], | ||
], | ||
]; | ||
} |
This file contains 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
62 changes: 62 additions & 0 deletions
62
tests/Responses/GenerativeModel/BatchEmbedContentsResponse.php
This file contains 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,62 @@ | ||
<?php | ||
|
||
use Gemini\Data\ContentEmbedding; | ||
use Gemini\Responses\GenerativeModel\BatchEmbedContentsResponse; | ||
|
||
test('from', function () { | ||
$response = BatchEmbedContentsResponse::from(BatchEmbedContentsResponse::fake()->toArray()); | ||
|
||
expect($response) | ||
->toBeInstanceOf(BatchEmbedContentsResponse::class) | ||
->embeddings->each->toBeInstanceOf(ContentEmbedding::class) | ||
->embeddings->each(fn ($embeddingModel) => $embeddingModel->values->toBeArray()->toMatchArray([ | ||
0.008624583, | ||
-0.030451821, | ||
-0.042496547, | ||
-0.029230341, | ||
0.05486475, | ||
0.006694871, | ||
0.004025645, | ||
])); | ||
}); | ||
|
||
test('fake', function () { | ||
$response = BatchEmbedContentsResponse::fake(); | ||
|
||
expect($response) | ||
->toBeInstanceOf(BatchEmbedContentsResponse::class) | ||
->embeddings->each->toBeInstanceOf(ContentEmbedding::class) | ||
->embeddings->each(fn ($embeddingModel) => $embeddingModel->values->toBeArray()->toMatchArray([ | ||
0.008624583, | ||
-0.030451821, | ||
-0.042496547, | ||
-0.029230341, | ||
0.05486475, | ||
0.006694871, | ||
0.004025645, | ||
])); | ||
}); | ||
|
||
test('to array', function () { | ||
$attributes = BatchEmbedContentsResponse::fake()->toArray(); | ||
$response = BatchEmbedContentsResponse::from($attributes); | ||
|
||
expect($response->toArray()) | ||
->toBeArray()->toBe($attributes); | ||
}); | ||
|
||
test('fake with override', function () { | ||
$response = BatchEmbedContentsResponse::fake([ | ||
'embeddings' => [ | ||
[ | ||
'values' => [ | ||
0, | ||
], | ||
], | ||
], | ||
]); | ||
expect($response) | ||
->embeddings->{0}->values->toBeArray()->toMatchArray([ | ||
0, | ||
]); | ||
}); |