WordPress\AiClient\Providers\OpenAiCompatibleImplementation

AbstractOpenAiCompatibleImageGenerationModel::parseResponseChoiceToCandidateprotectedWP 0.1.0

Parses a single choice from the API response into a Candidate object.

Method of the class: AbstractOpenAiCompatibleImageGenerationModel{}

No Hooks.

Returns

Candidate. The parsed candidate.

Usage

// protected - for code of main (parent) or child class
$result = $this->parseResponseChoiceToCandidate( $choiceData, $index, $expectedMimeType ): Candidate;
$choiceData(ChoiceData) (required)
The choice data from the API response.
$index(int) (required)
The index of the choice in the choices array.
$expectedMimeType(string)
The expected MIME type the response is in.
Default: 'image/png'

Changelog

Since 0.1.0 Introduced.

AbstractOpenAiCompatibleImageGenerationModel::parseResponseChoiceToCandidate() code WP 7.0.2

protected function parseResponseChoiceToCandidate(array $choiceData, int $index, string $expectedMimeType = 'image/png'): Candidate
{
    if (isset($choiceData['url']) && is_string($choiceData['url'])) {
        $imageFile = new File($choiceData['url'], $expectedMimeType);
    } elseif (isset($choiceData['b64_json']) && is_string($choiceData['b64_json'])) {
        $imageFile = new File($choiceData['b64_json'], $expectedMimeType);
    } else {
        throw ResponseException::fromInvalidData($this->providerMetadata()->getName(), "choices[{$index}]", 'The value must contain either a url or b64_json key with a string value.');
    }
    $parts = [new MessagePart($imageFile)];
    $message = new Message(MessageRoleEnum::model(), $parts);
    return new Candidate($message, FinishReasonEnum::stop());
}