Filter uncanny-automator

automator_openai_image_generate_request_body

Filters the request body for generating OpenAI images, allowing modification before the API call.

add_filter( 'automator_openai_image_generate_request_body', $callback, 10, 2 );

Description

Filters the request body for OpenAI image generation. Developers can modify parameters like image size, quality, and number of images before the API call is made. This hook is ideal for customizing image generation settings or adding custom fields to the request.


Usage

add_filter( 'automator_openai_image_generate_request_body', 'your_function_name', 10, 2 );

Parameters

$request_body (mixed)
This filter allows modification of the request body being sent to the OpenAI API for image generation.
$this (mixed)
This parameter contains the data that will be sent to the OpenAI API to generate an image.

Return Value

The filtered value.


Examples

/**
 * Example of how to modify the request body for OpenAI image generation.
 * This example adds a 'size' parameter to the request if it's not already set,
 * defaulting to a common image size.
 */
add_filter(
	'automator_openai_image_generate_request_body',
	function ( $request_body, $openai_image_generator_instance ) {
		// Define a default image size if one isn't provided.
		$default_size = '1024x1024';

		// Check if the 'size' parameter is already present in the request body.
		if ( ! isset( $request_body['size'] ) || empty( $request_body['size'] ) ) {
			// If not, add the default size.
			$request_body['size'] = $default_size;
		}

		// You could also conditionally modify other parameters here, for instance:
		// if ( isset( $request_body['prompt'] ) && strpos( $request_body['prompt'], 'realistic' ) === false ) {
		//     $request_body['prompt'] .= ', photorealistic';
		// }

		// Always return the modified (or original) request body.
		return $request_body;
	},
	10, // Priority of the filter
	2   // Number of accepted arguments
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/open-ai/actions/open-ai-image-generate.php:135

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		$model             = $this->get_parsed_meta_value( 'MODEL', 'gpt-image-1' );
		$prompt            = $this->get_parsed_meta_value( 'PROMPT', '' );
		$image_size        = $this->get_parsed_meta_value( 'SIZE', '1024x1024' );
		$quality           = $this->get_parsed_meta_value( 'QUALITY', 'auto' );
		$image_format      = $this->get_parsed_meta_value( 'OUTPUT_FORMAT', 'png' );
		$background        = $this->get_parsed_meta_value( 'BACKGROUND', 'auto' );
		$moderation        = $this->get_parsed_meta_value( 'MODERATION', 'auto' );
		$compression_level = $this->get_parsed_meta_value( 'COMPRESSION', '' );

		$this->image_hydrator->set_output_format( $image_format );

		$request_body = array(
			'model'         => $model,
			'prompt'        => $prompt,
			'n'             => 1,
			'size'          => $image_size,
			'quality'       => $quality,
			'output_format' => $image_format,
			'background'    => $background,
			'moderation'    => $moderation,
		);

		if ( ! empty( $compression_level ) ) {
			$request_body['output_compression'] = intval( $compression_level );
		}

		$request_body = apply_filters( 'automator_openai_image_generate_request_body', $request_body, $this );

		$response = $this->api->openai_request( 'v1/images/generations', $request_body );

		$tokens = $this->image_hydrator->hydrate_from_response( $response );

		$this->hydrate_tokens( $tokens );

		return true;
	}

Scroll to Top