Skip to content

Commit

Permalink
Update regenerate embeddings processing code
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdharmesh committed Sep 6, 2024
1 parent 2dea03e commit 85239c4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 28 deletions.
57 changes: 56 additions & 1 deletion includes/Classifai/Admin/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function maybe_render_notices() {
$this->thresholds_update_notice();
$this->v3_migration_completed_notice();
$this->render_embeddings_notice();
$this->render_notices();
}

/**
Expand Down Expand Up @@ -258,7 +259,7 @@ public function render_embeddings_notice() {
sprintf(
// translators: %1$s: Feature specific message; %2$s: URL to Feature settings.
__( 'ClassifAI has updated to the <code>text-embedding-3-small</code> embeddings model. <br>This requires regenerating any stored embeddings for functionality to work properly. <br><a href="%1$s">Click here to do that</a>, noting this will make multiple API requests to OpenAI.', 'classifai' ),
wp_nonce_url( admin_url( 'tools.php?page=classifai&tab=language_processing&feature=feature_classification' ), 'regen_embeddings', 'embeddings_nonce' )
wp_nonce_url( admin_url( 'admin-post.php?action=classifai_regen_embeddings' ), 'regen_embeddings', 'embeddings_nonce' )
)
);
?>
Expand Down Expand Up @@ -337,4 +338,58 @@ public function ajax_maybe_dismiss_notice() {

update_user_meta( get_current_user_id(), "classifai_dismissed_{$notice_id}", true );
}

/**
* Render any saved notices to display.
*/
public function render_notices() {
$notices = $this->get_notices();
if ( empty( $notices ) ) {
return;
}

foreach ( $notices as $notice ) {
if ( ! empty( $notice['message'] ) ) {
?>
<div class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> is-dismissible">
<p><?php echo esc_html( $notice['message'] ); ?></p>
</div>
<?php
}
}
}

/**
* Get any saved notices to display.
*
* @return mixed
*/
public function get_notices() {
$notices = get_transient( 'classifai_notices' );
delete_transient( 'classifai_notices' );

return $notices;
}

/**
* Set a notice to be displayed.
*
* This will be displayed on the next page load.
* The notice will be stored in a transient.
*
* @param string $message The notice message.
* @param string $type The notice type.
*/
public function set_notice( $message, $type = 'info' ) {
$notices = get_transient( 'classifai_notices' );
if ( ! is_array( $notices ) ) {
$notices = [];
}

$notices[] = [
'type' => $type,
'message' => $message,
];
set_transient( 'classifai_notices', $notices );
}
}
56 changes: 29 additions & 27 deletions includes/Classifai/Providers/OpenAI/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Classifai\Providers\OpenAI;

use Classifai\Admin\Notifications;
use Classifai\Providers\Provider;
use Classifai\Providers\OpenAI\APIRequest;
use Classifai\Providers\OpenAI\EmbeddingCalculations;
Expand All @@ -13,6 +14,7 @@
use Classifai\Features\Feature;
use Classifai\EmbeddingsScheduler;
use WP_Error;
use function Classifai\should_use_legacy_settings_panel;

class Embeddings extends Provider {

Expand Down Expand Up @@ -241,32 +243,6 @@ public function render_provider_fields() {
]
);

// If embeddings regeneration is being requested, run that.
if (
isset( $_GET['feature'] ) &&
'feature_classification' === sanitize_text_field( wp_unslash( $_GET['feature'] ) )
) {
if ( isset( $_GET['embedding_regen_completed'] ) ) {
add_action(
'admin_notices',
function () {
?>
<div class="notice notice-success is-dismissible">
<p><?php esc_html_e( 'Embeddings have been regenerated.', 'classifai' ); ?></p>
</div>
<?php
}
);
}

if (
isset( $_GET['embeddings_nonce'] ) &&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['embeddings_nonce'] ) ), 'regen_embeddings' )
) {
$this->regenerate_embeddings();
}
}

do_action( 'classifai_' . static::ID . '_render_provider_fields', $this );
}

Expand Down Expand Up @@ -311,6 +287,7 @@ public function register() {
add_action( 'created_term', [ $this, 'generate_embeddings_for_term' ] );
add_action( 'edited_terms', [ $this, 'generate_embeddings_for_term' ] );
add_action( 'wp_ajax_get_post_classifier_embeddings_preview_data', array( $this, 'get_post_classifier_embeddings_preview_data' ) );
add_action( 'admin_post_classifai_regen_embeddings', [ $this, 'classifai_regen_embeddings' ] );
}

/**
Expand Down Expand Up @@ -443,8 +420,19 @@ public function regenerate_embeddings() {
// Hide the admin notice.
update_option( 'classifai_hide_embeddings_notice', true, false );

// Set a notice to let the user know the embeddings have been regenerated.
$notifications = new Notifications();
$notifications->set_notice(
esc_html__( 'Embeddings have been regenerated.', 'classifai' ),
'success',
);

// Redirect to the same page but remove the nonce so we don't run this again.
wp_safe_redirect( admin_url( 'tools.php?page=classifai&tab=language_processing&feature=feature_classification&embedding_regen_completed' ) );
$redirect_url = admin_url( 'tools.php?page=classifai#/language_processing/feature_classification' );
if ( should_use_legacy_settings_panel() ) {
$redirect_url = admin_url( 'tools.php?page=classifai&tab=language_processing&feature=feature_classification' );
}
wp_safe_redirect( $redirect_url );
exit;
}

Expand Down Expand Up @@ -477,6 +465,20 @@ public function get_post_classifier_embeddings_preview_data(): void {
wp_send_json_success( $embeddings_terms );
}

/**
* Regenerate embeddings.
*/
public function classifai_regen_embeddings(): void {
if (
! isset( $_GET['embeddings_nonce'] ) ||
! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['embeddings_nonce'] ) ), 'regen_embeddings' )
) {
wp_die( esc_html__( 'You do not have permission to perform this operation.', 'classifai' ) );
}

$this->regenerate_embeddings();
}

/**
* Trigger embedding generation for content being saved.
*
Expand Down

0 comments on commit 85239c4

Please sign in to comment.