WordPress\AiClient\Providers\OpenAiCompatibleImplementation

AbstractOpenAiCompatibleTextGenerationModel::getMessagePartToolCallDataprotectedWP 0.1.0

Returns the OpenAI API specific tool calls data for a message part.

Method of the class: AbstractOpenAiCompatibleTextGenerationModel{}

No Hooks.

Returns

?Array. mixed> The data for the message tool call part, or null if not applicable.

Usage

// protected - for code of main (parent) or child class
$result = $this->getMessagePartToolCallData( $part ): ?array;
$part(MessagePart) (required)
The message part to get the data for.

Changelog

Since 0.1.0 Introduced.

AbstractOpenAiCompatibleTextGenerationModel::getMessagePartToolCallData() code WP 7.0

protected function getMessagePartToolCallData(MessagePart $part): ?array
{
    $type = $part->getType();
    if ($type->isFunctionCall()) {
        $functionCall = $part->getFunctionCall();
        if (!$functionCall) {
            // This should be impossible due to class internals, but still needs to be checked.
            throw new RuntimeException('The function call typed message part must contain a function call.');
        }
        $args = $functionCall->getArgs();
        /*
         * Ensure null or empty arrays become empty objects for JSON encoding.
         * While in theory the JSON schema could also dictate a type of
         * 'array', in practice function arguments are typically of type
         * 'object'. More importantly, the OpenAI API specification seems
         * to expect that, and does not support passing arrays as the root
         * value. The null check handles the case where FunctionCall normalizes
         * empty arrays to null.
         */
        if ($args === null || is_array($args) && count($args) === 0) {
            $args = new \stdClass();
        }
        return ['type' => 'function', 'id' => $functionCall->getId(), 'function' => ['name' => $functionCall->getName(), 'arguments' => json_encode($args)]];
    }
    // All other types are handled in `getMessagePartContentData()`.
    return null;
}