Skip to content

Commit 3ff7e85

Browse files
committed
Consolidate embeddings generator field
1 parent ff89987 commit 3ff7e85

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

includes/classes/Feature/VectorEmbeddings/Indexables/Post/Post.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,26 @@ public function setup() {
3232
// Alter post and term mapping to store our vector embeddings
3333
add_filter( 'ep_post_mapping', [ $this, 'add_post_vector_field_mapping' ] );
3434

35-
// Only trigger embeddings when external embeddings are turned off
36-
if ( ! $this->feature->get_setting( 'ep_embeddings_external_embedding' ) ) {
37-
add_action( 'init', [ $this, 'register_meta' ], 20 );
38-
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
39-
40-
add_action( 'post_submitbox_misc_actions', [ $this, 'output_embedding_exclude_setting' ] );
41-
add_action( 'attachment_submitbox_misc_actions', [ $this, 'output_embedding_exclude_setting' ], 15 );
42-
43-
add_action( 'edit_post', [ $this, 'save_embedding_exclude_meta' ] );
44-
add_action( 'edit_attachment', [ $this, 'save_embedding_exclude_meta' ] );
45-
46-
if ( $this->feature->get_setting( 'ep_embeddings_use_epio' ) ) {
47-
add_filter( 'ep_bulk_index_action_args', [ $this, 'maybe_add_chunks_to_bulk_index_action_args' ], 10, 2 );
48-
add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'maybe_add_chunks_to_text_chunks_fields' ], 10, 2 );
49-
add_filter( 'ep_doc_status', [ $this, 'maybe_set_doc_status' ], 10, 3 );
50-
} else {
51-
add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'add_vector_field_to_post_sync' ], 10, 2 );
52-
}
35+
$generator = $this->feature->get_setting( 'ep_embeddings_generator' );
36+
if ( 'external' === $generator ) {
37+
return;
38+
}
39+
40+
add_action( 'init', [ $this, 'register_meta' ], 20 );
41+
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
42+
43+
add_action( 'post_submitbox_misc_actions', [ $this, 'output_embedding_exclude_setting' ] );
44+
add_action( 'attachment_submitbox_misc_actions', [ $this, 'output_embedding_exclude_setting' ], 15 );
45+
46+
add_action( 'edit_post', [ $this, 'save_embedding_exclude_meta' ] );
47+
add_action( 'edit_attachment', [ $this, 'save_embedding_exclude_meta' ] );
48+
49+
if ( 'epio' === $generator ) {
50+
add_filter( 'ep_bulk_index_action_args', [ $this, 'maybe_add_chunks_to_bulk_index_action_args' ], 10, 2 );
51+
add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'maybe_add_chunks_to_text_chunks_fields' ], 10, 2 );
52+
add_filter( 'ep_doc_status', [ $this, 'maybe_set_doc_status' ], 10, 3 );
53+
} else {
54+
add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'add_vector_field_to_post_sync' ], 10, 2 );
5355
}
5456
}
5557

includes/classes/Feature/VectorEmbeddings/VectorEmbeddings.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ class VectorEmbeddings extends Feature {
3939
* @var array $default_settings.
4040
*/
4141
public $default_settings = [
42-
'ep_embeddings_api_key' => '',
43-
'ep_embeddings_api_url' => 'https://api.openai.com/v1/embeddings',
44-
'ep_embeddings_embedding_model' => 'text-embedding-3-small',
45-
'ep_embeddings_dimensions' => 512,
46-
'ep_embeddings_external_embedding' => '0',
47-
'ep_embeddings_use_epio' => '0',
42+
'ep_embeddings_generator' => 'openai',
43+
'ep_embeddings_api_key' => '',
44+
'ep_embeddings_api_url' => 'https://api.openai.com/v1/embeddings',
45+
'ep_embeddings_embedding_model' => 'text-embedding-3-small',
46+
'ep_embeddings_dimensions' => 512,
4847
];
4948

5049
/**
@@ -81,7 +80,7 @@ public function setup() {
8180
$this->indexables['post'] = new Indexables\Post\Post( $this );
8281
$this->indexables['post']->setup();
8382

84-
if ( $this->get_setting( 'ep_embeddings_use_epio' ) ) {
83+
if ( 'epio' === $this->get_setting( 'ep_embeddings_generator' ) ) {
8584
add_filter( 'ep_status_report_reports', [ $this, 'add_status_report' ] );
8685
}
8786
}
@@ -107,7 +106,37 @@ public function requirements_status() {
107106
* Set the `settings_schema` attribute
108107
*/
109108
public function set_settings_schema() {
109+
$generator = [
110+
'key' => 'ep_embeddings_generator',
111+
'label' => __( 'Generator', 'elasticpress-labs' ),
112+
'options' => [
113+
[
114+
'label' => __( 'External embedding processing', 'elasticpress' ),
115+
'value' => 'external',
116+
],
117+
[
118+
'label' => __( 'OpenAI', 'elasticpress' ),
119+
'value' => 'openai',
120+
],
121+
],
122+
'help' => __( '<b>External embedding processing:</b> An external process is providing the vector_embeddings meta field.<br><b>OpenAI:</b> Use the fields below to generate the embeddings.', 'elasticpress-labs' ),
123+
'default' => 'openai',
124+
'type' => 'radio',
125+
];
126+
if ( Utils\is_epio() ) {
127+
array_unshift(
128+
$generator['options'],
129+
[
130+
'label' => __( 'ElasticPress.io', 'elasticpress' ),
131+
'value' => 'epio',
132+
]
133+
);
134+
$generator['help'] = __( '<b>ElasticPress.io:</b> Your vectors will be generated by the ElasticPress.io service.<br>', 'elasticpress-labs' ) . $generator['help'];
135+
$generator['default'] = 'epio';
136+
}
137+
110138
$this->settings_schema = [
139+
$generator,
111140
[
112141
'key' => 'ep_embeddings_api_key',
113142
'label' => __( 'OpenAI API Key', 'elasticpress-labs' ),
@@ -147,22 +176,7 @@ public function set_settings_schema() {
147176
'type' => 'number',
148177
'default' => $this->default_settings['ep_embeddings_dimensions'],
149178
],
150-
[
151-
'key' => 'ep_embeddings_external_embedding',
152-
'help' => __( 'Enable this if an external process is providing the vector_embeddings meta field provided above with content. This will disable ElasticPress\'s control over embedding generation', 'elasticpress-labs' ),
153-
'label' => __( 'External embedding processing', 'elasticpress-labs' ),
154-
'type' => 'checkbox',
155-
],
156179
];
157-
158-
if ( Utils\is_epio() ) {
159-
$this->settings_schema[] = [
160-
'key' => 'ep_embeddings_use_epio',
161-
'help' => __( 'Enable this if you want to use ElasticPress.io to vectorize your content.', 'elasticpress-labs' ),
162-
'label' => __( 'Use EP.io', 'elasticpress-labs' ),
163-
'type' => 'checkbox',
164-
];
165-
}
166180
}
167181

168182
/**

0 commit comments

Comments
 (0)