WordPress\AiClient\Providers\OpenAiCompatibleImplementation

AbstractOpenAiCompatibleImageGenerationModel::preparePromptParamprotectedWP 0.1.0

Prepares the prompt parameter for the API request.

Method of the class: AbstractOpenAiCompatibleImageGenerationModel{}

No Hooks.

Returns

String. The prepared prompt parameter.

Usage

// protected - for code of main (parent) or child class
$result = $this->preparePromptParam( $messages ): string;
$messages(list) (required)
The messages to prepare. However as of today, OpenAI compatible image generation endpoints only support a single user message.

Changelog

Since 0.1.0 Introduced.

AbstractOpenAiCompatibleImageGenerationModel::preparePromptParam() code WP 7.0

protected function preparePromptParam(array $messages): string
{
    if (count($messages) !== 1) {
        throw new InvalidArgumentException('The API requires a single user message as prompt.');
    }
    $message = $messages[0];
    if (!$message->getRole()->isUser()) {
        throw new InvalidArgumentException('The API requires a user message as prompt.');
    }
    $text = null;
    foreach ($message->getParts() as $part) {
        $text = $part->getText();
        if ($text !== null) {
            break;
        }
    }
    if ($text === null) {
        throw new InvalidArgumentException('The API requires a single text message part as prompt.');
    }
    return $text;
}