Skip to content

Commit

Permalink
Fix all issues found during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dkotter committed Sep 12, 2024
1 parent 24e292d commit 811b1d8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Examples:

```php
// Render the results.
Classifai\render_results(
Classifai\render_smart_404_results(
[
'index' => 'post',
'num' => 3,
Expand All @@ -512,7 +512,7 @@ Classifai\render_results(

```php
// Get the results.
$results = Classifai\get_results(
$results = Classifai\get_smart_404_results(
[
'index' => 'post',
'num' => 10,
Expand All @@ -522,9 +522,6 @@ $results = Classifai\get_results(
]
);

// Convert the results to normal WP_Post objects.
$results = ( new \Classifai\Features\Smart404EPIntegration() )->convert_es_results_to_post_objects( $results );

ob_start();

// Render the results.
Expand Down
5 changes: 2 additions & 3 deletions includes/Classifai/Features/Smart404.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use WP_Query;

use function Classifai\is_elasticpress_installed;
use function Speedyseo\Utility\is_elasticpress_active;

/**
* Class Smart404
Expand Down Expand Up @@ -244,7 +243,7 @@ public function sanitize_default_feature_settings( array $new_settings ): array
*/
public function exact_knn_search( string $query, array $args = [] ) {
// Ensure the Feature is enabled and configured before trying to use it.
if ( ! is_elasticpress_active() || ! $this->is_configured() || ! $this->is_enabled() ) {
if ( ! is_elasticpress_installed() || ! $this->is_configured() || ! $this->is_enabled() ) {
return new WP_Error( 'not_enabled', __( 'Feature is not enabled.', 'classifai' ) );
}

Expand Down Expand Up @@ -284,7 +283,7 @@ public function exact_knn_search( string $query, array $args = [] ) {
$args['post_type'] = [ $args['post_type'] ];
}

$integration = new Smart404EPIntegration();
$integration = new Smart404EPIntegration( $this->get_feature_provider_instance() );

// Run our search. Note that this will take our query and generate embeddings for it.
if ( 'no' === $args['rescore'] || false === $args['rescore'] ) {
Expand Down
23 changes: 13 additions & 10 deletions includes/Classifai/Features/Smart404EPIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Classifai\Features;

use Classifai\Features\Smart404;
use Classifai\Providers\OpenAI\Tokenizer;
use ElasticPress\Indexables;
use ElasticPress\Indexable;
Expand Down Expand Up @@ -45,7 +46,7 @@ class Smart404EPIntegration {
*
* @var string $embeddings_meta_key
*/
protected $embeddings_meta_key;
protected $embeddings_meta_key = '';

/**
* Post content hash meta key.
Expand All @@ -61,13 +62,15 @@ class Smart404EPIntegration {
*/
public function __construct( $provider = null ) {
$this->embeddings_handler = $provider;
$this->es_version = Elasticsearch::factory()->get_elasticsearch_version();
$this->tokenizer = new Tokenizer( (int) $this->embeddings_handler->get_max_tokens() );

if ( 'openai_embeddings' === $provider::ID ) {
$this->embeddings_meta_key = 'classifai_openai_embeddings';
} elseif ( 'azure_openai_embeddings' === $provider::ID ) {
$this->embeddings_meta_key = 'classifai_azure_openai_embeddings';
$this->es_version = ! $provider ? '7.0' : Elasticsearch::factory()->get_elasticsearch_version();
$this->tokenizer = ! $this->embeddings_handler ? new Tokenizer( 8191 ) : new Tokenizer( (int) $this->embeddings_handler->get_max_tokens() );

if ( $provider ) {
if ( 'openai_embeddings' === $provider::ID ) {
$this->embeddings_meta_key = 'classifai_openai_embeddings';
} elseif ( 'azure_openai_embeddings' === $provider::ID ) {
$this->embeddings_meta_key = 'classifai_azure_openai_embeddings';
}
}
}

Expand Down Expand Up @@ -306,7 +309,7 @@ public function get_embedding( string $text, bool $cache = false ) {
}

// Generate the embedding.
$embedding = $this->embeddings_handler->generate_embedding( $text, new Feature() );
$embedding = $this->embeddings_handler->generate_embedding( $text, new Smart404() );

if ( is_wp_error( $embedding ) ) {
return $embedding;
Expand All @@ -328,7 +331,7 @@ public function get_embedding( string $text, bool $cache = false ) {
*/
public function get_embeddings( array $strings ) {
// Generate the embeddings.
$embeddings = $this->embeddings_handler->generate_embeddings( $strings, new Feature() );
$embeddings = $this->embeddings_handler->generate_embeddings( $strings, new Smart404() );

return $embeddings;
}
Expand Down
6 changes: 3 additions & 3 deletions includes/Classifai/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ function get_smart_404_results( array $args = [] ): array {
return [];
}

// Convert the results to normal WP_Post objects.
$results = ( new Smart404EPIntegration() )->convert_es_results_to_post_objects( $results );

return $results;
}

Expand All @@ -730,9 +733,6 @@ function render_smart_404_results( array $args = [] ) {
// Get the results.
$results = get_smart_404_results( $args );

// Convert the results to normal WP_Post objects.
$results = ( new Smart404EPIntegration() )->convert_es_results_to_post_objects( $results );

// Handle situation where we don't have results.
if ( empty( $results ) ) {
return;
Expand Down

0 comments on commit 811b1d8

Please sign in to comment.