Skip to content

Commit e8dcaef

Browse files
committed
Min score for kNN searches + sys prompt change
1 parent 911c4d1 commit e8dcaef

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

includes/classes/Feature/KnnSearch/KnnSearch.php

+40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
* kNN Search Feature
1919
*/
2020
class KnnSearch extends Feature {
21+
/**
22+
* Default settings
23+
*
24+
* @var array $default_settings.
25+
*/
26+
public $default_settings = [
27+
'ep_knn_search_min_score' => 0.7,
28+
];
29+
2130
/**
2231
* Initialize feature setting it's config
2332
*/
@@ -43,4 +52,35 @@ public function setup() {
4352
\ElasticPress\SearchAlgorithms::factory()->register( new SearchAlgorithm\KnnCosine() );
4453
\ElasticPress\SearchAlgorithms::factory()->register( new SearchAlgorithm\Hybrid() );
4554
}
55+
56+
/**
57+
* Set the `settings_schema` attribute
58+
*/
59+
public function set_settings_schema() {
60+
$this->settings_schema = [
61+
[
62+
'key' => 'ep_knn_search_min_score',
63+
'label' => __( 'Minimum score', 'elasticpress-labs' ),
64+
'help' => __( 'The minimum score to be used by kNN searches. Input a number between 0 and 1.', 'elasticpress-labs' ),
65+
'type' => 'number',
66+
'default' => $this->default_settings['ep_knn_search_min_score'],
67+
],
68+
];
69+
}
70+
71+
/**
72+
* Get the min_score setting.
73+
*
74+
* @return float
75+
*/
76+
public function get_min_score() {
77+
/**
78+
* Filters the minimum score for KNN (k-Nearest Neighbors) search.
79+
*
80+
* @since 2.5.0
81+
* @param {float} $min_score The minimum score for KNN search. Default is retrieved from the 'ep_knn_search_min_score' setting.
82+
* @return {float} The minimum score for KNN search.
83+
*/
84+
return (float) apply_filters( 'ep_knn_search_min_score', $this->get_setting( 'ep_knn_search_min_score' ) );
85+
}
4686
}

includes/classes/Feature/KnnSearch/SearchAlgorithm/Hybrid.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ public function get_es_query( $formatted_args, $args, $query ): array {
6363
return $formatted_args;
6464
}
6565

66+
$knn_search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'knn_search' );
67+
6668
return [
6769
'from' => $formatted_args['from'],
6870
'size' => $formatted_args['size'],
6971
'post_filter' => $formatted_args['post_filter'],
72+
'min_score' => $knn_search_feature->get_min_score(),
7073
'query' => $formatted_args['query'],
7174
'knn' => [
7275
'field' => 'chunks.vector',

includes/classes/Feature/KnnSearch/SearchAlgorithm/Knn.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ public function get_es_query( $formatted_args, $args, $query ): array {
6363
return $formatted_args;
6464
}
6565

66+
$knn_search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'knn_search' );
67+
6668
return [
6769
'from' => $formatted_args['from'],
6870
'size' => $formatted_args['size'],
6971
'post_filter' => $formatted_args['post_filter'],
72+
'min_score' => $knn_search_feature->get_min_score(),
7073
'knn' => [
7174
'field' => 'chunks.vector',
7275
'query_vector' => array_map( 'floatval', $query_embedding ),

includes/classes/Feature/KnnSearch/SearchAlgorithm/KnnCosine.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ public function get_es_query( $formatted_args, $args, $query ): array {
6363
return $formatted_args;
6464
}
6565

66+
$knn_search_feature = \ElasticPress\Features::factory()->get_registered_feature( 'knn_search' );
67+
6668
return [
6769
'from' => $formatted_args['from'],
6870
'size' => $formatted_args['size'],
6971
'post_filter' => $formatted_args['post_filter'],
72+
'min_score' => $knn_search_feature->get_min_score(),
7073
'query' => [
7174
'bool' => [
7275
'must' => [

includes/classes/Feature/RAG.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ public function ai_api_request( $prompt, $search_term ) {
403403
],
404404
[
405405
'role' => 'system',
406-
'content' => 'Send your response as a JSON object with the following keys: "response" (the asnwer, in HTML format) and "references" (an array of objects with the URLs you used to build the response, having "url" and "title" as attributes). Do not wrap the response in any other tags or limiters like "```json". Make sure the JSON object returned is properly escaped.',
406+
'content' => __( 'Send your response as a JSON object with the following keys: "response" (the asnwer, in HTML format) and "references" (an array of objects with the URLs you used to build the response, having "url" and "title" as attributes). Do not wrap the response in any other tags or limiters like "```json". Make sure the JSON object returned is properly escaped. Do not append the list of URLs used to the "response" value, as it will be displayed using the values in "references".', 'elasticpress-labs' ),
407407
],
408408
[
409409
'role' => 'user',

0 commit comments

Comments
 (0)