Skip to content

Commit

Permalink
feat: add gpt-4o-mini
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Sep 18, 2024
1 parent 964f9e0 commit a8951b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
43 changes: 43 additions & 0 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,27 @@ public function register_settings(): void {
$input['auto_generate'] = isset( $input['auto_generate'] ) && $input['auto_generate'];
$input['detail'] = isset( $input['detail'] ) ? sanitize_text_field( $input['detail'] ) : 'low';

// Sanitize and validate the model.
if ( isset( $input['model'] ) && in_array( $input['model'], AltGeneratorPlugin::SUPPORTED_MODELS, true ) ) {
$input['model'] = sanitize_text_field( $input['model'] );
} else {
$input['model'] = AltGeneratorPlugin::DEFAULT_MODEL;
add_settings_error(
AltGeneratorPlugin::OPTION_NAME,
'invalid_model',
sprintf(
// translators: %s is for model name.
__( 'Invalid model selected. Default model (%s) has been set.', 'alt-text-generator-gpt-vision' ),
AltGeneratorPlugin::DEFAULT_MODEL
)
);
}

return $input;
},
'default' => [
'api_key' => null,
'model' => AltGeneratorPlugin::DEFAULT_MODEL,
'auto_generate' => false,
'detail' => 'low',
],
Expand Down Expand Up @@ -101,6 +118,32 @@ function () use ( $options ) {
]
);

add_settings_field(
'acpl_ai_alt_generator_model',
__( 'Model', 'alt-text-generator-gpt-vision' ),
function () use ( $options ) {
printf( '<select id="model" name="%s[model]">', esc_attr( AltGeneratorPlugin::OPTION_NAME ) );
foreach ( AltGeneratorPlugin::SUPPORTED_MODELS as $model ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $model ),
selected( $options['model'] ?? AltGeneratorPlugin::DEFAULT_MODEL, $model, false ),
esc_html( $model )
);
}
echo '</select>';

echo '<p class="description">' .
esc_html__( 'Select the OpenAI model to use for generating alt text. Different models may have varying capabilities and costs.', 'alt-text-generator-gpt-vision' ) .
'</p>';
},
'media',
self::SETTINGS_SECTION_ID,
[
'label_for' => 'model',
]
);

add_settings_field(
'acpl_ai_alt_generator_auto_generate',
__( 'Auto generate alt text on image upload', 'alt-text-generator-gpt-vision' ),
Expand Down
3 changes: 1 addition & 2 deletions includes/AltGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class AltGenerator {
const API_URL = 'https://api.openai.com/v1/chat/completions';
const MODEL = 'gpt-4o';

public static function generate_alt_text( int $attachment_id, string $user_prompt = '' ): string|WP_Error {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
Expand Down Expand Up @@ -42,7 +41,7 @@ public static function generate_alt_text( int $attachment_id, string $user_promp
'httpversion' => '1.1',
'body' => json_encode(
[
'model' => self::MODEL,
'model' => $options['model'] ?? AltGeneratorPlugin::DEFAULT_MODEL,
'messages' => [
[
'role' => 'system',
Expand Down
7 changes: 6 additions & 1 deletion includes/AltGeneratorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
use WP_Error;

class AltGeneratorPlugin {
public const OPTION_NAME = 'acpl_ai_alt_generator';
public const OPTION_NAME = 'acpl_ai_alt_generator';
public const DEFAULT_MODEL = 'gpt-4o';
public const SUPPORTED_MODELS = [
'gpt-4o',
'gpt-4o-mini',
];

public function __construct() {
add_filter( 'wp_generate_attachment_metadata', [ AltGenerator::class, 'on_attachment_upload' ], 10, 3 );
Expand Down

0 comments on commit a8951b4

Please sign in to comment.