Breakdance4fun Logo

Breakdance AI : how to use other OpenAI models

Breakdance AI lets us choose between these 3 models:

  • GPT-3.5 Turbo (gpt-3.5-turbo)
  • GPT-4 Turbo (gpt-4-turbo-preview)
  • GPT-4o

If you want to use another model from OpenAI, like for example gpt-4o-mini , you can use one of these two methods:

Method 1 - safe and easy

Simply add this PHP snippet (in a snippet manager like WPCodeBox for example):

<?php

add_filter('breakdance_ai_model', function($model_version) {
return 'gpt-4o-mini';
});

The full list of models available from OpenAI can be found here, so if new models are added in the future, you can update the snippet:

https://platform.openai.com/docs/models/

Important: Some models may not work properly with Breakdance. Only those on the Breakdance AI settings page are tested. Use this snippet as your own risk! 

Method 2 - risky but better

This script adds an option in the WordPress settings, allowing you to automatically retrieve all OpenAI models and select your desired one.

The script was made with AI, so USE IT AT YOUR OWN RISK !

Breakdance Ai Model Selector
<?php
/*
Plugin Name: AI Model Selector
Description: A simple plugin to select an AI model with a settings page, fetching models from OpenAI.
Version: 1.0
Author: Your Name
*/

// Add menu item to the admin panel
function ai_model_selector_menu() {
add_options_page('BD AI Model Selector', 'BD AI Model Selector', 'manage_options', 'ai-model-selector', 'ai_model_selector_page');
}
add_action('admin_menu', 'ai_model_selector_menu');

// Create the settings page
function ai_model_selector_page() {
?>
<div class="wrap">
<h1>BREAKDANCE AI Model Selector</h1>
<form method="post" action="options.php">
<?php
settings_fields('ai_model_selector_options');
do_settings_sections('ai-model-selector');
submit_button();
?>
</form>
<button id="fetch-models" class="button button-secondary">Fetch GPT Models</button>
</div>
<script>
jQuery(document).ready(function($) {
$('#fetch-models').click(function(e) {
e.preventDefault();
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'fetch_and_save_models'
},
success: function(response) {
if(response.success) {
alert('GPT models fetched and saved successfully!');
location.reload();
} else {
alert('Error: ' + response.data);
}
}
});
});
});
</script>
<?php
}

// Register settings
function ai_model_selector_settings() {
register_setting('ai_model_selector_options', 'ai_model_selector_model');
add_settings_section('ai_model_selector_main', 'Main Settings', 'ai_model_selector_section_text', 'ai-model-selector');
add_settings_field('ai_model_selector_model', 'AI Model', 'ai_model_selector_model_dropdown', 'ai-model-selector', 'ai_model_selector_main');
}
add_action('admin_init', 'ai_model_selector_settings');

// Section text
function ai_model_selector_section_text() {
echo '<p>Select the AI model you want to use:</p>';
}

// Fetch models from OpenAI and save to database
function ai_model_selector_fetch_and_save_models() {
$api_key = Breakdance\APIKeys\getKey('openai');
if (empty($api_key)) {
wp_send_json_error('API key is not set.');
}

$response = wp_remote_get('https://api.openai.com/v1/models', array(
'headers' => array(
'Authorization' => 'Bearer ' . $api_key,
),
));

if (is_wp_error($response)) {
wp_send_json_error('Failed to fetch models from OpenAI.');
}

$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

if (!isset($data['data']) || !is_array($data['data'])) {
wp_send_json_error('Invalid response from OpenAI.');
}

$models = array();
foreach ($data['data'] as $model) {
if (strpos($model['id'], 'gpt') === 0) {
$models[$model['id']] = $model['id'];
}
}

update_option('ai_model_selector_models', $models);
wp_send_json_success();
}
add_action('wp_ajax_fetch_and_save_models', 'ai_model_selector_fetch_and_save_models');

// Create the dropdown
function ai_model_selector_model_dropdown() {
$models = get_option('ai_model_selector_models', array());
$selected_model = get_option('ai_model_selector_model', '');

if (empty($models)) {
echo "<p>No GPT models available. Please fetch models using the button below.</p>";
return;
}

echo "<select name='ai_model_selector_model'>";
foreach ($models as $value => $label) {
$selected = ($selected_model === $value) ? 'selected' : '';
echo "<option value='$value' $selected>$label</option>";
}
echo "</select>";
}

// Add the filter
function ai_model_selector_add_filter() {
$selected_model = get_option('ai_model_selector_model', '');
add_filter('breakdance_ai_model', function($model_version) use ($selected_model) {
return $selected_model;
});
}
add_action('init', 'ai_model_selector_add_filter');