WordPress\AiClient\Providers\OpenAiCompatibleImplementation

AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessagePartsprotectedWP 0.1.0

Parses the message parts from a choice in the API response.

Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}

No Hooks.

Returns

MessagePart[]. The parsed message parts.

Usage

// protected - for code of main (parent) or child class
$result = $this->parseResponseChoiceMessageParts( $messageData, $index ): array;
$messageData(MessageData) (required)
The message data from the API response.
$index(int) (required)
The index of the choice in the choices array.

Changelog

Since 0.1.0 Introduced.

AbstractOpenAiCompatibleTextGenerationModel::parseResponseChoiceMessageParts() code WP 7.0

protected function parseResponseChoiceMessageParts(array $messageData, int $index): array
{
    $parts = [];
    if (isset($messageData['reasoning_content']) && is_string($messageData['reasoning_content'])) {
        $parts[] = new MessagePart($messageData['reasoning_content'], MessagePartChannelEnum::thought());
    }
    if (isset($messageData['content']) && is_string($messageData['content'])) {
        $parts[] = new MessagePart($messageData['content']);
    }
    if (isset($messageData['tool_calls']) && is_array($messageData['tool_calls'])) {
        foreach ($messageData['tool_calls'] as $toolCallIndex => $toolCallData) {
            $toolCallPart = $this->parseResponseChoiceMessageToolCallPart($toolCallData);
            if (!$toolCallPart) {
                throw ResponseException::fromInvalidData($this->providerMetadata()->getName(), "choices[{$index}].message.tool_calls[{$toolCallIndex}]", 'The response includes a tool call of an unexpected type.');
            }
            $parts[] = $toolCallPart;
        }
    }
    return $parts;
}