WordPress\AiClient\Providers\OpenAiCompatibleImplementation
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceToCandidate
Parses a single choice from the API response into a Candidate object.
Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}
No Hooks.
Returns
Candidate. The parsed candidate.
Usage
// protected - for code of main (parent) or child class $result = $this->parseResponseChoiceToCandidate( $choiceData, $index ): Candidate;
- $choiceData(ChoiceData) (required)
- The choice data from the API response.
- $index(int) (required)
- The index of the choice in the choices array.
Changelog
| Since 0.1.0 | Introduced. |
AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceToCandidate() AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceToCandidate code WP 7.0
protected function parseResponseChoiceToCandidate(array $choiceData, int $index): Candidate
{
if (!isset($choiceData['message']) || !is_array($choiceData['message']) || array_is_list($choiceData['message'])) {
throw ResponseException::fromMissingData($this->providerMetadata()->getName(), "choices[{$index}].message");
}
if (!isset($choiceData['finish_reason']) || !is_string($choiceData['finish_reason'])) {
throw ResponseException::fromMissingData($this->providerMetadata()->getName(), "choices[{$index}].finish_reason");
}
$messageData = $choiceData['message'];
$message = $this->parseResponseChoiceMessage($messageData, $index);
switch ($choiceData['finish_reason']) {
case 'stop':
$finishReason = FinishReasonEnum::stop();
break;
case 'length':
$finishReason = FinishReasonEnum::length();
break;
case 'content_filter':
$finishReason = FinishReasonEnum::contentFilter();
break;
case 'tool_calls':
$finishReason = FinishReasonEnum::toolCalls();
break;
default:
throw ResponseException::fromInvalidData($this->providerMetadata()->getName(), "choices[{$index}].finish_reason", sprintf('Invalid finish reason "%s".', $choiceData['finish_reason']));
}
return new Candidate($message, $finishReason);
}