Filter
uncanny-automator
automator_openai_image_generate
Filters the request body sent to OpenAI for image generation, allowing for modification before processing.
add_filter( 'automator_openai_image_generate', $callback, 10, 1 );
Description
This filter hook allows developers to modify the payload sent to OpenAI for image generation. Use it to customize parameters like model, prompt, or size before the API call is made. It fires after the initial request arguments are prepared and before the API request is sent.
Usage
add_filter( 'automator_openai_image_generate', 'your_function_name', 10, 1 );
Parameters
-
$body(mixed) - This parameter contains the sanitized text prompt that will be sent to the OpenAI API to generate an image.
Return Value
The filtered value.
Examples
add_filter(
'automator_openai_image_generate',
function( $body ) {
// Example: Modify the image generation prompt to include a negative prompt.
// This is a hypothetical modification as the OpenAI API for DALL-E 2 doesn't directly support negative prompts in this way.
// However, this demonstrates how you *could* add or modify parameters.
// Check if the prompt exists and if we can add a hypothetical negative prompt instruction.
if ( isset( $body['prompt'] ) && is_string( $body['prompt'] ) ) {
// In a real scenario, you might add an instruction for the AI model to avoid certain elements.
// For DALL-E 2, prompt engineering is key. This example simulates adding a constraint.
$body['prompt'] .= ', avoid explicit gore and violence';
}
// You could also modify other parameters like 'size' or 'n' if needed.
// Example: If you wanted to generate 2 images instead of 1.
// $body['n'] = 2;
return $body;
},
10, // Priority
1 // Accepted args
);
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-dall-e.php:214
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$prompt = isset( $parsed[ $this->get_action_meta() ] ) ? sanitize_textarea_field( $parsed[ $this->get_action_meta() ] ) : '';
$size = isset( $parsed['SIZE'] ) ? $parsed['SIZE'] : '1024x1024';
$model = isset( $parsed['MODEL'] ) ? $parsed['MODEL'] : $this->default_model;
$request_args = array(
'model' => $model,
'prompt' => $prompt,
'size' => $size,
'n' => 1,
);
$body = $this->create_body( $model, $request_args );
$body = apply_filters( 'automator_openai_image_generate', $body );
$response = $this->api->openai_request( 'v1/images/generations', $body );
$attachment_id = $this->insert_to_media( $response['data'][0]['url'], $prompt );
$this->hydrate_tokens_from_response( $response, $attachment_id );
return true;
}