WordPress\AiClient\Providers\OpenAiCompatibleImplementation
AbstractOpenAiCompatibleImageGenerationModel::prepareGenerateImageParams
Prepares the given prompt and the model configuration into parameters for the API request.
Method of the class: AbstractOpenAiCompatibleImageGenerationModel{}
No Hooks.
Returns
ImageGenerationParams. The parameters for the API request.
Usage
// protected - for code of main (parent) or child class $result = $this->prepareGenerateImageParams( $prompt ): array;
- $prompt(list
) (required) - 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.
Changelog
| Since 0.1.0 | Introduced. |
AbstractOpenAiCompatibleImageGenerationModel::prepareGenerateImageParams() AbstractOpenAiCompatibleImageGenerationModel::prepareGenerateImageParams code WP 7.0
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;
}