Skip to content

Commit 2767aea

Browse files
authored
Merge pull request #858 from 10up/feature/add-embedding-similarity-hooks
Add embedding similarity hooks
2 parents 805f0cc + 7fed8e5 commit 2767aea

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

includes/Classifai/Providers/Azure/Embeddings.php

+42
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,19 @@ public function set_terms( int $post_id = 0, array $embeddings = [], bool $link
481481
return new WP_Error( 'invalid', esc_html__( 'No matching terms found.', 'classifai' ) );
482482
}
483483

484+
/**
485+
* Fires after the embeddings similarity has been run but before results are sorted.
486+
*
487+
* @since x.x.x
488+
* @hook classifai_azure_openai_embeddings_pre_sort_embeddings_similarity
489+
*
490+
* @param {array} $embeddings_similarity The embeddings similarity results.
491+
* @param {int} $post_id ID of post to set terms on.
492+
* @param {array} $embeddings Embeddings data.
493+
* @param {bool} $link Whether to link the terms or not.
494+
*/
495+
do_action( 'classifai_azure_openai_embeddings_pre_sort_embeddings_similarity', $embeddings_similarity, $post_id, $embeddings, $link );
496+
484497
// Sort the results by similarity.
485498
usort(
486499
$embeddings_similarity,
@@ -500,6 +513,20 @@ function ( $a, $b ) {
500513
$sorted_results[ $item['taxonomy'] ][] = $item;
501514
}
502515

516+
/**
517+
* Fires after the embeddings similarity has been run and sorted.
518+
*
519+
* @since x.x.x
520+
* @hook classifai_azure_openai_embeddings_post_sort_embeddings_similarity
521+
*
522+
* @param {array} $sorted_results The sorted embeddings similarity results.
523+
* @param {array} $embeddings_similarity The embeddings similarity results.
524+
* @param {int} $post_id ID of post to set terms on.
525+
* @param {array} $embeddings Embeddings data.
526+
* @param {bool} $link Whether to link the terms or not.
527+
*/
528+
do_action( 'classifai_azure_openai_embeddings_post_sort_embeddings_similarity', $sorted_results, $embeddings_similarity, $post_id, $embeddings, $link );
529+
503530
$return = [];
504531

505532
/**
@@ -668,6 +695,21 @@ private function get_embeddings_similarity( array $embedding, bool $consider_thr
668695
foreach ( $term_embedding as $chunk ) {
669696
$similarity = $calculations->cosine_similarity( $embedding, $chunk );
670697

698+
/**
699+
* Fires after the embeddings similarity has been run for a single chunk.
700+
*
701+
* @since x.x.x
702+
* @hook classifai_azure_openai_embeddings_single_embedding_similarity
703+
*
704+
* @param {bool|float} $similarity The embeddings similarity result.
705+
* @param {array} $embedding Post embedding data.
706+
* @param {array} $chunk Term chunk embedding data.
707+
* @param {int} $term_id ID of term we're comparing.
708+
* @param {string} $tax Taxonomy of term.
709+
* @param {bool} $consider_threshold Whether to consider the threshold or not.
710+
*/
711+
do_action( 'classifai_azure_openai_embeddings_single_embedding_similarity', $similarity, $embedding, $chunk, $term_id, $tax, $consider_threshold );
712+
671713
if ( false !== $similarity && ( ! $consider_threshold || $similarity <= $threshold ) ) {
672714
$embedding_similarity[] = [
673715
'taxonomy' => $tax,

includes/Classifai/Providers/Localhost/OllamaEmbeddings.php

+42
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,19 @@ public function set_terms( int $post_id = 0, array $embeddings = [], bool $link
554554
return new WP_Error( 'invalid', esc_html__( 'No matching terms found.', 'classifai' ) );
555555
}
556556

557+
/**
558+
* Fires after the embeddings similarity has been run but before results are sorted.
559+
*
560+
* @since x.x.x
561+
* @hook classifai_ollama_embeddings_pre_sort_embeddings_similarity
562+
*
563+
* @param {array} $embeddings_similarity The embeddings similarity results.
564+
* @param {int} $post_id ID of post to set terms on.
565+
* @param {array} $embeddings Embeddings data.
566+
* @param {bool} $link Whether to link the terms or not.
567+
*/
568+
do_action( 'classifai_ollama_embeddings_pre_sort_embeddings_similarity', $embeddings_similarity, $post_id, $embeddings, $link );
569+
557570
// Sort the results by similarity.
558571
usort(
559572
$embeddings_similarity,
@@ -573,6 +586,20 @@ function ( $a, $b ) {
573586
$sorted_results[ $item['taxonomy'] ][] = $item;
574587
}
575588

589+
/**
590+
* Fires after the embeddings similarity has been run and sorted.
591+
*
592+
* @since x.x.x
593+
* @hook classifai_ollama_embeddings_post_sort_embeddings_similarity
594+
*
595+
* @param {array} $sorted_results The sorted embeddings similarity results.
596+
* @param {array} $embeddings_similarity The embeddings similarity results.
597+
* @param {int} $post_id ID of post to set terms on.
598+
* @param {array} $embeddings Embeddings data.
599+
* @param {bool} $link Whether to link the terms or not.
600+
*/
601+
do_action( 'classifai_ollama_embeddings_post_sort_embeddings_similarity', $sorted_results, $embeddings_similarity, $post_id, $embeddings, $link );
602+
576603
$return = [];
577604

578605
/**
@@ -741,6 +768,21 @@ private function get_embeddings_similarity( array $embedding, bool $consider_thr
741768
foreach ( $term_embedding as $chunk ) {
742769
$similarity = $calculations->cosine_similarity( $embedding, $chunk );
743770

771+
/**
772+
* Fires after the embeddings similarity has been run for a single chunk.
773+
*
774+
* @since x.x.x
775+
* @hook classifai_ollama_embeddings_single_embedding_similarity
776+
*
777+
* @param {bool|float} $similarity The embeddings similarity result.
778+
* @param {array} $embedding Post embedding data.
779+
* @param {array} $chunk Term chunk embedding data.
780+
* @param {int} $term_id ID of term we're comparing.
781+
* @param {string} $tax Taxonomy of term.
782+
* @param {bool} $consider_threshold Whether to consider the threshold or not.
783+
*/
784+
do_action( 'classifai_ollama_embeddings_single_embedding_similarity', $similarity, $embedding, $chunk, $term_id, $tax, $consider_threshold );
785+
744786
if ( false !== $similarity && ( ! $consider_threshold || $similarity <= $threshold ) ) {
745787
$embedding_similarity[] = [
746788
'taxonomy' => $tax,

includes/Classifai/Providers/OpenAI/Embeddings.php

+42
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,19 @@ public function set_terms( int $post_id = 0, array $embeddings = [], bool $link
595595
return new WP_Error( 'invalid', esc_html__( 'No matching terms found.', 'classifai' ) );
596596
}
597597

598+
/**
599+
* Fires after the embeddings similarity has been run but before results are sorted.
600+
*
601+
* @since x.x.x
602+
* @hook classifai_openai_embeddings_pre_sort_embeddings_similarity
603+
*
604+
* @param {array} $embeddings_similarity The embeddings similarity results.
605+
* @param {int} $post_id ID of post to set terms on.
606+
* @param {array} $embeddings Embeddings data.
607+
* @param {bool} $link Whether to link the terms or not.
608+
*/
609+
do_action( 'classifai_openai_embeddings_pre_sort_embeddings_similarity', $embeddings_similarity, $post_id, $embeddings, $link );
610+
598611
// Sort the results by similarity.
599612
usort(
600613
$embeddings_similarity,
@@ -614,6 +627,20 @@ function ( $a, $b ) {
614627
$sorted_results[ $item['taxonomy'] ][] = $item;
615628
}
616629

630+
/**
631+
* Fires after the embeddings similarity has been run and sorted.
632+
*
633+
* @since x.x.x
634+
* @hook classifai_openai_embeddings_post_sort_embeddings_similarity
635+
*
636+
* @param {array} $sorted_results The sorted embeddings similarity results.
637+
* @param {array} $embeddings_similarity The embeddings similarity results.
638+
* @param {int} $post_id ID of post to set terms on.
639+
* @param {array} $embeddings Embeddings data.
640+
* @param {bool} $link Whether to link the terms or not.
641+
*/
642+
do_action( 'classifai_openai_embeddings_post_sort_embeddings_similarity', $sorted_results, $embeddings_similarity, $post_id, $embeddings, $link );
643+
617644
$return = [];
618645

619646
/**
@@ -784,6 +811,21 @@ private function get_embeddings_similarity( array $embedding, bool $consider_thr
784811
foreach ( $term_embedding as $chunk ) {
785812
$similarity = $calculations->cosine_similarity( $embedding, $chunk );
786813

814+
/**
815+
* Fires after the embeddings similarity has been run for a single chunk.
816+
*
817+
* @since x.x.x
818+
* @hook classifai_openai_embeddings_single_embedding_similarity
819+
*
820+
* @param {bool|float} $similarity The embeddings similarity result.
821+
* @param {array} $embedding Post embedding data.
822+
* @param {array} $chunk Term chunk embedding data.
823+
* @param {int} $term_id ID of term we're comparing.
824+
* @param {string} $tax Taxonomy of term.
825+
* @param {bool} $consider_threshold Whether to consider the threshold or not.
826+
*/
827+
do_action( 'classifai_openai_embeddings_single_embedding_similarity', $similarity, $embedding, $chunk, $term_id, $tax, $consider_threshold );
828+
787829
if ( false !== $similarity && ( ! $consider_threshold || $similarity <= $threshold ) ) {
788830
$embedding_similarity[] = [
789831
'taxonomy' => $tax,

0 commit comments

Comments
 (0)