Skip to content

Commit

Permalink
Move number of suggestions field to provider (ChatGPT).
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Feb 8, 2024
1 parent e9a71af commit 41e5e33
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 50 deletions.
22 changes: 2 additions & 20 deletions includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,22 +236,6 @@ public function get_enable_description(): string {
public function add_custom_settings_fields() {
$settings = $this->get_settings();

add_settings_field(
'number_of_suggestions',
esc_html__( 'Number of suggestions', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'number_of_suggestions',
'input_type' => 'number',
'min' => 1,
'step' => 1,
'default_value' => $settings['number_of_suggestions'],
'description' => esc_html__( 'Number of suggestions that will be generated in one request.', 'classifai' ),
]
);

add_settings_field(
'condense_text_prompt',
esc_html__( 'Condense text prompt', 'classifai' ),
Expand Down Expand Up @@ -288,7 +272,6 @@ public function add_custom_settings_fields() {
*/
public function get_feature_default_settings(): array {
return [
'number_of_suggestions' => 1,
'condense_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
Expand Down Expand Up @@ -316,9 +299,8 @@ public function get_feature_default_settings(): array {
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['number_of_suggestions'] = sanitize_number_of_responses_field( 'number_of_suggestions', $new_settings, $settings );
$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );
$new_settings['condense_text_prompt'] = sanitize_prompts( 'condense_text_prompt', $new_settings );
$new_settings['expand_text_prompt'] = sanitize_prompts( 'expand_text_prompt', $new_settings );

return $new_settings;
}
Expand Down
12 changes: 6 additions & 6 deletions includes/Classifai/Providers/GoogleAI/GeminiAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public function resize_content( int $post_id, array $args = array() ) {
$args = wp_parse_args(
array_filter( $args ),
[
'num' => $settings['number_of_suggestions'] ?? 1,
'num' => 1, // Gemini API only returns 1 variation as of now.
]
);

Expand Down Expand Up @@ -581,16 +581,16 @@ public function get_debug_information(): array {

if ( $this->feature_instance instanceof TitleGeneration ) {
$debug_info[ __( 'No. of titles', 'classifai' ) ] = 1;
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_title_prompt'] ?? [] );
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_title_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_title_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ExcerptGeneration ) {
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_excerpt_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ContentResizing ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_googleai_gemini_api_content_resizing_latest_response' ) );
}

Expand Down
93 changes: 69 additions & 24 deletions includes/Classifai/Providers/OpenAI/ChatGPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,47 @@ public function render_provider_fields() {
]
);

add_settings_field(
'number_of_titles',
esc_html__( 'Number of titles', 'classifai' ),
[ $this->feature_instance, 'render_input' ],
$this->feature_instance->get_option_name(),
$this->feature_instance->get_option_name() . '_section',
[
'option_index' => static::ID,
'label_for' => 'number_of_titles',
'input_type' => 'number',
'min' => 1,
'step' => 1,
'default_value' => $settings['number_of_titles'],
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
'description' => esc_html__( 'Number of titles that will be generated in one request.', 'classifai' ),
]
);
switch ( $this->feature_instance::ID ) {
case TitleGeneration::ID:
add_settings_field(
'number_of_titles',
esc_html__( 'Number of titles', 'classifai' ),
[ $this->feature_instance, 'render_input' ],
$this->feature_instance->get_option_name(),
$this->feature_instance->get_option_name() . '_section',
[
'option_index' => static::ID,
'label_for' => 'number_of_titles',
'input_type' => 'number',
'min' => 1,
'step' => 1,
'default_value' => $settings['number_of_titles'],
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
'description' => esc_html__( 'Number of titles that will be generated in one request.', 'classifai' ),
]
);
break;

case ContentResizing::ID:
add_settings_field(
'number_of_suggestions',
esc_html__( 'Number of suggestions', 'classifai' ),
[ $this->feature_instance, 'render_input' ],
$this->feature_instance->get_option_name(),
$this->feature_instance->get_option_name() . '_section',
[
'option_index' => static::ID,
'label_for' => 'number_of_suggestions',
'input_type' => 'number',
'min' => 1,
'step' => 1,
'default_value' => $settings['number_of_suggestions'],
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
'description' => esc_html__( 'Number of suggestions that will be generated in one request.', 'classifai' ),
]
);
break;
}

do_action( 'classifai_' . static::ID . '_render_provider_fields', $this );
}
Expand All @@ -115,9 +139,21 @@ public function get_default_provider_settings(): array {
$common_settings = [
'api_key' => '',
'authenticated' => false,
'number_of_titles' => 1,
];

/**
* Default values for feature specific settings.
*/
switch ( $this->feature_instance::ID ) {
case TitleGeneration::ID:
$common_settings['number_of_titles'] = 1;
break;

case ContentResizing::ID:
$common_settings['number_of_suggestions'] = 1;
break;
}

return $common_settings;
}

Expand All @@ -133,7 +169,16 @@ public function sanitize_settings( array $new_settings ): array {

$new_settings[ static::ID ]['api_key'] = $api_key_settings[ static::ID ]['api_key'];
$new_settings[ static::ID ]['authenticated'] = $api_key_settings[ static::ID ]['authenticated'];
$new_settings[ static::ID ]['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings[ static::ID ], $settings[ static::ID ] );

switch ( $this->feature_instance::ID ) {
case TitleGeneration::ID:
$new_settings[ static::ID ]['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings[ static::ID ], $settings[ static::ID ] );
break;

case ContentResizing::ID:
$new_settings[ static::ID ]['number_of_suggestions'] = sanitize_number_of_responses_field( 'number_of_suggestions', $new_settings[ static::ID ], $settings[ static::ID ] );
break;
}

return $new_settings;
}
Expand Down Expand Up @@ -412,7 +457,7 @@ public function resize_content( int $post_id, array $args = array() ) {
$args = wp_parse_args(
array_filter( $args ),
[
'num' => $settings['number_of_suggestions'] ?? 1,
'num' => $settings[ static::ID ]['number_of_suggestions'] ?? 1,
]
);

Expand Down Expand Up @@ -574,16 +619,16 @@ public function get_debug_information(): array {

if ( $this->feature_instance instanceof TitleGeneration ) {
$debug_info[ __( 'No. of titles', 'classifai' ) ] = $provider_settings['number_of_titles'] ?? 1;
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_title_prompt'] ?? [] );
$debug_info[ __( 'Generate title prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_title_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_title_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ExcerptGeneration ) {
$debug_info[ __( 'Excerpt length', 'classifai' ) ] = $settings['length'] ?? 55;
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Generate excerpt prompt', 'classifai' ) ] = wp_json_encode( $settings['generate_excerpt_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_excerpt_generation_latest_response' ) );
} elseif ( $this->feature_instance instanceof ContentResizing ) {
$debug_info[ __( 'No. of suggestions', 'classifai' ) ] = $provider_settings['number_of_suggestions'] ?? 1;
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $provider_settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Expand text prompt', 'classifai' ) ] = wp_json_encode( $settings['expand_text_prompt'] ?? [] );
$debug_info[ __( 'Condense text prompt', 'classifai' ) ] = wp_json_encode( $settings['condense_text_prompt'] ?? [] );
$debug_info[ __( 'Latest response', 'classifai' ) ] = $this->get_formatted_latest_response( get_transient( 'classifai_openai_chatgpt_content_resizing_latest_response' ) );
}

Expand Down

0 comments on commit 41e5e33

Please sign in to comment.