Skip to content

Commit

Permalink
fix OpenAI trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Nov 7, 2023
1 parent 3868b80 commit a765c73
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
5 changes: 4 additions & 1 deletion includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function get_default_settings() {
ChatGPT::ID => [
'api_key' => '',
'number_of_suggestions' => 1,
'authenticated' => false,
'condense_text_prompt' => array(
array(
'title' => esc_html__( 'Decrease the content length no more than 2 to 4 sentences.', 'classifai' ),
Expand Down Expand Up @@ -179,7 +180,9 @@ public function sanitize_settings( $settings ) {
}

if ( isset( $settings[ ChatGPT::ID ] ) ) {
$new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings );
$api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings );
$new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key'];
$new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated'];
$new_settings[ ChatGPT::ID ]['number_of_suggestions'] = $chat_gpt->sanitize_number_of_responses_field( 'number_of_suggestions', $settings );
$new_settings[ ChatGPT::ID ]['condense_text_prompt'] = $chat_gpt->sanitize_prompts( 'condense_text_prompt', $settings );
$new_settings[ ChatGPT::ID ]['expand_text_prompt'] = $chat_gpt->sanitize_prompts( 'expand_text_prompt', $settings );
Expand Down
5 changes: 4 additions & 1 deletion includes/Classifai/Features/ExcerptGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public function get_default_settings() {
'provider' => \Classifai\Providers\OpenAI\ChatGPT::ID,
ChatGPT::ID => [
'api_key' => '',
'authenticated' => false,
'generate_excerpt_prompt' => array(
array(
'title' => esc_html__( 'Default', 'classifai' ),
Expand Down Expand Up @@ -174,7 +175,9 @@ public function sanitize_settings( $settings ) {
}

if ( isset( $settings[ ChatGPT::ID ] ) ) {
$new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings );
$api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings );
$new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key'];
$new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated'];
$new_settings[ ChatGPT::ID ]['generate_excerpt_prompt'] = $chat_gpt->sanitize_prompts( 'generate_excerpt_prompt', $settings );
}

Expand Down
21 changes: 4 additions & 17 deletions includes/Classifai/Features/TitleGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ public function setup_fields_sections() {
]
);

add_settings_field(
'length',
esc_html__( 'Excerpt length', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'length',
'input_type' => 'number',
'min' => 1,
'step' => 1,
'default_value' => $default_settings['length'],
'description' => __( 'How many words should the excerpt be? Note that the final result may not exactly match this. In testing, ChatGPT tended to exceed this number by 10-15 words.', 'classifai' ),
]
);

add_settings_field(
'provider',
esc_html__( 'Select a provider', 'classifai' ),
Expand Down Expand Up @@ -142,6 +126,7 @@ public function get_default_settings() {
ChatGPT::ID => [
'api_key' => '',
'number_of_titles' => 1,
'authenticated' => false,
'generate_title_prompt' => array(
array(
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
Expand Down Expand Up @@ -176,7 +161,9 @@ public function sanitize_settings( $settings ) {
}

if ( isset( $settings[ ChatGPT::ID ] ) ) {
$new_settings[ ChatGPT::ID ]['api_key'] = $chat_gpt->sanitize_api_key( $settings );
$api_key_settings = $chat_gpt->sanitize_api_key_settings( $settings );
$new_settings[ ChatGPT::ID ]['api_key'] = $api_key_settings[ ChatGPT::ID ]['api_key'];
$new_settings[ ChatGPT::ID ]['authenticated'] = $api_key_settings[ ChatGPT::ID ]['authenticated'];
$new_settings[ ChatGPT::ID ]['number_of_titles'] = $chat_gpt->sanitize_number_of_responses_field( 'number_of_titles', $settings );
$new_settings[ ChatGPT::ID ]['generate_title_prompt'] = $chat_gpt->sanitize_prompts( 'generate_title_prompt', $settings );
}
Expand Down
13 changes: 3 additions & 10 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ class ChatGPT extends Provider {
*/
protected $grow_content_prompt = 'Increase the content length no more than 2 to 4 sentences.';

/**
* Feature instance.
*
* @var \Classifai\Features\Feature
*/
protected $feature_instance = null;

/**
* OpenAI ChatGPT constructor.
*
Expand Down Expand Up @@ -590,7 +583,7 @@ public function generate_excerpt( int $post_id = 0, array $args = [] ) {

$excerpt_length = absint( $settings['length'] ?? 55 );

$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() );
$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() );

$excerpt_prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['generate_excerpt_prompt'] ) ?? $this->generate_excerpt_prompt );

Expand Down Expand Up @@ -694,7 +687,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) {
return new WP_Error( 'not_enabled', esc_html__( 'Title generation is disabled or OpenAI authentication failed. Please check your settings.', 'classifai' ) );
}

$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() );
$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() );

$prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['generate_title_prompt'] ) ?? $this->generate_title_prompt );

Expand Down Expand Up @@ -795,7 +788,7 @@ public function resize_content( int $post_id, array $args = array() ) {
]
);

$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $this->get_option_name() );
$request = new APIRequest( $settings[ static::ID ]['api_key'] ?? '', $feature->get_option_name() );

if ( 'shrink' === $args['resize_type'] ) {
$prompt = esc_textarea( $this->get_default_prompt( $settings[ static::ID ]['shrink_content_prompt'] ) ?? $this->shrink_content_prompt );
Expand Down
27 changes: 14 additions & 13 deletions includes/Classifai/Providers/OpenAI/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ trait OpenAI {
* @param string $default_api_key Default API key.
*/
protected function setup_api_fields( string $default_api_key = '' ) {
$existing_settings = $this->get_settings();
$existing_settings = $this->feature_instance->get_settings();
$description = '';

// Add the settings section.
add_settings_section(
$this->get_option_name(),
$this->feature_instance->get_option_name(),
$this->provider_service_name,
function() {
printf(
Expand All @@ -47,7 +47,7 @@ function() {
esc_url( 'https://platform.openai.com/signup' )
);
},
$this->get_option_name()
$this->feature_instance->get_option_name()
);

// Determine which other OpenAI provider to look for an API key in.
Expand Down Expand Up @@ -77,8 +77,8 @@ function() {
'api-key',
esc_html__( 'API Key', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name(),
$this->feature_instance->get_option_name(),
$this->feature_instance->get_option_name(),
[
'label_for' => 'api_key',
'input_type' => 'password',
Expand All @@ -95,17 +95,18 @@ function() {
* @param array $old_settings Existing settings, if any.
* @return array
*/
protected function sanitize_api_key_settings( array $new_settings = [], array $old_settings = [] ) {
$authenticated = $this->authenticate_credentials( $old_settings['api_key'] ?? '' );
public function sanitize_api_key_settings( array $old_settings = [] ) {
$new_settings = $this->feature_instance->get_settings();
$authenticated = $this->authenticate_credentials( $old_settings[ static::ID ]['api_key'] ?? '' );

if ( is_wp_error( $authenticated ) ) {
$new_settings['authenticated'] = false;
$error_message = $authenticated->get_error_message();
$new_settings[ static::ID ]['authenticated'] = false;
$error_message = $authenticated->get_error_message();

// For response code 429, credentials are valid but rate limit is reached.
if ( 429 === (int) $authenticated->get_error_code() ) {
$new_settings['authenticated'] = true;
$error_message = str_replace( 'plan and billing details', '<a href="https://platform.openai.com/account/billing/overview" target="_blank" rel="noopener">plan and billing details</a>', $error_message );
$new_settings[ static::ID ]['authenticated'] = true;
$error_message = str_replace( 'plan and billing details', '<a href="https://platform.openai.com/account/billing/overview" target="_blank" rel="noopener">plan and billing details</a>', $error_message );
} else {
$error_message = str_replace( 'https://platform.openai.com/account/api-keys', '<a href="https://platform.openai.com/account/api-keys" target="_blank" rel="noopener">https://platform.openai.com/account/api-keys</a>', $error_message );
}
Expand All @@ -117,10 +118,10 @@ protected function sanitize_api_key_settings( array $new_settings = [], array $o
'error'
);
} else {
$new_settings['authenticated'] = true;
$new_settings[ static::ID ]['authenticated'] = true;
}

$new_settings['api_key'] = sanitize_text_field( $old_settings['api_key'] ?? '' );
$new_settings[ static::ID ]['api_key'] = sanitize_text_field( $old_settings[ static::ID ]['api_key'] ?? '' );

return $new_settings;
}
Expand Down
6 changes: 6 additions & 0 deletions includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ abstract class Provider {
*/
public $onboarding_options;

/**
* Feature instance.
*
* @var \Classifai\Features\Feature
*/
protected $feature_instance = null;

/**
* Provider constructor.
Expand Down

0 comments on commit a765c73

Please sign in to comment.