Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google AI (Gemini API) Provider for the Title Generation, Excerpt Generation and Content resizing. #700

Merged
merged 10 commits into from
Feb 13, 2024
113 changes: 78 additions & 35 deletions README.md

Large diffs are not rendered by default.

32 changes: 8 additions & 24 deletions includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Classifai\Features;

use Classifai\Providers\GoogleAI\GeminiAPI;
use Classifai\Providers\OpenAI\ChatGPT;
use Classifai\Services\LanguageProcessing;
use WP_REST_Server;
Expand Down Expand Up @@ -48,7 +49,8 @@ public function __construct() {

// Contains just the providers this feature supports.
$this->supported_providers = [
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
GeminiAPI::ID => __( 'Google AI (Gemini API)', 'classifai' ),
];
}

Expand Down Expand Up @@ -234,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 @@ -286,22 +272,21 @@ public function add_custom_settings_fields() {
*/
public function get_feature_default_settings(): array {
return [
'number_of_suggestions' => 1,
'condense_text_prompt' => [
'condense_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->condense_prompt,
'original' => 1,
],
],
'expand_text_prompt' => [
'expand_text_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'prompt' => $this->expand_prompt,
'original' => 1,
],
],
'provider' => ChatGPT::ID,
'provider' => ChatGPT::ID,
];
}

Expand All @@ -314,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
8 changes: 5 additions & 3 deletions includes/Classifai/Features/ExcerptGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Classifai\Features;

use Classifai\Providers\GoogleAI\GeminiAPI;
use Classifai\Services\LanguageProcessing;
use Classifai\Providers\OpenAI\ChatGPT;
use WP_REST_Server;
Expand Down Expand Up @@ -40,7 +41,8 @@ public function __construct() {

// Contains just the providers this feature supports.
$this->supported_providers = [
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
GeminiAPI::ID => __( 'Google AI (Gemini API)', 'classifai' ),
];
}

Expand Down Expand Up @@ -263,7 +265,7 @@ public function enqueue_admin_assets( string $hook_suffix ) {
* @return string
*/
public function get_enable_description(): string {
return esc_html__( 'A button will be added to the status panel that can be used to generate titles.', 'classifai' );
return esc_html__( 'A button will be added to the excerpt panel that can be used to generate an excerpt.', 'classifai' );
}

/**
Expand Down Expand Up @@ -320,7 +322,7 @@ public function add_custom_settings_fields() {
'min' => 1,
'step' => 1,
'default_value' => $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' ),
'description' => __( 'How many words should the excerpt be? Note that the final result may not exactly match this, it often tends to exceed this number by 10-15 words.', 'classifai' ),
]
);
}
Expand Down
25 changes: 3 additions & 22 deletions includes/Classifai/Features/TitleGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Classifai\Features;

use Classifai\Providers\GoogleAI\GeminiAPI;
use Classifai\Services\LanguageProcessing;
use Classifai\Providers\OpenAI\ChatGPT;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;

use function Classifai\sanitize_prompts;
use function Classifai\sanitize_number_of_responses_field;
use function Classifai\get_asset_info;

/**
Expand Down Expand Up @@ -41,7 +41,8 @@ public function __construct() {

// Contains just the providers this feature supports.
$this->supported_providers = [
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
GeminiAPI::ID => __( 'Google AI (Gemini API)', 'classifai' ),
];
}

Expand Down Expand Up @@ -315,22 +316,6 @@ public function get_enable_description(): string {
public function add_custom_settings_fields() {
$settings = $this->get_settings();

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

add_settings_field(
'generate_title_prompt',
esc_html__( 'Prompt', 'classifai' ),
Expand All @@ -353,7 +338,6 @@ public function add_custom_settings_fields() {
*/
public function get_feature_default_settings(): array {
return [
'number_of_titles' => 1,
'generate_title_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
Expand All @@ -372,9 +356,6 @@ public function get_feature_default_settings(): array {
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$settings = $this->get_settings();

$new_settings['number_of_titles'] = sanitize_number_of_responses_field( 'number_of_titles', $new_settings, $settings );
$new_settings['generate_title_prompt'] = sanitize_prompts( 'generate_title_prompt', $new_settings );

return $new_settings;
Expand Down
Loading
Loading