wppaste
WordPress

AbstractOpenAiCompatibleImageGenerationModel

Since
0.1.0
Source
wp-includes/php-ai-client/src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleImageGenerationModel.php:57
Base class for an image generation model for providers that implement OpenAI's API format.

Description

This abstract class is designed to work with any AI provider that offers an OpenAI-compatible API endpoint for image generation, including but not limited to Anthropic, Google, and other providers that have adopted OpenAI's image generation API specification as a standard interface.

Compatibility

WordPress
since 0.1.0
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 0.1.0.

Methods · 9

Source code

abstract class AbstractOpenAiCompatibleImageGenerationModel extends AbstractApiBasedModel implements ImageGenerationModelInterface{    /**     * {@inheritDoc}     *     * @since 0.1.0     */    public function generateImageResult(array $prompt): GenerativeAiResult    {        $httpTransporter = $this->getHttpTransporter();        $params = $this->prepareGenerateImageParams($prompt);        $request = $this->createRequest(HttpMethodEnum::POST(), 'images/generations', ['Content-Type' => 'application/json'], $params);        // Add authentication credentials to the request.        $request = $this->getRequestAuthentication()->authenticateRequest($request);        // Send and process the request.        $response = $httpTransporter->send($request);        $this->throwIfNotSuccessful($response);        return $this->parseResponseToGenerativeAiResult($response, isset($params['output_format']) && is_string($params['output_format']) ? "image/{$params['output_format']}" : 'image/png');    }    /**     * Prepares the given prompt and the model configuration into parameters for the API request.     *     * @since 0.1.0     *     * @param list<Message> $prompt The prompt to generate an image for. Either a single message or a list of messages     *                              from a chat. However as of today, OpenAI compatible image generation endpoints only     *                              support a single user message.     * @return ImageGenerationParams The parameters for the API request.     */    protected function prepareGenerateImageParams(array $prompt): array    {        $config = $this->getConfig();        $params = ['model' => $this->metadata()->getId(), 'prompt' => $this->preparePromptParam($prompt)];        $candidateCount = $config->getCandidateCount();        if ($candidateCount !== null) {            $params['n'] = $candidateCount;        }        $outputFileType = $config->getOutputFileType();        if ($outputFileType !== null) {            $params['response_format'] = $outputFileType->isRemote() ? 'url' : 'b64_json';        } else {            // The 'response_format' parameter is required, so we default to 'b64_json' if not set.            $params['response_format'] = 'b64_json';        }        $outputMimeType = $config->getOutputMimeType();        if ($outputMimeType !== null) {            $params['output_format'] = preg_replace('/^image\//', '', $outputMimeType);        }        $outputMediaOrientation = $config->getOutputMediaOrientation();        $outputMediaAspectRatio = $config->getOutputMediaAspectRatio();        if ($outputMediaOrientation !== null || $outputMediaAspectRatio !== null) {            $params['size'] = $this->prepareSizeParam($outputMediaOrientation, $outputMediaAspectRatio);        }        /*         * Any custom options are added to the parameters as well.         * This allows developers to pass other options that may be more niche or not yet supported by the SDK.         */        $customOptions = $config->getCustomOptions();        foreach ($customOptions as $key => $value) {            if (isset($params[$key])) {                throw new InvalidArgumentException(sprintf('The custom option "%s" conflicts with an existing parameter.', $key));            }            $params[$key] = $value;        }        /** @var ImageGenerationParams $params */        return $params;    }    /**     * Prepares the prompt parameter for the API request.     *     * @since 0.1.0     *     * @param list<Message> $messages The messages to prepare. However as of today, OpenAI compatible image generation     *                                endpoints only support a single user message.     * @return string The prepared prompt parameter.     */    protected function preparePromptParam(array $messages): string    {        if (count($messages) !== 1) {            throw new InvalidArgumentException('The API requires a single user message as prompt.');

Changelog

Introduced in 0.1.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/php-ai-client/src/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleImageGenerationModel.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.