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

Enhancement/577 Threshold values for Embeddings #621

Merged
merged 21 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions includes/Classifai/Providers/OpenAI/EmbeddingCalculations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ class EmbeddingCalculations {
*
* @param array $source_embedding Embedding data of the source item.
* @param array $compare_embedding Embedding data of the item to compare.
* @param float $threshold The threshold to use for the similarity calculation.
*
* @return bool|float
*/
public function similarity( array $source_embedding = [], array $compare_embedding = [] ) {
public function similarity( array $source_embedding = [], array $compare_embedding = [], $threshold = 1 ) {
if ( empty( $source_embedding ) || empty( $compare_embedding ) ) {
return false;
}
Expand Down Expand Up @@ -56,8 +58,15 @@ function( $x ) {
// Do the math.
$distance = 1.0 - ( $combined_average / sqrt( $source_average * $compare_average ) );

// Ensure we are within the range of 0 to 2.0.
return max( 0, min( abs( (float) $distance ), 2.0 ) );
/**
* Filter the threshold for the similarity calculation.
*
* @param float $threshold The threshold to use.
*/
dkotter marked this conversation as resolved.
Show resolved Hide resolved
$threshold = apply_filters( 'classifai_threshold', $threshold );

// Ensure we are within the range of 0 to 1.0 (i.e. $threshold).
return max( 0, min( abs( (float) $distance ), $threshold ) );
}

}
44 changes: 42 additions & 2 deletions includes/Classifai/Providers/OpenAI/Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,15 @@ public function sanitize_settings( $settings ) {
} else {
$new_settings['taxonomies'][ $taxonomy_key ] = '0';
}

// Sanitize the threshold setting.
$taxonomy_key = $taxonomy_key . '_threshold';
if ( isset( $settings['taxonomies'][ $taxonomy_key ] ) && '0' !== $settings['taxonomies'][ $taxonomy_key ] ) {
$threshold_value = min( absint( $settings['taxonomies'][ $taxonomy_key ] ), 100 );
$new_settings['taxonomies'][ $taxonomy_key ] = $threshold_value ? $threshold_value : 75;
} else {
$new_settings['taxonomies'][ $taxonomy_key ] = 75;
}
}

// Sanitize the number setting.
Expand Down Expand Up @@ -354,6 +363,34 @@ public function supported_post_types() {
return apply_filters( 'classifai_openai_embeddings_post_types', $this->get_supported_post_types() );
}

/**
* Get the threshold for the similarity calculation.
*
* @since 2.6.0
dkotter marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $taxonomy Taxonomy slug.
* @return float
*/
public function get_threshold( $taxonomy = '' ) {
$settings = $this->get_settings();
$threshold = 1;

if ( ! empty( $taxonomy ) ) {
$threshold = isset( $settings['taxonomies'][ $taxonomy . '_threshold' ] ) ? $settings['taxonomies'][ $taxonomy . '_threshold' ] : 75;
}

// Convert $threshold (%) to decimal.
$threshold = 1 - ( (float) $threshold / 100 );

/**
* Filter the threshold for the similarity calculation.
*
* @param float $threshold The threshold to use.
* @param string $taxonomy The taxonomy to get the threshold for.
*/
dkotter marked this conversation as resolved.
Show resolved Hide resolved
return apply_filters( 'classifai_threshold', $threshold, $taxonomy );
}

/**
* The list of supported post statuses.
*
Expand Down Expand Up @@ -468,6 +505,9 @@ private function set_terms( int $post_id = 0, array $embedding = [] ) {
continue;
}

// Get threshold setting for this taxonomy.
$threshold = $this->get_threshold( $tax );

// Get embedding similarity for each term.
foreach ( $terms as $term_id ) {
if ( ! current_user_can( 'assign_term', $term_id ) && ( ! defined( 'WP_CLI' ) || ! WP_CLI ) ) {
Expand All @@ -477,9 +517,9 @@ private function set_terms( int $post_id = 0, array $embedding = [] ) {
$term_embedding = get_term_meta( $term_id, 'classifai_openai_embeddings', true );

if ( $term_embedding ) {
$similarity = $calculations->similarity( $embedding, $term_embedding );
$similarity = $calculations->similarity( $embedding, $term_embedding, $threshold );
if ( false !== $similarity ) {
$embedding_similarity[ $tax ][ $term_id ] = $calculations->similarity( $embedding, $term_embedding );
$embedding_similarity[ $tax ][ $term_id ] = $similarity;
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions includes/Classifai/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,12 @@ public function render_checkbox_group( array $args = array() ) {
foreach ( $args['options'] as $option_value => $option_label ) {
$value = '';
$default_key = array_search( $option_value, $args['default_values'], true );
$option_value_theshold = $option_value . '_threshold';

// Get saved value, if any.
if ( isset( $setting_index[ $args['label_for'] ] ) ) {
$value = $setting_index[ $args['label_for'] ][ $option_value ] ?? '';
$threshold_value = $setting_index[ $args['label_for'] ][ $option_value_theshold ] ?? '';
}

// Check for backward compatibility.
Expand Down Expand Up @@ -435,6 +437,11 @@ public function render_checkbox_group( array $args = array() ) {
checked( $value, $option_value, false ),
esc_html( $option_label )
);

// Render Threshold field.
if ( 'openai_embeddings' === $this->option_name && 'taxonomies' === $args['label_for'] ) {
$this->render_threshold_field( $args, $option_value_theshold, $threshold_value );
}
}

// Render description, if any.
Expand All @@ -446,6 +453,34 @@ public function render_checkbox_group( array $args = array() ) {
}
}

/**
* Render a threshold field.
*
* @since 2.6.0
dkotter marked this conversation as resolved.
Show resolved Hide resolved
*
* @param array $args The args passed to add_settings_field
* @param string $option_value The option value.
* @param string $value The value.
*
* @return void
*/
public function render_threshold_field( $args, $option_value, $value ) {
printf(
'<p class="threshold_wrapper">
<label for="%1$s_%2$s_%3$s">%4$s</label>
<br>
<input type="number" id="%1$s_%2$s_%3$s" class="small-text" name="classifai_%1$s[%2$s][%3$s]" value="%5$s" />
</p>',
esc_attr( $this->option_name ),
esc_attr( $args['label_for'] ),
dkotter marked this conversation as resolved.
Show resolved Hide resolved
esc_attr( $option_value ),
esc_html__( 'Threshold (%)', 'classifai' ),
esc_attr( $value )
);
}



/**
* Renders the checkbox group for 'Generate descriptive text' setting.
*
Expand Down
17 changes: 17 additions & 0 deletions src/scss/language-processing.scss
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,20 @@
color: rgb(117, 117, 117);
}
}

.form-table {
td {
p.threshold_wrapper {
margin: 10px 0 40px;

&:last-of-type {
margin-bottom: 20px;
}

label {
display: inline-block;
margin: 0 0 10px;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe( '[Language processing] Classify Content (OpenAI) Tests', () => {
cy.get( '#openai_embeddings_post_types_post' ).check();
cy.get( '#openai_embeddings_post_statuses_publish' ).check();
cy.get( '#openai_embeddings_taxonomies_category' ).check();
cy.get( '#openai_embeddings_taxonomies_category_threshold' ).type( 80 ); // "Test" requires 80% confidence. At 81%, it does not apply.
cy.get( '#number' ).clear().type( 1 );
cy.get( '#submit' ).click();
} );
Expand Down
Loading