Skip to content

Commit

Permalink
Merge pull request #9 from aydinfatih/main
Browse files Browse the repository at this point in the history
fix: Fixed an error due to recitation finish reason
  • Loading branch information
aydinfatih authored Apr 23, 2024
2 parents 6e48284 + e160637 commit 0653619
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Data/Candidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Gemini\Contracts\Arrayable;
use Gemini\Enums\FinishReason;
use Gemini\Enums\Role;

/**
* A response candidate generated from the model.
Expand Down Expand Up @@ -33,22 +34,30 @@ public function __construct(
}

/**
* @param array{ content: array{ parts: array{ array{ text: ?string, inlineData: array{ mimeType: string, data: string } } }, role: string }, finishReason: string, safetyRatings: array{ array{ category: string, probability: string, blocked: ?bool } }, citationMetadata: ?array{ citationSources: array{ array{ startIndex: int, endIndex: int, uri: string, license: string} } }, index: int, tokenCount: ?int } $attributes
* @param array{ content: ?array{ parts: array{ array{ text: ?string, inlineData: array{ mimeType: string, data: string } } }, role: string }, finishReason: string, safetyRatings: ?array{ array{ category: string, probability: string, blocked: ?bool } }, citationMetadata: ?array{ citationSources: array{ array{ startIndex: int, endIndex: int, uri: string, license: string} } }, index: int, tokenCount: ?int } $attributes
*/
public static function from(array $attributes): self
{
$safetyRatings = array_map(
static fn (array $rating): SafetyRating => SafetyRating::from($rating),
$attributes['safetyRatings'],
);
$safetyRatings = match (true) {
isset($attributes['safetyRatings']) => array_map(
static fn (array $rating): SafetyRating => SafetyRating::from($rating),
$attributes['safetyRatings'],
),
default => [],
};

$citationMetadata = match (true) {
isset($attributes['citationMetadata']) => CitationMetadata::from($attributes['citationMetadata']),
default => new CitationMetadata(),
};

$content = match (true) {
isset($attributes['content']) => Content::from($attributes['content']),
default => new Content(parts: [], role: Role::MODEL),
};

return new self(
content: Content::from($attributes['content']),
content: $content,
finishReason: FinishReason::from($attributes['finishReason']),
safetyRatings: $safetyRatings,
citationMetadata: $citationMetadata,
Expand Down
30 changes: 30 additions & 0 deletions tests/Responses/GenerativeModel/GenerateContentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@
->promptFeedback->toBeInstanceOf(PromptFeedback::class);
});

test('recitation finish reason', function () {
$response = GenerateContentResponse::from([
'candidates' => [
[
'finishReason' => FinishReason::RECITATION->value,
'index' => 0,
],
],
]);

expect($response)
->toBeInstanceOf(GenerateContentResponse::class)
->candidates->each->toBeInstanceOf(Candidate::class)
->candidates->toHaveCount(1)
->candidates->{0}->content->parts->toBeEmpty()
->candidates->{0}->safetyRatings->toBeEmpty()
->candidates->{0}->citationMetadata->citationSources->toBeEmpty()
->candidates->{0}->index->toEqual(0)
->candidates->{0}->tokenCount->toBeNull()
->candidates->{0}->finishReason->toEqual(FinishReason::RECITATION)
->and(fn () => $response->text())
->toThrow(function (ValueError $e) {
expect($e->getMessage())
->toBe('The `GenerateContentResponse::text()` quick accessor only works when the response contains a valid '.
'`Part`, but none was returned. Check the `candidate.safety_ratings` to see if the '.
'response was blocked.');
});

});

test('fake', function () {
$response = GenerateContentResponse::fake();

Expand Down

0 comments on commit 0653619

Please sign in to comment.